From 1130547fc19c445b02f01473e582a7b7d7f78869 Mon Sep 17 00:00:00 2001 From: mateusmolina Date: Tue, 3 Dec 2024 15:41:17 +0100 Subject: [PATCH 1/4] test: extract test suite from aasmanagermultithreading test --- ...ctedAasManagerMultithreadingTestSuite.java | 99 +++++++++++++++++++ ...ctedAasManagerMultithreadingInMemory.java} | 80 ++------------- 2 files changed, 108 insertions(+), 71 deletions(-) create mode 100644 basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManagerMultithreadingTestSuite.java rename basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/{TestConnectedAasManagerMultithreading.java => TestConnectedAasManagerMultithreadingInMemory.java} (55%) diff --git a/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManagerMultithreadingTestSuite.java b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManagerMultithreadingTestSuite.java new file mode 100644 index 000000000..8ff6b6948 --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManagerMultithreadingTestSuite.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (C) 2024 the Eclipse BaSyx Authors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * SPDX-License-Identifier: MIT + ******************************************************************************/ + +package org.eclipse.digitaltwin.basyx.aasenvironment.client; + +import static org.junit.Assert.*; + +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.stream.IntStream; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.Key; +import org.eclipse.digitaltwin.aas4j.v3.model.Reference; +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel; +import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; +import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; +import org.junit.Test; + +public abstract class ConnectedAasManagerMultithreadingTestSuite { + static final int N_THREADS = 20; + + protected abstract AasRepository getAasRepository(); + + protected abstract ConnectedAasManager getConnectedAasManager(); + + @Test + public void testParallelSubmodelCreation() throws ExecutionException, InterruptedException { + AssetAdministrationShell shell = createShell(); + + ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS); + ConcurrentLinkedDeque createdSubmodelIds = new ConcurrentLinkedDeque<>(); + + List> futures = IntStream.range(0, N_THREADS).mapToObj(i -> executorService.submit(() -> createdSubmodelIds.add(createSubmodel(shell.getId(), i)))).toList(); + + try { + for (int i = 0; i < N_THREADS; i++) { + futures.get(i).get(); + } + } finally { + executorService.shutdown(); + } + + createdSubmodelIds.forEach(submodelId -> assertSubmodelWasCreatedAndRegistered(shell.getId(), submodelId)); + } + + void assertSubmodelWasCreatedAndRegistered(String shellId, String submodelId) { + assertEquals(submodelId, getConnectedAasManager().getSubmodelService(submodelId).getSubmodel().getId()); + assertTrue(getAasRepository().getSubmodelReferences(shellId, PaginationInfo.NO_LIMIT).getResult().stream().map(Reference::getKeys).flatMap(Collection::stream).map(Key::getValue).anyMatch(submodelId::equals)); + } + + private AssetAdministrationShell createShell() { + String id = UUID.randomUUID().toString(); + DefaultAssetAdministrationShell shell = new DefaultAssetAdministrationShell.Builder().id(id).build(); + getConnectedAasManager().createAas(shell); + return getConnectedAasManager().getAasService(id).getAAS(); + } + + private String createSubmodel(String aasId, int threadId) { + try { + String id = aasId + "-thread" + threadId; + DefaultSubmodel submodel = new DefaultSubmodel.Builder().id(id).build(); + getConnectedAasManager().createSubmodelInAas(aasId, submodel); + return id; + } catch (Exception e) { + throw new RuntimeException("Failed at thread " + threadId, e); + } + } + +} diff --git a/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreading.java b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingInMemory.java similarity index 55% rename from basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreading.java rename to basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingInMemory.java index 8ff0ea860..001301975 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreading.java +++ b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingInMemory.java @@ -25,60 +25,33 @@ package org.eclipse.digitaltwin.basyx.aasenvironment.client; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.Collection; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.stream.IntStream; - -import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.eclipse.digitaltwin.aas4j.v3.model.Key; -import org.eclipse.digitaltwin.aas4j.v3.model.Reference; -import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell; -import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel; import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository; -import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi; -import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository; import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Test; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; -public class TestConnectedAasManagerMultithreading { +public class TestConnectedAasManagerMultithreadingInMemory extends ConnectedAasManagerMultithreadingTestSuite { static final String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081"; static final String SM_REPOSITORY_BASE_PATH = "http://localhost:8081"; static final String AAS_REGISTRY_BASE_PATH = "http://localhost:8050"; static final String SM_REGISTRY_BASE_PATH = "http://localhost:8060"; - static final int N_THREADS = 20; - - static ConfigurableApplicationContext appContext; - static AasRepository aasRepository; - static SubmodelRepository smRepository; + static ConnectedAasManager aasManager; static ConnectedAasRepository connectedAasRepository; static ConnectedSubmodelRepository connectedSmRepository; static RegistryAndDiscoveryInterfaceApi aasRegistryApi; static SubmodelRegistryApi smRegistryApi; - - static ConnectedAasManager aasManager; + static ConfigurableApplicationContext appContext; @BeforeClass public static void setupRepositories() { appContext = new SpringApplication(DummyAasEnvironmentComponent.class).run(new String[] {}); - connectedAasRepository = new ConnectedAasRepository(AAS_REPOSITORY_BASE_PATH); connectedSmRepository = new ConnectedSubmodelRepository(SM_REPOSITORY_BASE_PATH); aasRegistryApi = new RegistryAndDiscoveryInterfaceApi(AAS_REGISTRY_BASE_PATH); @@ -98,32 +71,6 @@ public static void stopContext() { appContext.close(); } - @Test - public void testParallelSubmodelCreation() throws ExecutionException, InterruptedException { - AssetAdministrationShell shell = createShell(); - - ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS); - ConcurrentLinkedDeque createdSubmodelIds = new ConcurrentLinkedDeque<>(); - - List> futures = IntStream.range(0, N_THREADS).mapToObj(i -> executorService.submit(() -> createdSubmodelIds.add(createSubmodel(shell.getId(), i)))).toList(); - - try { - for (int i = 0; i < N_THREADS; i++) { - futures.get(i).get(); - } - } finally { - executorService.shutdown(); - } - - createdSubmodelIds.forEach(submodelId -> assertSubmodelWasCreatedAndRegistered(shell.getId(), submodelId)); - } - - static void assertSubmodelWasCreatedAndRegistered(String shellId, String submodelId) { - assertEquals(submodelId, aasManager.getSubmodelService(submodelId).getSubmodel().getId()); - assertTrue(connectedAasRepository.getSubmodelReferences(shellId, PaginationInfo.NO_LIMIT).getResult().stream().map(Reference::getKeys).flatMap(Collection::stream).map(Key::getValue).anyMatch(submodelId::equals)); - } - - private static void cleanUpRegistries() { try { aasRegistryApi.deleteAllShellDescriptors(); @@ -137,22 +84,13 @@ private static void cleanUpRegistries() { } } - private static AssetAdministrationShell createShell() { - String id = UUID.randomUUID().toString(); - DefaultAssetAdministrationShell shell = new DefaultAssetAdministrationShell.Builder().id(id).build(); - aasManager.createAas(shell); - return aasManager.getAasService(id).getAAS(); + @Override + public AasRepository getAasRepository() { + return connectedAasRepository; } - private static String createSubmodel(String aasId, int threadId) { - try { - String id = aasId + "-thread" + threadId; - DefaultSubmodel submodel = new DefaultSubmodel.Builder().id(id).build(); - aasManager.createSubmodelInAas(aasId, submodel); - return id; - } catch (Exception e) { - throw new RuntimeException("Failed at thread " + threadId, e); - } + @Override + public ConnectedAasManager getConnectedAasManager() { + return aasManager; } - } From 5faa294863e3ca99bfbad85cc68375f23645309e Mon Sep 17 00:00:00 2001 From: mateusmolina Date: Wed, 4 Dec 2024 09:23:16 +0100 Subject: [PATCH 2/4] test: add failing test for aasenv multithreading mongoDB --- ...nectedAasManagerMultithreadingMongoDb.java | 96 +++++++++++++++++++ .../resources/application-mongodb.properties | 8 ++ 2 files changed, 104 insertions(+) create mode 100644 basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingMongoDb.java create mode 100644 basyx.aasenvironment/basyx.aasenvironment-client/src/test/resources/application-mongodb.properties diff --git a/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingMongoDb.java b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingMongoDb.java new file mode 100644 index 000000000..d92b08272 --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/TestConnectedAasManagerMultithreadingMongoDb.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (C) 2024 the Eclipse BaSyx Authors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * SPDX-License-Identifier: MIT + ******************************************************************************/ + +package org.eclipse.digitaltwin.basyx.aasenvironment.client; + +import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi; +import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; +import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository; +import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi; +import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; + +public class TestConnectedAasManagerMultithreadingMongoDb extends ConnectedAasManagerMultithreadingTestSuite { + static final String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081"; + static final String SM_REPOSITORY_BASE_PATH = "http://localhost:8081"; + static final String AAS_REGISTRY_BASE_PATH = "http://localhost:8050"; + static final String SM_REGISTRY_BASE_PATH = "http://localhost:8060"; + + static ConnectedAasManager aasManager; + static ConnectedAasRepository connectedAasRepository; + static ConnectedSubmodelRepository connectedSmRepository; + static RegistryAndDiscoveryInterfaceApi aasRegistryApi; + static SubmodelRegistryApi smRegistryApi; + static ConfigurableApplicationContext appContext; + + @BeforeClass + public static void setupRepositories() { + appContext = new SpringApplicationBuilder(DummyAasEnvironmentComponent.class).profiles("mongodb").run(new String[] {}); + connectedAasRepository = new ConnectedAasRepository(AAS_REPOSITORY_BASE_PATH); + connectedSmRepository = new ConnectedSubmodelRepository(SM_REPOSITORY_BASE_PATH); + aasRegistryApi = new RegistryAndDiscoveryInterfaceApi(AAS_REGISTRY_BASE_PATH); + smRegistryApi = new SubmodelRegistryApi(SM_REGISTRY_BASE_PATH); + aasManager = new ConnectedAasManager(AAS_REGISTRY_BASE_PATH, AAS_REPOSITORY_BASE_PATH, SM_REGISTRY_BASE_PATH, SM_REPOSITORY_BASE_PATH); + + cleanUpRegistries(); + } + + @After + public void cleanUpComponents() { + cleanUpRegistries(); + } + + @AfterClass + public static void stopContext() { + appContext.close(); + } + + private static void cleanUpRegistries() { + try { + aasRegistryApi.deleteAllShellDescriptors(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + try { + smRegistryApi.deleteAllSubmodelDescriptors(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + @Override + public AasRepository getAasRepository() { + return connectedAasRepository; + } + + @Override + public ConnectedAasManager getConnectedAasManager() { + return aasManager; + } +} diff --git a/basyx.aasenvironment/basyx.aasenvironment-client/src/test/resources/application-mongodb.properties b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/resources/application-mongodb.properties new file mode 100644 index 000000000..4afb4e37e --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment-client/src/test/resources/application-mongodb.properties @@ -0,0 +1,8 @@ +basyx.backend = MongoDB + +spring.data.mongodb.host=127.0.0.1 +spring.data.mongodb.port=27017 +spring.data.mongodb.database=aas-env +spring.data.mongodb.authentication-database=admin +spring.data.mongodb.username=mongoAdmin +spring.data.mongodb.password=mongoPassword \ No newline at end of file From 485639f3d42fc3bb1d055e3806286c0d1b5197b3 Mon Sep 17 00:00:00 2001 From: mateusmolina Date: Thu, 5 Dec 2024 11:12:15 +0100 Subject: [PATCH 3/4] refactor: experimental change of the design of the AasBackend + CrudAasRepository --- .../AasEnvironmentLoaderTest.java | 4 +- .../TestAASEnvironmentSerialization.java | 4 +- ...a => AasInMemoryBackendConfiguration.java} | 16 ++--- .../inmemory/TestInMemoryAasRepository.java | 2 +- .../mongodb/AasMongoDBBackendProvider.java | 72 ------------------- .../backend/mongodb/MongoDBAasBackend.java | 13 ++++ ...va => MongoDBAasBackendConfiguration.java} | 21 +++++- .../basyx.aasrepository-backend/pom.xml | 4 ++ .../aasrepository/backend/AasBackend.java | 8 +++ .../backend/AasBackendProvider.java | 37 ---------- .../backend/CrudAasRepository.java | 47 ++++++------ .../backend/SimpleAasRepositoryFactory.java | 27 ++----- .../mqtt/TestMqttV2AASAggregatorObserver.java | 4 +- ...yRegistryLinkDescriptorGenerationTest.java | 4 +- .../testconfig/DummyAasRepositoryConfig.java | 4 +- .../feature/mqtt/TestMqttAasService.java | 4 +- 16 files changed, 98 insertions(+), 173 deletions(-) rename basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/{AasInMemoryBackendProvider.java => AasInMemoryBackendConfiguration.java} (82%) delete mode 100644 basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBBackendProvider.java create mode 100644 basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java rename basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/{AasMongoDBRepositoryConfiguration.java => MongoDBAasBackendConfiguration.java} (67%) create mode 100644 basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java delete mode 100644 basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackendProvider.java diff --git a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/AasEnvironmentLoaderTest.java b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/AasEnvironmentLoaderTest.java index c3fd3001b..5fe8ccf15 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/AasEnvironmentLoaderTest.java +++ b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/AasEnvironmentLoaderTest.java @@ -36,7 +36,7 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.backend.CrudAasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.backend.CrudConceptDescriptionRepository; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.conceptdescriptionrepository.ConceptDescriptionInMemoryBackendProvider; import org.eclipse.digitaltwin.basyx.conceptdescriptionrepository.ConceptDescriptionRepository; @@ -77,7 +77,7 @@ public class AasEnvironmentLoaderTest { @Before public void setUp() { submodelRepository = Mockito.spy(new CrudSubmodelRepository(new SubmodelInMemoryBackendProvider(), new InMemorySubmodelServiceFactory(new InMemoryFileRepository()))); - aasRepository = Mockito.spy(new CrudAasRepository(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository()))); + aasRepository = Mockito.spy(new CrudAasRepository(new AasInMemoryBackendConfiguration(), new InMemoryAasServiceFactory(new InMemoryFileRepository()))); conceptDescriptionRepository = Mockito.spy(new CrudConceptDescriptionRepository(new ConceptDescriptionInMemoryBackendProvider())); } diff --git a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java index 0564b0d4a..bb361b4b7 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java +++ b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java @@ -54,7 +54,7 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleConceptDescriptionRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.conceptdescriptionrepository.ConceptDescriptionInMemoryBackendProvider; import org.eclipse.digitaltwin.basyx.conceptdescriptionrepository.ConceptDescriptionRepository; @@ -88,7 +88,7 @@ public class TestAASEnvironmentSerialization { @Before public void setup() { submodelRepository = new SimpleSubmodelRepositoryFactory(new SubmodelInMemoryBackendProvider(), new InMemorySubmodelServiceFactory(new InMemoryFileRepository())).create(); - aasRepository = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); + aasRepository = new SimpleAasRepositoryFactory(new AasInMemoryBackendConfiguration(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); conceptDescriptionRepository = new SimpleConceptDescriptionRepositoryFactory(new ConceptDescriptionInMemoryBackendProvider(), createDummyConceptDescriptions(), "cdRepo").create(); for (Submodel submodel : createDummySubmodels()) { diff --git a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendConfiguration.java similarity index 82% rename from basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java rename to basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendConfiguration.java index 44860711d..b254af055 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java +++ b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendConfiguration.java @@ -26,11 +26,11 @@ package org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackend; import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; /** * @@ -39,12 +39,12 @@ * @author mateusmolina, danish */ @ConditionalOnExpression("'${basyx.backend}'.equals('InMemory')") -@Component -public class AasInMemoryBackendProvider implements AasBackendProvider { +@Configuration +public class AasInMemoryBackendConfiguration { - @Override - public CrudRepository getCrudRepository() { - return new InMemoryCrudRepository(AssetAdministrationShell::getId); + @Bean + public AasBackend getAasBackend() { + return (AasBackend) new InMemoryCrudRepository<>(AssetAdministrationShell::getId); } } diff --git a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/TestInMemoryAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/TestInMemoryAasRepository.java index 5cfb59865..9a0fc267b 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/TestInMemoryAasRepository.java +++ b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/TestInMemoryAasRepository.java @@ -56,7 +56,7 @@ public class TestInMemoryAasRepository extends AasRepositorySuite { private static final String CONFIGURED_AAS_REPO_NAME = "configured-aas-repo-name"; - private AasBackendProvider backendProvider = new AasInMemoryBackendProvider(); + private AasBackendProvider backendProvider = new AasInMemoryBackendConfiguration(); private static FileRepository fileRepository; diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBBackendProvider.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBBackendProvider.java deleted file mode 100644 index 65ca1d6b2..000000000 --- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBBackendProvider.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2023 the Eclipse BaSyx Authors - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * SPDX-License-Identifier: MIT - ******************************************************************************/ - -package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; - -import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackendProvider; -import org.eclipse.digitaltwin.basyx.common.mongocore.BasyxMongoMappingContext; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; -import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation; -import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Component; - -/** - * - * MongoDB Backend Provider for the AAS - * - * @author mateusmolina, despen - */ -@ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") -@Component -public class AasMongoDBBackendProvider implements AasBackendProvider { - - private BasyxMongoMappingContext mappingContext; - - private MongoTemplate template; - - @Autowired - public AasMongoDBBackendProvider(BasyxMongoMappingContext mappingContext, @Value("${basyx.aasrepository.mongodb.collectionName:aas-repo}") String collectionName, MongoTemplate template) { - super(); - this.mappingContext = mappingContext; - this.template = template; - - mappingContext.addEntityMapping(AssetAdministrationShell.class, collectionName); - } - - @Override - public CrudRepository getCrudRepository() { - @SuppressWarnings("unchecked") - MongoPersistentEntity entity = (MongoPersistentEntity) mappingContext.getPersistentEntity(AssetAdministrationShell.class); - - return new SimpleMongoRepository<>(new MappingMongoEntityInformation<>(entity), template); - } - -} diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java new file mode 100644 index 000000000..aea608fb8 --- /dev/null +++ b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java @@ -0,0 +1,13 @@ +package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackend; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +@ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") +public interface MongoDBAasBackend extends AasBackend, MongoRepository { + +} diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBRepositoryConfiguration.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java similarity index 67% rename from basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBRepositoryConfiguration.java rename to basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java index 6a6eb6371..c59fb0e19 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBRepositoryConfiguration.java +++ b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java @@ -23,15 +23,20 @@ * SPDX-License-Identifier: MIT ******************************************************************************/ - package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; +import org.eclipse.digitaltwin.basyx.common.mongocore.BasyxMongoMappingContext; import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; +import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation; /** * Provides a InMemoryAasServiceFactory for usage in the MongoDB Aas Repository @@ -45,9 +50,21 @@ */ @Configuration @ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") -public class AasMongoDBRepositoryConfiguration { +@EnableMongoRepositories +public class MongoDBAasBackendConfiguration { @Bean public AasServiceFactory getAasServiceFactory(FileRepository fileRepository) { return new InMemoryAasServiceFactory(fileRepository); } + + @Bean + public MappingMongoEntityInformation getMappingMongoEntityInformation(BasyxMongoMappingContext mappingContext, @Value("${basyx.aasrepository.mongodb.collectionName:aas-repo}") String collectionName) { + mappingContext.addEntityMapping(AssetAdministrationShell.class, collectionName); + + @SuppressWarnings("unchecked") + MongoPersistentEntity entity = (MongoPersistentEntity) mappingContext.getPersistentEntity(AssetAdministrationShell.class); + + return new MappingMongoEntityInformation<>(entity); + } + } diff --git a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml index 5936dd14a..6088c12b5 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml +++ b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml @@ -27,5 +27,9 @@ commons-io commons-io + + org.springframework + spring-tx + \ No newline at end of file diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java new file mode 100644 index 000000000..eedb633e4 --- /dev/null +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java @@ -0,0 +1,8 @@ +package org.eclipse.digitaltwin.basyx.aasrepository.backend; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.springframework.data.repository.CrudRepository; + +public interface AasBackend extends CrudRepository { + +} diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackendProvider.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackendProvider.java deleted file mode 100644 index 2fe0fd33a..000000000 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackendProvider.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2023 the Eclipse BaSyx Authors - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * SPDX-License-Identifier: MIT - ******************************************************************************/ -package org.eclipse.digitaltwin.basyx.aasrepository.backend; - -import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.springframework.data.repository.CrudRepository; - -/** - * Backend provider for the AAS - * - * @author mateusmolina, despen - */ -public interface AasBackendProvider { - public CrudRepository getCrudRepository(); -} diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java index f78c7e045..4ee1a8cea 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java @@ -46,6 +46,9 @@ import org.eclipse.digitaltwin.basyx.core.pagination.PaginationSupport; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; /** * Default Implementation for the {@link AasRepository} based on Spring @@ -54,22 +57,18 @@ * @author mateusmolina, despen, zhangzai, kammognie * */ +@Service public class CrudAasRepository implements AasRepository { - private CrudRepository aasBackend; + private final AasBackend aasBackend; - private AasServiceFactory aasServiceFactory; + private final AasServiceFactory aasServiceFactory; - private String aasRepositoryName = null; + private final String aasRepositoryName; - public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory) { - this.aasBackend = aasBackendProvider.getCrudRepository(); + public CrudAasRepository(AasBackend aasBackend, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { + this.aasBackend = aasBackend; this.aasServiceFactory = aasServiceFactory; - } - - public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { - this(aasBackendProvider, aasServiceFactory); - this.aasRepositoryName = aasRepositoryName; } @@ -77,7 +76,7 @@ public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactor public CursorResult> getAllAas(PaginationInfo pInfo) { Iterable iterable = aasBackend.findAll(); - List allAas = StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); + List allAas = StreamSupport.stream(iterable.spliterator(), false).toList(); TreeMap aasMap = allAas.stream().collect(Collectors.toMap(AssetAdministrationShell::getId, aas -> aas, (a, b) -> a, TreeMap::new)); @@ -92,15 +91,17 @@ public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistE } @Override + @Transactional public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException { throwIfAasIdEmptyOrNull(aas.getId()); - + throwIfAasExists(aas); aasBackend.save(aas); } @Override + @Transactional public void deleteAas(String aasId) { throwIfAasDoesNotExist(aasId); @@ -108,6 +109,7 @@ public void deleteAas(String aasId) { } @Override + @Transactional public void updateAas(String aasId, AssetAdministrationShell aas) { throwIfAasDoesNotExist(aasId); @@ -122,38 +124,41 @@ public CursorResult> getSubmodelReferences(String aasId, Paginat } @Override + @Transactional public void addSubmodelReference(String aasId, Reference submodelReference) { AasService aasService = getAasServiceOrThrow(aasId); aasService.addSubmodelReference(submodelReference); - updateAas(aasId, aasService.getAAS()); + aasBackend.save(aasService.getAAS()); } @Override + @Transactional public void removeSubmodelReference(String aasId, String submodelId) { AasService aasService = getAasServiceOrThrow(aasId); aasService.removeSubmodelReference(submodelId); - updateAas(aasId, aasService.getAAS()); + aasBackend.save(aasService.getAAS()); } @Override + @Transactional public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException { AasService aasService = getAasServiceOrThrow(aasId); aasService.setAssetInformation(aasInfo); - updateAas(aasId, aasService.getAAS()); + aasBackend.save(aasService.getAAS()); } @Override + @Transactional public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException { return getAasServiceOrThrow(aasId).getAssetInformation(); } - @Override public String getName() { return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName; @@ -165,21 +170,23 @@ public File getThumbnail(String aasId) { } @Override + @Transactional public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { AasService aasService = getAasServiceOrThrow(aasId); aasService.setThumbnail(fileName, contentType, inputStream); - updateAas(aasId, aasService.getAAS()); + aasBackend.save(aasService.getAAS()); } @Override + @Transactional public void deleteThumbnail(String aasId) { AasService aasService = getAasServiceOrThrow(aasId); aasService.deleteThumbnail(); - updateAas(aasId, aasService.getAAS()); + aasBackend.save(aasService.getAAS()); } private AasService getAasServiceOrThrow(String aasId) { @@ -200,9 +207,9 @@ private void throwIfAasExists(AssetAdministrationShell aas) { if (aasBackend.existsById(aasId)) throw new CollidingIdentifierException(aasId); } - + private void throwIfAasIdEmptyOrNull(String aasId) { - if(aasId == null || aasId.isBlank()) + if (aasId == null || aasId.isBlank()) throw new MissingIdentifierException(aasId); } diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java index c9bbdb3c3..8639072f5 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java @@ -27,15 +27,12 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.stereotype.Component; /** - * Simple AAS repository factory that creates a {@link CrudAasRepository} with - * a backend provider and a service factory + * Simple AAS repository factory that creates a {@link CrudAasRepository} with a + * backend provider and a service factory * * @author mateusmolina * @@ -44,27 +41,15 @@ @ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${basyx.backend:}')") public class SimpleAasRepositoryFactory implements AasRepositoryFactory { - private AasBackendProvider aasBackendProvider; + private CrudAasRepository crudAasRepository; - private AasServiceFactory aasServiceFactory; - - private String aasRepositoryName = null; - - @Autowired(required = false) - public SimpleAasRepositoryFactory(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory) { - this.aasBackendProvider = aasBackendProvider; - this.aasServiceFactory = aasServiceFactory; - } - - @Autowired(required = false) - public SimpleAasRepositoryFactory(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { - this(aasBackendProvider, aasServiceFactory); - this.aasRepositoryName = aasRepositoryName; + public SimpleAasRepositoryFactory(CrudAasRepository crudAasRepository) { + this.crudAasRepository = crudAasRepository; } @Override public AasRepository create() { - return new CrudAasRepository(aasBackendProvider, aasServiceFactory, aasRepositoryName); + return crudAasRepository; } } diff --git a/basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/TestMqttV2AASAggregatorObserver.java b/basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/TestMqttV2AASAggregatorObserver.java index fd61cf76b..df006ca6a 100644 --- a/basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/TestMqttV2AASAggregatorObserver.java +++ b/basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/TestMqttV2AASAggregatorObserver.java @@ -40,7 +40,7 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepositoryFactory; import org.eclipse.digitaltwin.basyx.aasrepository.DummyAasFactory; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.common.mqttcore.encoding.Base64URLEncoder; import org.eclipse.digitaltwin.basyx.common.mqttcore.encoding.URLEncoder; @@ -170,7 +170,7 @@ private AssetAdministrationShell createAasWithSubmodelReference(String aasId) { } private static AasRepository createMqttAasRepository(MqttClient client) { - AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())); + AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendConfiguration(), new InMemoryAasServiceFactory(new InMemoryFileRepository())); return new MqttAasRepositoryFactory(repoFactory, client, new MqttAasRepositoryTopicFactory(new URLEncoder())).create(); } diff --git a/basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/AasRepositoryRegistryLinkDescriptorGenerationTest.java b/basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/AasRepositoryRegistryLinkDescriptorGenerationTest.java index 8b3739033..3683d46ca 100644 --- a/basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/AasRepositoryRegistryLinkDescriptorGenerationTest.java +++ b/basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/AasRepositoryRegistryLinkDescriptorGenerationTest.java @@ -39,7 +39,7 @@ import org.eclipse.digitaltwin.basyx.aasregistry.main.client.mapper.AttributeMapper; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.core.filerepository.InMemoryFileRepository; import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier; @@ -115,7 +115,7 @@ private AssetAdministrationShell createDummyAas() { } protected AasRepository getAasRepository() { - return new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); + return new SimpleAasRepositoryFactory(new AasInMemoryBackendConfiguration(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); } } diff --git a/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/testconfig/DummyAasRepositoryConfig.java b/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/testconfig/DummyAasRepositoryConfig.java index e46cdd789..d5e1b8c82 100644 --- a/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/testconfig/DummyAasRepositoryConfig.java +++ b/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/testconfig/DummyAasRepositoryConfig.java @@ -27,7 +27,7 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.core.filerepository.InMemoryFileRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -49,6 +49,6 @@ public class DummyAasRepositoryConfig { @Bean @ConditionalOnMissingBean public AasRepository createAasRepository() { - return new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); + return new SimpleAasRepositoryFactory(new AasInMemoryBackendConfiguration(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create(); } } diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java index d4129dca9..d961e54fa 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java @@ -42,7 +42,7 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepositoryFactory; import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendProvider; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory.AasInMemoryBackendConfiguration; import org.eclipse.digitaltwin.basyx.aasservice.AasService; import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; import org.eclipse.digitaltwin.basyx.aasservice.AasServiceSuite; @@ -197,7 +197,7 @@ private static ObjectMapper configureObjectMapper() { } private static AasRepository createMqttAasRepository() { - AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), mqttAasServiceFactory); + AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendConfiguration(), mqttAasServiceFactory); return repoFactory.create(); } From 36fadb72df4e2401f8fb998c11a307e517f5f45d Mon Sep 17 00:00:00 2001 From: mateusmolina Date: Tue, 10 Dec 2024 15:40:47 +0100 Subject: [PATCH 4/4] refactor: replace AasService dep in AasRespository through Repository Fragment --- .../mongodb/AasServiceBackendImpl.java | 70 ++++++++++++ .../backend/mongodb/MongoDBAasBackend.java | 13 --- .../MongoDBAasBackendConfiguration.java | 22 +--- .../basyx.aasrepository-backend/pom.xml | 4 - .../aasrepository/backend/AasBackend.java | 6 +- .../backend/AasServiceBackend.java | 100 ++++++++++++++++++ .../backend/CrudAasRepository.java | 60 ++--------- .../backend/SimpleAasRepositoryFactory.java | 4 +- .../component/AasRepositoryConfiguration.java | 10 -- 9 files changed, 190 insertions(+), 99 deletions(-) create mode 100644 basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasServiceBackendImpl.java delete mode 100644 basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java create mode 100644 basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasServiceBackend.java diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasServiceBackendImpl.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasServiceBackendImpl.java new file mode 100644 index 000000000..e434868ae --- /dev/null +++ b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasServiceBackendImpl.java @@ -0,0 +1,70 @@ +package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; + +import java.io.File; +import java.io.InputStream; +import java.util.List; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; +import org.eclipse.digitaltwin.aas4j.v3.model.Reference; +import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasServiceBackend; +import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; +import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; +import org.springframework.data.mongodb.core.MongoOperations; + +public class AasServiceBackendImpl implements AasServiceBackend { + + private final MongoOperations mongoOperations; + + public AasServiceBackendImpl(MongoOperations mongoOperations) { + this.mongoOperations = mongoOperations; + } + + @Override + public CursorResult> getSubmodelReferences(String aasId, PaginationInfo pInfo) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getSubmodelReferences'"); + } + + @Override + public void addSubmodelReference(String aasId, Reference submodelReference) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'addSubmodelReference'"); + } + + @Override + public void removeSubmodelReference(String aasId, String submodelId) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'removeSubmodelReference'"); + } + + @Override + public void setAssetInformation(String aasId, AssetInformation aasInfo) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'setAssetInformation'"); + } + + @Override + public AssetInformation getAssetInformation(String aasId) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getAssetInformation'"); + } + + @Override + public File getThumbnail(String aasId) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getThumbnail'"); + } + + @Override + public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'setThumbnail'"); + } + + @Override + public void deleteThumbnail(String aasId) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'deleteThumbnail'"); + } + +} diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java deleted file mode 100644 index aea608fb8..000000000 --- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackend.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; - -import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackend; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.stereotype.Repository; - -@Repository -@ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") -public interface MongoDBAasBackend extends AasBackend, MongoRepository { - -} diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java index c59fb0e19..0fae84dcc 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java +++ b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java @@ -26,10 +26,7 @@ package org.eclipse.digitaltwin.basyx.aasrepository.backend.mongodb; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; -import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.common.mongocore.BasyxMongoMappingContext; -import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; @@ -39,31 +36,22 @@ import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation; /** - * Provides a InMemoryAasServiceFactory for usage in the MongoDB Aas Repository - * backend.
- *
- * This is needed to ensure that the AasServiceFeatures are processed correctly - * when utilizing MongoDb * - * @author schnicke + * Provides the MongoDB configuration for the {@link AasRepository} + * + * @author schnicke, mateusmolina * */ @Configuration @ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") -@EnableMongoRepositories +@EnableMongoRepositories(basePackages = "org.eclipse.digitaltwin.basyx.aasrepository.backend") public class MongoDBAasBackendConfiguration { @Bean - public AasServiceFactory getAasServiceFactory(FileRepository fileRepository) { - return new InMemoryAasServiceFactory(fileRepository); - } - - @Bean - public MappingMongoEntityInformation getMappingMongoEntityInformation(BasyxMongoMappingContext mappingContext, @Value("${basyx.aasrepository.mongodb.collectionName:aas-repo}") String collectionName) { + MappingMongoEntityInformation getMappingMongoEntityInformation(BasyxMongoMappingContext mappingContext, @Value("${basyx.aasrepository.mongodb.collectionName:aas-repo}") String collectionName) { mappingContext.addEntityMapping(AssetAdministrationShell.class, collectionName); @SuppressWarnings("unchecked") MongoPersistentEntity entity = (MongoPersistentEntity) mappingContext.getPersistentEntity(AssetAdministrationShell.class); - return new MappingMongoEntityInformation<>(entity); } diff --git a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml index 6088c12b5..5936dd14a 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml +++ b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml @@ -27,9 +27,5 @@ commons-io commons-io - - org.springframework - spring-tx - \ No newline at end of file diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java index eedb633e4..17fcc6226 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java @@ -2,7 +2,9 @@ import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; -public interface AasBackend extends CrudRepository { +@Repository +public interface AasBackend extends CrudRepository, AasServiceBackend { -} +} \ No newline at end of file diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasServiceBackend.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasServiceBackend.java new file mode 100644 index 000000000..345b6c6b2 --- /dev/null +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasServiceBackend.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (C) 2023 the Eclipse BaSyx Authors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * SPDX-License-Identifier: MIT + ******************************************************************************/ +package org.eclipse.digitaltwin.basyx.aasrepository.backend; + +import java.io.File; +import java.io.InputStream; +import java.util.List; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; +import org.eclipse.digitaltwin.aas4j.v3.model.Reference; +import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; +import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; + +/** + * AASService Backend Fragment + * + * #TODO this could be moved to the AASService package and reused there and + * underlying AasService Implementation + * + * @author mateusmolina + * + */ +public interface AasServiceBackend { + + /** + * Retrieves all Submodel References + * + * @return A List containing all Submodel References + */ + public CursorResult> getSubmodelReferences(String aasId, PaginationInfo pInfo); + + /** + * Adds a Submodel Reference + */ + public void addSubmodelReference(String aasId, Reference submodelReference); + + /** + * Removes a Submodel Reference + */ + public void removeSubmodelReference(String aasId, String submodelId); + + /** + * Sets the asset-information of the AAS contained in the server + */ + public void setAssetInformation(String aasId, AssetInformation aasInfo); + + /** + * Retrieves the asset-information of the AAS contained in the server + * + * @return the Asset-Information of the AAS + */ + public AssetInformation getAssetInformation(String aasId); + + /** + * Get Thumbnail of the specific aas + * + * @return the file of the thumbnail + */ + public File getThumbnail(String aasId); + + /** + * Set Thumbnail of the AAS + * + * @param fileName + * name of the thumbnail file with extension + * @param contentType + * content type of the file + * @param inputStream + * inputstream of the thumbnail file + */ + public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream); + + /** + * Delete the thumbnail file of the AAS + * + */ + public void deleteThumbnail(String aasId); +} diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java index 4ee1a8cea..1420c7f40 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java @@ -35,8 +35,6 @@ import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; import org.eclipse.digitaltwin.aas4j.v3.model.Reference; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; -import org.eclipse.digitaltwin.basyx.aasservice.AasService; -import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException; import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.exceptions.IdentificationMismatchException; @@ -47,8 +45,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; /** * Default Implementation for the {@link AasRepository} based on Spring @@ -62,13 +58,10 @@ public class CrudAasRepository implements AasRepository { private final AasBackend aasBackend; - private final AasServiceFactory aasServiceFactory; - private final String aasRepositoryName; - public CrudAasRepository(AasBackend aasBackend, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { + public CrudAasRepository(AasBackend aasBackend, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { this.aasBackend = aasBackend; - this.aasServiceFactory = aasServiceFactory; this.aasRepositoryName = aasRepositoryName; } @@ -91,7 +84,6 @@ public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistE } @Override - @Transactional public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException { throwIfAasIdEmptyOrNull(aas.getId()); @@ -101,7 +93,6 @@ public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierEx } @Override - @Transactional public void deleteAas(String aasId) { throwIfAasDoesNotExist(aasId); @@ -109,7 +100,6 @@ public void deleteAas(String aasId) { } @Override - @Transactional public void updateAas(String aasId, AssetAdministrationShell aas) { throwIfAasDoesNotExist(aasId); @@ -120,43 +110,27 @@ public void updateAas(String aasId, AssetAdministrationShell aas) { @Override public CursorResult> getSubmodelReferences(String aasId, PaginationInfo pInfo) { - return getAasServiceOrThrow(aasId).getSubmodelReferences(pInfo); + return aasBackend.getSubmodelReferences(aasId, pInfo); } @Override - @Transactional public void addSubmodelReference(String aasId, Reference submodelReference) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.addSubmodelReference(submodelReference); - - aasBackend.save(aasService.getAAS()); + aasBackend.addSubmodelReference(aasId, submodelReference); } @Override - @Transactional public void removeSubmodelReference(String aasId, String submodelId) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.removeSubmodelReference(submodelId); - - aasBackend.save(aasService.getAAS()); + aasBackend.removeSubmodelReference(aasId, submodelId); } @Override - @Transactional public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.setAssetInformation(aasInfo); - - aasBackend.save(aasService.getAAS()); + aasBackend.setAssetInformation(aasId, aasInfo); } @Override - @Transactional public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException { - return getAasServiceOrThrow(aasId).getAssetInformation(); + return aasBackend.getAssetInformation(aasId); } @Override @@ -166,33 +140,17 @@ public String getName() { @Override public File getThumbnail(String aasId) { - return getAasServiceOrThrow(aasId).getThumbnail(); + return aasBackend.getThumbnail(aasId); } @Override - @Transactional public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.setThumbnail(fileName, contentType, inputStream); - - aasBackend.save(aasService.getAAS()); + aasBackend.setThumbnail(aasId, fileName, contentType, inputStream); } @Override - @Transactional public void deleteThumbnail(String aasId) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.deleteThumbnail(); - - aasBackend.save(aasService.getAAS()); - } - - private AasService getAasServiceOrThrow(String aasId) { - AssetAdministrationShell aas = aasBackend.findById(aasId).orElseThrow(() -> new ElementDoesNotExistException(aasId)); - - return aasServiceFactory.create(aas); + aasBackend.deleteThumbnail(aasId); } private void throwIfMismatchingIds(String aasId, AssetAdministrationShell newAas) { diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java index 8639072f5..c3a9f8bb4 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/SimpleAasRepositoryFactory.java @@ -31,8 +31,8 @@ import org.springframework.stereotype.Component; /** - * Simple AAS repository factory that creates a {@link CrudAasRepository} with a - * backend provider and a service factory + * Simple AAS repository factory that provides the {@link CrudAasRepository} + * component * * @author mateusmolina * diff --git a/basyx.aasrepository/basyx.aasrepository.component/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/component/AasRepositoryConfiguration.java b/basyx.aasrepository/basyx.aasrepository.component/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/component/AasRepositoryConfiguration.java index cd5c065b3..fc01e0db5 100644 --- a/basyx.aasrepository/basyx.aasrepository.component/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/component/AasRepositoryConfiguration.java +++ b/basyx.aasrepository/basyx.aasrepository.component/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/component/AasRepositoryConfiguration.java @@ -32,13 +32,9 @@ import org.eclipse.digitaltwin.basyx.aasrepository.feature.AasRepositoryFeature; import org.eclipse.digitaltwin.basyx.aasrepository.feature.DecoratedAasRepositoryFactory; import org.eclipse.digitaltwin.basyx.aasservice.AasService; -import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; -import org.eclipse.digitaltwin.basyx.aasservice.feature.AasServiceFeature; -import org.eclipse.digitaltwin.basyx.aasservice.feature.DecoratedAasServiceFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; /** * Provides the spring bean configuration for the {@link AasRepository} and @@ -54,10 +50,4 @@ public class AasRepositoryConfiguration { public static AasRepository getAasRepository(AasRepositoryFactory aasRepositoryFactory, List features) { return new DecoratedAasRepositoryFactory(aasRepositoryFactory, features).create(); } - - @Primary - @Bean - public static AasServiceFactory getAasService(AasServiceFactory aasServiceFactory, List features) { - return new DecoratedAasServiceFactory(aasServiceFactory, features); - } }