From e750f64f5b1d479ec194e31370f91208e4a4b35c Mon Sep 17 00:00:00 2001 From: KrupaNagabhushan Date: Tue, 5 Apr 2022 19:41:15 +0100 Subject: [PATCH] Fix error handling for VSP usage check in VF Issue-ID: SDC-3953 Signed-off-by: KrupaNagabhushan Change-Id: I37b2944672694ccdc9d8ee84a554b7284dbe7723 --- .../vsp/rest/services/CatalogVspClientImpl.java | 64 ++++++++++++++-------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/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/CatalogVspClientImpl.java b/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/CatalogVspClientImpl.java index c9ad46850e..eb732fa0e7 100644 --- a/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/CatalogVspClientImpl.java +++ b/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/CatalogVspClientImpl.java @@ -28,7 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; import java.util.Optional; import java.util.Properties; -import org.jetbrains.annotations.Nullable; +import org.apache.http.HttpStatus; import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; import org.openecomp.sdc.common.CommonConfigurationManager; import org.openecomp.sdc.common.api.Constants; @@ -46,45 +46,61 @@ public class CatalogVspClientImpl implements CatalogVspClient { private static final Logger LOGGER = LoggerFactory.getLogger(CatalogVspClientImpl.class); private static final String URL_GET_RESOURCE_BY_CSAR_UUID = "%s://%s:%s/sdc2/rest/v1/catalog/resources/csar/%s"; private static final String CONFIG_SECTION = "catalogNotificationsConfig"; + private static final String VSP_USE_NOT_FOUND = "SVC4635"; public static final String NAME = "name"; public static final String SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID = "sdc2/rest/v1/catalog/resources/csar/{csaruuid}"; /** - * Returns the name of a VF which is using the provided VSP. - * It returns an empty optional in case the VSP is not used by any VF, - * or throws ans exception if any error occurs during the process. + * Returns the name of a VF which is using the provided VSP. It returns an empty optional in case the VSP is not used by any VF, or throws an + * exception if any error occurs during the process. * - * @param vspId the id of the vsp - * @param user the user to perform the action + * @param vspId the id of the vsp + * @param user the user to perform the action */ @Override public Optional findNameOfVfUsingVsp(String vspId, String user) throws CatalogRestClientException { + HttpConfiguration httpConfig = getHttpConfiguration(); + if (null == httpConfig) { + throw new CatalogRestClientException( + ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(), + vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID)); + } + final Properties headers = new Properties(); + headers.put(Constants.USER_ID_HEADER, user); + headers.put(ACCEPT, APPLICATION_JSON); + String url = String.format(URL_GET_RESOURCE_BY_CSAR_UUID, httpConfig.getCatalogBeProtocol(), + httpConfig.getCatalogBeFqdn(), httpConfig.getCatalogBeHttpPort(), vspId); + final HttpResponse httpResponse; try { - HttpConfiguration httpConfig = getHttpConfiguration(); - if (null == httpConfig) { - throw new CatalogRestClientException(ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(), - vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID)); - } - final Properties headers = new Properties(); - headers.put(Constants.USER_ID_HEADER, user); - headers.put(ACCEPT, APPLICATION_JSON); - String url = String.format(URL_GET_RESOURCE_BY_CSAR_UUID, httpConfig.getCatalogBeProtocol(), - httpConfig.getCatalogBeFqdn(), httpConfig.getCatalogBeHttpPort(), vspId); - final HttpResponse httpResponse; httpResponse = HttpRequest.get(url, headers); + } catch (final Exception e) { + String formattedErrorMessage = ErrorMessagesFormatBuilder.getErrorWithParameters( + Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(), + vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID); + LOGGER.error(formattedErrorMessage, e); + throw new CatalogRestClientException(formattedErrorMessage, e); + } + if (httpResponse.getStatusCode() != HttpStatus.SC_OK) { + if (httpResponse.getResponse().contains(VSP_USE_NOT_FOUND)) { + LOGGER.debug("VSP usage in VF not found", httpResponse.getResponse()); + return Optional.empty(); + } + throw new CatalogRestClientException(ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF + .getErrorMessage(), vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID)); + } + return Optional.of(getVspNameUsedInVf(httpResponse)); + } + + private String getVspNameUsedInVf(HttpResponse httpResponse) { + try { ObjectMapper mapper = new ObjectMapper(); Map respObject = mapper.readValue(httpResponse.getResponse(), Map.class); - return Optional.of((String) respObject.get(NAME)); - + return (String) respObject.get(NAME); } catch (Exception e) { - String formattedErrorMessage = ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(), - vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID); - LOGGER.error(formattedErrorMessage, e); - throw new CatalogRestClientException(formattedErrorMessage, e); + throw new CatalogRestClientException("Could not parse find VSP usage response", e); } } - @Nullable private HttpConfiguration getHttpConfiguration() throws CatalogRestClientException { HttpConfiguration httpConfig; try { -- 2.16.6