diff options
author | tragait <rahul.tyagi@est.tech> | 2021-07-08 15:42:19 +0100 |
---|---|---|
committer | Niamh Core <niamh.core@est.tech> | 2021-08-04 09:53:58 +0000 |
commit | 2270d76e4f33ad231cdae317e88ea1769297cfec (patch) | |
tree | fe456072ca8254f6e4435c2d955d766e9cba6eed /src | |
parent | 7c4a9aa88269dbdb21c5c54bc47508463548bc1e (diff) |
create post request for cmhandles registration
Issue-ID: CPS-406
Change-Id: I64e88643221403e117146443287d03788fb71c3e
Signed-off-by: tragait <rahul.tyagi@est.tech>
Diffstat (limited to 'src')
12 files changed, 357 insertions, 11 deletions
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 33d6ae89..614435f2 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 @@ 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 index 00000000..1874389e --- /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 9ad561fc..a6ec6dfa 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 @@ 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 index 00000000..8ddd42f8 --- /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 00000000..9198d7da --- /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 8081b73d..0e1d3d67 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 @@ 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); + } } 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 e595bd5f..d9196ecb 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 @@ 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); + } 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 4367bf45..990a421e 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 @@ 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()); + } + } + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 408fc103..47b80786 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 @@ 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 diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy index f249de92..993b80c6 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/rest/controller/DmiRestControllerSpec.groovy @@ -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(_) + } } diff --git a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy index 66612960..9d6bc358 100644 --- a/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy +++ b/src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy @@ -20,8 +20,13 @@ 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 index 00000000..b82a6f5c --- /dev/null +++ b/src/test/java/org/onap/cps/ncmp/dmi/TestUtils.java @@ -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())); + } +} |