Merge "SOL003 Adapter Package Management - Terminate Subscription"
authorByung-Woo Jun <byung-woo.jun@est.tech>
Thu, 13 Feb 2020 13:48:26 +0000 (13:48 +0000)
committerGerrit Code Review <gerrit@onap.org>
Thu, 13 Feb 2020 13:48:26 +0000 (13:48 +0000)
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProvider.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/etsicatalog/EtsiCatalogServiceProviderImpl.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/SubscriptionManager.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/packagemanagement/subscriptionmanagement/cache/PackageManagementCacheServiceProvider.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionController.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementSubscriptionControllerTest.java

index 62b3657..e0eed15 100644 (file)
@@ -23,7 +23,6 @@ package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog;
 import java.util.Optional;
 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
-import org.springframework.http.ResponseEntity;
 
 /**
  * Provides methods for invoking REST calls to the ETSI Catalog Manager.
@@ -74,11 +73,18 @@ public interface EtsiCatalogServiceProvider {
     Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath);
 
     /**
-     * Post the SubscriptionRequest Object.
+     * POST the SubscriptionRequest Object.
      *
-     * @return The ResponseEntity containing the ETSI Catalog Manager's PkgmSubscription object.
+     * @return The ETSI Catalog Manager's PkgmSubscription object.
      */
     Optional<PkgmSubscription> postSubscription(
             final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest);
 
+    /**
+     * DELETE the SubscriptionRequest Object.
+     *
+     * @return A Boolean representing if the delete was successful or not.
+     */
+    Boolean deleteSubscription(final String subscriptionId);
+
 }
index 1a48494..573a798 100644 (file)
 package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog;
 
 import java.util.Optional;
-import javax.swing.text.html.Option;
 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
-import org.onap.so.adapters.vnfmadapter.rest.exceptions.*;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerBadRequestException;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerRequestFailureException;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgBadRequestException;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException;
 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
 import org.onap.so.rest.exceptions.InvalidRestRequestException;
 import org.onap.so.rest.exceptions.RestProcessingException;
@@ -190,7 +193,8 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide
             return Optional.empty();
         } catch (final InvalidRestRequestException invalidRestRequestException) {
             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
-            throw new EtsiCatalogManagerBadRequestException("Bad Request Received on postSubscription call.");
+            throw new EtsiCatalogManagerBadRequestException(
+                    "Bad Request Received on postSubscription call to ETSI Catalog Manager.");
         } catch (final RestProcessingException restProcessingException) {
             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
                     restProcessingException);
@@ -200,6 +204,28 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide
         }
     }
 
+    public Boolean deleteSubscription(final String subscriptionId) {
+        try {
+            final ResponseEntity<Void> responseEntity = httpServiceProvider
+                    .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class);
+
+            if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
+                logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager",
+                        subscriptionId);
+                return true;
+            }
+            logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode());
+            return false;
+        } catch (final InvalidRestRequestException invalidRestRequestException) {
+            logger.error("Caught InvalidRestRequestException on deleteSubscription call to ETSI Catalog Manager.",
+                    invalidRestRequestException);
+            throw new EtsiCatalogManagerBadRequestException(
+                    "Bad Request Received on deleteSubscription call to ETSI Catalog Manager.");
+        }
+    }
+
+
+
     private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
             final String vnfRequestName) {
         try {
index 30a16f7..bbf8b74 100644 (file)
@@ -143,6 +143,15 @@ public class SubscriptionManager {
         return response;
     }
 
+    public boolean deleteSubscription(final String subscriptionId) {
+        if (getSubscription(subscriptionId).isPresent()) {
+            if (etsiCatalogServiceProvider.deleteSubscription(subscriptionId)) {
+                return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId);
+            }
+        }
+        return false;
+    }
+
     public URI getSubscriptionUri(final String subscriptionId) {
         return URI.create(
                 vnfmAdapterEndpoint + Constants.PACKAGE_MANAGEMENT_BASE_URL + "/subscriptions/" + subscriptionId);
index cbad564..4be0838 100644 (file)
@@ -23,14 +23,13 @@ package org.onap.so.adapters.vnfmadapter.rest;
 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
 import static org.slf4j.LoggerFactory.getLogger;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.security.GeneralSecurityException;
 import java.util.List;
 import java.util.Optional;
 import javax.ws.rs.core.MediaType;
+import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
-import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
 import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.SubscriptionManager;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -38,6 +37,7 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 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;
@@ -139,6 +139,24 @@ public class Sol003PackageManagementSubscriptionController {
         return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ProblemDetails().detail(errorMessage));
     }
 
+    /**
+     * DELETE a specific subscription, by subscriptionId. Section Number: 10.4.8.3.5
+     *
+     * @param subscriptionId The ID of the subscription that you wish to delete.
+     * @return Empty response if successful. Object: Void Response Code: 204 No Content
+     */
+    @DeleteMapping(value = "/subscriptions/{subscriptionId}")
+    public ResponseEntity<?> deleteSubscription(@PathVariable("subscriptionId") final String subscriptionId) {
+        if (subscriptionManager.deleteSubscription(subscriptionId)) {
+            logger.debug("Successfully deleted subscription with id {}", subscriptionId);
+            return ResponseEntity.noContent().build();
+        }
+        final String errorMessage =
+                "The requested subscription: " + subscriptionId + " was not found on call deleteSubscription";
+        logger.error(errorMessage);
+        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ProblemDetails().detail(errorMessage));
+    }
+
     /**
      * Method to set the Location in the header with the URI parameter
      * 
index f90978e..3d26c33 100644 (file)
@@ -68,6 +68,7 @@ import org.springframework.web.client.RestTemplate;
 import org.springframework.http.HttpMethod;
 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
 import static org.hamcrest.Matchers.is;
+import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
 
 /**
@@ -199,6 +200,41 @@ public class Sol003PackageManagementSubscriptionControllerTest {
         assertEquals(HttpStatus.OK, response.getStatusCode());
     }
 
+    @Test
+    public void testSuccessDeleteSubscriptionWithSubscriptionId() throws GeneralSecurityException {
+        final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
+        final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
+        final String subscriptionId = pkgmSubscription.getId();
+
+        mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
+                .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
+        mockRestServer.expect(requestTo(msbEndpoint + "/" + subscriptionId)).andExpect(method(HttpMethod.DELETE))
+                .andRespond(withStatus(HttpStatus.NO_CONTENT));
+
+        final ResponseEntity<InlineResponse2002> responsePost =
+                (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
+                        .postSubscriptionRequest(pkgmSubscriptionRequest);
+
+
+        final ResponseEntity responseDelete =
+                sol003PackageManagementSubscriptionController.deleteSubscription(subscriptionId);
+
+        // Attempt to retrieve the subscription after delete
+        final ResponseEntity<InlineResponse2002> responseGetSubscription =
+                (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
+                        .getSubscription(subscriptionId);
+
+        assertEquals(HttpStatus.NOT_FOUND, responseGetSubscription.getStatusCode());
+        assertEquals(HttpStatus.NO_CONTENT, responseDelete.getStatusCode());
+    }
+
+    @Test
+    public void testFailDeleteSubscriptionWithInvalidSubscriptionId() throws URISyntaxException, InterruptedException {
+        final ResponseEntity<Void> responseDelete = (ResponseEntity<Void>) sol003PackageManagementSubscriptionController
+                .deleteSubscription("invalidSubscriptionId");
+        assertEquals(HttpStatus.NOT_FOUND, responseDelete.getStatusCode());
+    }
+
     private PkgmSubscriptionRequest buildPkgmSubscriptionRequest() {
         final PkgmSubscriptionRequest pkgmSubscriptionRequest = new PkgmSubscriptionRequest();
         final SubscriptionsFilter sub = buildSubscriptionsFilter();