3232import java .util .Collections ;
3333import java .util .HashSet ;
3434import java .util .List ;
35+ import java .util .Map ;
3536import java .util .Set ;
3637
3738import javax .xml .parsers .ParserConfigurationException ;
4849import org .eclipse .basyx .aas .factory .aasx .SubmodelFileEndpointLoader ;
4950import org .eclipse .basyx .aas .factory .json .JSONAASBundleFactory ;
5051import org .eclipse .basyx .aas .factory .xml .XMLAASBundleFactory ;
52+ import org .eclipse .basyx .aas .manager .ConnectedAssetAdministrationShellManager ;
53+ import org .eclipse .basyx .aas .metamodel .api .IAssetAdministrationShell ;
54+ import org .eclipse .basyx .aas .metamodel .map .AssetAdministrationShell ;
5155import org .eclipse .basyx .aas .metamodel .map .descriptor .AASDescriptor ;
5256import org .eclipse .basyx .aas .metamodel .map .descriptor .SubmodelDescriptor ;
5357import org .eclipse .basyx .aas .registration .api .IAASRegistry ;
5458import org .eclipse .basyx .aas .registration .proxy .AASRegistryProxy ;
59+ import org .eclipse .basyx .aas .restapi .MultiSubmodelProvider ;
5560import org .eclipse .basyx .components .IComponent ;
5661import org .eclipse .basyx .components .aas .aascomponent .IAASServerDecorator ;
5762import org .eclipse .basyx .components .aas .aascomponent .IAASServerFeature ;
7277import org .eclipse .basyx .extensions .aas .aggregator .aasxupload .AASAggregatorAASXUpload ;
7378import org .eclipse .basyx .submodel .metamodel .api .ISubmodel ;
7479import org .eclipse .basyx .submodel .metamodel .api .identifier .IIdentifier ;
80+ import org .eclipse .basyx .submodel .metamodel .map .Submodel ;
81+ import org .eclipse .basyx .vab .exception .provider .ProviderException ;
7582import org .eclipse .basyx .vab .exception .provider .ResourceNotFoundException ;
7683import org .eclipse .basyx .vab .modelprovider .VABPathTools ;
84+ import org .eclipse .basyx .vab .protocol .api .IConnectorFactory ;
85+ import org .eclipse .basyx .vab .protocol .http .connector .HTTPConnectorFactory ;
7786import org .eclipse .basyx .vab .protocol .http .server .BaSyxContext ;
7887import org .eclipse .basyx .vab .protocol .http .server .BaSyxHTTPServer ;
7988import org .eclipse .basyx .vab .protocol .http .server .VABHTTPInterface ;
8695 * remote. It uses the Aggregator API, i.e. AAS should be pushed to
8796 * ${URL}/shells
8897 *
89- * @author schnicke, espen, fried, fischer
98+ * @author schnicke, espen, fried, fischer, danish
9099 *
91100 */
92101@ SuppressWarnings ("deprecation" )
@@ -107,8 +116,13 @@ public class AASServerComponent implements IComponent {
107116 // Initial AASBundle
108117 protected Collection <AASBundle > aasBundles ;
109118
119+ private IAASAggregator aggregator ;
110120 // Watcher for AAS Aggregator functionality
111121 private boolean isAASXUploadEnabled = false ;
122+
123+ private static final String PREFIX_SUBMODEL_PATH = "/aas/submodels/" ;
124+
125+ private ConnectedAssetAdministrationShellManager manager ;
112126
113127 /**
114128 * Constructs an empty AAS server using the passed context
@@ -181,13 +195,37 @@ public void setRegistry(IAASRegistry registry) {
181195 this .registry = registry ;
182196 }
183197
198+ /**
199+ * Explicitly sets AAS bundles that should be loaded during startup
200+ *
201+ * @param aasBundles
202+ * The bundles that will be loaded during startup
203+ */
204+ public void setAASBundles (Collection <AASBundle > aasBundles ) {
205+ this .aasBundles = aasBundles ;
206+ }
207+
208+ /**
209+ * Explicitly sets an AAS bundle that should be loaded during startup
210+ *
211+ * @param aasBundle
212+ * The bundle that will be loaded during startup
213+ */
214+ public void setAASBundle (AASBundle aasBundle ) {
215+ this .aasBundles = Collections .singleton (aasBundle );
216+ }
217+
184218 /**
185219 * Starts the AASX component at http://${hostName}:${port}/${path}
186220 */
187221 @ Override
188222 public void startComponent () {
189223 logger .info ("Create the server..." );
190224 registry = createRegistryFromConfig (aasConfig );
225+
226+ IConnectorFactory connectorFactory = new HTTPConnectorFactory ();
227+
228+ manager = new ConnectedAssetAdministrationShellManager (registry , connectorFactory );
191229
192230 loadAASServerFeaturesFromConfig ();
193231 initializeAASServerFeatures ();
@@ -210,6 +248,63 @@ public void startComponent() {
210248 logger .info ("Start the server" );
211249 server = new BaSyxHTTPServer (context );
212250 server .start ();
251+
252+ registerPreexistingAASAndSMIfPossible ();
253+ }
254+
255+ private void registerPreexistingAASAndSMIfPossible () {
256+ if (!shouldRegisterPreexistingAASAndSM ()) {
257+ return ;
258+ }
259+
260+ aggregator .getAASList ().stream ().forEach (this ::registerAASAndSubmodels );
261+ }
262+
263+ private boolean shouldRegisterPreexistingAASAndSM () {
264+ return isMongoDBBackend () && registry != null ;
265+ }
266+
267+ private void registerAASAndSubmodels (IAssetAdministrationShell aas ) {
268+ registerAAS (aas );
269+
270+ registerSubmodels (aas );
271+ }
272+
273+ private void registerAAS (IAssetAdministrationShell aas ) {
274+ try {
275+ manager .createAAS ((AssetAdministrationShell ) aas , getURL ());
276+ logger .info ("The AAS " + aas .getIdShort () + " is Successfully Registered from DB" );
277+ } catch (Exception e ) {
278+ logger .info ("The AAS " + aas .getIdShort () + " could not be Registered from DB" + e );
279+ }
280+ }
281+
282+ private void registerSubmodels (IAssetAdministrationShell aas ) {
283+ List <ISubmodel > submodels = getSubmodelFromAggregator (aggregator , aas .getIdentification ());
284+ try {
285+ submodels .stream ().forEach (submodel -> manager .createSubmodel (aas .getIdentification (), (Submodel ) submodel ));
286+ logger .info ("The submodels from AAS " + aas .getIdShort () + " are Successfully Registered from DB" );
287+ } catch (Exception e ) {
288+ logger .info ("The submodel from AAS " + aas .getIdShort () + " could not be Registered from DB " + e );
289+ }
290+ }
291+
292+ private List <ISubmodel > getSubmodelFromAggregator (IAASAggregator aggregator , IIdentifier iIdentifier ) {
293+ MultiSubmodelProvider aasProvider = (MultiSubmodelProvider ) aggregator .getAASProvider (iIdentifier );
294+
295+ @ SuppressWarnings ("unchecked" )
296+ List <Object > submodelObject = (List <Object >) aasProvider .getValue (PREFIX_SUBMODEL_PATH );
297+
298+ List <ISubmodel > persistentSubmodelList = new ArrayList <>();
299+
300+ submodelObject .stream ().map (this ::getSubmodel ).forEach (persistentSubmodelList ::add );
301+
302+ return persistentSubmodelList ;
303+ }
304+
305+ @ SuppressWarnings ("unchecked" )
306+ private ISubmodel getSubmodel (Object submodelObject ) {
307+ return Submodel .createAsFacade ((Map <String , Object >) submodelObject );
213308 }
214309
215310 private void loadAASServerFeaturesFromConfig () {
@@ -239,13 +334,57 @@ public String getURL() {
239334
240335 @ Override
241336 public void stopComponent () {
242-
243- // Remove all AASs/SMs that were registered on startup
244- AASBundleHelper .deregister (registry , aasBundles );
337+ deregisterAASAndSmAddedDuringRuntime ();
338+
245339 cleanUpAASServerFeatures ();
246340
247341 server .shutdown ();
248342 }
343+
344+ private void deregisterAASAndSmAddedDuringRuntime () {
345+ if (registry == null ) {
346+ return ;
347+ }
348+
349+ try {
350+ aggregator .getAASList ().stream ().forEach (this ::deregisterAASAndAccompanyingSM );
351+ } catch (RuntimeException e ) {
352+ logger .info ("The resource could not be found in the aggregator " + e );
353+ }
354+
355+ }
356+
357+ private void deregisterAASAndAccompanyingSM (IAssetAdministrationShell aas ) {
358+ getSubmodelDescriptors (aas .getIdentification ()).stream ().forEach (submodelDescriptor -> deregisterSubmodel (aas .getIdentification (), submodelDescriptor ));
359+
360+ deregisterAAS (aas .getIdentification ());
361+ }
362+
363+ private List <SubmodelDescriptor > getSubmodelDescriptors (IIdentifier aasIdentifier ) {
364+ try {
365+ return registry .lookupSubmodels (aasIdentifier );
366+ } catch (ResourceNotFoundException e ) {
367+ return Collections .emptyList ();
368+ }
369+ }
370+
371+ private void deregisterSubmodel (IIdentifier aasIdentifier , SubmodelDescriptor submodelDescriptor ) {
372+ try {
373+ registry .delete (aasIdentifier , submodelDescriptor .getIdentifier ());
374+ logger .info ("The SM '" + submodelDescriptor .getIdShort () + "' successfully deregistered." );
375+ } catch (ProviderException e ) {
376+ logger .info ("The SM '" + submodelDescriptor .getIdShort () + "' can't be deregistered. It was not found in registry." );
377+ }
378+ }
379+
380+ private void deregisterAAS (IIdentifier aasIdentifier ) {
381+ try {
382+ registry .delete (aasIdentifier );
383+ logger .info ("The AAS '" + aasIdentifier .getId () + "' successfully deregistered." );
384+ } catch (ProviderException e ) {
385+ logger .info ("The AAS '" + aasIdentifier .getId () + "' can't be deregistered. It was not found in registry." );
386+ }
387+ }
249388
250389 public void addAASServerFeature (IAASServerFeature aasServerFeature ) {
251390 aasServerFeatureList .add (aasServerFeature );
@@ -303,9 +442,9 @@ private static Set<AASBundle> loadBundleFromAASX(String aasxPath) throws IOExcep
303442 }
304443
305444 private VABHTTPInterface <?> createAggregatorServlet () {
306- IAASAggregator aggregator = createAASAggregator ();
307- aasBundles = loadAASFromSource ( aasConfig . getAASSourceAsList () );
308-
445+ aggregator = createAASAggregator ();
446+ loadAASBundles ( );
447+
309448 if (aasBundles != null ) {
310449 AASBundleHelper .integrate (aggregator , aasBundles );
311450 }
@@ -327,7 +466,7 @@ private IAASAggregator createAASAggregator() {
327466 private boolean isMongoDBBackend () {
328467 return aasConfig .getAASBackend ().equals (AASServerBackend .MONGODB );
329468 }
330-
469+
331470 private BaSyxMongoDBConfiguration createMongoDbConfiguration () {
332471 BaSyxMongoDBConfiguration config ;
333472 if (this .mongoDBConfig == null ) {
@@ -349,6 +488,15 @@ private List<IAASServerDecorator> createAASServerDecoratorList() {
349488 return aasServerDecoratorList ;
350489 }
351490
491+ private void loadAASBundles () {
492+ if (aasBundles != null ) {
493+ return ;
494+ }
495+
496+ List <String > aasSources = aasConfig .getAASSourceAsList ();
497+ aasBundles = loadAASFromSource (aasSources );
498+ }
499+
352500 private Set <AASBundle > loadAASFromSource (List <String > aasSources ) {
353501 if (aasSources .isEmpty ()) {
354502 return Collections .emptySet ();
0 commit comments