diff options
26 files changed, 590 insertions, 210 deletions
diff --git a/cps-dependencies/pom.xml b/cps-dependencies/pom.xml index 6286391a59..9e0269b3a2 100644..100755 --- a/cps-dependencies/pom.xml +++ b/cps-dependencies/pom.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.onap.cps</groupId> @@ -21,6 +21,7 @@ <onap.nexus.url>https://nexus.onap.org</onap.nexus.url> <releaseNexusPath>/content/repositories/releases/</releaseNexusPath> <snapshotNexusPath>/content/repositories/snapshots/</snapshotNexusPath> + <modelmapper.version>2.3.8</modelmapper.version> <spock-core.version>2.0-M2-groovy-3.0</spock-core.version> <springboot.version>2.3.3.RELEASE</springboot.version> <springfox.version>3.0.0</springfox.version> @@ -93,6 +94,11 @@ <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> + <dependency> + <groupId>org.modelmapper</groupId> + <artifactId>modelmapper</artifactId> + <version>${modelmapper.version}</version> + </dependency> </dependencies> </dependencyManagement> </project>
\ No newline at end of file diff --git a/cps-parent/pom.xml b/cps-parent/pom.xml index bd7e1a529a..99f60951fd 100644 --- a/cps-parent/pom.xml +++ b/cps-parent/pom.xml @@ -19,7 +19,9 @@ <app>org.onap.cps.Application</app> <base.image>openjdk:11-jre-slim</base.image> <java.version>11</java.version> + <jacoco-maven-plugin.version>0.8.6</jacoco-maven-plugin.version> <jib-maven-plugin.version>2.6.0</jib-maven-plugin.version> + <minimum-coverage>0.15</minimum-coverage> <nexusproxy>https://nexus.onap.org</nexusproxy> <onap.nexus.url>https://nexus.onap.org</onap.nexus.url> <oparent.version>3.1.0</oparent.version> @@ -177,6 +179,26 @@ </execution> </executions> </plugin> + <plugin> + <groupId>com.google.cloud.tools</groupId> + <artifactId>jib-maven-plugin</artifactId> + <version>${jib-maven-plugin.version}</version> + <configuration> + <container> + <mainClass>${app}</mainClass> + <creationTime>USE_CURRENT_TIMESTAMP</creationTime> + </container> + <from> + <image>${base.image}</image> + </from> + <to> + <image>${repository.name}</image> + <tags> + <tag>${tag.version}</tag> + </tags> + </to> + </configuration> + </plugin> </plugins> </pluginManagement> <plugins> @@ -240,6 +262,7 @@ <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> + <argLine>@{argLine}</argLine> <useFile>false</useFile> <includes> <include>**/*Spec.java</include> @@ -248,24 +271,37 @@ </configuration> </plugin> <plugin> - <groupId>com.google.cloud.tools</groupId> - <artifactId>jib-maven-plugin</artifactId> - <version>${jib-maven-plugin.version}</version> - <configuration> - <container> - <mainClass>${app}</mainClass> - <creationTime>USE_CURRENT_TIMESTAMP</creationTime> - </container> - <from> - <image>${base.image}</image> - </from> - <to> - <image>${repository.name}</image> - <tags> - <tag>${tag.version}</tag> - </tags> - </to> - </configuration> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <version>${jacoco-maven-plugin.version}</version> + <executions> + <execution> + <id>default-prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + <execution> + <id>coverage-check</id> + <goals> + <goal>check</goal> + </goals> + <configuration> + <rules> + <rule> + <element>BUNDLE</element> + <limits> + <limit> + <counter>INSTRUCTION</counter> + <value>COVEREDRATIO</value> + <minimum>${minimum-coverage}</minimum> + </limit> + </limits> + </rule> + </rules> + </configuration> + </execution> + </executions> </plugin> </plugins> </build> diff --git a/cps-rest/docs/api/swagger/openapi.yml b/cps-rest/docs/api/swagger/openapi.yml index 82f47c088f..0c7c83c561 100644..100755 --- a/cps-rest/docs/api/swagger/openapi.yml +++ b/cps-rest/docs/api/swagger/openapi.yml @@ -81,26 +81,19 @@ paths: type: string requestBody: content: - multipart/form-data: + application/json: schema: - required: - - file - properties: - multipartFile: - type: string - description: multipartFile - format: binary + title: Anchor + description: anchor + $ref: '#/components/schemas/Anchor' required: true responses: - 200: - description: OK + 201: + description: Created content: application/json: schema: - type: object - 201: - description: Created - content: {} + type: string 401: description: Unauthorized content: {} @@ -370,4 +363,19 @@ paths: 404: description: Not Found content: {} -components: {}
\ No newline at end of file +components: + schemas: + Anchor: + type: object + title: Anchor + required: + - anchorName + - namespace + - revision + properties: + anchorName: + type: string + namespace: + type: string + revision: + type: string
\ No newline at end of file diff --git a/cps-rest/pom.xml b/cps-rest/pom.xml index fc3e6325e2..3a82ca3770 100644..100755 --- a/cps-rest/pom.xml +++ b/cps-rest/pom.xml @@ -51,6 +51,10 @@ <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
+ <groupId>org.modelmapper</groupId>
+ <artifactId>modelmapper</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
diff --git a/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java b/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java new file mode 100755 index 0000000000..cca5fe7d8d --- /dev/null +++ b/cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java @@ -0,0 +1,50 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.config;
+
+import org.modelmapper.ModelMapper;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+
+@Configuration
+public class CpsConfig {
+
+ /**
+ * Swagger configuration.
+ */
+ @Bean
+ public Docket api() {
+ return new Docket(DocumentationType.OAS_30).select().apis(RequestHandlerSelectors.any())
+ .paths(PathSelectors.any()).build();
+ }
+
+ /**
+ * ModelMapper configuration.
+ */
+ @Bean
+ public ModelMapper modelMapper() {
+ return new ModelMapper();
+ }
+}
\ No newline at end of file diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java index f0c5fcbc43..9e57408fb7 100644..100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java @@ -27,10 +27,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.validation.Valid; +import org.modelmapper.ModelMapper; import org.onap.cps.api.CpService; +import org.onap.cps.api.model.AnchorDetails; import org.onap.cps.exceptions.CpsException; import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.rest.api.CpsRestApi; +import org.onap.cps.rest.model.Anchor; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -49,9 +52,22 @@ public class CpsRestController implements CpsRestApi { @Autowired private CpService cpService; + @Autowired + private ModelMapper modelMapper; + + /** + * Create a new anchor. + * + * @param anchor the anchor details object. + * @param dataspaceName the dataspace name. + * @return a ResponseEntity with the anchor name. + */ @Override - public ResponseEntity<Object> createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) { - return null; + public final ResponseEntity<String> createAnchor(@Valid Anchor anchor, String dataspaceName) { + final AnchorDetails anchorDetails = modelMapper.map(anchor, AnchorDetails.class); + anchorDetails.setDataspace(dataspaceName); + final String anchorName = cpService.createAnchor(anchorDetails); + return new ResponseEntity<String>(anchorName, HttpStatus.CREATED); } @Override @@ -151,7 +167,7 @@ public class CpsRestController implements CpsRestApi { try { final Gson gson = new Gson(); gson.fromJson(getJsonString(multipartFile), Object.class); - } catch (JsonSyntaxException e) { + } catch (final JsonSyntaxException e) { throw new CpsValidationException("Not a valid JSON file.", e); } } @@ -160,13 +176,12 @@ public class CpsRestController implements CpsRestApi { try { final File file = File.createTempFile("tempFile", ".yang"); file.deleteOnExit(); - try (OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(multipartFile.getBytes()); } return file; - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @@ -174,7 +189,7 @@ public class CpsRestController implements CpsRestApi { private static String getJsonString(final MultipartFile multipartFile) { try { return new String(multipartFile.getBytes()); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } diff --git a/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java b/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java deleted file mode 100644 index 73e179511b..0000000000 --- a/cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Bell Canada. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.swagger.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; - -/** - * Swagger configuration. - */ -@Configuration -public class SpringFoxConfig { - - /** - * Define api configuration. - */ - @Bean - public Docket api() { - return new Docket(DocumentationType.OAS_30) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build(); - } -} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java index 627a14467d..aeab4f844b 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java @@ -19,6 +19,7 @@ package org.onap.cps.spi.entities; +import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -41,7 +42,9 @@ import lombok.Setter; @AllArgsConstructor @NoArgsConstructor @Table(name = "dataspace") -public class Dataspace { +public class Dataspace implements Serializable { + + private static final long serialVersionUID = 8395254649813051882L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java index 12422dc5f6..7afdb3e092 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java @@ -21,6 +21,7 @@ package org.onap.cps.spi.entities;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
+import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@@ -32,6 +33,7 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
+import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@@ -47,8 +49,11 @@ import org.hibernate.annotations.TypeDefs; @Entity
@AllArgsConstructor
@NoArgsConstructor
+@Builder
@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})
-public class Fragment {
+public class Fragment implements Serializable {
+
+ private static final long serialVersionUID = 7737669789097119667L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -62,6 +67,10 @@ public class Fragment { @Column(columnDefinition = "jsonb")
private String attributes;
+ @Column(columnDefinition = "text")
+ private String anchorName;
+
+ @NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dataspace_id")
private Dataspace dataspace;
@@ -73,4 +82,8 @@ public class Fragment { @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Fragment parentFragment;
+
+ @OneToOne(fetch = FetchType.LAZY)
+ @JoinColumn(name = "module_id")
+ private Module module;
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Module.java index d2130aeec1..ef6895c7d4 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/ModuleEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Module.java @@ -20,6 +20,7 @@ package org.onap.cps.spi.entities; +import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; @@ -45,7 +46,9 @@ import lombok.Setter; @AllArgsConstructor @NoArgsConstructor @Table(name = "module") -public class ModuleEntity { +public class Module implements Serializable { + + private static final long serialVersionUID = -748666970938314895L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -75,7 +78,7 @@ public class ModuleEntity { * @param revision the revision number of the module. * @param dataspace the dataspace related to the module. */ - public ModuleEntity(String namespace, String moduleContent, String revision, Dataspace dataspace) { + public Module(String namespace, String moduleContent, String revision, Dataspace dataspace) { this.namespace = namespace; this.moduleContent = moduleContent; this.revision = revision; diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistenceServiceImpl.java index 2b4f1357de..9ed111271a 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistenceServiceImpl.java @@ -19,7 +19,7 @@ package org.onap.cps.spi.impl; -import org.onap.cps.spi.DataPersistencyService; +import org.onap.cps.spi.DataPersistenceService; import org.onap.cps.spi.entities.JsonDataEntity; import org.onap.cps.spi.repository.DataRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +27,7 @@ import org.springframework.stereotype.Component; @Component -public class DataPersistencyServiceImpl implements DataPersistencyService { +public class DataPersistenceServiceImpl implements DataPersistenceService { @Autowired private DataRepository dataRepository; diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java new file mode 100755 index 0000000000..0ecf72d772 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java @@ -0,0 +1,72 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl;
+
+import org.onap.cps.api.model.AnchorDetails;
+import org.onap.cps.exceptions.CpsNotFoundException;
+import org.onap.cps.exceptions.CpsValidationException;
+import org.onap.cps.spi.FragmentPersistenceService;
+import org.onap.cps.spi.entities.Dataspace;
+import org.onap.cps.spi.entities.Fragment;
+import org.onap.cps.spi.entities.Module;
+import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.FragmentRepository;
+import org.onap.cps.spi.repository.ModuleRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.stereotype.Component;
+
+@Component
+public class FragmentPersistenceServiceImpl implements FragmentPersistenceService {
+
+ @Autowired
+ private DataspaceRepository dataspaceRepository;
+
+ @Autowired
+ private FragmentRepository fragmentRepository;
+
+ @Autowired
+ private ModuleRepository moduleRepository;
+
+ @Override
+ public String createAnchor(final AnchorDetails anchorDetails) {
+ try {
+ final Dataspace dataspace = dataspaceRepository.getByName(anchorDetails.getDataspace());
+ final Module module =
+ moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,
+ anchorDetails.getNamespace(), anchorDetails.getRevision());
+
+ final Fragment fragment = Fragment.builder().xpath(anchorDetails.getAnchorName())
+ .anchorName(anchorDetails.getAnchorName())
+ .dataspace(dataspace).module(module).build();
+
+ fragmentRepository.save(fragment);
+ return anchorDetails.getAnchorName();
+ } catch (final CpsNotFoundException ex) {
+ throw new CpsValidationException("Validation Error",
+ String.format("Dataspace and/or Module do not exist."));
+ } catch (final DataIntegrityViolationException ex) {
+ throw new CpsValidationException("Duplication Error",
+ String.format("Anchor with name %s already exist in dataspace %s.",
+ anchorDetails.getAnchorName(), anchorDetails.getDataspace()));
+ }
+ }
+}
\ No newline at end of file diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java index 01c7a7b53b..03679b3160 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistenceServiceImpl.java @@ -20,28 +20,24 @@ package org.onap.cps.spi.impl; -import org.onap.cps.spi.ModelPersistencyService; +import org.onap.cps.exceptions.CpsValidationException; +import org.onap.cps.spi.ModelPersistenceService; import org.onap.cps.spi.entities.Dataspace; -import org.onap.cps.spi.entities.ModuleEntity; +import org.onap.cps.spi.entities.Module; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.ModuleRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; @Component -public class ModelPersistencyServiceImpl implements ModelPersistencyService { +public class ModelPersistenceServiceImpl implements ModelPersistenceService { - - private final ModuleRepository moduleRepository; - - private final DataspaceRepository dataspaceRepository; + @Autowired + private ModuleRepository moduleRepository; @Autowired - public ModelPersistencyServiceImpl(final ModuleRepository moduleRepository, - final DataspaceRepository dataspaceRepository) { - this.moduleRepository = moduleRepository; - this.dataspaceRepository = dataspaceRepository; - } + private DataspaceRepository dataspaceRepository; @Override public void storeModule(final String namespace, final String moduleContent, final String revision, @@ -50,8 +46,13 @@ public class ModelPersistencyServiceImpl implements ModelPersistencyService { if (Boolean.FALSE.equals(dataspaceRepository.existsByName(dataspaceName))) { dataspaceRepository.save(dataspace); } - dataspace.setId(dataspaceRepository.findByName(dataspaceName).getId()); - final ModuleEntity moduleEntity = new ModuleEntity(namespace, moduleContent, revision, dataspace); - moduleRepository.save(moduleEntity); + dataspace.setId(dataspaceRepository.getByName(dataspaceName).getId()); + final Module module = new Module(namespace, moduleContent, revision, dataspace); + try { + moduleRepository.save(module); + } catch (final DataIntegrityViolationException ex) { + throw new CpsValidationException("Duplicate Entry", + String.format("Module already exist in dataspace %s.", dataspaceName)); + } } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java index 46a5266103..ad8004c07d 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java @@ -20,6 +20,9 @@ package org.onap.cps.spi.repository; +import java.util.Optional; +import javax.validation.constraints.NotNull; +import org.onap.cps.exceptions.CpsNotFoundException; import org.onap.cps.spi.entities.Dataspace; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -28,5 +31,10 @@ import org.springframework.stereotype.Repository; public interface DataspaceRepository extends JpaRepository<Dataspace, Integer> { Boolean existsByName(String name); //Checks if there are any records by name() - Dataspace findByName(String name); + Optional<Dataspace> findByName(@NotNull String name); + + default Dataspace getByName(@NotNull String name) { + return findByName(name).orElseThrow( + () -> new CpsNotFoundException("Not Found", "Dataspace " + name + " does not exist.")); + } }
\ No newline at end of file diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java new file mode 100755 index 0000000000..ba83f15881 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -0,0 +1,29 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.repository;
+
+import org.onap.cps.spi.entities.Fragment;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface FragmentRepository extends JpaRepository<Fragment, Integer> {
+}
\ No newline at end of file diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java index f9078d7c14..d3a8bc1f09 100644..100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java @@ -20,10 +20,36 @@ package org.onap.cps.spi.repository; -import org.onap.cps.spi.entities.ModuleEntity; +import java.util.Optional; +import javax.validation.constraints.NotNull; +import org.onap.cps.exceptions.CpsNotFoundException; +import org.onap.cps.spi.entities.Dataspace; +import org.onap.cps.spi.entities.Module; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface ModuleRepository extends JpaRepository<ModuleEntity, Integer> { +public interface ModuleRepository extends JpaRepository<Module, Integer> { + + Optional<Module> findByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, + @NotNull String namespace, + @NotNull String revision); + + /** + * This method gets a Module by dataspace, namespace and revision. + * + * @param dataspace the dataspace + * @param namespace the namespace + * @param revision the revision + * @return the Module + * @throws CpsNotFoundException if Module not found + */ + default Module getByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, @NotNull String namespace, + @NotNull String revision) { + return findByDataspaceAndNamespaceAndRevision(dataspace, namespace, + revision) + .orElseThrow(() -> new CpsNotFoundException("Validation Error", String.format( + "Module with dataspace %s, revision %s does not exist in namespace %s.", + dataspace.getName(), revision, namespace))); + } }
\ No newline at end of file diff --git a/cps-ri/src/main/resources/schema.sql b/cps-ri/src/main/resources/schema.sql index ba05048e89..3fabc6c9f9 100644..100755 --- a/cps-ri/src/main/resources/schema.sql +++ b/cps-ri/src/main/resources/schema.sql @@ -1,64 +1,65 @@ -CREATE TABLE IF NOT EXISTS RELATION_TYPE -( - RELATION_TYPE TEXT NOT NULL, - ID SERIAL PRIMARY KEY -); - -CREATE TABLE IF NOT EXISTS DATASPACE -( - ID SERIAL PRIMARY KEY, - NAME TEXT NOT NULL, - CONSTRAINT "UQ_NAME" UNIQUE (NAME) -); - -CREATE TABLE IF NOT EXISTS SCHEMA_NODE -( - SCHEMA_NODE_IDENTIFIER TEXT NOT NULL, - ID SERIAL PRIMARY KEY -); - -CREATE TABLE IF NOT EXISTS MODULE -( - NAMESPACE TEXT NOT NULL, - REVISION TEXT NOT NULL, - MODULE_CONTENT TEXT NOT NULL, - DATASPACE_ID BIGINT NOT NULL, - ID SERIAL PRIMARY KEY, - UNIQUE (DATASPACE_ID, NAMESPACE, REVISION), - CONSTRAINT module_dataspace FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS FRAGMENT -( - ID BIGSERIAL PRIMARY KEY, - XPATH TEXT NOT NULL, - DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID), - ATTRIBUTES JSONB, - ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID), - PARENT_ID BIGINT REFERENCES FRAGMENT(ID), - MODULE_ID INTEGER REFERENCES MODULE(ID), - SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID) -); - -CREATE TABLE IF NOT EXISTS RELATION -( - FROM_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID), - TO_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID), - RELATION_TYPE_ID INTEGER NOT NULL REFERENCES RELATION_TYPE(ID), - FROM_REL_XPATH TEXT NOT NULL, - TO_REL_XPATH TEXT NOT NULL, - CONSTRAINT RELATION_PKEY PRIMARY KEY (TO_FRAGMENT_ID, FROM_FRAGMENT_ID, RELATION_TYPE_ID) -); - - -CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK" ON FRAGMENT USING BTREE(DATASPACE_ID) ; -CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_MODULE_ID_FK" ON FRAGMENT USING BTREE(MODULE_ID) ; -CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK" ON FRAGMENT USING BTREE(PARENT_ID) ; -CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK" ON FRAGMENT USING BTREE(ANCHOR_ID) ; -CREATE INDEX IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID" ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ; -CREATE INDEX IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID" ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ; -CREATE INDEX IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK" ON RELATION USING BTREE(RELATION_TYPE_ID); -CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_FROM_ID_FK" ON RELATION USING BTREE(FROM_FRAGMENT_ID); -CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_TO_ID_FK" ON RELATION USING BTREE(TO_FRAGMENT_ID); -CREATE INDEX IF NOT EXISTS "PERF_MODULE_MODULE_CONTENT" ON MODULE USING BTREE(MODULE_CONTENT); +CREATE TABLE IF NOT EXISTS RELATION_TYPE
+(
+ RELATION_TYPE TEXT NOT NULL,
+ ID SERIAL PRIMARY KEY
+);
+
+CREATE TABLE IF NOT EXISTS DATASPACE
+(
+ ID SERIAL PRIMARY KEY,
+ NAME TEXT NOT NULL,
+ CONSTRAINT "UQ_NAME" UNIQUE (NAME)
+);
+
+CREATE TABLE IF NOT EXISTS SCHEMA_NODE
+(
+ SCHEMA_NODE_IDENTIFIER TEXT NOT NULL,
+ ID SERIAL PRIMARY KEY
+);
+
+CREATE TABLE IF NOT EXISTS MODULE
+(
+ ID SERIAL PRIMARY KEY,
+ NAMESPACE TEXT NOT NULL,
+ REVISION TEXT NOT NULL,
+ MODULE_CONTENT TEXT NOT NULL,
+ DATASPACE_ID BIGINT NOT NULL,
+ UNIQUE (DATASPACE_ID, NAMESPACE, REVISION),
+ CONSTRAINT MODULE_DATASPACE FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE
+);
+
+CREATE TABLE IF NOT EXISTS FRAGMENT
+(
+ ID BIGSERIAL PRIMARY KEY,
+ XPATH TEXT NOT NULL,
+ ATTRIBUTES JSONB,
+ ANCHOR_NAME TEXT,
+ ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID),
+ PARENT_ID BIGINT REFERENCES FRAGMENT(ID),
+ MODULE_ID INTEGER REFERENCES MODULE(ID),
+ DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),
+ SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID),
+ UNIQUE (DATASPACE_ID, ANCHOR_NAME, XPATH)
+);
+
+CREATE TABLE IF NOT EXISTS RELATION
+(
+ FROM_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),
+ TO_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),
+ RELATION_TYPE_ID INTEGER NOT NULL REFERENCES RELATION_TYPE(ID),
+ FROM_REL_XPATH TEXT NOT NULL,
+ TO_REL_XPATH TEXT NOT NULL,
+ CONSTRAINT RELATION_PKEY PRIMARY KEY (TO_FRAGMENT_ID, FROM_FRAGMENT_ID, RELATION_TYPE_ID)
+);
+
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK" ON FRAGMENT USING BTREE(DATASPACE_ID) ;
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_MODULE_ID_FK" ON FRAGMENT USING BTREE(MODULE_ID) ;
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK" ON FRAGMENT USING BTREE(PARENT_ID) ;
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK" ON FRAGMENT USING BTREE(ANCHOR_ID) ;
+CREATE INDEX IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID" ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ;
+CREATE INDEX IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID" ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ;
+CREATE INDEX IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK" ON RELATION USING BTREE(RELATION_TYPE_ID);
+CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_FROM_ID_FK" ON RELATION USING BTREE(FROM_FRAGMENT_ID);
+CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_TO_ID_FK" ON RELATION USING BTREE(TO_FRAGMENT_ID);
+CREATE INDEX IF NOT EXISTS "PERF_MODULE_MODULE_CONTENT" ON MODULE USING BTREE(MODULE_CONTENT);
CREATE UNIQUE INDEX IF NOT EXISTS "UQ_FRAGMENT_XPATH"ON FRAGMENT USING btree(xpath COLLATE pg_catalog."default" text_pattern_ops, dataspace_id);
\ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/api/CpService.java b/cps-service/src/main/java/org/onap/cps/api/CpService.java index 4d94a46547..726ca0f28d 100644..100755 --- a/cps-service/src/main/java/org/onap/cps/api/CpService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpService.java @@ -21,9 +21,9 @@ package org.onap.cps.api; import java.io.File; -import java.io.IOException; +import org.onap.cps.api.model.AnchorDetails; +import org.onap.cps.exceptions.CpsValidationException; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; /** * Configuration and persistency service interface which holds methods for parsing and storing yang models and data. @@ -36,7 +36,7 @@ public interface CpService { * @param yangModelContent the input stream * @return the schema context */ - SchemaContext parseAndValidateModel(final String yangModelContent); + SchemaContext parseAndValidateModel(String yangModelContent); /** * Parse and validate a file representing a yang model to generate a schema context. @@ -44,15 +44,16 @@ public interface CpService { * @param yangModelFile the yang file * @return the schema context */ - SchemaContext parseAndValidateModel(final File yangModelFile); + SchemaContext parseAndValidateModel(File yangModelFile); /** * Store schema context for a yang model. * * @param schemaContext the schema context * @param dataspaceName the dataspace name + * @throws CpsValidationException if input data already exists. */ - void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName); + void storeSchemaContext(SchemaContext schemaContext, String dataspaceName); /** * Store the JSON structure in the database. @@ -60,7 +61,7 @@ public interface CpService { * @param jsonStructure the JSON structure. * @return entity ID. */ - Integer storeJsonStructure(final String jsonStructure); + Integer storeJsonStructure(String jsonStructure); /** * Read a JSON Object using the object identifier. @@ -68,12 +69,21 @@ public interface CpService { * @param jsonObjectId the JSON object identifier. * @return the JSON structure. */ - String getJsonById(final int jsonObjectId); + String getJsonById(int jsonObjectId); /** * Delete a JSON Object using the object identifier. * * @param jsonObjectId the JSON object identifier. */ - void deleteJsonById(final int jsonObjectId); + void deleteJsonById(int jsonObjectId); + + /** + * Create an anchor using provided anchorDetails object. + * + * @param anchorDetails the anchor details object. + * @return the anchor name. + * @throws CpsValidationException if input data is invalid. + */ + String createAnchor(AnchorDetails anchorDetails); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java index c33746e861..93ed8b8468 100644..100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java @@ -26,10 +26,12 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Optional; import org.onap.cps.api.CpService; +import org.onap.cps.api.model.AnchorDetails; import org.onap.cps.exceptions.CpsException; import org.onap.cps.exceptions.CpsValidationException; -import org.onap.cps.spi.DataPersistencyService; -import org.onap.cps.spi.ModelPersistencyService; +import org.onap.cps.spi.DataPersistenceService; +import org.onap.cps.spi.FragmentPersistenceService; +import org.onap.cps.spi.ModelPersistenceService; import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.model.api.Module; @@ -38,15 +40,17 @@ import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - @Component public class CpServiceImpl implements CpService { @Autowired - private ModelPersistencyService modelPersistencyService; + private ModelPersistenceService modelPersistenceService; + + @Autowired + private DataPersistenceService dataPersistenceService; @Autowired - private DataPersistencyService dataPersistencyService; + private FragmentPersistenceService fragmentPersistenceService; @Override public final SchemaContext parseAndValidateModel(final String yangModelContent) { @@ -57,7 +61,7 @@ public class CpServiceImpl implements CpService { writer.write(yangModelContent); } return parseAndValidateModel(tempFile); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @@ -66,35 +70,40 @@ public class CpServiceImpl implements CpService { public final SchemaContext parseAndValidateModel(final File yangModelFile) { try { return YangUtils.parseYangModelFile(yangModelFile); - } catch (YangParserException e) { + } catch (final YangParserException e) { throw new CpsValidationException("Yang file validation failed", e.getMessage()); - } catch (IOException e) { + } catch (final IOException e) { throw new CpsException(e); } } @Override public final Integer storeJsonStructure(final String jsonStructure) { - return dataPersistencyService.storeJsonStructure(jsonStructure); + return dataPersistenceService.storeJsonStructure(jsonStructure); } @Override public final String getJsonById(final int jsonObjectId) { - return dataPersistencyService.getJsonById(jsonObjectId); + return dataPersistenceService.getJsonById(jsonObjectId); } @Override public void deleteJsonById(int jsonObjectId) { - dataPersistencyService.deleteJsonById(jsonObjectId); + dataPersistenceService.deleteJsonById(jsonObjectId); } @Override public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) { for (final Module module : schemaContext.getModules()) { - Optional<Revision> optionalRevision = module.getRevision(); - String revisionValue = optionalRevision.isPresent() ? optionalRevision.get().toString() : null; - modelPersistencyService.storeModule(module.getNamespace().toString(), module.toString(), + final Optional<Revision> optionalRevision = module.getRevision(); + final String revisionValue = optionalRevision.map(Object::toString).orElse(null); + modelPersistenceService.storeModule(module.getNamespace().toString(), module.toString(), revisionValue, dataspaceName); } } -} + + @Override + public String createAnchor(AnchorDetails anchorDetails) { + return fragmentPersistenceService.createAnchor(anchorDetails); + } +}
\ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java b/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java new file mode 100755 index 0000000000..576168ae75 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java @@ -0,0 +1,42 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.api.model;
+
+import java.io.Serializable;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@Getter
+@Setter
+@NoArgsConstructor
+public class AnchorDetails implements Serializable {
+
+ private static final long serialVersionUID = 1464791260718603291L;
+
+ private String anchorName;
+
+ private String dataspace;
+
+ private String namespace;
+
+ private String revision;
+}
\ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java b/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java index b54453cd90..4dd19dd82b 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java +++ b/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java @@ -20,13 +20,14 @@ package org.onap.cps.exceptions; import lombok.Getter; -import org.springframework.core.NestedExceptionUtils; /** * CP Service exception. */ public class CpsException extends RuntimeException { + private static final long serialVersionUID = 5573438585188332404L; + @Getter String details; diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java b/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java index f44fe806cf..4613da8f23 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java +++ b/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java @@ -19,13 +19,14 @@ package org.onap.cps.exceptions; -import lombok.Getter; /** * CP Service exception. Indicates the requested data being absent. */ public class CpsNotFoundException extends CpsException { + private static final long serialVersionUID = -1852996415384288431L; + /** * Constructor. * diff --git a/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java b/cps-service/src/main/java/org/onap/cps/spi/DataPersistenceService.java index ce51f40ac6..a3cbc28c55 100644..100755 --- a/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/DataPersistenceService.java @@ -22,7 +22,7 @@ package org.onap.cps.spi; /** * Defines methods to access and manipulate data using the chosen database solution. */ -public interface DataPersistencyService { +public interface DataPersistenceService { /** * Store the JSON structure in the database. diff --git a/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java new file mode 100755 index 0000000000..48dbb0cc25 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java @@ -0,0 +1,34 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi;
+
+import org.onap.cps.api.model.AnchorDetails;
+
+public interface FragmentPersistenceService {
+
+ /**
+ * Create an Anchor.
+ *
+ * @param anchorDetails the anchorDetails object.
+ * @return the anchor name.
+ */
+ String createAnchor(AnchorDetails anchorDetails);
+}
diff --git a/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java b/cps-service/src/main/java/org/onap/cps/spi/ModelPersistenceService.java index 2afdaa82a8..3f0b3c1109 100644..100755 --- a/cps-service/src/main/java/org/onap/cps/spi/ModelPersistencyService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/ModelPersistenceService.java @@ -23,7 +23,7 @@ package org.onap.cps.spi; /** * Defines methods to access and manipulate data using the chosen database solution. */ -public interface ModelPersistencyService { +public interface ModelPersistenceService { /** * Store the module from a yang model in the database. diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy index 5f42810bd5..709378ebee 100644..100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy @@ -21,24 +21,28 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils +import org.onap.cps.api.model.AnchorDetails import org.onap.cps.exceptions.CpsValidationException -import org.onap.cps.spi.DataPersistencyService +import org.onap.cps.spi.DataPersistenceService +import org.onap.cps.spi.FragmentPersistenceService import org.opendaylight.yangtools.yang.common.Revision import org.opendaylight.yangtools.yang.model.api.SchemaContext import spock.lang.Specification class CpServiceImplSpec extends Specification { - def mockDataPersistencyService = Mock(DataPersistencyService) + def mockDataPersistenceService = Mock(DataPersistenceService) + def mockFragmentPersistenceService = Mock(FragmentPersistenceService) def objectUnderTest = new CpServiceImpl() def setup() { - objectUnderTest.dataPersistencyService = mockDataPersistencyService + objectUnderTest.dataPersistenceService = mockDataPersistenceService + objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService } def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() { - given: 'that data persistency service is giving id 123 to a data structure it is asked to store' - mockDataPersistencyService.storeJsonStructure(_) >> 123 + given: 'that data persistence service is giving id 123 to a data structure it is asked to store' + mockDataPersistenceService.storeJsonStructure(_) >> 123 expect: 'Cps service returns the same id when storing data structure' objectUnderTest.storeJsonStructure('') == 123 } @@ -83,7 +87,7 @@ class CpServiceImplSpec extends Specification { def 'Read a JSON object with a valid identifier'(){ given: 'that the data persistence service returns a JSON structure for identifier 1' - mockDataPersistencyService.getJsonById(1) >> '{name : hello}' + mockDataPersistenceService.getJsonById(1) >> '{name : hello}' expect: 'that the same JSON structure is returned by CPS' objectUnderTest.getJsonById(1) == '{name : hello}' } @@ -91,7 +95,7 @@ class CpServiceImplSpec extends Specification { def 'Read a JSON object with an identifier that does not exist'(){ given: 'that the data persistence service throws an exception' def exceptionThrownByPersistenceService = new IllegalStateException() - mockDataPersistencyService.getJsonById(_) >> {throw exceptionThrownByPersistenceService} + mockDataPersistenceService.getJsonById(_) >> {throw exceptionThrownByPersistenceService} when: 'we try to get the JSON structure' objectUnderTest.getJsonById(1); then: 'the same exception is thrown by CPS' @@ -100,17 +104,67 @@ class CpServiceImplSpec extends Specification { def 'Delete a JSON object with a valid identifier'(){ given: 'that the data persistence service can delete a JSON structure for identifier 1' - mockDataPersistencyService.deleteJsonById(1) + mockDataPersistenceService.deleteJsonById(1) expect: 'No exception is thrown when we delete a JSON structure with identifier 1' objectUnderTest.deleteJsonById(1) } def 'Delete a JSON object with an identifier that does not exist'(){ given: 'that the data persistence service throws an exception' - mockDataPersistencyService.deleteJsonById(_) >> {throw new IllegalStateException()} + mockDataPersistenceService.deleteJsonById(_) >> {throw new IllegalStateException()} when: 'we try to delete a JSON structure' objectUnderTest.deleteJsonById(100); then: 'the same exception is thrown by CPS' thrown(IllegalStateException) } + + def 'Create an anchor with a non-existant dataspace'(){ + given: 'that the dataspace does not exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a anchor with a non-existant dataspace' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create an anchor with invalid dataspace, namespace and revision'(){ + given: 'that the dataspace, namespace and revison combination does not exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create a duplicate anchor'(){ + given: 'that the anchor already exist service throws an exception' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + anchorDetails.setRevision('dummyAnchorName') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)} + when: 'we try to create a duplicate anchor' + objectUnderTest.createAnchor(anchorDetails) + then: 'the same exception is thrown by CPS' + thrown(CpsValidationException) + } + + def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'(){ + given: 'that the anchor does not pre-exist service creates an anchor' + AnchorDetails anchorDetails = new AnchorDetails() + anchorDetails.setDataspace('dummyDataspace') + anchorDetails.setNamespace('dummyNamespace') + anchorDetails.setRevision('dummyRevision') + anchorDetails.setRevision('dummyAnchorName') + mockFragmentPersistenceService.createAnchor(anchorDetails) >> 'dummyAnchorName' + expect: 'anchor name is returned by service' + objectUnderTest.createAnchor(anchorDetails) == 'dummyAnchorName' + } }
\ No newline at end of file |