15
15
*/
16
16
package de .fraunhofer .iosb .app .aas ;
17
17
18
+ import static java .lang .String .format ;
19
+
20
+ import java .io .IOException ;
21
+ import java .net .URISyntaxException ;
22
+ import java .net .URL ;
23
+ import java .util .ArrayList ;
24
+ import java .util .Arrays ;
25
+ import java .util .Collection ;
26
+ import java .util .List ;
27
+ import java .util .Objects ;
28
+
29
+ import org .eclipse .edc .spi .EdcException ;
30
+
18
31
import com .fasterxml .jackson .databind .DeserializationFeature ;
19
32
import com .fasterxml .jackson .databind .ObjectMapper ;
33
+
20
34
import de .fraunhofer .iosb .app .Logger ;
21
- import de .fraunhofer .iosb .app .model .aas .*;
35
+ import de .fraunhofer .iosb .app .model .aas .CustomAssetAdministrationShell ;
36
+ import de .fraunhofer .iosb .app .model .aas .CustomAssetAdministrationShellEnvironment ;
37
+ import de .fraunhofer .iosb .app .model .aas .CustomConceptDescription ;
38
+ import de .fraunhofer .iosb .app .model .aas .CustomSubmodel ;
39
+ import de .fraunhofer .iosb .app .model .aas .CustomSubmodelElement ;
40
+ import de .fraunhofer .iosb .app .model .aas .CustomSubmodelElementCollection ;
41
+ import de .fraunhofer .iosb .app .model .aas .Identifier ;
22
42
import de .fraunhofer .iosb .app .util .AASUtil ;
23
43
import de .fraunhofer .iosb .app .util .Encoder ;
24
44
import de .fraunhofer .iosb .app .util .HttpRestClient ;
29
49
import io .adminshell .aas .v3 .model .impl .DefaultSubmodel ;
30
50
import jakarta .ws .rs .core .Response ;
31
51
import okhttp3 .OkHttpClient ;
32
- import org .eclipse .edc .spi .EdcException ;
33
-
34
- import java .io .IOException ;
35
- import java .net .URISyntaxException ;
36
- import java .net .URL ;
37
- import java .util .*;
38
-
39
- import static java .lang .String .format ;
40
52
41
53
/**
42
54
* Communicating with AAS service
@@ -123,11 +135,11 @@ public Response deleteModel(URL aasServiceUrl, String element) {
123
135
* @return AAS model enriched with each elements access URL as string in assetId
124
136
* field.
125
137
*/
126
- public CustomAssetAdministrationShellEnvironment getAasEnvWithUrls (URL aasServiceUrl )
138
+ public CustomAssetAdministrationShellEnvironment getAasEnvWithUrls (URL aasServiceUrl , boolean onlySubmodels )
127
139
throws IOException , DeserializationException {
128
140
var aasServiceUrlString = aasServiceUrl .toString ();
129
141
130
- var model = readModel (aasServiceUrl );
142
+ var model = readModel (aasServiceUrl , onlySubmodels );
131
143
// Add urls to all shells
132
144
model .getAssetAdministrationShells ().forEach (shell -> shell .setSourceUrl (
133
145
format ("%s/shells/%s" , aasServiceUrlString ,
@@ -139,9 +151,9 @@ public CustomAssetAdministrationShellEnvironment getAasEnvWithUrls(URL aasServic
139
151
Encoder .encodeBase64 (submodel .getIdentification ().getId ())));
140
152
submodel .getSubmodelElements ()
141
153
.forEach (elem -> putUrlRec (
142
- format ("%s/submodels/%s/submodel/submodel-elements" , aasServiceUrlString ,
143
- Encoder .encodeBase64 (submodel .getIdentification ().getId ())),
144
- elem ));
154
+ format ("%s/submodels/%s/submodel/submodel-elements" , aasServiceUrlString ,
155
+ Encoder .encodeBase64 (submodel .getIdentification ().getId ())),
156
+ elem ));
145
157
});
146
158
model .getSubmodels ().forEach (submodel -> AASUtil .getAllSubmodelElements (submodel )
147
159
.forEach (element -> element .setSourceUrl (
@@ -158,36 +170,53 @@ public CustomAssetAdministrationShellEnvironment getAasEnvWithUrls(URL aasServic
158
170
/**
159
171
* Returns the AAS model.
160
172
*/
161
- private CustomAssetAdministrationShellEnvironment readModel (URL aasServiceUrl )
173
+ private CustomAssetAdministrationShellEnvironment readModel (URL aasServiceUrl , boolean onlySubmodels )
162
174
throws IOException , DeserializationException {
175
+ var aasEnv = new CustomAssetAdministrationShellEnvironment ();
163
176
164
- String shellResponse ;
165
- String conceptResponse ;
166
- String submodelResponse ;
177
+ URL shellsUrl ;
178
+ URL conceptDescriptionsUrl ;
179
+ URL submodelUrl ;
167
180
try {
168
- shellResponse =
169
- Objects .requireNonNull (httpRestClient .get (aasServiceUrl .toURI ().resolve ("/shells" ).toURL ()).body ()).string ();
170
- submodelResponse =
171
- Objects .requireNonNull (httpRestClient .get (aasServiceUrl .toURI ().resolve ("/submodels" ).toURL ()).body ()).string ();
172
- conceptResponse = Objects .requireNonNull (httpRestClient .get (aasServiceUrl .toURI ().resolve ("/concept" +
173
- "-descriptions" ).toURL ()).body ())
174
- .string ();
175
- } catch (URISyntaxException e ) {
176
- throw new EdcException (e .getMessage ());
181
+ submodelUrl = aasServiceUrl .toURI ().resolve ("/submodels" ).toURL ();
182
+ shellsUrl = aasServiceUrl .toURI ().resolve ("/shells" ).toURL ();
183
+ conceptDescriptionsUrl = aasServiceUrl .toURI ().resolve ("/concept-descriptions" ).toURL ();
184
+ } catch (URISyntaxException resolveUriException ) {
185
+ throw new EdcException (
186
+ format ("Error while building URLs for reading from the AAS Service at %s" , aasServiceUrl ),
187
+ resolveUriException );
177
188
}
178
189
179
- CustomAssetAdministrationShell [] shells = objectMapper .readValue (shellResponse ,
180
- CustomAssetAdministrationShell [].class );
181
- CustomConceptDescription [] conceptDescriptions = objectMapper .readValue (conceptResponse ,
182
- CustomConceptDescription [].class );
190
+ aasEnv .setSubmodels (readSubmodels (submodelUrl , onlySubmodels ));
191
+ if (!onlySubmodels ) {
192
+ aasEnv .setAssetAdministrationShells (readShells (shellsUrl ));
193
+ aasEnv .setConceptDescriptions (readConceptDescriptions (conceptDescriptionsUrl ));
194
+ }
195
+ return aasEnv ;
196
+ }
197
+
198
+ private List <CustomConceptDescription > readConceptDescriptions (URL conceptDescriptionsUrl ) throws IOException {
183
199
184
- // Because of SMCs "value" field, submodels have to be parsed manually
200
+ var conceptResponse = Objects .requireNonNull (httpRestClient .get (conceptDescriptionsUrl ).body ()).string ();
201
+
202
+ return Arrays .asList (objectMapper .readValue (conceptResponse , CustomConceptDescription [].class ));
203
+ }
185
204
186
- // First, parse into full admin-shell.io submodels:
205
+ private List <CustomAssetAdministrationShell > readShells (URL shellsUrl ) throws IOException {
206
+ var shellHttpResponse = Objects .requireNonNull (httpRestClient .get (shellsUrl ).body ()).string ();
207
+
208
+ return Arrays .asList (objectMapper .readValue (shellHttpResponse , CustomAssetAdministrationShell [].class ));
209
+ }
210
+
211
+ private List <CustomSubmodel > readSubmodels (URL submodelUrl , boolean onlySubmodels )
212
+ throws IOException , DeserializationException {
213
+ var submodelHttpResponse = Objects .requireNonNull (httpRestClient .get (submodelUrl ).body ()).string ();
214
+
215
+ // First, parse into "full" admin-shell.io submodels:
187
216
JsonDeserializer jsonDeserializer = new JsonDeserializer ();
188
- List <DefaultSubmodel > submodels = jsonDeserializer .readReferables (submodelResponse , DefaultSubmodel .class );
217
+ List <DefaultSubmodel > submodels = jsonDeserializer .readReferables (submodelHttpResponse , DefaultSubmodel .class );
189
218
190
- // Now, create custom submodels from the data of the full submodels
219
+ // Now, create customSubmodels from the " full" submodels
191
220
List <CustomSubmodel > customSubmodels = new ArrayList <>();
192
221
for (Submodel submodel : submodels ) {
193
222
var customSubmodel = new CustomSubmodel ();
@@ -197,20 +226,14 @@ private CustomAssetAdministrationShellEnvironment readModel(URL aasServiceUrl)
197
226
customSubmodel .setIdentification (customIdentification );
198
227
199
228
customSubmodel .setIdShort (submodel .getIdShort ());
200
-
201
- // Recursively add submodelElements
202
- var customElements = AASUtil .getCustomSubmodelElementStructureFromSubmodel (submodel );
203
- customSubmodel .setSubmodelElements ((List <CustomSubmodelElement >) customElements );
204
-
229
+ if (! onlySubmodels ) {
230
+ // Recursively add submodelElements
231
+ var customElements = AASUtil .getCustomSubmodelElementStructureFromSubmodel (submodel );
232
+ customSubmodel .setSubmodelElements ((List <CustomSubmodelElement >) customElements );
233
+ }
205
234
customSubmodels .add (customSubmodel );
206
235
}
207
- var aasEnv = new CustomAssetAdministrationShellEnvironment ();
208
-
209
- aasEnv .setAssetAdministrationShells (Arrays .asList (shells ));
210
- aasEnv .setSubmodels (customSubmodels );
211
- aasEnv .setConceptDescriptions (Arrays .asList (conceptDescriptions ));
212
-
213
- return aasEnv ;
236
+ return customSubmodels ;
214
237
}
215
238
216
239
/**
0 commit comments