SOL003 Adapter Package Management - Read VNFD 54/100054/4
authoreHanan <eoin.hanan@est.tech>
Tue, 7 Jan 2020 14:20:42 +0000 (14:20 +0000)
committerEoin Hanan <eoin.hanan@est.tech>
Wed, 8 Jan 2020 11:22:44 +0000 (11:22 +0000)
Change-Id: I2710b199ccc02af1782cd5fa75e85c4ed5c69aa9
Issue-ID: SO-2414
Signed-off-by: eHanan <eoin.hanan@est.tech>
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/extclients/etsicatalog/EtsiCatalogUrlProvider.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementController.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003PackageManagementControllerTest.java

index d406395..34fc264 100644 (file)
@@ -53,6 +53,14 @@ public interface EtsiCatalogServiceProvider {
      */
     Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId);
 
+    /**
+     * GET specific VNF package VNFD from ETSI Catalog.
+     *
+     * @param vnfPkgId The ID of the VNF Package that you want to query.
+     * @return The VNF package retrieved from the ETSI Catalog
+     */
+    Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId);
+
     /**
      * GET Package Artifact, from VNF Package.
      *
index 11a59c2..779cb2a 100644 (file)
@@ -62,26 +62,9 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide
     @Override
     public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
             throws EtsiCatalogManagerRequestFailureException {
-        try {
-            final ResponseEntity<byte[]> response = httpServiceProvider
-                    .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId), byte[].class);
-            logger.info("getVnfPackageContent Request to ETSI Catalog Manager Status Code: {}",
-                    response.getStatusCodeValue());
-            if (response.getStatusCode() == HttpStatus.OK) {
-                return Optional.ofNullable(response.getBody());
-            }
-        } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
-            logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
-            throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
-        } catch (final RestProcessingException restProcessingException) {
-            logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
-                    restProcessingException);
-            if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
-                throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
-                        + "due to the attribute: onboardingState not being set to ONBOARDED.");
-            }
-        }
-        throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
+        final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
+        final String vnfRequestName = "getVnfPackageContent";
+        return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
     }
 
     @Override
@@ -179,4 +162,34 @@ public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvide
             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
         }
     }
+
+    @Override
+    public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
+        final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
+        final String vnfRequestName = "getVnfPackageVnfd";
+        return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
+    }
+
+    private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
+            final String vnfRequestName) {
+        try {
+            final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
+            logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
+                    response.getStatusCodeValue());
+            if (response.getStatusCode() == HttpStatus.OK) {
+                return Optional.ofNullable(response.getBody());
+            }
+        } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
+            logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
+            throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
+        } catch (final RestProcessingException restProcessingException) {
+            logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
+                    restProcessingException);
+            if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
+                throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
+                        + "due to the attribute: onboardingState not being set to ONBOARDED.");
+            }
+        }
+        throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
+    }
 }
index cc2c7a6..051f60a 100644 (file)
@@ -91,4 +91,16 @@ public class EtsiCatalogUrlProvider {
         logger.info("getVnfPackageArtifactUrl: {}", url);
         return url;
     }
+
+    /**
+     * Get the URL for retrieving VNF packages vnfd from ETSI Catalog.
+     *
+     * @param vnfPkgId The ID of the VNF Package
+     * @return the URL for the GET operation
+     */
+    public String getVnfPackageVnfdUrl(final String vnfPkgId) {
+        final String url = msbEndpoint + vnfpkgmServiceUrl + "/vnf_packages/" + vnfPkgId + "/vnfd";
+        logger.info("getEtsiCatalogVnfPackageVnfd: {}", url);
+        return url;
+    }
 }
index 3f975fb..9d8e29b 100644 (file)
@@ -103,15 +103,25 @@ public class Sol003PackageManagementController {
     /**
      * GET VNFD, from VNF package. Will return a copy of the file representing the VNFD or a ZIP file that contains the
      * file/multiple files representing the VNFD specified. Section Number: 10.4.4
-     * 
+     *
      * @param vnfPkgId The ID of the VNF Package that you want to retrieve the VNFD from.
      * @return The VNFD of a VNF Package as a single file or within a ZIP file. Object: byte[] Response Code: 200 OK
      */
     @GetMapping(value = "/vnf_packages/{vnfPkgId}/vnfd",
             produces = {MediaType.TEXT_PLAIN, APPLICATION_ZIP, MediaType.APPLICATION_JSON})
-    public ResponseEntity<?> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) {
-        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd: ", vnfPkgId);
-        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+    public ResponseEntity<byte[]> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) {
+        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Endpoint Invoked with VNF Package ID: ", vnfPkgId);
+        final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageVnfd(vnfPkgId);
+        if (response.isPresent()) {
+            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Response: ", HttpStatus.OK);
+            return new ResponseEntity(response.get(), HttpStatus.OK);
+        }
+        final String errorMessage = "An error occurred, a null response was received by the\n"
+                + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnfd\" \n"
+                + "endpoint.";
+
+        logger.error(errorMessage);
+        return new ResponseEntity(buildProblemDetails(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
     }
 
     /**
index dbc8ef2..623cba7 100644 (file)
@@ -470,9 +470,100 @@ public class Sol003PackageManagementControllerTest {
 
     // The below test method is here to improve code coverage and provide a foundation for writing future tests
     @Test
-    public void testGetVnfd_Not_Implemented() {
+    public void testGetPackageVnfd_ValidArray_Success() {
+        final byte[] responseArray = buildByteArrayWithRandomData(10);
+
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET))
+                .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
+
+        final String testURL =
+                "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
+        final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
+        final ResponseEntity<byte[]> responseEntity =
+                restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
+
+        assertEquals(byte[].class, responseEntity.getBody().getClass());
+        assertArrayEquals(responseEntity.getBody(), responseArray);
+        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_Conflict_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
+
+        final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_NotFound_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
+
+        final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
+        final String testURL =
+                "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
+        final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
+        final ResponseEntity<ProblemDetails> responseEntity =
+                restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_InternalServerError_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
+
         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
-        assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_BadRequest_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
+
+        final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
+
+        final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
+
+        assertTrue(responseEntity.getBody() instanceof ProblemDetails);
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
+    }
+
+    @Test
+    public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
+        mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
+                .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
+
+        final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
+
+        assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
     }
 
     // Simply returns a byte array filled with random data, for use in the tests.