Adding service delete endpoint in sdnc simulator 49/96149/2
authorwaqas.ikram <waqas.ikram@est.tech>
Mon, 23 Sep 2019 14:11:20 +0000 (15:11 +0100)
committerwaqas.ikram <waqas.ikram@est.tech>
Mon, 23 Sep 2019 15:37:11 +0000 (16:37 +0100)
Change-Id: I7c849b32a7b920becd6b7f9622e5f5bbe01eebe0
Issue-ID: SO-2342
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/main/java/org/onap/so/sdncsimulator/controller/OperationsController.java
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/main/java/org/onap/so/sdncsimulator/providers/ServiceOperationsCacheServiceProvider.java
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/main/java/org/onap/so/sdncsimulator/providers/ServiceOperationsCacheServiceProviderimpl.java
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/test/java/org/onap/so/sdncsimulator/controller/OperationsControllerTest.java
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/test/java/org/onap/so/sdncsimulator/controller/TestUtils.java
plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/test/resources/test-data/deleteServiceInput.json [new file with mode: 0644]

index 88970e7..9696beb 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.onap.so.sdncsimulator.controller;
 
+import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE;
 import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETEVNFINSTANCE;
 import static org.onap.so.sdncsimulator.utils.Constants.OPERATIONS_URL;
 import javax.servlet.http.HttpServletRequest;
@@ -73,14 +74,14 @@ public class OperationsController {
             return ResponseEntity.badRequest().build();
         }
 
-        final Output output = cacheServiceProvider.putServiceOperationInformation(apiServiceOperationInformation);
+        final Output output = getOutput(apiServiceOperationInformation);
         final OutputRequest outputRequest = new OutputRequest(output);
 
         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
-            LOGGER.info("Sucessfully added service in cache sending response: {}", outputRequest);
+            LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
             return ResponseEntity.ok(outputRequest);
         }
-        LOGGER.error("Unable to add input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
+        LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
         return ResponseEntity.badRequest().body(outputRequest);
 
     }
@@ -100,19 +101,31 @@ public class OperationsController {
         }
 
         final Output output = getOutput(apiVnfOperationInformation);
-
         final OutputRequest outputRequest = new OutputRequest(output);
 
         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
-            LOGGER.info("Sucessfully added vnf in cache sending response: {}", outputRequest);
+            LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
             return ResponseEntity.ok(outputRequest);
         }
 
-        LOGGER.error("Unable to add input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
+        LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
         return ResponseEntity.badRequest().body(outputRequest);
 
     }
 
+    private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
+        final GenericResourceApiRequestinformationRequestInformation requestInformation =
+                serviceOperationInformation.getRequestInformation();
+        if (requestInformation != null) {
+            final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
+            if (DELETESERVICEINSTANCE.equals(requestAction)) {
+                LOGGER.info("RequestAction: {} will delete service instance from cache ...", requestAction);
+                return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
+            }
+        }
+        return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
+    }
+
     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
         final GenericResourceApiRequestinformationRequestInformation requestInformation =
                 apiVnfOperationInformation.getRequestInformation();
index 5b32b91..c3a80ec 100644 (file)
@@ -34,6 +34,9 @@ public interface ServiceOperationsCacheServiceProvider {
     Output putServiceOperationInformation(
             final GenericResourceApiServiceOperationInformation apiServiceOperationInformation);
 
+    Output deleteServiceOperationInformation(
+            final GenericResourceApiServiceOperationInformation serviceOperationInformation);
+
     Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
             final String serviceInstanceId);
 
index 616a56c..180e636 100644 (file)
@@ -118,6 +118,34 @@ public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServ
                 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
     }
 
+    @Override
+    public Output deleteServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
+        final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
+        final String svcRequestId = getSvcRequestId(input.getSdncRequestHeader());
+
+        if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
+            final String serviceInstanceId = serviceInformation.getServiceInstanceId();
+            final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
+                    getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
+            if (optional.isPresent()) {
+                final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
+                LOGGER.info("Deleting GenericResourceApiServiceOperationInformation from cache using key: {}",
+                        serviceInstanceId);
+                cache.evict(serviceInstanceId);
+                return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.OK.toString())
+                        .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId).serviceResponseInformation(
+                                new GenericResourceApiInstanceReference().instanceId(serviceInstanceId));
+            }
+            LOGGER.error(
+                    "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
+                    serviceInstanceId);
+
+        }
+        LOGGER.error("Unable to remove service instance from cache due to invalid input: {}... ", input);
+        return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
+                .responseMessage("Unable to remove service").svcRequestId(svcRequestId);
+    }
+
     @Override
     public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
             final String serviceInstanceId) {
@@ -210,7 +238,14 @@ public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServ
                             getExistingVnf(vnfId, vnfsList);
 
                     if (vnfInstanceOptional.isPresent()) {
-                        vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
+                        vnfsList.removeIf(vnf -> {
+                            final String existingVnfId = vnf.getVnfId();
+                            if (existingVnfId != null && existingVnfId.equals(vnfId)) {
+                                LOGGER.info("Remove vnf with id: {} ... ", existingVnfId);
+                                return true;
+                            }
+                            return false;
+                        });
 
                         return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.OK.toString())
                                 .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId)
index a338283..3c17a37 100644 (file)
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.onap.so.sdncsimulator.controller.TestUtils.getInvalidRequestInput;
 import static org.onap.so.sdncsimulator.controller.TestUtils.getRequestInput;
+import static org.onap.so.sdncsimulator.controller.TestUtils.getServiceRequestWithRequestActionDeleteServiceInput;
 import static org.onap.so.sdncsimulator.controller.TestUtils.getVnfRequestInput;
 import static org.onap.so.sdncsimulator.controller.TestUtils.getVnfRequestWithRequestActionDeleteVnfInput;
 import static org.onap.so.sdncsimulator.controller.TestUtils.getVnfRequestWithSvcActionActivateInput;
@@ -302,7 +303,7 @@ public class OperationsControllerTest {
         assertNotNull(vnf.getVnfId());
         assertEquals(VNF_INSTANCE_ID, vnf.getVnfId());
         assertNotNull(vnf.getVnfData());
-        GenericResourceApiOperStatusData vnfLevelOperStatus = vnf.getVnfData().getVnfLevelOperStatus();
+        final GenericResourceApiOperStatusData vnfLevelOperStatus = vnf.getVnfData().getVnfLevelOperStatus();
         assertNotNull(vnfLevelOperStatus);
         assertEquals(GenericResourceApiLastRpcActionEnumeration.ACTIVATE, vnfLevelOperStatus.getLastRpcAction());
 
@@ -364,6 +365,43 @@ public class OperationsControllerTest {
 
     }
 
+    @Test
+    public void test_postServiceOperationInformation_withActionDeleteServiceInstance_successfullyRemoveServiceFromExistingServiceInCache()
+            throws Exception {
+        final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
+        final ResponseEntity<OutputRequest> responseEntity =
+                restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
+
+        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
+        Optional<GenericResourceApiServicemodelinfrastructureService> serviceOptional =
+                cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
+        assertTrue(serviceOptional.isPresent());
+
+        final GenericResourceApiServicemodelinfrastructureService service = serviceOptional.get();
+        assertNotNull(service.getServiceInstanceId());
+
+        final HttpEntity<?> httpRemoveServiceEntity =
+                new HttpEntity<>(getServiceRequestWithRequestActionDeleteServiceInput(), getHttpHeaders());
+        final ResponseEntity<OutputRequest> responseRemoveServiceEntity =
+                restTemplate.exchange(getUrl(), HttpMethod.POST, httpRemoveServiceEntity, OutputRequest.class);
+        assertEquals(HttpStatus.OK, responseRemoveServiceEntity.getStatusCode());
+
+        final OutputRequest actualOutputRequest = responseRemoveServiceEntity.getBody();
+        assertNotNull(actualOutputRequest);
+        assertNotNull(actualOutputRequest.getOutput());
+
+        final Output actualObject = actualOutputRequest.getOutput();
+
+        assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
+        assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
+        assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
+
+        serviceOptional =
+                cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
+        assertFalse(serviceOptional.isPresent());
+
+    }
+
     private HttpHeaders getHttpHeaders() {
         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
     }
index b43ecf4..8e0e9b4 100644 (file)
@@ -58,6 +58,10 @@ public class TestUtils {
         return getFileAsString(getFile("test-data/deleteVnfInput.json").toPath());
     }
 
+    public static String getServiceRequestWithRequestActionDeleteServiceInput() throws IOException {
+        return getFileAsString(getFile("test-data/deleteServiceInput.json").toPath());
+    }
+
     public static String getFileAsString(final Path path) throws IOException {
         return new String(Files.readAllBytes(path));
     }
diff --git a/plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/test/resources/test-data/deleteServiceInput.json b/plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/test/resources/test-data/deleteServiceInput.json
new file mode 100644 (file)
index 0000000..1040209
--- /dev/null
@@ -0,0 +1,28 @@
+{
+    "input": {
+        "request-information": {
+            "request-action": "DeleteServiceInstance",
+            "source": "MSO",
+            "request-id": "33ebd358-a189-4664-90f5-cf9e23658e0a"
+        },
+        "sdnc-request-header": {
+            "svc-request-id": "04fc9f50-87b8-430d-a232-ef24bd6c4150",
+            "svc-action": "deactivate"
+        },
+        "service-information": {
+            "onap-model-information": {
+                "model-name": "Sol004Zip3Service",
+                "model-version": "2.0",
+                "model-uuid": "c112a499-6148-488b-ba82-3f5938cf26d2",
+                "model-invariant-uuid": "e9acd081-9c89-4b4d-bcb3-e0e2b9715b2a"
+            },
+            "subscription-service-type": "vCPE",
+            "service-id": "ccece8fe-13da-456a-baf6-41b3a4a2bc2b",
+            "global-customer-id": "NordixDemoCustomer",
+            "service-instance-id": "ccece8fe-13da-456a-baf6-41b3a4a2bc2b"
+        },
+        "service-request-input": {
+            "service-instance-name": "ServiceTest_24_07_2019"
+        }
+    }
+}