Skip to content

Commit

Permalink
Resolves Review Remarks
Browse files Browse the repository at this point in the history
Signed-off-by: Jannis Jung <jannis.jung@iese.fraunhofer.de>
  • Loading branch information
jannisjung committed Jul 7, 2023
1 parent 7fb1c30 commit 3a1e8db
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.basyx.components.internal.mongodb.MongoDBBaSyxStorageAPIFactory;
import org.eclipse.basyx.submodel.metamodel.api.reference.IKey;
import org.eclipse.basyx.submodel.metamodel.api.reference.IReference;
import org.eclipse.basyx.vab.exception.FeatureNotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -48,41 +49,38 @@
public class MongoDBAASAPI implements IAASAPI {
private Logger logger = LoggerFactory.getLogger(getClass());
private static final String DEFAULT_CONFIG_PATH = "mongodb.properties";
// private static final String AASIDPATH = Identifiable.IDENTIFICATION + "." +
// Identifier.ID;

// protected MongoOperations mongoOps;
protected String collectionName;
private MongoDBBaSyxStorageAPI<AssetAdministrationShell> storageApi;
private String identificationId;
private String shellIdentificationId;

/**
* Receives the path of the configuration.properties file in its constructor.
*
* @param config
*/
public MongoDBAASAPI(BaSyxMongoDBConfiguration config, String identificationId, MongoClient client) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(config.getAASCollection(), AssetAdministrationShell.class, config, client), identificationId);
public MongoDBAASAPI(BaSyxMongoDBConfiguration config, String shellIdentificationId, MongoClient client) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(config.getAASCollection(), AssetAdministrationShell.class, config, client), shellIdentificationId);
}

public MongoDBAASAPI(MongoDBBaSyxStorageAPI<AssetAdministrationShell> mongoDBStorageAPI, String identificationId) {
public MongoDBAASAPI(MongoDBBaSyxStorageAPI<AssetAdministrationShell> mongoDBStorageAPI, String shellIdentificationId) {
super();
this.storageApi = mongoDBStorageAPI;
this.identificationId = identificationId;
this.shellIdentificationId = shellIdentificationId;
}

/**
* Receives the path of the .properties file in its constructor from a resource.
*/
public MongoDBAASAPI(String resourceConfigPath, String identificationId, MongoClient client) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(configFromResource(resourceConfigPath).getSubmodelCollection(), AssetAdministrationShell.class, configFromResource(resourceConfigPath), client), identificationId);
public MongoDBAASAPI(String resourceConfigPath, String shellIdentificationId, MongoClient client) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(configFromResource(resourceConfigPath).getSubmodelCollection(), AssetAdministrationShell.class, configFromResource(resourceConfigPath), client), shellIdentificationId);
}

/**
* Constructor using default MongoDB connections
*/
public MongoDBAASAPI(String aasId, MongoClient client) {
this(DEFAULT_CONFIG_PATH, aasId, client);
public MongoDBAASAPI(String shellIdentificationId, MongoClient client) {
this(DEFAULT_CONFIG_PATH, shellIdentificationId, client);
}

/**
Expand All @@ -92,8 +90,8 @@ public MongoDBAASAPI(String aasId, MongoClient client) {
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAPI(BaSyxMongoDBConfiguration config, String identificationId) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(config.getAASCollection(), AssetAdministrationShell.class, config), identificationId);
public MongoDBAASAPI(BaSyxMongoDBConfiguration config, String shellIdentificationId) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(config.getAASCollection(), AssetAdministrationShell.class, config), shellIdentificationId);
}

/**
Expand All @@ -102,8 +100,8 @@ public MongoDBAASAPI(BaSyxMongoDBConfiguration config, String identificationId)
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAPI(String resourceConfigPath, String identificationId) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(configFromResource(resourceConfigPath).getSubmodelCollection(), AssetAdministrationShell.class, configFromResource(resourceConfigPath)), identificationId);
public MongoDBAASAPI(String resourceConfigPath, String shellIdentificationId) {
this(MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(configFromResource(resourceConfigPath).getSubmodelCollection(), AssetAdministrationShell.class, configFromResource(resourceConfigPath)), shellIdentificationId);
}

/**
Expand All @@ -112,8 +110,8 @@ public MongoDBAASAPI(String resourceConfigPath, String identificationId) {
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAPI(String aasId) {
this(DEFAULT_CONFIG_PATH, aasId);
public MongoDBAASAPI(String shellIdentificationId) {
this(DEFAULT_CONFIG_PATH, shellIdentificationId);
}

private static BaSyxMongoDBConfiguration configFromResource(String resourceConfigPath) {
Expand All @@ -129,7 +127,7 @@ private static BaSyxMongoDBConfiguration configFromResource(String resourceConfi
*/
@Deprecated
public void setConfiguration(BaSyxMongoDBConfiguration config) {
// Do nothing
throw new FeatureNotImplementedException("The Configuration shall be set set during instantiation using one of the constructors.");
}

/**
Expand All @@ -139,23 +137,24 @@ public void setConfiguration(BaSyxMongoDBConfiguration config) {
*/
@Deprecated
public void setConfiguration(BaSyxMongoDBConfiguration config, MongoClient client) {
// Do nothing
throw new FeatureNotImplementedException("The Configuration and the MongoClient shall be set set during instantiation using one of the constructors.");
}

/**
* Sets the aas id, so that this API points to the aas with aasId. Can be
* changed to point to a different aas in the database.
* Sets the shellIdentificationId, so that this API points to the shell with
* shellIdentificationId. Can be changed to point to a different shell in the
* database.
*
* @param identificationId
* @param shellIdentificationId
*/
public void setAASId(String identificationId) {
this.identificationId = identificationId;
public void setAASId(String shellIdentificationId) {
this.shellIdentificationId = shellIdentificationId;
}

/**
* Depending on whether the model is already in the db, this method inserts or
* replaces the existing data. The new aas id for this API is taken from the
* given aas.
* replaces the existing data. The new shell id for this API is taken from the
* given shell.
*
* @param shell
*/
Expand All @@ -167,30 +166,29 @@ public void setAAS(AssetAdministrationShell shell) {

@Override
public IAssetAdministrationShell getAAS() {
return storageApi.retrieve(identificationId);
return storageApi.retrieve(shellIdentificationId);
}

@Override
public void addSubmodel(IReference submodelReference) {
AssetAdministrationShell shell = (AssetAdministrationShell) getAAS();
shell.addSubmodelReference(submodelReference);
storageApi.update(shell, identificationId);
storageApi.update(shell, shellIdentificationId);
}

@Override
public void removeSubmodel(String identificationId) {
public void removeSubmodel(String submodelIdShort) {
AssetAdministrationShell shell = (AssetAdministrationShell) this.getAAS();
Collection<IReference> submodelReferences = shell.getSubmodelReferences();

Optional<IReference> toBeRemoved = submodelReferences.stream().filter(submodelReference -> getLastSubmodelReferenceKey(submodelReference).getValue().equals(identificationId)).findFirst();
Optional<IReference> toBeRemoved = submodelReferences.stream().filter(submodelReference -> getLastSubmodelReferenceKey(submodelReference).getValue().equals(submodelIdShort)).findFirst();
if (!toBeRemoved.isPresent() || toBeRemoved.isEmpty()) {
logger.warn("Submodel reference could not be removed. Shell with identification id '{}' does not contain submodel with idShort '{}'.", shell.getIdentification().getId(), identificationId);
logger.warn("Submodel reference could not be removed. Shell with identification id '{}' does not contain submodel with idShort '{}'.", shell.getIdentification().getId(), submodelIdShort);
return;
}
submodelReferences.remove(toBeRemoved.get());
logger.info("Removed submodel reference with idShort '{}' from shell with identification id '{}'.", identificationId, shell.getIdentification().getId());
shell.setSubmodelReferences(submodelReferences);
storageApi.update(shell, identificationId);
storageApi.update(shell, submodelIdShort);
}

private IKey getLastSubmodelReferenceKey(IReference submodelReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public MongoDBAASAPIFactory(MongoDBBaSyxStorageAPI<AssetAdministrationShell> mon
}

@Override
public IAASAPI getAASApi(AssetAdministrationShell sehll) {
new MongoDBAASAPI(storageAPI, sehll.getIdentification().getId());
MongoDBAASAPI api = new MongoDBAASAPI(storageAPI, sehll.getIdentification().getId());
api.setAAS(sehll);
public IAASAPI getAASApi(AssetAdministrationShell shell) {
new MongoDBAASAPI(storageAPI, shell.getIdentification().getId());
MongoDBAASAPI api = new MongoDBAASAPI(storageAPI, shell.getIdentification().getId());
api.setAAS(shell);
return api;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
import org.eclipse.basyx.vab.modelprovider.api.IModelProvider;
import org.eclipse.basyx.vab.protocol.api.IConnectorFactory;
import org.eclipse.basyx.vab.protocol.http.connector.HTTPConnectorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
Expand All @@ -75,10 +73,7 @@
*
*/
public class MongoDBAASAggregator implements IAASAggregator {
private static Logger logger = LoggerFactory.getLogger(MongoDBAASAggregator.class);

protected Map<String, MultiSubmodelProvider> shellProviderMap = new HashMap<>();
// protected BaSyxMongoDBConfiguration config;

private IAASRegistry registry;

Expand Down Expand Up @@ -166,25 +161,25 @@ public MongoDBAASAggregator(String resourceConfigPath, IAASRegistry registry, IA
* a MongoClient to create a persistent MongoDB backend.
*
* @param resourceConfigPath
* @param aasAPIFactory
* @param shellAPIFactory
* @param submodelAggregatorFactory
* @param client
*
*/
public MongoDBAASAggregator(String resourceConfigPath, IAASAPIFactory aasAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory, MongoClient client) {
this(loadConfigFromPath(resourceConfigPath), aasAPIFactory, submodelAggregatorFactory, client);
public MongoDBAASAggregator(String resourceConfigPath, IAASAPIFactory shellAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory, MongoClient client) {
this(loadConfigFromPath(resourceConfigPath), shellAPIFactory, submodelAggregatorFactory, client);
}

/**
* Constructor using the default configuration, with the given
* IAASAPIFactory,ISubmodelAggregatorFactory and MongoClient.
*
* @param aasAPIFactory
* @param shellAPIFactory
* @param submodelAggregatorFactory
* @param client
*/
public MongoDBAASAggregator(IAASAPIFactory aasAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory, MongoClient client) {
this(BaSyxMongoDBConfiguration.DEFAULT_CONFIG_PATH, aasAPIFactory, submodelAggregatorFactory, client);
public MongoDBAASAggregator(IAASAPIFactory shellAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory, MongoClient client) {
this(BaSyxMongoDBConfiguration.DEFAULT_CONFIG_PATH, shellAPIFactory, submodelAggregatorFactory, client);
}

/**
Expand Down Expand Up @@ -280,27 +275,27 @@ public MongoDBAASAggregator(IAASRegistry registry) {
*
* @param config
* @param registry
* @param aasAPIFactory
* @param shellAPIFactory
* @param submodelAggregatorFactory
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAggregator(BaSyxMongoDBConfiguration config, IAASRegistry registry, IAASAPIFactory aasAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(config, registry, aasAPIFactory, submodelAggregatorFactory, MongoClients.create(config.getConnectionUrl()));
public MongoDBAASAggregator(BaSyxMongoDBConfiguration config, IAASRegistry registry, IAASAPIFactory shellAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(config, registry, shellAPIFactory, submodelAggregatorFactory, MongoClients.create(config.getConnectionUrl()));
}

/**
* Receives a BaSyxMongoDBConfiguration, IAASAPIFactory and a
* ISubmodelAggregatorFactory to create a persistent MongoDB backend.
*
* @param config
* @param aasAPIFactory
* @param shellAPIFactory
* @param submodelAggregatorFactory
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAggregator(BaSyxMongoDBConfiguration config, IAASAPIFactory aasAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(config, aasAPIFactory, submodelAggregatorFactory, MongoClients.create(config.getConnectionUrl()));
public MongoDBAASAggregator(BaSyxMongoDBConfiguration config, IAASAPIFactory shellAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(config, shellAPIFactory, submodelAggregatorFactory, MongoClients.create(config.getConnectionUrl()));
}

/**
Expand Down Expand Up @@ -336,13 +331,13 @@ public MongoDBAASAggregator(String resourceConfigPath, IAASAPIFactory shellAPIFa
* Constructor using the default configuration, with the given IAASAPIFactory
* and ISubmodelAggregatorFactory.
*
* @param aasAPIFactory
* @param shellAPIFactory
* @param submodelAggregatorFactory
* @deprecated Use the new constructor using a MongoClient
*/
@Deprecated
public MongoDBAASAggregator(IAASAPIFactory aasAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(BaSyxMongoDBConfiguration.DEFAULT_CONFIG_PATH, aasAPIFactory, submodelAggregatorFactory);
public MongoDBAASAggregator(IAASAPIFactory shellAPIFactory, ISubmodelAggregatorFactory submodelAggregatorFactory) {
this(BaSyxMongoDBConfiguration.DEFAULT_CONFIG_PATH, shellAPIFactory, submodelAggregatorFactory);
}

private static ISubmodelAggregatorFactory initSubmodelAggregatorFactory(BaSyxMongoDBConfiguration config) {
Expand Down Expand Up @@ -418,7 +413,6 @@ private void init() {
Collection<AssetAdministrationShell> data = shellStorageApi.retrieveAll();
data.forEach(shell -> {
String shellIdentificationId = shell.getIdentification().getId();
logger.info("Adding AAS from DB: " + shellIdentificationId);
IAASAPI shellApi = shellApiFactory.create(shell);
MultiSubmodelProvider provider = createMultiSubmodelProvider(shellApi);
addSubmodelsFromDB(provider, shell);
Expand Down Expand Up @@ -464,10 +458,7 @@ private void addSubmodelsFromDB(MultiSubmodelProvider provider, AssetAdministrat
}

private void createProviderForSubmodels(MultiSubmodelProvider provider, List<String> submodelIdentificationIds) {
submodelIdentificationIds.forEach(identificationId -> {
logger.info("Adding Submodel from DB: " + identificationId);
addSubmodelProvidersById(identificationId, provider);
});
submodelIdentificationIds.forEach(submodelIdentificationId -> addSubmodelProvidersById(submodelIdentificationId, provider));
}

private List<String> completeSubmodelIdentificationsIdsByIdShorts(List<String> submodelIdentificationIds, List<String> submodelIdShorts) {
Expand Down Expand Up @@ -553,16 +544,15 @@ public void createAAS(AssetAdministrationShell shell) {

@Override
public void updateAAS(AssetAdministrationShell shell) {
IIdentifier identification = shell.getIdentification();
String identificationId = identification.getId();
IIdentifier shellIdentification = shell.getIdentification();
String shellIdentificationId = shellIdentification.getId();

MultiSubmodelProvider oldProvider = (MultiSubmodelProvider) getAASProvider(identification);
MultiSubmodelProvider oldProvider = (MultiSubmodelProvider) getAASProvider(shellIdentification);

IAASAPI shellApi = this.shellApiFactory.create(shell);
MultiSubmodelProvider updatedProvider = updateAASProvider(shellApi, oldProvider);

shellProviderMap.put(identificationId, updatedProvider);
logger.info("update shell with id {}", identificationId);
shellProviderMap.put(shellIdentificationId, updatedProvider);
}

private MultiSubmodelProvider updateAASProvider(IAASAPI shellApi, MultiSubmodelProvider oldProvider) {
Expand All @@ -576,9 +566,9 @@ private MultiSubmodelProvider updateAASProvider(IAASAPI shellApi, MultiSubmodelP

@Override
public void deleteAAS(IIdentifier shellIdentifier) {
String identificationId = shellIdentifier.getId();
shellStorageApi.delete(identificationId);
shellProviderMap.remove(identificationId);
String shellIdentificationId = shellIdentifier.getId();
shellStorageApi.delete(shellIdentificationId);
shellProviderMap.remove(shellIdentificationId);
}

public MultiSubmodelProvider getProviderForAASId(String shellIdentificationId) {
Expand Down
Loading

0 comments on commit 3a1e8db

Please sign in to comment.