create post request for cmhandles registration 22/122522/11
authortragait <rahul.tyagi@est.tech>
Thu, 8 Jul 2021 14:42:19 +0000 (15:42 +0100)
committerNiamh Core <niamh.core@est.tech>
Wed, 4 Aug 2021 09:53:58 +0000 (09:53 +0000)
Issue-ID: CPS-406
Change-Id: I64e88643221403e117146443287d03788fb71c3e
Signed-off-by: tragait <rahul.tyagi@est.tech>
14 files changed:
docs/openapi/components.yml
docs/openapi/openapi.yml
src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java
src/main/java/org/onap/cps/ncmp/dmi/exception/CmHandleRegistrationException.java [new file with mode: 0644]
src/main/java/org/onap/cps/ncmp/dmi/exception/DmiExceptionHandler.java
src/main/java/org/onap/cps/ncmp/dmi/model/CmHandleOperation.java [new file with mode: 0644]
src/main/java/org/onap/cps/ncmp/dmi/model/CreatedCmHandle.java [new file with mode: 0644]
src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java
src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java
src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java
src/main/resources/application.yml
src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy
src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy
src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java [new file with mode: 0644]

index bc3a63a..f38ed64 100644 (file)
@@ -11,6 +11,14 @@ components:
         details:
           type: string
 
+    CmHandles:
+      type: object
+      properties:
+        cmHandles:
+          type: array
+          items:
+            type: string
+
   responses:
     NotFound:
       description: The specified resource was not found
index 7d0569d..44747a9 100644 (file)
@@ -71,5 +71,30 @@ paths:
           $ref: 'components.yml#/components/responses/BadRequest'
         '401':
           $ref: 'components.yml#/components/responses/Unauthorized'
+        '403':
+          $ref: 'components.yml#/components/responses/Forbidden'
+
+  /v1/inventory/cmHandles:
+    post:
+      tags:
+        - dmi-plugin-internal
+      summary: register given list of cm handles (internal use only)
+      description: register given list of cm handles (internal use only)
+      x-api-audience: component-internal
+      operationId: registerCmHandles
+      requestBody:
+        description: list of cm handles
+        content:
+          application/json:
+            schema:
+                $ref: 'components.yml#/components/schemas/CmHandles'
+        required: true
+      responses:
+        '201':
+          $ref: 'components.yml#/components/responses/Created'
+        '400':
+          $ref: 'components.yml#/components/responses/BadRequest'
+        '401':
+          $ref: 'components.yml#/components/responses/Unauthorized'
         '403':
           $ref: 'components.yml#/components/responses/Forbidden'
\ No newline at end of file
index 33d6ae8..614435f 100644 (file)
 
 package org.onap.cps.ncmp.dmi.config;
 
+import lombok.Getter;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
 import springfox.documentation.spi.DocumentationType;
@@ -42,5 +45,13 @@ public class DmiPluginConfig {
                 .paths(PathSelectors.any())
                 .build();
     }
+
+    @Getter
+    @Component
+    public static class DmiPluginProperties {
+
+        @Value("${dmi.service.name}")
+        private String dmiServiceName;
+    }
 }
 
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/exception/CmHandleRegistrationException.java b/src/main/java/org/onap/cps/ncmp/dmi/exception/CmHandleRegistrationException.java
new file mode 100644 (file)
index 0000000..1874389
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation
+ *  ================================================================================
+ *  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.ncmp.dmi.exception;
+
+public class CmHandleRegistrationException extends DmiException {
+
+    private static final long serialVersionUID = 8973438585188332404L;
+
+    /**
+     * Constructor.
+     *
+     * @param details the error details
+     */
+    public CmHandleRegistrationException(final String details) {
+        super("Not able to register the given cm-handles.", details);
+    }
+
+}
index 9ad561f..a6ec6df 100644 (file)
@@ -51,7 +51,7 @@ public class DmiExceptionHandler {
         return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
     }
 
-    @ExceptionHandler({DmiException.class})
+    @ExceptionHandler({CmHandleRegistrationException.class, DmiException.class})
     public static ResponseEntity<Object> handleAnyOtherDmiExceptions(final DmiException exception) {
         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
     }
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/model/CmHandleOperation.java b/src/main/java/org/onap/cps/ncmp/dmi/model/CmHandleOperation.java
new file mode 100644 (file)
index 0000000..8ddd42f
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation
+ *  ================================================================================
+ *  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.ncmp.dmi.model;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@Getter
+@Setter
+public class CmHandleOperation {
+
+    private String dmiPlugin;
+    private List<CreatedCmHandle> createdCmHandles;
+}
\ No newline at end of file
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/model/CreatedCmHandle.java b/src/main/java/org/onap/cps/ncmp/dmi/model/CreatedCmHandle.java
new file mode 100644 (file)
index 0000000..9198d7d
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation
+ *  ================================================================================
+ *  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.ncmp.dmi.model;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import java.util.Map;
+import lombok.Getter;
+import lombok.Setter;
+
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@Getter
+@Setter
+public class CreatedCmHandle {
+
+    private String cmHandle;
+    private Map<String, String> cmHandleProperties;
+
+}
\ No newline at end of file
index 8081b73..0e1d3d6 100644 (file)
 
 package org.onap.cps.ncmp.dmi.rest.controller;
 
+import java.util.List;
+import javax.validation.Valid;
+import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.ncmp.dmi.model.CmHandles;
 import org.onap.cps.ncmp.dmi.rest.api.DmiPluginApi;
+import org.onap.cps.ncmp.dmi.rest.api.DmiPluginInternalApi;
 import org.onap.cps.ncmp.dmi.service.DmiService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -30,7 +35,8 @@ import org.springframework.web.bind.annotation.RestController;
 
 @RequestMapping("${rest.api.dmi-base-path}")
 @RestController
-public class DmiRestController implements DmiPluginApi {
+@Slf4j
+public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi {
 
     private DmiService dmiService;
 
@@ -45,4 +51,19 @@ public class DmiRestController implements DmiPluginApi {
         final String modulesListAsJson = dmiService.getModulesForCmHandle(cmHandle);
         return new ResponseEntity<>(modulesListAsJson, HttpStatus.OK);
     }
+
+    /**
+     * This method register given list of cm-handles to ncmp.
+     *
+     * @param cmHandles list of cm-handles
+     * @return (@code ResponseEntity) response entity
+     */
+    public ResponseEntity<String> registerCmHandles(final @Valid CmHandles cmHandles) {
+        final List<String> cmHandlesList = cmHandles.getCmHandles();
+        if (cmHandlesList.isEmpty()) {
+            return new ResponseEntity<>("Need at least one cmHandle to process.", HttpStatus.BAD_REQUEST);
+        }
+        dmiService.registerCmHandles(cmHandlesList);
+        return new ResponseEntity<>("cm-handle registered successfully.", HttpStatus.CREATED);
+    }
 }
index e595bd5..d9196ec 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.cps.ncmp.dmi.service;
 
+import java.util.List;
 import org.onap.cps.ncmp.dmi.exception.DmiException;
 
 /**
@@ -36,4 +37,12 @@ public interface DmiService {
      */
     String getModulesForCmHandle(String cmHandle) throws DmiException;
 
+    /**
+     * This method used to register the given {@code CmHandles}
+     * which contains list of {@code CmHandle} to cps repository.
+     *
+     * @param cmHandles list of cm-handles
+     */
+    void registerCmHandles(List<String> cmHandles);
+
 }
index 4367bf4..990a421 100644 (file)
 
 package org.onap.cps.ncmp.dmi.service;
 
-import io.micrometer.core.instrument.util.StringUtils;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.ArrayList;
+import java.util.List;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.groovy.parser.antlr4.util.StringUtils;
+import org.onap.cps.ncmp.dmi.config.DmiPluginConfig.DmiPluginProperties;
+import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException;
 import org.onap.cps.ncmp.dmi.exception.DmiException;
 import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException;
+import org.onap.cps.ncmp.dmi.model.CmHandleOperation;
+import org.onap.cps.ncmp.dmi.model.CreatedCmHandle;
+import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient;
 import org.onap.cps.ncmp.dmi.service.operation.SdncOperations;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
+
 @Service
+@Slf4j
 public class DmiServiceImpl implements DmiService {
 
     private SdncOperations sdncOperations;
+    private NcmpRestClient ncmpRestClient;
+    private ObjectMapper objectMapper;
+    private DmiPluginProperties dmiPluginProperties;
 
+    /**
+     * Constructor.
+     *
+     * @param dmiPluginProperties dmiPluginProperties
+     * @param ncmpRestClient ncmpRestClient
+     * @param objectMapper objectMapper
+     * @param sdncOperations sdncOperations
+     */
     @Autowired
-    public DmiServiceImpl(final SdncOperations sdncOperations) {
+    public DmiServiceImpl(final DmiPluginProperties dmiPluginProperties,
+                          final NcmpRestClient ncmpRestClient,
+                          final ObjectMapper objectMapper,
+                          final SdncOperations sdncOperations) {
+        this.dmiPluginProperties = dmiPluginProperties;
+        this.ncmpRestClient = ncmpRestClient;
+        this.objectMapper = objectMapper;
         this.sdncOperations = sdncOperations;
     }
 
@@ -53,4 +82,30 @@ public class DmiServiceImpl implements DmiService {
                     "response code : " + responseEntity.getStatusCode() + " message : " + responseEntity.getBody());
         }
     }
+
+    @Override
+    public void registerCmHandles(final List<String> cmHandles) {
+        final CmHandleOperation cmHandleOperation = new CmHandleOperation();
+        cmHandleOperation.setDmiPlugin(dmiPluginProperties.getDmiServiceName());
+        final List<CreatedCmHandle> createdCmHandleList = new ArrayList<>();
+        for (final String cmHandle: cmHandles) {
+            final CreatedCmHandle createdCmHandle = new CreatedCmHandle();
+            createdCmHandle.setCmHandle(cmHandle);
+            createdCmHandleList.add(createdCmHandle);
+        }
+        cmHandleOperation.setCreatedCmHandles(createdCmHandleList);
+        final String cmHandlesJson;
+        try {
+            cmHandlesJson = objectMapper.writeValueAsString(cmHandleOperation);
+        } catch (final JsonProcessingException e) {
+            log.error("Parsing error occurred while converting cm-handles to JSON {}", cmHandles);
+            throw new DmiException("Internal Server Error.",
+                    "Parsing error occurred while converting given cm-handles object list to JSON ");
+        }
+        final ResponseEntity<String> responseEntity = ncmpRestClient.registerCmHandlesWithNcmp(cmHandlesJson);
+        if (!(responseEntity.getStatusCode() == HttpStatus.CREATED)) {
+            throw new CmHandleRegistrationException(responseEntity.getBody());
+        }
+    }
+
 }
index 408fc10..47b8078 100644 (file)
 server:
   port: 8080
 
+dmi:
+  service:
+    name: onap-dmi-plugin
+
 rest:
   api:
     dmi-base-path: /dmi/api
@@ -61,3 +65,12 @@ sdnc:
   auth:
     username: ${SDNC_USERNAME}
     password: ${SDNC_PASSWORD}
+
+logging:
+  level:
+    org.springframework: ERROR
+    org.onap.cps: DEBUG
+  pattern:
+    console: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
+    file: "%d %p %c{1.} [%t] %m%n"
+  file: dmi.log
index f249de9..993b80c 100644 (file)
@@ -56,8 +56,8 @@ class DmiRestControllerSpec extends Specification {
             mockDmiService.getModulesForCmHandle('node1') >> someJson
         when: 'post is being called'
             def response = mvc.perform( post(getModuleUrl)
-                            .contentType(MediaType.APPLICATION_JSON))
-                            .andReturn().response
+                    .contentType(MediaType.APPLICATION_JSON))
+                    .andReturn().response
         then: 'status is OK'
             response.status == HttpStatus.OK.value()
         and: 'the response content matches the result from the DMI service'
@@ -81,4 +81,35 @@ class DmiRestControllerSpec extends Specification {
             'no modules found'             |  ModulesNotFoundException.class || HttpStatus.NOT_FOUND.value()
             'any other runtime exception'  |  RuntimeException.class         || HttpStatus.INTERNAL_SERVER_ERROR.value()
     }
+
+    def 'Register given list of cm handles.'() {
+        given: 'register cm handle url and cm handles json'
+            def registerCmhandlesPost = "${basePathV1}/inventory/cmHandles"
+            def cmHandleJson =  '{"cmHandles":["node1", "node2"]}'
+        when: 'post register cm handles api is invoked'
+            def response = mvc.perform(
+                    post(registerCmhandlesPost)
+                            .contentType(MediaType.APPLICATION_JSON)
+                            .content(cmHandleJson)
+            ).andReturn().response
+        then: 'register cm handles in dmi service is called once'
+            1 * mockDmiService.registerCmHandles(_ as List<String>)
+        and: 'response status is created'
+            response.status == HttpStatus.CREATED.value()
+    }
+
+    def 'register cm handles called with empty content.'() {
+        given: 'register cm handle url and empty json'
+            def registerCmhandlesPost = "${basePathV1}/inventory/cmHandles"
+            def emptyJson = '{"cmHandles":[]}'
+        when: 'register cm handles post api is invoked with no content'
+            def response = mvc.perform(
+                    post(registerCmhandlesPost).contentType(MediaType.APPLICATION_JSON)
+                            .content(emptyJson)
+            ).andReturn().response
+        then: 'response status is "bad request"'
+            response.status == HttpStatus.BAD_REQUEST.value()
+        and: 'dmi service is not called'
+            0 * mockDmiService.registerCmHandles(_)
+    }
 }
index 6661296..9d6bc35 100644 (file)
 
 package org.onap.cps.ncmp.dmi.service
 
+import com.fasterxml.jackson.core.JsonProcessingException
+import com.fasterxml.jackson.databind.ObjectMapper
+import org.onap.cps.ncmp.dmi.config.DmiPluginConfig
+import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException
 import org.onap.cps.ncmp.dmi.exception.DmiException
 import org.onap.cps.ncmp.dmi.exception.ModulesNotFoundException
+import org.onap.cps.ncmp.dmi.service.client.NcmpRestClient
 import org.onap.cps.ncmp.dmi.service.operation.SdncOperations
 import org.springframework.http.HttpStatus
 import org.springframework.http.ResponseEntity
@@ -29,13 +34,13 @@ import spock.lang.Specification
 
 class DmiServiceImplSpec extends Specification {
 
-    def objectUnderTest = new DmiServiceImpl()
 
+    def mockNcmpRestClient = Mock(NcmpRestClient)
+    def mockDmiPluginProperties = Mock(DmiPluginConfig.DmiPluginProperties)
+    def objectMapper = new ObjectMapper()
+    def mockObjectMapper = Mock(ObjectMapper)
     def mockSdncOperations = Mock(SdncOperations)
-
-    def setup() {
-        objectUnderTest.sdncOperations = mockSdncOperations
-    }
+    def objectUnderTest = new DmiServiceImpl(mockDmiPluginProperties, mockNcmpRestClient, objectMapper, mockSdncOperations)
 
     def 'Call get modules for cm-handle on dmi Service.'() {
         given: 'cm handle id'
@@ -70,4 +75,45 @@ class DmiServiceImplSpec extends Specification {
         then: 'ModulesNotFoundException is thrown'
             thrown( ModulesNotFoundException )
     }
+
+    def 'Register cm handles with ncmp.'() {
+        given: 'cm-handle list and json payload'
+            def givenCmHandlesList = ['node1', 'node2']
+            def expectedJson = '{"dmiPlugin":"test-dmi-service","createdCmHandles":[{"cmHandle":"node1"},{"cmHandle":"node2"}]}'
+        and: 'mockDmiPluginProperties returns test-dmi-service'
+            mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service'
+        when: 'register cm handles service method with the given cm handles'
+            objectUnderTest.registerCmHandles(givenCmHandlesList)
+        then: 'register cm handle with ncmp called once and return "created" status'
+            1 * mockNcmpRestClient.registerCmHandlesWithNcmp(expectedJson) >> new ResponseEntity<>(HttpStatus.CREATED)
+    }
+
+    def 'Register cm handles with ncmp called with exception #scenario.'() {
+        given: 'cm-handle list'
+            def cmHandlesList = ['node1', 'node2']
+        and: 'dmi plugin service name is "test-dmi-service"'
+            mockDmiPluginProperties.getDmiServiceName() >> 'test-dmi-service'
+        and: 'ncmp rest client returns #responseEntity'
+            mockNcmpRestClient.registerCmHandlesWithNcmp(_ as String) >> responseEntity
+        when: 'register cm handles service method called'
+            objectUnderTest.registerCmHandles(cmHandlesList)
+        then: 'a registration exception is thrown'
+            thrown(CmHandleRegistrationException.class)
+        where: 'given #scenario'
+            scenario                                        |   responseEntity
+            'ncmp rest client returns bad request'          |   new ResponseEntity<>(HttpStatus.BAD_REQUEST)
+            'ncmp rest client returns internal server error'|   new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR)
+    }
+
+    def 'Register cm handles with ncmp with wrong data.'() {
+        given: 'objectMapper mock and cm-handle list'
+            def cmHandlesList = ['node1', 'node2']
+        and: 'objectMapper returns "JsonProcessingException" during parse'
+            objectUnderTest.objectMapper = mockObjectMapper
+            mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('some error.') }
+        when: 'register cm handles service method called'
+            objectUnderTest.registerCmHandles(cmHandlesList)
+        then: 'a dmi exception is thrown'
+            thrown(DmiException.class)
+    }
 }
diff --git a/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java b/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java
new file mode 100644 (file)
index 0000000..b82a6f5
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation
+ *  ================================================================================
+ *  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.ncmp.dmi;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
+/**
+ * Common convenience methods for testing.
+ */
+public class TestUtils {
+
+    /**
+     * Convert a file in the test resource folder to file.
+     *
+     * @param filename to name of the file in test/resources
+     * @return the file
+     * @throws IOException when there is an IO issue
+     */
+    public static File readFile(final String filename) {
+        return new File(ClassLoader.getSystemClassLoader().getResource(filename).getFile());
+    }
+
+    /**
+     * Convert a file in the test resource folder to a string.
+     *
+     * @param filename to name of the file in test/resources
+     * @return the content of the file as a String
+     * @throws IOException when there is an IO issue
+     */
+    public static String getResourceFileContent(final String filename) throws IOException {
+        final File file = readFile(filename);
+        return new String(Files.readAllBytes(file.toPath()));
+    }
+}