Fix Update VSP when no version id is provided 33/124333/1
authorandre.schmid <andre.schmid@est.tech>
Fri, 17 Sep 2021 14:48:43 +0000 (15:48 +0100)
committerandre.schmid <andre.schmid@est.tech>
Fri, 17 Sep 2021 15:19:57 +0000 (16:19 +0100)
Issue-ID: SDC-3730
Change-Id: Ic5a5a6ecf3b6775e022be649145e44b0907cbecf
Signed-off-by: andre.schmid <andre.schmid@est.tech>
catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/templates/default/BE-configuration.yaml.erb
catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogic.java
catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/api/OnboardingClient.java
catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/impl/OnboardingClientImpl.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CsarOperation.java
common-app-api/src/main/java/org/openecomp/sdc/be/config/Configuration.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/VendorSoftwareProducts.java
openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java

index 60616c2..118b48d 100644 (file)
@@ -948,6 +948,7 @@ onboarding:
     getVspPackageUri: "/onboarding-api/v1.0/vendor-software-products/packages/%s?versionId=%s" # /onboarding-api/v1.0/vendor-software-products/packages/:vspId?versionId=:vspVersionId
     getLatestVspPackageUri: "/onboarding-api/v1.0/vendor-software-products/packages/%s" # /onboarding-api/v1.0/vendor-software-products/packages/:vspId
     getVspUri: "/onboarding-api/v1.0/vendor-software-products/%s/versions/%s" # /onboarding-api/v1.0/vendor-software-products/:vspId/versions/:vspVersionId
+    getLatestVspUri: "/onboarding-api/v1.0/vendor-software-products/%s" # /onboarding-api/v1.0/vendor-software-products/:vspId
     healthCheckUri: "/onboarding-api/v1.0/healthcheck"
 
 # #GSS IDNS
index b8a5d67..a85b554 100644 (file)
@@ -223,7 +223,11 @@ public class CsarBusinessLogic extends BaseBusinessLogic {
     private VendorSoftwareProduct getCsar(final Resource resource, final User user) {
         final Optional<VendorSoftwareProduct> vendorSoftwareProductOpt;
         try {
-            vendorSoftwareProductOpt = csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user);
+            if (resource.getCsarVersionId() == null) {
+                vendorSoftwareProductOpt = csarOperation.findLatestVsp(resource.getCsarUUID(), user);
+            } else {
+                vendorSoftwareProductOpt = csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user);
+            }
         } catch (final Exception exception) {
             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, CsarBusinessLogic.class.getName(), exception.getMessage());
             auditGetCsarError(resource, user, resource.getCsarUUID(), StorageOperationStatus.GENERAL_ERROR);
index 34db17c..1abb1bf 100644 (file)
@@ -50,4 +50,13 @@ public interface OnboardingClient {
      */
     Optional<VendorSoftwareProduct> findVendorSoftwareProduct(String id, String versionId, String userId);
 
+    /**
+     * Finds the latest version of the Vendor Software Product (VSP) from the onboarding repository.
+     *
+     * @param id        the VSP id
+     * @param userId    the logged user id
+     * @return a VSP representation if found, empty otherwise.
+     */
+    Optional<VendorSoftwareProduct> findLatestVendorSoftwareProduct(String id, String userId);
+
 }
index 1aa6cd8..ebc9996 100644 (file)
@@ -143,6 +143,53 @@ public class OnboardingClientImpl implements OnboardingClient {
         return Optional.of(vendorSoftwareProduct);
     }
 
+    @Override
+    public Optional<VendorSoftwareProduct> findLatestVendorSoftwareProduct(final String id, final String userId) {
+        final Either<Map<String, byte[]>, StorageOperationStatus> csarEither = this.findLatestPackage(id, userId);
+        if (csarEither.isRight()) {
+            final StorageOperationStatus operationStatus = csarEither.right().value();
+            if (operationStatus == StorageOperationStatus.CSAR_NOT_FOUND || operationStatus == StorageOperationStatus.NOT_FOUND) {
+                return Optional.empty();
+            }
+            var errorMsg = String.format("An error has occurred while retrieving the latest package with for '%s': '%s'",
+                id, operationStatus);
+            throw new OnboardingClientException(errorMsg);
+        }
+        final String url = buildGetLatestVspUrl(id);
+        final Properties headers = buildDefaultHeader(userId);
+        headers.put(ACCEPT, APPLICATION_JSON);
+        LOGGER.debug("Find VSP built url '{}', with headers '{}'", url, headers);
+        final HttpResponse<String> httpResponse;
+        try {
+            httpResponse = HttpRequest.get(url, headers);
+        } catch (final Exception e) {
+            throw new OnboardingClientException("An error has occurred while retrieving the package", e);
+        }
+
+        if (httpResponse.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+            return Optional.empty();
+        }
+
+        if (httpResponse.getStatusCode() != HttpStatus.SC_OK) {
+            var errorMsg = String.format("An error has occurred while retrieving the package. Http status was %s", httpResponse.getStatusCode());
+            throw new OnboardingClientException(errorMsg);
+        }
+
+        final String responseData = httpResponse.getResponse();
+        LOGGER.debug("Find vsp response data: '{}'", responseData);
+
+        final VendorSoftwareProductDto vendorSoftwareProductDto;
+        try {
+            vendorSoftwareProductDto = new ObjectMapper().readValue(responseData, VendorSoftwareProductDto.class);
+        } catch (final JsonProcessingException e) {
+            throw new OnboardingClientException("Could not parse retrieve package response to VendorSoftwareProductDto.class.", e);
+        }
+        final Map<String, byte[]> csarFileMap = csarEither.left().value();
+        final var vendorSoftwareProduct = VendorSoftwareProductMapper.mapFrom(vendorSoftwareProductDto);
+        vendorSoftwareProduct.setFileMap(csarFileMap);
+        return Optional.of(vendorSoftwareProduct);
+    }
+
     private Properties buildDefaultHeader(final String userId) {
         final var headers = new Properties();
         if (userId != null) {
@@ -180,6 +227,15 @@ public class OnboardingClientImpl implements OnboardingClient {
         return String.format("%s://%s:%s%s", protocol, host, port, uri);
     }
 
+    private String buildGetLatestVspUrl(final String id) {
+        final var onboardingConfig = getOnboardingConfig();
+        final String protocol = onboardingConfig.getProtocol();
+        final String host = onboardingConfig.getHost();
+        final Integer port = onboardingConfig.getPort();
+        final var uri = String.format(onboardingConfig.getGetLatestVspUri(), id);
+        return String.format("%s://%s:%s%s", protocol, host, port, uri);
+    }
+
     private OnboardingConfig getOnboardingConfig() {
         return ConfigurationManager.getConfigurationManager().getConfiguration().getOnboarding();
     }
index bcdcb0e..5a90bb8 100644 (file)
@@ -74,4 +74,8 @@ public class CsarOperation {
         return onboardingClient.findVendorSoftwareProduct(id, versionId, user.getUserId());
     }
 
+    public Optional<VendorSoftwareProduct> findLatestVsp(final String id, final User user) {
+        return onboardingClient.findLatestVendorSoftwareProduct(id, user.getUserId());
+    }
+
 }
index 96688ce..9b3772f 100644 (file)
@@ -351,6 +351,7 @@ public class Configuration extends BasicConfiguration {
         private String getLatestVspPackageUri;
         private String getVspPackageUri;
         private String getVspUri;
+        private String getLatestVspUri;
         @ToString.Exclude
         private String healthCheckUri;
     }
index 328c962..fbe7d37 100644 (file)
@@ -75,6 +75,12 @@ public interface VendorSoftwareProducts extends VspEntities {
                           + "Default value = 'ACTIVE'.") @QueryParam("Status") String itemStatus,
                       @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user);
 
+    @GET
+    @Path("/{vspId}")
+    @Parameter(description = "Get details of the latest certified vendor software product")
+    Response getLatestVsp(@PathParam("vspId") String vspId,
+                          @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user);
+
     @GET
     @Path("/{vspId}/versions/{versionId}")
     @Parameter(description = "Get details of a vendor software product")
index 5367e1e..76e3f67 100644 (file)
@@ -208,6 +208,15 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts {
         return Response.ok(vspDetailsDto).build();
     }
 
+    @Override
+    public Response getLatestVsp(final String vspId, final String user) {
+        final List<Version> versions = versioningManager.list(vspId);
+        final Version version = versions.stream().filter(ver -> VersionStatus.Certified == ver.getStatus())
+            .max(Comparator.comparingDouble(o -> Double.parseDouble(o.getName())))
+            .orElseThrow(() -> new CoreException(new PackageNotFoundErrorBuilder(vspId).build()));
+        return getVsp(vspId, version.getId(), user);
+    }
+
     private void submitHealedVersion(VspDetails vspDetails, String baseVersionId, String user) {
         try {
             if (vspDetails.getVlmVersion() != null) {