From a93dde0bf310f1fea0c8d4a8ba6b966f8be0224f Mon Sep 17 00:00:00 2001 From: Zai Zhang Date: Wed, 9 Aug 2023 12:22:19 +0200 Subject: [PATCH] Change getSubmodelList() von MongoDBSubmodelAggregator.java to retrieve only submodels that belongs to the shell. Signed-off-by: Zai Zhang Co-authored-by: Jannis Jung --- .../aas/mongodb/MongoDBAASAggregator.java | 1 - .../mongodb/MongoDBSubmodelAggregator.java | 46 +++++++++++++------ .../TestMongoDBSubmodelAggregator.java | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBAASAggregator.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBAASAggregator.java index 05e98726..881e86a9 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBAASAggregator.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBAASAggregator.java @@ -491,7 +491,6 @@ private void addSubmodelProvidersById(String submodelIdentificationId, MultiSubm } - @SuppressWarnings("unchecked") @Override public Collection getAASList() { return shellStorageApi.retrieveAll().stream().map(aas -> (IAssetAdministrationShell) aas).collect(Collectors.toList()); diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBSubmodelAggregator.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBSubmodelAggregator.java index bf8ec5d8..791e001f 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBSubmodelAggregator.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/main/java/org/eclipse/basyx/components/aas/mongodb/MongoDBSubmodelAggregator.java @@ -25,26 +25,25 @@ package org.eclipse.basyx.components.aas.mongodb; -import static org.springframework.data.mongodb.core.query.Criteria.where; -import static org.springframework.data.mongodb.core.query.Query.query; - import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; +import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration; import org.eclipse.basyx.components.internal.mongodb.MongoDBBaSyxStorageAPI; import org.eclipse.basyx.components.internal.mongodb.MongoDBBaSyxStorageAPIFactory; import org.eclipse.basyx.submodel.aggregator.SubmodelAggregator; import org.eclipse.basyx.submodel.metamodel.api.ISubmodel; import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; +import org.eclipse.basyx.submodel.metamodel.api.reference.IKey; +import org.eclipse.basyx.submodel.metamodel.api.reference.IReference; import org.eclipse.basyx.submodel.metamodel.map.Submodel; import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPI; import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPIFactory; import org.eclipse.basyx.submodel.restapi.vab.VABSubmodelAPIFactory; import org.eclipse.basyx.vab.exception.provider.ResourceNotFoundException; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.query.Query; import com.mongodb.client.MongoClient; @@ -56,6 +55,7 @@ */ public class MongoDBSubmodelAggregator extends SubmodelAggregator { private MongoDBBaSyxStorageAPI storageApi; + private MongoDBBaSyxStorageAPI aasStorageApi; private IIdentifier shellId; public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMongoDBConfiguration config, MongoClient client) { @@ -64,6 +64,7 @@ public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMo public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMongoDBConfiguration config, MongoClient client, IIdentifier shellId) { this(submodelApiFactory, MongoDBBaSyxStorageAPIFactory.create(config.getSubmodelCollection(), Submodel.class, config, client)); + aasStorageApi = MongoDBBaSyxStorageAPIFactory.create(config.getAASCollection(), AssetAdministrationShell.class, config, client); this.shellId = shellId; } @@ -90,13 +91,33 @@ public void deleteSubmodelByIdShort(String idShort) { @Override public Collection getSubmodelList() { - MongoOperations mongoOperation = (MongoOperations) storageApi.getStorageConnection(); - Query hasParentId = query(where("parent.keys.[0].value").is(shellId.getId())); - List submodels = mongoOperation.find(hasParentId, ISubmodel.class, storageApi.getCollectionName()); - return submodels; + if (shellId == null) + return returnAllSubmodels(); + + AssetAdministrationShell shell = aasStorageApi.retrieve(shellId.getId()); + Collection submodelRefs = shell.getSubmodelReferences(); + + if (submodelRefs.isEmpty()) { + return returnAllSubmodels(); + } + List submodelIds = submodelRefs.stream().map(ref -> { + return getLastKeyFromReference(ref).getValue(); + }).collect(Collectors.toList()); + + return submodelIds.stream().map(sm -> { + return storageApi.retrieve(sm); + }).collect(Collectors.toList()); } + private List returnAllSubmodels() { + return storageApi.retrieveAll().stream().map(submodel -> (ISubmodel) submodel).collect(Collectors.toList()); + } + private IKey getLastKeyFromReference(IReference reference) { + List keys = reference.getKeys(); + IKey lastKey = keys.get(keys.size() - 1); + return lastKey; + } @Override public ISubmodel getSubmodel(IIdentifier identifier) throws ResourceNotFoundException { @@ -121,18 +142,13 @@ public void createSubmodel(ISubmodelAPI submodelAPI) { @Override public ISubmodel getSubmodelbyIdShort(String idShort) throws ResourceNotFoundException { Optional submodelOptional = getSubmodelList().stream().filter(submodel -> { - return isTargetSubmodel(shellId, submodel, idShort); + return submodel.getIdShort().equals(idShort); }).findAny(); if (submodelOptional.isEmpty()) throw new ResourceNotFoundException("The submodel with idShort '" + idShort + "' could not be found"); return submodelOptional.get(); } - private boolean isTargetSubmodel(IIdentifier shellId, ISubmodel submodel, String idShort) { - String shellIdValue = submodel.getParent().getKeys().get(0).getValue(); - return (submodel.getIdShort().equals(idShort)) && shellIdValue.equals(shellId.getId()); - } - @Override public ISubmodelAPI getSubmodelAPIById(IIdentifier identifier) throws ResourceNotFoundException { Submodel submodel = (Submodel) getSubmodel(identifier); diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAggregator.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAggregator.java index 5bbb4c83..a75bff98 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAggregator.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAggregator.java @@ -53,7 +53,7 @@ public class TestMongoDBSubmodelAggregator extends SubmodelAggregatorSuite { public static void initialize() { BaSyxMongoDBConfiguration config = getMongoDBConfiguration(); MongoClient client = MongoClients.create(config.getConnectionUrl()); - aggregator = new MongoDBSubmodelAggregator(new MongoDBSubmodelAPIFactory(config, client), config, client, new CustomId("testShellId")); + aggregator = new MongoDBSubmodelAggregator(new MongoDBSubmodelAPIFactory(config, client), config, client); } @Override