13
13
import java .net .URISyntaxException ;
14
14
import java .nio .file .Files ;
15
15
import java .nio .file .Paths ;
16
+ import java .security .ProviderException ;
16
17
import java .util .Collection ;
17
18
import java .util .List ;
18
19
import java .util .Set ;
34
35
import org .eclipse .basyx .aas .metamodel .map .descriptor .SubmodelDescriptor ;
35
36
import org .eclipse .basyx .aas .registration .api .IAASRegistry ;
36
37
import org .eclipse .basyx .aas .registration .proxy .AASRegistryProxy ;
37
- import org .eclipse .basyx .aas .restapi .api .IAASAPIFactory ;
38
- import org .eclipse .basyx .aas .restapi .vab .VABAASAPIFactory ;
39
38
import org .eclipse .basyx .components .IComponent ;
40
39
import org .eclipse .basyx .components .aas .aasx .AASXPackageManager ;
41
40
import org .eclipse .basyx .components .aas .configuration .AASServerBackend ;
42
41
import org .eclipse .basyx .components .aas .configuration .BaSyxAASServerConfiguration ;
43
42
import org .eclipse .basyx .components .aas .mongodb .MongoDBAASAggregator ;
44
- import org .eclipse .basyx .components .aas .mqtt .MqttSubmodelAPIFactory ;
45
43
import org .eclipse .basyx .components .aas .servlet .AASAggregatorAASXUploadServlet ;
46
44
import org .eclipse .basyx .components .aas .servlet .AASAggregatorServlet ;
47
45
import org .eclipse .basyx .components .configuration .BaSyxConfiguration ;
48
46
import org .eclipse .basyx .components .configuration .BaSyxContextConfiguration ;
49
47
import org .eclipse .basyx .components .configuration .BaSyxMongoDBConfiguration ;
50
48
import org .eclipse .basyx .components .configuration .BaSyxMqttConfiguration ;
51
49
import org .eclipse .basyx .extensions .aas .aggregator .aasxupload .AASAggregatorAASXUpload ;
50
+ import org .eclipse .basyx .extensions .aas .aggregator .mqtt .MqttAASAggregator ;
52
51
import org .eclipse .basyx .submodel .metamodel .api .ISubmodel ;
53
52
import org .eclipse .basyx .submodel .metamodel .api .identifier .IIdentifier ;
54
- import org .eclipse .basyx .submodel .restapi .api .ISubmodelAPIFactory ;
55
53
import org .eclipse .basyx .vab .exception .provider .ResourceNotFoundException ;
56
54
import org .eclipse .basyx .vab .modelprovider .VABPathTools ;
57
55
import org .eclipse .basyx .vab .protocol .http .server .BaSyxContext ;
58
56
import org .eclipse .basyx .vab .protocol .http .server .BaSyxHTTPServer ;
59
57
import org .eclipse .basyx .vab .protocol .http .server .VABHTTPInterface ;
58
+ import org .eclipse .paho .client .mqttv3 .MqttException ;
60
59
import org .slf4j .Logger ;
61
60
import org .slf4j .LoggerFactory ;
62
61
import org .xml .sax .SAXException ;
@@ -84,7 +83,7 @@ public class AASServerComponent implements IComponent {
84
83
85
84
// Initial AASBundle
86
85
protected Collection <AASBundle > aasBundles ;
87
-
86
+
88
87
// Watcher for AAS Aggregator functionality
89
88
private boolean isAASXUploadEnabled = false ;
90
89
@@ -107,17 +106,17 @@ public AASServerComponent(BaSyxContextConfiguration contextConfig, BaSyxAASServe
107
106
/**
108
107
* Constructs an empty AAS server using the passed configuration
109
108
*/
110
- public AASServerComponent (BaSyxContextConfiguration contextConfig , BaSyxAASServerConfiguration aasConfig ,
111
- BaSyxMongoDBConfiguration mongoDBConfig ) {
109
+ public AASServerComponent (BaSyxContextConfiguration contextConfig , BaSyxAASServerConfiguration aasConfig , BaSyxMongoDBConfiguration mongoDBConfig ) {
112
110
this .contextConfig = contextConfig ;
113
111
this .aasConfig = aasConfig ;
114
112
this .aasConfig .setAASBackend (AASServerBackend .MONGODB );
115
113
this .mongoDBConfig = mongoDBConfig ;
116
114
}
117
115
118
116
/**
119
- * Sets and enables mqtt connection configuration for this component. Has to be called before the component is
120
- * started. Currently only works for InMemory backend.
117
+ * Sets and enables mqtt connection configuration for this component. Has to be
118
+ * called before the component is started. Currently only works for InMemory
119
+ * backend.
121
120
*
122
121
* @param configuration
123
122
*/
@@ -126,12 +125,13 @@ public void enableMQTT(BaSyxMqttConfiguration configuration) {
126
125
}
127
126
128
127
/**
129
- * Disables mqtt configuration. Has to be called before the component is started.
128
+ * Disables mqtt configuration. Has to be called before the component is
129
+ * started.
130
130
*/
131
131
public void disableMQTT () {
132
132
this .mqttConfig = null ;
133
133
}
134
-
134
+
135
135
/**
136
136
* Enables AASX upload functionality
137
137
*/
@@ -155,13 +155,11 @@ public void setRegistry(IAASRegistry registry) {
155
155
public void startComponent () {
156
156
logger .info ("Create the server..." );
157
157
// Load the aggregator servlet
158
- createRegistryFromUrl ();
159
- VABHTTPInterface <?> aggregatorServlet = loadAggregatorServlet ();
158
+ registry = createRegistryFromConfig (aasConfig );
160
159
161
160
// Init HTTP context and add an XMLAASServlet according to the configuration
162
161
BaSyxContext context = contextConfig .createBaSyxContext ();
163
- context .addServletMapping ("/*" , aggregatorServlet );
164
-
162
+ context .addServletMapping ("/*" , createAggregatorServlet ());
165
163
166
164
// An initial AAS has been loaded from the drive?
167
165
if (aasBundles != null ) {
@@ -191,10 +189,10 @@ public String getURL() {
191
189
192
190
@ Override
193
191
public void stopComponent () {
194
-
192
+
195
193
// Remove all AASs/SMs that were registered on startup
196
194
AASBundleHelper .deregister (registry , aasBundles );
197
-
195
+
198
196
server .shutdown ();
199
197
}
200
198
@@ -221,8 +219,7 @@ private void loadBundleFromJSON(String jsonPath) throws IOException {
221
219
this .aasBundles = new JSONAASBundleFactory (jsonContent ).create ();
222
220
}
223
221
224
- private void loadBundleFromAASX (String aasxPath )
225
- throws IOException , ParserConfigurationException , SAXException , URISyntaxException , InvalidFormatException {
222
+ private void loadBundleFromAASX (String aasxPath ) throws IOException , ParserConfigurationException , SAXException , URISyntaxException , InvalidFormatException {
226
223
logger .info ("Loading aas from aasx \" " + aasxPath + "\" " );
227
224
228
225
// Instantiate the aasx package manager
@@ -235,25 +232,73 @@ private void loadBundleFromAASX(String aasxPath)
235
232
this .aasBundles = packageManager .retrieveAASBundles ();
236
233
}
237
234
238
- private VABHTTPInterface <?> loadAggregatorServlet () {
239
- // Load the initial AAS bundles from given source
235
+ private VABHTTPInterface <?> createAggregatorServlet () {
240
236
loadAASFromSource (aasConfig .getAASSource ());
237
+ IAASAggregator aggregator = createAggregator ();
241
238
242
- // Load the aggregator
243
- IAASAggregator aggregator = loadAASAggregator ();
244
-
245
- // Integrate the loaded bundles into the aggregator
246
239
if (aasBundles != null ) {
247
240
AASBundleHelper .integrate (aggregator , aasBundles );
248
241
}
249
242
250
- // Return the servlet for the resulting aggregator
251
243
if (isAASXUploadEnabled ) {
252
- return new AASAggregatorAASXUploadServlet (new AASAggregatorAASXUpload (aggregator ));
244
+ return new AASAggregatorAASXUploadServlet (new AASAggregatorAASXUpload (aggregator ));
253
245
} else {
254
246
return new AASAggregatorServlet (aggregator );
255
247
}
256
-
248
+ }
249
+
250
+ private IAASAggregator createAggregator () {
251
+ final IAASAggregator aggregatorBackend = createAggregatorBackend ();
252
+ final IAASAggregator decoratedRegistry = decorate (aggregatorBackend );
253
+ return decoratedRegistry ;
254
+ }
255
+
256
+ private IAASAggregator decorate (IAASAggregator aasAggregator ) {
257
+ IAASAggregator decoratedAggregator = aasAggregator ;
258
+ if (this .mqttConfig != null ) {
259
+ try {
260
+ decoratedAggregator = new MqttAASAggregator (decoratedAggregator , mqttConfig .getServer (), getMqttClientId ());
261
+ } catch (MqttException e ) {
262
+ throw new ProviderException ("moquette.conf Error" + e .getMessage ());
263
+ }
264
+ logger .info ("Enable MQTT events for broker " + this .mqttConfig .getServer ());
265
+ }
266
+ return decoratedAggregator ;
267
+ }
268
+
269
+ private IAASAggregator createAggregatorBackend () {
270
+ final AASServerBackend backendType = aasConfig .getAASBackend ();
271
+ switch (backendType ) {
272
+ case MONGODB :
273
+ return createMongoDBAggregatorBackend ();
274
+ case INMEMORY :
275
+ return createInMemoryAggregatorBackend ();
276
+ default :
277
+ throw new RuntimeException ("Unknown backend type " + backendType );
278
+ }
279
+ }
280
+
281
+ private IAASAggregator createMongoDBAggregatorBackend () {
282
+ logger .info ("Using MongoDB backend" );
283
+ return createMongoDBAggregator ();
284
+ }
285
+
286
+ private IAASAggregator createInMemoryAggregatorBackend () {
287
+ logger .info ("Using InMemory backend" );
288
+ return new AASAggregator (registry );
289
+ }
290
+
291
+ private IAASAggregator createMongoDBAggregator () {
292
+ BaSyxMongoDBConfiguration config ;
293
+ if (this .mongoDBConfig == null ) {
294
+ config = new BaSyxMongoDBConfiguration ();
295
+ config .loadFromDefaultSource ();
296
+ } else {
297
+ config = this .mongoDBConfig ;
298
+ }
299
+ MongoDBAASAggregator aggregator = new MongoDBAASAggregator (config );
300
+ aggregator .setRegistry (registry );
301
+ return aggregator ;
257
302
}
258
303
259
304
private void loadAASFromSource (String aasSource ) {
@@ -278,17 +323,19 @@ private void loadAASFromSource(String aasSource) {
278
323
/**
279
324
* Only creates the registry, if it hasn't been set explicitly before
280
325
*/
281
- private void createRegistryFromUrl ( ) {
326
+ private IAASRegistry createRegistryFromConfig ( BaSyxAASServerConfiguration aasConfig ) {
282
327
if (this .registry != null ) {
283
328
// Do not overwrite an explicitly set registry
284
- return ;
329
+ return this . registry ;
285
330
}
286
- // Load registry url from config
287
- String registryUrl = this .aasConfig .getRegistry ();
288
- if (registryUrl != null && !registryUrl .isEmpty ()) {
289
- registry = new AASRegistryProxy (registryUrl );
290
- logger .info ("Registry loaded at \" " + registryUrl + "\" " );
331
+ String registryUrl = aasConfig .getRegistry ();
332
+ if (registryUrl == null || registryUrl .isEmpty ()) {
333
+ return null ;
291
334
}
335
+ // Load registry url from config
336
+ logger .info ("Registry loaded at \" " + registryUrl + "\" " );
337
+ return new AASRegistryProxy (registryUrl );
338
+
292
339
}
293
340
294
341
private void registerEnvironment () {
@@ -387,7 +434,8 @@ private String getComponentBasePath() {
387
434
}
388
435
389
436
/**
390
- * Fixes the File submodel element value paths according to the given endpoint configuration
437
+ * Fixes the File submodel element value paths according to the given endpoint
438
+ * configuration
391
439
*/
392
440
private void modifyFilePaths (String hostName , int port , String rootPath ) {
393
441
rootPath = rootPath + "/files" ;
@@ -399,41 +447,11 @@ private void modifyFilePaths(String hostName, int port, String rootPath) {
399
447
}
400
448
}
401
449
402
- /**
403
- * Loads a aas aggregator servlet with a backend according to the configuration
404
- *
405
- * @return
406
- */
407
- private IAASAggregator loadAASAggregator () {
408
- // Get aggregator according to backend config
409
- AASServerBackend backendType = aasConfig .getAASBackend ();
410
- IAASAggregator aggregator = null ;
411
- if (backendType == AASServerBackend .INMEMORY && mqttConfig == null ) {
412
- logger .info ("Using InMemory backend" );
413
- aggregator = new AASAggregator (registry );
414
- } else if (backendType == AASServerBackend .INMEMORY && mqttConfig != null ) {
415
- logger .info ("Using InMemory backend with MQTT providers" );
416
- IAASAPIFactory aasApiProvider = new VABAASAPIFactory ();
417
- ISubmodelAPIFactory smApiProvider = new MqttSubmodelAPIFactory (mqttConfig );
418
- aggregator = new AASAggregator (aasApiProvider , smApiProvider , registry );
419
- } else if ( backendType == AASServerBackend .MONGODB ) {
420
- logger .info ("Using MongoDB backend" );
421
- aggregator = loadMongoDBAggregator ();
450
+ private String getMqttClientId () {
451
+ if (aasBundles == null || aasBundles .isEmpty ()) {
452
+ return "defaultNoShellId" ;
422
453
}
423
-
424
- return aggregator ;
454
+ return aasBundles .stream ().findFirst ().get ().getAAS ().getIdShort ();
425
455
}
426
456
427
- private IAASAggregator loadMongoDBAggregator () {
428
- BaSyxMongoDBConfiguration config ;
429
- if (this .mongoDBConfig == null ) {
430
- config = new BaSyxMongoDBConfiguration ();
431
- config .loadFromDefaultSource ();
432
- } else {
433
- config = this .mongoDBConfig ;
434
- }
435
- MongoDBAASAggregator aggregator = new MongoDBAASAggregator (config );
436
- aggregator .setRegistry (registry );
437
- return aggregator ;
438
- }
439
- }
457
+ }
0 commit comments