VSE: Create an anchor in the given dataspace 67/114767/14
authorRishi.Chail <rishi.chail@est.tech>
Mon, 9 Nov 2020 03:28:44 +0000 (03:28 +0000)
committerRishi.Chail <rishi.chail@est.tech>
Wed, 18 Nov 2020 13:30:01 +0000 (13:30 +0000)
Issue-ID: CPS-42
https://jira.onap.org/browse/CPS-42

Signed-off-by: Rishi Chail <rishi.chail@est.tech>
Change-Id: If67be6f13889808da4d9fe830595766af67e4fdf

21 files changed:
cps-dependencies/pom.xml [changed mode: 0644->0755]
cps-rest/docs/api/swagger/openapi.yml [changed mode: 0644->0755]
cps-rest/pom.xml [changed mode: 0644->0755]
cps-rest/src/main/java/org/onap/cps/config/CpsConfig.java [new file with mode: 0755]
cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java [changed mode: 0644->0755]
cps-rest/src/main/java/org/onap/cps/swagger/config/SpringFoxConfig.java [deleted file]
cps-ri/src/main/java/org/onap/cps/spi/entities/Dataspace.java
cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java
cps-ri/src/main/java/org/onap/cps/spi/impl/FragmentPersistenceServiceImpl.java [new file with mode: 0755]
cps-ri/src/main/java/org/onap/cps/spi/impl/ModelPersistencyServiceImpl.java [changed mode: 0644->0755]
cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java [changed mode: 0644->0755]
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java [new file with mode: 0755]
cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java [changed mode: 0644->0755]
cps-ri/src/main/resources/schema.sql [changed mode: 0644->0755]
cps-service/src/main/java/org/onap/cps/api/CpService.java [changed mode: 0644->0755]
cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java [changed mode: 0644->0755]
cps-service/src/main/java/org/onap/cps/api/model/AnchorDetails.java [new file with mode: 0755]
cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java
cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java
cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java [new file with mode: 0755]
cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 6286391..9e0269b
@@ -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>
                 <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
old mode 100644 (file)
new mode 100755 (executable)
index 82f47c0..0c7c83c
@@ -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
old mode 100644 (file)
new mode 100755 (executable)
index fc3e632..3a82ca3
             <groupId>org.apache.commons</groupId>\r
             <artifactId>commons-lang3</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.modelmapper</groupId>\r
+            <artifactId>modelmapper</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.springframework.boot</groupId>\r
             <artifactId>spring-boot-starter-test</artifactId>\r
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 (executable)
index 0000000..cca5fe7
--- /dev/null
@@ -0,0 +1,50 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.config;\r
+\r
+import org.modelmapper.ModelMapper;\r
+import org.springframework.context.annotation.Bean;\r
+import org.springframework.context.annotation.Configuration;\r
+import springfox.documentation.builders.PathSelectors;\r
+import springfox.documentation.builders.RequestHandlerSelectors;\r
+import springfox.documentation.spi.DocumentationType;\r
+import springfox.documentation.spring.web.plugins.Docket;\r
+\r
+@Configuration\r
+public class CpsConfig {\r
+\r
+    /**\r
+     * Swagger configuration.\r
+     */\r
+    @Bean\r
+    public Docket api() {\r
+        return new Docket(DocumentationType.OAS_30).select().apis(RequestHandlerSelectors.any())\r
+            .paths(PathSelectors.any()).build();\r
+    }\r
+\r
+    /**\r
+     * ModelMapper configuration.\r
+     */\r
+    @Bean\r
+    public ModelMapper modelMapper() {\r
+        return new ModelMapper();\r
+    }\r
+}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index f0c5fcb..9e57408
@@ -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 (file)
index 73e1795..0000000
+++ /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();
-    }
-}
index 627a144..aeab4f8 100644 (file)
@@ -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)
index 12422dc..4d8a90b 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.cps.spi.entities;\r
 \r
 import com.vladmihalcea.hibernate.type.json.JsonBinaryType;\r
+import java.io.Serializable;\r
 import javax.persistence.Column;\r
 import javax.persistence.Entity;\r
 import javax.persistence.FetchType;\r
@@ -32,6 +33,7 @@ import javax.persistence.ManyToOne;
 import javax.persistence.OneToOne;\r
 import javax.validation.constraints.NotNull;\r
 import lombok.AllArgsConstructor;\r
+import lombok.Builder;\r
 import lombok.Getter;\r
 import lombok.NoArgsConstructor;\r
 import lombok.Setter;\r
@@ -47,8 +49,11 @@ import org.hibernate.annotations.TypeDefs;
 @Entity\r
 @AllArgsConstructor\r
 @NoArgsConstructor\r
+@Builder\r
 @TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})\r
-public class Fragment {\r
+public class Fragment implements Serializable {\r
+\r
+    private static final long serialVersionUID = 7737669789097119667L;\r
 \r
     @Id\r
     @GeneratedValue(strategy = GenerationType.IDENTITY)\r
@@ -62,6 +67,10 @@ public class Fragment {
     @Column(columnDefinition = "jsonb")\r
     private String attributes;\r
 \r
+    @Column(columnDefinition = "text")\r
+    private String anchorName;\r
+\r
+    @NotNull\r
     @ManyToOne(fetch = FetchType.LAZY)\r
     @JoinColumn(name = "dataspace_id")\r
     private Dataspace dataspace;\r
@@ -73,4 +82,8 @@ public class Fragment {
     @OneToOne(fetch = FetchType.LAZY)\r
     @JoinColumn(name = "parent_id")\r
     private Fragment parentFragment;\r
+\r
+    @OneToOne(fetch = FetchType.LAZY)\r
+    @JoinColumn(name = "module_id")\r
+    private ModuleEntity module;\r
 }\r
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 (executable)
index 0000000..47d98c9
--- /dev/null
@@ -0,0 +1,72 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi.impl;\r
+\r
+import org.onap.cps.api.model.AnchorDetails;\r
+import org.onap.cps.exceptions.CpsNotFoundException;\r
+import org.onap.cps.exceptions.CpsValidationException;\r
+import org.onap.cps.spi.FragmentPersistenceService;\r
+import org.onap.cps.spi.entities.Dataspace;\r
+import org.onap.cps.spi.entities.Fragment;\r
+import org.onap.cps.spi.entities.ModuleEntity;\r
+import org.onap.cps.spi.repository.DataspaceRepository;\r
+import org.onap.cps.spi.repository.FragmentRepository;\r
+import org.onap.cps.spi.repository.ModuleRepository;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.dao.DataIntegrityViolationException;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class FragmentPersistenceServiceImpl implements FragmentPersistenceService {\r
+\r
+    @Autowired\r
+    private DataspaceRepository dataspaceRepository;\r
+\r
+    @Autowired\r
+    private FragmentRepository fragmentRepository;\r
+\r
+    @Autowired\r
+    private ModuleRepository moduleRepository;\r
+\r
+    @Override\r
+    public String createAnchor(final AnchorDetails anchorDetails) {\r
+        try {\r
+            final Dataspace dataspace = dataspaceRepository.getByName(anchorDetails.getDataspace());\r
+            final ModuleEntity moduleEntity =\r
+                moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,\r
+                anchorDetails.getNamespace(), anchorDetails.getRevision());\r
+\r
+            final Fragment fragment = Fragment.builder().xpath(anchorDetails.getAnchorName())\r
+                .anchorName(anchorDetails.getAnchorName())\r
+                .dataspace(dataspace).module(moduleEntity).build();\r
+\r
+            fragmentRepository.save(fragment);\r
+            return anchorDetails.getAnchorName();\r
+        } catch (final CpsNotFoundException ex) {\r
+            throw new CpsValidationException("Validation Error",\r
+                String.format("Dataspace and/or Module do not exist."));\r
+        } catch (final DataIntegrityViolationException ex) {\r
+            throw new CpsValidationException("Duplication Error",\r
+                String.format("Anchor with name %s already exist in dataspace %s.",\r
+                    anchorDetails.getAnchorName(), anchorDetails.getDataspace()));\r
+        }\r
+    }\r
+}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 01c7a7b..56bba04
@@ -31,17 +31,11 @@ import org.springframework.stereotype.Component;
 @Component
 public class ModelPersistencyServiceImpl implements ModelPersistencyService {
 
-
-    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,7 +44,7 @@ public class ModelPersistencyServiceImpl implements ModelPersistencyService {
         if (Boolean.FALSE.equals(dataspaceRepository.existsByName(dataspaceName))) {
             dataspaceRepository.save(dataspace);
         }
-        dataspace.setId(dataspaceRepository.findByName(dataspaceName).getId());
+        dataspace.setId(dataspaceRepository.getByName(dataspaceName).getId());
         final ModuleEntity moduleEntity = new ModuleEntity(namespace, moduleContent, revision, dataspace);
         moduleRepository.save(moduleEntity);
     }
old mode 100644 (file)
new mode 100755 (executable)
index 46a5266..ad8004c
@@ -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 (executable)
index 0000000..ba83f15
--- /dev/null
@@ -0,0 +1,29 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi.repository;\r
+\r
+import org.onap.cps.spi.entities.Fragment;\r
+import org.springframework.data.jpa.repository.JpaRepository;\r
+import org.springframework.stereotype.Repository;\r
+\r
+@Repository\r
+public interface FragmentRepository extends JpaRepository<Fragment, Integer> {\r
+}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index f9078d7..fe27c8e
 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.onap.cps.spi.entities.ModuleEntity;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
 @Repository
 public interface ModuleRepository extends JpaRepository<ModuleEntity, Integer> {
+
+    Optional<ModuleEntity> findByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace,
+        @NotNull String namespace,
+        @NotNull String revision);
+
+    /**
+     * This method gets a ModuleEntity by dataspace, namespace and revision.
+     *
+     * @param dataspace the dataspace
+     * @param namespace the namespace
+     * @param revision the revision
+     * @return the ModuleEntity
+     * @throws CpsNotFoundException if ModuleEntity not found
+     */
+    default ModuleEntity 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
old mode 100644 (file)
new mode 100755 (executable)
index ba05048..3fabc6c
@@ -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\r
+(\r
+    RELATION_TYPE TEXT NOT NULL,\r
+    ID SERIAL PRIMARY KEY\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS DATASPACE\r
+(\r
+    ID SERIAL PRIMARY KEY,\r
+    NAME TEXT NOT NULL,\r
+    CONSTRAINT "UQ_NAME" UNIQUE (NAME)\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS SCHEMA_NODE\r
+(\r
+    SCHEMA_NODE_IDENTIFIER TEXT NOT NULL,\r
+    ID SERIAL PRIMARY KEY\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS MODULE\r
+(\r
+    ID SERIAL PRIMARY KEY,\r
+    NAMESPACE TEXT NOT NULL,\r
+    REVISION TEXT NOT NULL,\r
+    MODULE_CONTENT TEXT NOT NULL,\r
+    DATASPACE_ID BIGINT NOT NULL,\r
+    UNIQUE (DATASPACE_ID, NAMESPACE, REVISION),\r
+    CONSTRAINT MODULE_DATASPACE FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS FRAGMENT\r
+(\r
+    ID BIGSERIAL PRIMARY KEY,\r
+    XPATH TEXT NOT NULL,\r
+    ATTRIBUTES JSONB,\r
+    ANCHOR_NAME TEXT,\r
+    ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID),\r
+    PARENT_ID BIGINT REFERENCES FRAGMENT(ID),\r
+    MODULE_ID INTEGER REFERENCES MODULE(ID),\r
+    DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),\r
+    SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID),\r
+    UNIQUE (DATASPACE_ID, ANCHOR_NAME, XPATH)\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS RELATION\r
+(\r
+    FROM_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),\r
+    TO_FRAGMENT_ID   BIGINT NOT NULL REFERENCES FRAGMENT(ID),\r
+    RELATION_TYPE_ID  INTEGER NOT NULL REFERENCES RELATION_TYPE(ID),\r
+    FROM_REL_XPATH TEXT NOT NULL,\r
+    TO_REL_XPATH TEXT NOT NULL,\r
+    CONSTRAINT RELATION_PKEY PRIMARY KEY (TO_FRAGMENT_ID, FROM_FRAGMENT_ID, RELATION_TYPE_ID)\r
+);\r
+\r
+CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK"     ON FRAGMENT USING BTREE(DATASPACE_ID) ;\r
+CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_MODULE_ID_FK"        ON FRAGMENT USING BTREE(MODULE_ID) ;\r
+CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK"        ON FRAGMENT USING BTREE(PARENT_ID) ;\r
+CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK"        ON FRAGMENT USING BTREE(ANCHOR_ID) ;\r
+CREATE INDEX  IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID"  ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ;\r
+CREATE INDEX  IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID"         ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ;\r
+CREATE INDEX  IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK"          ON RELATION USING BTREE(RELATION_TYPE_ID);\r
+CREATE INDEX  IF NOT EXISTS "FKI_RELATIONS_FROM_ID_FK"         ON RELATION USING BTREE(FROM_FRAGMENT_ID);\r
+CREATE INDEX  IF NOT EXISTS "FKI_RELATIONS_TO_ID_FK"           ON RELATION USING BTREE(TO_FRAGMENT_ID);\r
+CREATE INDEX  IF NOT EXISTS "PERF_MODULE_MODULE_CONTENT"       ON MODULE USING BTREE(MODULE_CONTENT);\r
 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
old mode 100644 (file)
new mode 100755 (executable)
index 4d94a46..6b59949
@@ -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,7 +44,7 @@ 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.
@@ -52,7 +52,7 @@ public interface CpService {
      * @param schemaContext the schema context
      * @param dataspaceName the dataspace name
      */
-    void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName);
+    void storeSchemaContext(SchemaContext schemaContext, String dataspaceName);
 
     /**
      * Store the JSON structure in the database.
@@ -60,7 +60,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 +68,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);
 }
old mode 100644 (file)
new mode 100755 (executable)
index c33746e..8cdadbe
@@ -26,9 +26,11 @@ 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.FragmentPersistenceService;
 import org.onap.cps.spi.ModelPersistencyService;
 import org.onap.cps.utils.YangUtils;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -48,6 +50,9 @@ public class CpServiceImpl implements CpService {
     @Autowired
     private DataPersistencyService dataPersistencyService;
 
+    @Autowired
+    private FragmentPersistenceService fragmentPersistenceService;
+
     @Override
     public final SchemaContext parseAndValidateModel(final String yangModelContent) {
 
@@ -57,7 +62,7 @@ public class CpServiceImpl implements CpService {
                 writer.write(yangModelContent);
             }
             return parseAndValidateModel(tempFile);
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new CpsException(e);
         }
     }
@@ -66,9 +71,9 @@ 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);
         }
     }
@@ -91,10 +96,15 @@ public class CpServiceImpl implements CpService {
     @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;
+            final Optional<Revision> optionalRevision = module.getRevision();
+            final String revisionValue = optionalRevision.map(Object::toString).orElse(null);
             modelPersistencyService.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 (executable)
index 0000000..576168a
--- /dev/null
@@ -0,0 +1,42 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.api.model;\r
+\r
+import java.io.Serializable;\r
+import lombok.Getter;\r
+import lombok.NoArgsConstructor;\r
+import lombok.Setter;\r
+\r
+@Getter\r
+@Setter\r
+@NoArgsConstructor\r
+public class AnchorDetails implements Serializable {\r
+\r
+    private static final long serialVersionUID = 1464791260718603291L;\r
+\r
+    private String anchorName;\r
+\r
+    private String dataspace;\r
+\r
+    private String namespace;\r
+\r
+    private String revision;\r
+}
\ No newline at end of file
index b54453c..4dd19dd 100644 (file)
 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;
 
index f44fe80..4613da8 100644 (file)
 
 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/FragmentPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/FragmentPersistenceService.java
new file mode 100755 (executable)
index 0000000..48dbb0c
--- /dev/null
@@ -0,0 +1,34 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi;\r
+\r
+import org.onap.cps.api.model.AnchorDetails;\r
+\r
+public interface FragmentPersistenceService {\r
+\r
+    /**\r
+     * Create an Anchor.\r
+     *\r
+     * @param anchorDetails the anchorDetails object.\r
+     * @return the anchor name.\r
+     */\r
+    String createAnchor(AnchorDetails anchorDetails);\r
+}\r
old mode 100644 (file)
new mode 100755 (executable)
index 5f42810..3c51cca
 package org.onap.cps.api.impl
 
 import org.onap.cps.TestUtils
+import org.onap.cps.api.model.AnchorDetails
+import org.onap.cps.exceptions.CpsNotFoundException
 import org.onap.cps.exceptions.CpsValidationException
 import org.onap.cps.spi.DataPersistencyService
+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
@@ -30,10 +33,12 @@ import spock.lang.Specification
 class CpServiceImplSpec extends Specification {
 
     def mockDataPersistencyService = Mock(DataPersistencyService)
+    def mockFragmentPersistenceService = Mock(FragmentPersistenceService)
     def objectUnderTest = new CpServiceImpl()
 
     def setup() {
         objectUnderTest.dataPersistencyService = mockDataPersistencyService
+        objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService
     }
 
     def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
@@ -113,4 +118,54 @@ class CpServiceImplSpec extends Specification {
         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