Adding service delete endpoint in aai simulator 70/96170/1
authorwaqas.ikram <waqas.ikram@est.tech>
Tue, 24 Sep 2019 10:32:39 +0000 (11:32 +0100)
committerwaqas.ikram <waqas.ikram@est.tech>
Tue, 24 Sep 2019 10:32:41 +0000 (11:32 +0100)
Change-Id: Ib4e99dadd3937657c63ed508b68384ecd98bfc88
Issue-ID: SO-2342
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/BusinessController.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CustomerCacheServiceProvider.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CustomerCacheServiceProviderImpl.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/BusinessControllerTest.java

index 347743b..4a0ed1b 100644 (file)
@@ -50,6 +50,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -324,4 +325,32 @@ public class BusinessController {
 
         return getRequestErrorResponseEntity(request);
     }
+
+    @DeleteMapping(
+            value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
+            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+    public ResponseEntity<?> deleteSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
+            @PathVariable("service-type") final String serviceType,
+            @PathVariable(name = "service-instance-id") final String serviceInstanceId,
+            @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
+
+        LOGGER.info(
+                "Will delete SericeInstance for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
+                globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
+
+        if (cacheServiceProvider.deleteSericeInstance(globalCustomerId, serviceType, serviceInstanceId,
+                resourceVersion)) {
+            LOGGER.info(
+                    "Successfully deleted SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
+                    globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
+            return ResponseEntity.noContent().build();
+        }
+
+        LOGGER.error(
+                "Unable to delete SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
+                globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
+
+        return getRequestErrorResponseEntity(request);
+
+    }
 }
index 268a6bc..7000fb3 100644 (file)
@@ -59,4 +59,7 @@ public interface CustomerCacheServiceProvider extends Clearable {
     Optional<Relationship> addRelationShip(final String globalCustomerId, final String serviceType,
             final String serviceInstanceId, final Relationship relationship, final String requestUri);
 
+    boolean deleteSericeInstance(final String globalCustomerId, final String serviceType,
+            final String serviceInstanceId, final String resourceVersion);
+
 }
index 4d42c24..7193ade 100644 (file)
@@ -152,6 +152,9 @@ public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvid
 
             }
         }
+        LOGGER.error(
+                "Unable to find ServiceInstance using globalCustomerId: {}, serviceType: {} and serviceInstanceId: {} ...",
+                globalCustomerId, serviceType, serviceInstanceId);
         return Optional.empty();
     }
 
@@ -228,6 +231,42 @@ public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvid
         return false;
     }
 
+    @Override
+    public boolean deleteSericeInstance(final String globalCustomerId, final String serviceType,
+            final String serviceInstanceId, final String resourceVersion) {
+        final Cache cache = getCache(CUSTOMER_CACHE.getName());
+        final Customer value = cache.get(globalCustomerId, Customer.class);
+
+        if (value != null) {
+            final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
+                    .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
+
+            if (serviceSubscription.isPresent()) {
+                LOGGER.info("Found service subscription ...");
+                final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
+                if (serviceInstances != null) {
+
+                    serviceInstances.getServiceInstance().removeIf(serviceInstance -> {
+                        final String existingServiceInstanceId = serviceInstance.getServiceInstanceId();
+                        final String existingResourceVersion = serviceInstance.getResourceVersion();
+                        if (existingServiceInstanceId != null && existingServiceInstanceId.equals(serviceInstanceId)
+                                && existingResourceVersion != null && existingResourceVersion.equals(resourceVersion)) {
+                            LOGGER.info("Removing ServiceInstance with serviceInstanceId: {} and resourceVersion: {}",
+                                    existingServiceInstanceId, existingResourceVersion);
+                            return true;
+                        }
+                        return false;
+                    });
+
+
+                    return true;
+                }
+
+            }
+        }
+        return false;
+    }
+
     private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
         final ServiceSubscription serviceSubscription = optional.get();
         final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
index 8c57db5..c08c51e 100644 (file)
@@ -335,8 +335,8 @@ public class BusinessControllerTest extends AbstractSpringBootTest {
 
         invokeServiceInstanceEndPointAndAssertResponse();
 
-        final String relationShipUrl =
-                getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL, BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
+        final String relationShipUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL,
+                BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
 
         final ResponseEntity<Relationship> responseEntity2 = testRestTemplateService.invokeHttpPut(relationShipUrl,
                 TestUtils.getRelationShipJsonObject(), Relationship.class);
@@ -360,6 +360,26 @@ public class BusinessControllerTest extends AbstractSpringBootTest {
         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
     }
 
+    @Test
+    public void test_DeleteSericeInstance_ServiceInstanceRemovedFromCache() throws Exception {
+        final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
+
+        invokeCustomerEndPointAndAssertResponse();
+
+        invokeServiceInstanceEndPointAndAssertResponse();
+
+        final Optional<ServiceInstance> optional =
+                cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
+        assertTrue(optional.isPresent());
+        final ServiceInstance serviceInstance = optional.get();
+
+        final ResponseEntity<Void> responseEntity = testRestTemplateService
+                .invokeHttpDelete(url + "?resource-version=" + serviceInstance.getResourceVersion(), Void.class);
+        assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
+        assertFalse(cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID)
+                .isPresent());
+    }
+
     private void invokeServiceInstanceEndPointAndAssertResponse() throws IOException {
         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
         final ResponseEntity<Void> responseEntity =