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; } - } 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 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/AasMongoDBRepositoryConfiguration.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBRepositoryConfiguration.java deleted file mode 100644 index 6a6eb6371..000000000 --- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBRepositoryConfiguration.java +++ /dev/null @@ -1,53 +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.basyx.aasservice.AasServiceFactory; -import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; -import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * 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 - * - */ -@Configuration -@ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')") -public class AasMongoDBRepositoryConfiguration { - @Bean - public AasServiceFactory getAasServiceFactory(FileRepository fileRepository) { - return new InMemoryAasServiceFactory(fileRepository); - } -} 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/AasMongoDBBackendProvider.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java similarity index 68% rename from basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/AasMongoDBBackendProvider.java rename to basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/MongoDBAasBackendConfiguration.java index 65ca1d6b2..0fae84dcc 100644 --- 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/MongoDBAasBackendConfiguration.java @@ -26,47 +26,33 @@ 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.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; -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 + * Provides the MongoDB configuration for the {@link AasRepository} * - * @author mateusmolina, despen + * @author schnicke, mateusmolina + * */ +@Configuration @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; - +@EnableMongoRepositories(basePackages = "org.eclipse.digitaltwin.basyx.aasrepository.backend") +public class MongoDBAasBackendConfiguration { + @Bean + MappingMongoEntityInformation getMappingMongoEntityInformation(BasyxMongoMappingContext mappingContext, @Value("${basyx.aasrepository.mongodb.collectionName:aas-repo}") String collectionName) { 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); + return new MappingMongoEntityInformation<>(entity); } } 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..17fcc6226 --- /dev/null +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/AasBackend.java @@ -0,0 +1,10 @@ +package org.eclipse.digitaltwin.basyx.aasrepository.backend; + +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@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/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/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 f78c7e045..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; @@ -46,6 +44,7 @@ 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; /** * Default Implementation for the {@link AasRepository} based on Spring @@ -54,22 +53,15 @@ * @author mateusmolina, despen, zhangzai, kammognie * */ +@Service public class CrudAasRepository implements AasRepository { - private CrudRepository aasBackend; + private final AasBackend aasBackend; - private AasServiceFactory aasServiceFactory; - - private String aasRepositoryName = null; - - public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory) { - this.aasBackend = aasBackendProvider.getCrudRepository(); - this.aasServiceFactory = aasServiceFactory; - } - - public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { - this(aasBackendProvider, aasServiceFactory); + private final String aasRepositoryName; + public CrudAasRepository(AasBackend aasBackend, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { + this.aasBackend = aasBackend; this.aasRepositoryName = aasRepositoryName; } @@ -77,7 +69,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)); @@ -94,7 +86,7 @@ public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistE @Override public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException { throwIfAasIdEmptyOrNull(aas.getId()); - + throwIfAasExists(aas); aasBackend.save(aas); @@ -118,42 +110,29 @@ 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 public void addSubmodelReference(String aasId, Reference submodelReference) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.addSubmodelReference(submodelReference); - - updateAas(aasId, aasService.getAAS()); + aasBackend.addSubmodelReference(aasId, submodelReference); } @Override public void removeSubmodelReference(String aasId, String submodelId) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.removeSubmodelReference(submodelId); - - updateAas(aasId, aasService.getAAS()); + aasBackend.removeSubmodelReference(aasId, submodelId); } @Override public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.setAssetInformation(aasInfo); - - updateAas(aasId, aasService.getAAS()); + aasBackend.setAssetInformation(aasId, aasInfo); } @Override public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException { - return getAasServiceOrThrow(aasId).getAssetInformation(); + return aasBackend.getAssetInformation(aasId); } - @Override public String getName() { return aasRepositoryName == null ? AasRepository.super.getName() : aasRepositoryName; @@ -161,31 +140,17 @@ public String getName() { @Override public File getThumbnail(String aasId) { - return getAasServiceOrThrow(aasId).getThumbnail(); + return aasBackend.getThumbnail(aasId); } @Override 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.setThumbnail(aasId, fileName, contentType, inputStream); } @Override public void deleteThumbnail(String aasId) { - AasService aasService = getAasServiceOrThrow(aasId); - - aasService.deleteThumbnail(); - - updateAas(aasId, 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) { @@ -200,9 +165,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..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 @@ -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 provides the {@link CrudAasRepository} + * component * * @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.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); - } } 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(); }