diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/pom.xml b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/pom.xml new file mode 100644 index 000000000..249a19656 --- /dev/null +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + + org.eclipse.digitaltwin.basyx + basyx.conceptdescriptionrepository + ${revision} + + + basyx.conceptdescriptionrepository-backend-tdb + Concept Description Repository Backend Triplestore + + + + org.eclipse.digitaltwin.basyx + basyx.conceptdescriptionrepository-core + + + + com.squareup.okhttp3 + okhttp + 4.12.0 + + + + org.eclipse.digitaltwin.basyx + basyx.conceptdescriptionrepository-core + tests + test + + + + org.springframework + spring-context + + + org.springframework.boot + spring-boot-starter + + + org.eclipse.digitaltwin.aas4j + dataformat-json + + + \ No newline at end of file diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepository.java b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepository.java new file mode 100644 index 000000000..e9ea5c358 --- /dev/null +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepository.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * 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.conceptdescriptionrepository; + +import java.awt.*; +import java.io.IOException; +import java.util.List; + +import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; +import okhttp3.*; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; +import org.eclipse.digitaltwin.aas4j.v3.model.ConceptDescription; +import org.eclipse.digitaltwin.aas4j.v3.model.Reference; +import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException; +import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException; +import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException; +import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; +import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; + +/** + * + * TDB implementation of the ConceptDescriptionRepository + * + * @author mhrimaz + * + */ +public class TDBConceptDescriptionRepository implements ConceptDescriptionRepository { + + private String collectionName; + private String cdRepositoryName; + + public TDBConceptDescriptionRepository(String collectionName) { + this.collectionName = collectionName; + } + + public TDBConceptDescriptionRepository(String collectionName, String cdRepositoryName) { + this(collectionName); + this.cdRepositoryName = cdRepositoryName; + } + + @Override + public CursorResult> getAllConceptDescriptions(PaginationInfo pInfo) { + throw new RuntimeException(); + } + + @Override + public CursorResult> getAllConceptDescriptionsByIdShort(String idShort, PaginationInfo pInfo) { + throw new RuntimeException(); + } + + @Override + public CursorResult> getAllConceptDescriptionsByIsCaseOf(Reference reference, PaginationInfo pInfo) { + throw new RuntimeException(); + } + + @Override + public CursorResult> getAllConceptDescriptionsByDataSpecificationReference(Reference reference, PaginationInfo pInfo) { + throw new RuntimeException(); + } + + @Override + public ConceptDescription getConceptDescription(String conceptDescriptionId) throws ElementDoesNotExistException { + throw new RuntimeException(); + } + + @Override + public void updateConceptDescription(String conceptDescriptionId, ConceptDescription conceptDescription) throws ElementDoesNotExistException { + + throw new RuntimeException(); + } + + private static String conceptDescriptionAsRDF(final ConceptDescription conceptDescription) throws SerializationException { + // TODO: this should use AAS4j method in the future + JsonSerializer jsonSerializer = new JsonSerializer(); + String conceptDescriptionAsJson = jsonSerializer.write(conceptDescription); + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, conceptDescriptionAsJson); + Request request = new Request.Builder() + .url("http://127.0.0.1:9192/concept-description:jsontordf") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + try { + Response response = client.newCall(request).execute(); + String asRDF = response.body().string(); + String asRDFWithoutPrefixes = asRDF.replaceAll("(?m)^@.*$", ""); + return asRDFWithoutPrefixes; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void createConceptDescription(ConceptDescription conceptDescription) throws CollidingIdentifierException, MissingIdentifierException { + String conceptDescriptionAsRDF = null; + try { + conceptDescriptionAsRDF = conceptDescriptionAsRDF(conceptDescription); + } catch (SerializationException e) { + throw new RuntimeException(e); + } + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/sparql-update"); + RequestBody body = RequestBody.create(mediaType, "prefix aas: \r\nprefix xsd: \r\nINSERT DATA \r\n{"+conceptDescriptionAsRDF+"}"); + Request request = new Request.Builder() + .url("http://127.0.0.1:3030/test/update") + .method("POST", body) + .addHeader("Content-Type", "application/sparql-update") + .build(); + try { + Response response = client.newCall(request).execute(); + } catch (IOException e) { + throw new RuntimeException(e); + } + throw new RuntimeException(); + } + + @Override + public void deleteConceptDescription(String conceptDescriptionId) throws ElementDoesNotExistException { + throw new RuntimeException(); + } + + @Override + public String getName() { + return cdRepositoryName == null ? ConceptDescriptionRepository.super.getName() : cdRepositoryName; + } + +} diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepositoryFactory.java b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepositoryFactory.java new file mode 100644 index 000000000..8c269a7e2 --- /dev/null +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-tdb/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/TDBConceptDescriptionRepositoryFactory.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.conceptdescriptionrepository; + +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; + +/** + * ConceptDescriptionRepository factory returning a TDB backend + * ConceptDescriptionRepository + * + * @author mhrimaz + */ +@Component +@ConditionalOnExpression("'${basyx.backend}'.equals('tdb')") +public class TDBConceptDescriptionRepositoryFactory implements ConceptDescriptionRepositoryFactory { + + private String collectionName; + private String cdRepositoryName; + + @Autowired(required = false) + public TDBConceptDescriptionRepositoryFactory( + @Value("${basyx.conceptdescriptionrepository.mongodb.collectionName:conceptdescription-repo}") String collectionName) { + + this.collectionName = collectionName; + } + + @Autowired(required = false) + public TDBConceptDescriptionRepositoryFactory( + @Value("${basyx.conceptdescriptionrepository.mongodb.collectionName:conceptdescription-repo}") String collectionName, + @Value("${basyx.cdrepo.name:cd-repo}") String cdRepositoryName) { + this(collectionName); + this.cdRepositoryName = cdRepositoryName; + } + + @Override + public ConceptDescriptionRepository create() { + return new TDBConceptDescriptionRepository( collectionName, cdRepositoryName); + } +} diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/pom.xml b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/pom.xml index 7efa9cca9..a2bd428cd 100644 --- a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/pom.xml +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/pom.xml @@ -35,6 +35,10 @@ org.eclipse.digitaltwin.basyx basyx.conceptdescriptionrepository-backend-mongodb + + org.eclipse.digitaltwin.basyx + basyx.conceptdescriptionrepository-backend-tdb + org.eclipse.digitaltwin.basyx basyx.conceptdescriptionrepository-http diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/src/main/resources/application.properties b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/src/main/resources/application.properties index d0e0d7448..587233b40 100644 --- a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/src/main/resources/application.properties +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository.component/src/main/resources/application.properties @@ -2,9 +2,9 @@ server.port=8081 spring.application.name=Concept Description Repository basyx.cdrepo.name = cd-repo -basyx.backend = InMemory +#basyx.backend = InMemory -#basyx.backend = MongoDB +basyx.backend = tdb #spring.data.mongodb.host=127.0.0.1 ##or spring.data.mongodb.host=127.0.0.1 #spring.data.mongodb.port=27017 diff --git a/basyx.conceptdescriptionrepository/pom.xml b/basyx.conceptdescriptionrepository/pom.xml index 6d081a852..7e7502fda 100644 --- a/basyx.conceptdescriptionrepository/pom.xml +++ b/basyx.conceptdescriptionrepository/pom.xml @@ -17,6 +17,7 @@ basyx.conceptdescriptionrepository-http basyx.conceptdescriptionrepository-backend-inmemory basyx.conceptdescriptionrepository-backend-mongodb + basyx.conceptdescriptionrepository-backend-tdb basyx.conceptdescriptionrepository-tck basyx.conceptdescriptionrepository.component diff --git a/pom.xml b/pom.xml index 569fc5010..5b90d8c85 100644 --- a/pom.xml +++ b/pom.xml @@ -513,6 +513,11 @@ basyx.conceptdescriptionrepository-backend-mongodb ${revision} + + org.eclipse.digitaltwin.basyx + basyx.conceptdescriptionrepository-backend-tdb + ${revision} + org.eclipse.digitaltwin.basyx basyx.conceptdescriptionrepository.component