create post request for cmhandles registration

Issue-ID: CPS-406
Change-Id: I64e88643221403e117146443287d03788fb71c3e
Signed-off-by: tragait <rahul.tyagi@est.tech>
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java b/src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java
index 33d6ae8..614435f 100644
--- a/src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java
+++ b/src/main/java/org/onap/cps/ncmp/dmi/config/DmiPluginConfig.java
@@ -20,8 +20,11 @@
 
 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 @@
                 .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
index 0000000..1874389
--- /dev/null
+++ b/src/main/java/org/onap/cps/ncmp/dmi/exception/CmHandleRegistrationException.java
@@ -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);
+    }
+
+}
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/exception/DmiExceptionHandler.java b/src/main/java/org/onap/cps/ncmp/dmi/exception/DmiExceptionHandler.java
index 9ad561f..a6ec6df 100644
--- a/src/main/java/org/onap/cps/ncmp/dmi/exception/DmiExceptionHandler.java
+++ b/src/main/java/org/onap/cps/ncmp/dmi/exception/DmiExceptionHandler.java
@@ -51,7 +51,7 @@
         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
index 0000000..8ddd42f
--- /dev/null
+++ b/src/main/java/org/onap/cps/ncmp/dmi/model/CmHandleOperation.java
@@ -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
index 0000000..9198d7d
--- /dev/null
+++ b/src/main/java/org/onap/cps/ncmp/dmi/model/CreatedCmHandle.java
@@ -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
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java
index 8081b73..0e1d3d6 100644
--- a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java
+++ b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java
@@ -20,7 +20,12 @@
 
 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 @@
 
 @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 @@
         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);
+    }
 }
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java
index e595bd5..d9196ec 100644
--- a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java
+++ b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiService.java
@@ -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 @@
      */
     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);
+
 }
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java
index 4367bf4..990a421 100644
--- a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java
+++ b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java
@@ -20,22 +20,51 @@
 
 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 @@
                     "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());
+        }
+    }
+
 }
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 408fc10..47b8078 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -20,6 +20,10 @@
 server:
   port: 8080
 
+dmi:
+  service:
+    name: onap-dmi-plugin
+
 rest:
   api:
     dmi-base-path: /dmi/api
@@ -61,3 +65,12 @@
   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