From d6b83fce723703f4dea0bce3675ecbf6996aaf9a Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Fri, 17 Sep 2021 15:48:43 +0100 Subject: Fix Update VSP when no version id is provided Issue-ID: SDC-3730 Change-Id: Ic5a5a6ecf3b6775e022be649145e44b0907cbecf Signed-off-by: andre.schmid --- .../be/client/onboarding/api/OnboardingClient.java | 9 ++++ .../onboarding/impl/OnboardingClientImpl.java | 56 ++++++++++++++++++++++ .../be/model/operations/impl/CsarOperation.java | 4 ++ 3 files changed, 69 insertions(+) (limited to 'catalog-model') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/api/OnboardingClient.java b/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/api/OnboardingClient.java index 34db17c4af..1abb1bf08a 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/api/OnboardingClient.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/api/OnboardingClient.java @@ -50,4 +50,13 @@ public interface OnboardingClient { */ Optional 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 findLatestVendorSoftwareProduct(String id, String userId); + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/impl/OnboardingClientImpl.java b/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/impl/OnboardingClientImpl.java index 1aa6cd8157..ebc9996975 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/impl/OnboardingClientImpl.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/client/onboarding/impl/OnboardingClientImpl.java @@ -143,6 +143,53 @@ public class OnboardingClientImpl implements OnboardingClient { return Optional.of(vendorSoftwareProduct); } + @Override + public Optional findLatestVendorSoftwareProduct(final String id, final String userId) { + final Either, 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 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 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(); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CsarOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CsarOperation.java index bcdcb0e6d1..5a90bb8382 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CsarOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CsarOperation.java @@ -74,4 +74,8 @@ public class CsarOperation { return onboardingClient.findVendorSoftwareProduct(id, versionId, user.getUserId()); } + public Optional findLatestVsp(final String id, final User user) { + return onboardingClient.findLatestVendorSoftwareProduct(id, user.getUserId()); + } + } -- cgit 1.2.3-korg