VSE: Create an anchor in the given dataspace

Issue-ID: CPS-42
https://jira.onap.org/browse/CPS-42

Signed-off-by: Rishi Chail <rishi.chail@est.tech>
Change-Id: If67be6f13889808da4d9fe830595766af67e4fdf
diff --git a/cps-rest/docs/api/swagger/openapi.yml b/cps-rest/docs/api/swagger/openapi.yml
old mode 100644
new mode 100755
index 82f47c0..0c7c83c
--- a/cps-rest/docs/api/swagger/openapi.yml
+++ b/cps-rest/docs/api/swagger/openapi.yml
@@ -81,26 +81,19 @@
             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 @@
         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
old mode 100644
new mode 100755
index fc3e632..3a82ca3
--- 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 0000000..cca5fe7
--- /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
old mode 100644
new mode 100755
index f0c5fcb..9e57408
--- 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.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 @@
     @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 @@
         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 @@
         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 @@
     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 73e1795..0000000
--- 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();
-    }
-}