Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
---
- op: add
path: /components/schemas/AssetAdministrationShellDescriptor/x-class-extra-annotation
value: '@org.springframework.data.mongodb.core.mapping.Document(collection = "aasdescriptors")'
- op: add
path: /components/schemas/AssetAdministrationShellDescriptor/allOf/1/properties/id/x-field-extra-annotation
value: '@org.springframework.data.annotation.Id'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.digitaltwin.basyx.aasregistry.service.storage.AasRegistryStorageFeature;
import org.eclipse.digitaltwin.basyx.aasregistry.service.storage.CursorEncodingRegistryStorage;
import org.eclipse.digitaltwin.basyx.aasregistry.service.storage.mongodb.MongoDbAasRegistryStorage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -51,12 +52,15 @@
@Log4j2
public class MongoDbConfiguration {

@Value("${basyx.aasregistry.mongodb.collectionName:submodeldescriptors}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronzi Should'nt it be aasdescriptors at this point? See #900

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

private String collectionName;

@Bean
public AasRegistryStorage createStorage(MongoTemplate template, List<AasRegistryStorageFeature> features) {
log.info("Creating mongodb storage");
log.info("Creating mongodb indices");
initializeIndices(template);
AasRegistryStorage storage = new CursorEncodingRegistryStorage(new MongoDbAasRegistryStorage(template));
AasRegistryStorage storage = new CursorEncodingRegistryStorage(new MongoDbAasRegistryStorage(template, collectionName));
return applyFeatures(storage, features);

}
Expand All @@ -70,7 +74,7 @@ private AasRegistryStorage applyFeatures(AasRegistryStorage storage, List<AasReg
}

private void initializeIndices(MongoTemplate template) {
IndexOperations ops = template.indexOps(AssetAdministrationShellDescriptor.class);
IndexOperations ops = template.indexOps(collectionName);
initializeGetShellDescriptorsIndices(ops);
initializeExtensionIndices(ops);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ public class MongoDbAasRegistryStorage implements AasRegistryStorage {
private static final String SUBMODEL_DESCRIPTORS_ID = "submodelDescriptors._id";
private static final String ASSET_TYPE = "assetType";
private static final String ASSET_KIND = "assetKind";

private final MongoTemplate template;

private final String collectionName;

@Override
public CursorResult<List<AssetAdministrationShellDescriptor>> getAllAasDescriptors(@NonNull PaginationInfo pRequest, @NonNull DescriptorFilter filter) {
List<AggregationOperation> allAggregations = new LinkedList<>();
applyFilter(filter, allAggregations);
applySorting(allAggregations);
applyPagination(pRequest, allAggregations);
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(Aggregation.newAggregation(allAggregations), AssetAdministrationShellDescriptor.class, AssetAdministrationShellDescriptor.class);
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(Aggregation.newAggregation(allAggregations), collectionName, AssetAdministrationShellDescriptor.class);
List<AssetAdministrationShellDescriptor> foundDescriptors = results.getMappedResults();
String cursor = resolveCursor(pRequest, foundDescriptors, AssetAdministrationShellDescriptor::getId);
return new CursorResult<>(cursor, foundDescriptors);
Expand Down Expand Up @@ -146,7 +148,7 @@ public Optional<Criteria> createFilterCriteria(DescriptorFilter filter) {

@Override
public AssetAdministrationShellDescriptor getAasDescriptor(@NonNull String aasDescriptorId) throws AasDescriptorNotFoundException {
AssetAdministrationShellDescriptor descriptor = template.findById(aasDescriptorId, AssetAdministrationShellDescriptor.class);
AssetAdministrationShellDescriptor descriptor = template.findById(aasDescriptorId, AssetAdministrationShellDescriptor.class, collectionName);
if (descriptor == null) {
throw new AasDescriptorNotFoundException(aasDescriptorId);
}
Expand All @@ -156,7 +158,7 @@ public AssetAdministrationShellDescriptor getAasDescriptor(@NonNull String aasDe
@Override
public void insertAasDescriptor(@Valid AssetAdministrationShellDescriptor descr) throws AasDescriptorAlreadyExistsException {
try {
template.insert(descr);
template.insert(descr, collectionName);
} catch (org.springframework.dao.DuplicateKeyException ex) {
throw new AasDescriptorAlreadyExistsException(descr.getId());
}
Expand All @@ -169,7 +171,7 @@ public void replaceAasDescriptor(@NonNull String aasDescriptorId, @NonNull Asset
moveInTransaction(aasDescriptorId, descriptor);
} else {
Query query = Query.query(Criteria.where(ID).is(aasDescriptorId));
AssetAdministrationShellDescriptor replaced = template.findAndReplace(query, descriptor);
AssetAdministrationShellDescriptor replaced = template.findAndReplace(query, descriptor, collectionName);
if (replaced == null) {
throw new AasDescriptorNotFoundException(aasDescriptorId);
}
Expand All @@ -180,10 +182,10 @@ private void moveInTransaction(@NonNull String aasDescriptorId, @NonNull AssetAd
SessionScoped scoped = template.withSession(ClientSessionOptions.builder().build());
boolean removed = scoped.execute(operations -> {
Query query = Query.query(Criteria.where(ID).is(aasDescriptorId));
if (operations.remove(query, AssetAdministrationShellDescriptor.class).getDeletedCount() == 0) {
if (operations.remove(query, AssetAdministrationShellDescriptor.class, collectionName).getDeletedCount() == 0) {
return false;
}
operations.save(descriptor);
operations.save(descriptor, collectionName);
return true;
});
if (!removed) {
Expand All @@ -194,14 +196,14 @@ private void moveInTransaction(@NonNull String aasDescriptorId, @NonNull AssetAd
@Override
public void removeAasDescriptor(@NonNull String aasDescriptorId) throws AasDescriptorNotFoundException {
Query query = Query.query(Criteria.where(ID).is(aasDescriptorId));
if (template.remove(query, AssetAdministrationShellDescriptor.class).getDeletedCount() == 0) {
if (template.remove(query, AssetAdministrationShellDescriptor.class, collectionName).getDeletedCount() == 0) {
throw new AasDescriptorNotFoundException(aasDescriptorId);
}
}

@Override
public CursorResult<List<SubmodelDescriptor>> getAllSubmodels(@NonNull String aasDescriptorId, @NonNull PaginationInfo pRequest) throws AasDescriptorNotFoundException {
if (!template.exists(Query.query(Criteria.where(ID).is(aasDescriptorId)), AssetAdministrationShellDescriptor.class)) {
if (!template.exists(Query.query(Criteria.where(ID).is(aasDescriptorId)), collectionName)) {
throw new AasDescriptorNotFoundException(aasDescriptorId);
}
List<AggregationOperation> allAggregations = new LinkedList<>();
Expand All @@ -210,7 +212,7 @@ public CursorResult<List<SubmodelDescriptor>> getAllSubmodels(@NonNull String aa
allAggregations.add(Aggregation.replaceRoot(SUBMODEL_DESCRIPTORS));
this.applySorting(allAggregations);
this.applyPagination(pRequest, allAggregations);
AggregationResults<SubmodelDescriptor> results = template.aggregate(Aggregation.newAggregation(allAggregations), AssetAdministrationShellDescriptor.class, SubmodelDescriptor.class);
AggregationResults<SubmodelDescriptor> results = template.aggregate(Aggregation.newAggregation(allAggregations), collectionName, SubmodelDescriptor.class);
List<SubmodelDescriptor> submodels = results.getMappedResults();
String cursor = resolveCursor(pRequest, submodels, SubmodelDescriptor::getId);
return new CursorResult<>(cursor, submodels);
Expand All @@ -222,7 +224,7 @@ public SubmodelDescriptor getSubmodel(@NonNull String aasDescriptorId, @NonNull
all.add(Aggregation.match(Criteria.where(ID).is(aasDescriptorId)));
ArrayOperators.Filter filter = ArrayOperators.arrayOf(SUBMODEL_DESCRIPTORS).filter().as(SUBMODEL_DESCRIPTORS).by(ComparisonOperators.valueOf(SUBMODEL_DESCRIPTORS_ID).equalToValue(submodelId));
all.add(Aggregation.project().and(filter).as(SUBMODEL_DESCRIPTORS));
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(Aggregation.newAggregation(all), AssetAdministrationShellDescriptor.class, AssetAdministrationShellDescriptor.class);
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(Aggregation.newAggregation(all), collectionName, AssetAdministrationShellDescriptor.class);
List<AssetAdministrationShellDescriptor> aasDescriptors = results.getMappedResults();
if (aasDescriptors.isEmpty()) {
throw new AasDescriptorNotFoundException(aasDescriptorId);
Expand All @@ -239,7 +241,7 @@ public void insertSubmodel(@NonNull String aasDescriptorId, @NonNull SubmodelDes
Criteria criteria = Criteria.where(ID).is(aasDescriptorId).and(SUBMODEL_DESCRIPTORS_ID).ne(submodel.getId());
Query query = Query.query(criteria);
UpdateDefinition def = new Update().push(SUBMODEL_DESCRIPTORS, submodel);
AssetAdministrationShellDescriptor descr = template.findAndModify(query, def, AssetAdministrationShellDescriptor.class);
AssetAdministrationShellDescriptor descr = template.findAndModify(query, def, AssetAdministrationShellDescriptor.class, collectionName);
assertInsertPerformed(descr, aasDescriptorId, submodel.getId());
}

Expand All @@ -248,7 +250,7 @@ public void replaceSubmodel(@NonNull String aasDescriptorId, @NonNull String sub
Criteria criteria = Criteria.where(ID).is(aasDescriptorId).and(SUBMODEL_DESCRIPTORS).elemMatch(Criteria.where(ID).is(submodelId));
Query query = Query.query(criteria);
UpdateDefinition def = Update.update(MATCHING_SUBMODEL_DESCRIPTORS, submodel);
AssetAdministrationShellDescriptor descr = template.findAndModify(query, def, AssetAdministrationShellDescriptor.class);
AssetAdministrationShellDescriptor descr = template.findAndModify(query, def, AssetAdministrationShellDescriptor.class, collectionName);
assertReplacePerformed(descr, aasDescriptorId, submodelId);
}

Expand All @@ -271,7 +273,7 @@ public void removeSubmodel(@NonNull String aasDescriptorId, @NonNull String subm
AggregationExpression notEquals = ComparisonOperators.valueOf(SUBMODEL_DESCRIPTORS_ID).notEqualToValue(submodelId);
AggregationExpression filterArray = ArrayOperators.arrayOf(SUBMODEL_DESCRIPTORS).filter().as(SUBMODEL_DESCRIPTORS).by(notEquals);
AggregationUpdate update = AggregationUpdate.update().set(SUBMODEL_DESCRIPTORS).toValue(filterArray);
AssetAdministrationShellDescriptor old = template.findAndModify(Query.query(Criteria.where(ID).is(aasDescriptorId)), update, AssetAdministrationShellDescriptor.class);
AssetAdministrationShellDescriptor old = template.findAndModify(Query.query(Criteria.where(ID).is(aasDescriptorId)), update, AssetAdministrationShellDescriptor.class, collectionName);
if (old == null) {
throw new AasDescriptorNotFoundException(submodelId);
}
Expand All @@ -298,7 +300,7 @@ private boolean containsSubmodel(AssetAdministrationShellDescriptor old, String
public Set<String> clear() {
Query query = Query.query(Criteria.where(ID).exists(true));
query.fields().include(ID);
List<AssetAdministrationShellDescriptor> list = template.findAllAndRemove(query, AssetAdministrationShellDescriptor.class);
List<AssetAdministrationShellDescriptor> list = template.findAllAndRemove(query, AssetAdministrationShellDescriptor.class, collectionName);
return list.stream().map(AssetAdministrationShellDescriptor::getId).collect(Collectors.toSet());
}

Expand All @@ -309,7 +311,7 @@ public ShellDescriptorSearchResponse searchAasDescriptors(@NonNull ShellDescript
GroupedQueries grouped = ShellDescriptorSearchRequests.groupQueries(dQuery);
Criteria mongoCriteria = qBuilder.buildCriteria(grouped);

long total = template.count(Query.query(mongoCriteria), AssetAdministrationShellDescriptor.class);
long total = template.count(Query.query(mongoCriteria), AssetAdministrationShellDescriptor.class, collectionName);

List<AggregationOperation> aggregationOps = new LinkedList<>();
MatchOperation matchOp = Aggregation.match(mongoCriteria);
Expand All @@ -319,7 +321,7 @@ public ShellDescriptorSearchResponse searchAasDescriptors(@NonNull ShellDescript
qBuilder.withProjection(grouped.getQueriesInsideSubmodel(), aggregationOps);

Aggregation aggregation = Aggregation.newAggregation(aggregationOps);
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(aggregation, AssetAdministrationShellDescriptor.class, AssetAdministrationShellDescriptor.class);
AggregationResults<AssetAdministrationShellDescriptor> results = template.aggregate(aggregation, collectionName, AssetAdministrationShellDescriptor.class);

List<AssetAdministrationShellDescriptor> descriptors = results.getMappedResults();
return new ShellDescriptorSearchResponse(total, descriptors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void whenGetByAasID_NotAllDocumentsScannedButIndexUsed() {
}

private void testIndexFilter(AssetKind kind, String type) {
MongoDbAasRegistryStorage storage = new MongoDbAasRegistryStorage(template);
MongoDbAasRegistryStorage storage = new MongoDbAasRegistryStorage(template, "aasdescriptors");
Optional<Criteria> criteriaOpt = storage.createFilterCriteria(new DescriptorFilter(kind, type));
assertThat(criteriaOpt).isNotEmpty();
Criteria criteria = criteriaOpt.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TestConfiguration extends AbstractMongoClientConfiguration {

@Bean
public AasRegistryStorage storage(MongoTemplate template) {
return new CursorEncodingRegistryStorage(new MongoDbAasRegistryStorage(template));
return new CursorEncodingRegistryStorage(new MongoDbAasRegistryStorage(template, "aasdescriptors"));
}

@Bean
Expand Down
18 changes: 18 additions & 0 deletions basyx.aasregistry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.maven-plugin.lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.authorization</artifactId>
<classifier>tests</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.http.HttpStatus;

import org.eclipse.digitaltwin.basyx.authorization.AccessTokenProvider;
import org.eclipse.digitaltwin.basyx.authorization.DummyCredential;
import org.eclipse.digitaltwin.basyx.authorization.DummyCredentialStore;
Expand Down Expand Up @@ -494,4 +495,4 @@ private AccessTokenProvider getAccessTokenProvider() {
return new AccessTokenProvider(authenticaltionServerTokenEndpoint, clientId);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
- op: add
path: /components/schemas/SubmodelDescriptor/x-class-extra-annotation
value: '@org.springframework.data.mongodb.core.mapping.Document(collection = "submodeldescriptors")'
- op: add
path: /components/schemas/SubmodelDescriptor/allOf/1/properties/id/x-field-extra-annotation
value: '@org.springframework.data.annotation.Id'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.digitaltwin.basyx.submodelregistry.service.storage.SubmodelRegistryStorage;
import org.eclipse.digitaltwin.basyx.submodelregistry.service.storage.SubmodelRegistryStorageFeature;
import org.eclipse.digitaltwin.basyx.submodelregistry.service.storage.mongodb.MongoDbSubmodelRegistryStorage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -45,11 +46,15 @@
@Log4j2
public class MongoDbConfiguration {


@Value("${basyx.submodelregistry.mongodb.collectionName:submodeldescriptors}")
public String collectionName;

@Bean
public SubmodelRegistryStorage createSubmodelRegistryStorage(MongoTemplate template, List<SubmodelRegistryStorageFeature> features) {
log.info("Creating mongodb storage");

SubmodelRegistryStorage storage = new CursorEncodingRegistryStorage(new MongoDbSubmodelRegistryStorage(template));
SubmodelRegistryStorage storage = new CursorEncodingRegistryStorage(new MongoDbSubmodelRegistryStorage(template, collectionName));

return applyFeatures(storage, features);
}
Expand Down
Loading
Loading