From f772969ac32331ce847e8193059cd39dd362c7a6 Mon Sep 17 00:00:00 2001 From: franciscovila Date: Thu, 10 Mar 2022 11:55:04 +0000 Subject: Restrict deletion of archived VSPs if used in VF Verify if a VSP is used in a VF in SDC-BE before proceeding with the deletion of a VSP Issue-ID: SDC-3894 Signed-off-by: franciscovila Change-Id: I7c8bdd55516a51cf8693cfdb8c109a7c20e7ea84 --- .../sdcrests/vsp/rest/CatalogVspClient.java | 35 +++++++ .../vsp/rest/services/CatalogVspClientImpl.java | 115 +++++++++++++++++++++ .../rest/services/VendorSoftwareProductsImpl.java | 20 +++- 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/CatalogVspClient.java create mode 100644 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 (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main') 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/CatalogVspClient.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/CatalogVspClient.java new file mode 100644 index 0000000000..22e9b54099 --- /dev/null +++ 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/CatalogVspClient.java @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ + +package org.openecomp.sdcrests.vsp.rest; + +import java.util.Optional; + +public interface CatalogVspClient { + + /** + * Returns the name of a VF which is using the provided VSP + * + * @param vspId the id of the vsp + * @param user the user to perform the action + */ + Optional findNameOfVfUsingVsp(String vspId, String user) throws Exception; +} 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 new file mode 100644 index 0000000000..221c3a7fc5 --- /dev/null +++ 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 @@ -0,0 +1,115 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ + +package org.openecomp.sdcrests.vsp.rest.services; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.jetbrains.annotations.Nullable; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; +import org.openecomp.sdc.common.CommonConfigurationManager; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.errors.CatalogRestClientException; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.http.client.api.HttpRequest; +import org.openecomp.sdc.common.http.client.api.HttpResponse; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdcrests.item.rest.services.catalog.notification.EntryNotConfiguredException; +import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpConfiguration; +import org.openecomp.sdcrests.vsp.rest.CatalogVspClient; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; + +import static javax.ws.rs.core.HttpHeaders.ACCEPT; +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + +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_FILE_PROPERTY = "configuration.yaml"; + private static final String CONFIG_SECTION = "catalogNotificationsConfig"; + 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}"; + + public CatalogVspClientImpl() { } + + /** + * 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. + * + * @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 { + 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); + ObjectMapper mapper = new ObjectMapper(); + Map respObject = mapper.readValue(httpResponse.getResponse(), Map.class); + return Optional.of((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); + } + } + + @Nullable + private HttpConfiguration getHttpConfiguration() throws CatalogRestClientException { + HttpConfiguration httpConfig; + try { + Object config = getEndpointConfiguration(); + ObjectMapper mapper = new ObjectMapper(); + httpConfig = mapper.convertValue(config, HttpConfiguration.class); + } catch (Exception e) { + LOGGER.error("Failed to load configuration. ", e); + throw new CatalogRestClientException("Failed to load configuration. ", e); + } + return httpConfig; + } + + private static Object getEndpointConfiguration() { + final var commonConfigurationManager = CommonConfigurationManager.getInstance(); + return commonConfigurationManager.getConfigValue(CONFIG_SECTION); + } +} 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/VendorSoftwareProductsImpl.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/VendorSoftwareProductsImpl.java index 86e676020c..5540e0f315 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/VendorSoftwareProductsImpl.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/VendorSoftwareProductsImpl.java @@ -42,6 +42,7 @@ import javax.ws.rs.core.Response; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.dao.UniqueValueDaoFactory; import org.openecomp.core.util.UniqueValueUtil; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; import org.openecomp.sdc.activitylog.ActivityLogManager; import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; @@ -98,6 +99,7 @@ import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspComputeDto; import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspDescriptionDto; import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspDetailsDto; import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspRequestDto; +import org.openecomp.sdcrests.vsp.rest.CatalogVspClient; import org.openecomp.sdcrests.vsp.rest.VendorSoftwareProducts; import org.openecomp.sdcrests.vsp.rest.mapping.MapComputeEntityToVspComputeDto; import org.openecomp.sdcrests.vsp.rest.mapping.MapItemToVspDetailsDto; @@ -132,6 +134,8 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { private final NotificationPropagationManager notifier; private final UniqueValueUtil uniqueValueUtil; private final ArtifactStorageManager artifactStorageManager; + private final CatalogVspClient catalogVspClient; + public VendorSoftwareProductsImpl() { this.itemManager = AsdcItemManagerFactory.getInstance().createInterface(); @@ -142,6 +146,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { this.notifier = NotificationPropagationManagerFactory.getInstance().createInterface(); this.uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); this.artifactStorageManager = new StorageFactory().createArtifactStorageManager(); + this.catalogVspClient = new CatalogVspClientImpl(); } public VendorSoftwareProductsImpl(AsdcItemManager itemManager, @@ -151,7 +156,8 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { ActivityLogManager activityLogManager, NotificationPropagationManager notifier, UniqueValueUtil uniqueValueUtil, - ArtifactStorageManager artifactStorageManager) { + ArtifactStorageManager artifactStorageManager, + CatalogVspClient catalogVspClient) { this.itemManager = itemManager; this.permissionsManager = permissionsManager; this.versioningManager = versioningManager; @@ -160,6 +166,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { this.notifier = notifier; this.uniqueValueUtil = uniqueValueUtil; this.artifactStorageManager = artifactStorageManager; + this.catalogVspClient = catalogVspClient; } @Override @@ -280,6 +287,17 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { if (!vsp.getType().equals(ItemType.vsp.name())) { throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage(String.format("Vsp with id %s does not exist.", vspId)).build())); } + try { + Optional optUsedInVf = catalogVspClient.findNameOfVfUsingVsp(vspId, user); + if (optUsedInVf.isPresent()) { + return Response.status(Response.Status.FORBIDDEN).entity( + new Exception(ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_ERROR_USED_BY_VF.getErrorMessage(), optUsedInVf.get(), optUsedInVf.get())) + ).build(); + } + } catch (Exception e) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage(String.format("Vsp with id %s cannot be deleted due to error %s.", vspId, e.getMessage())).build()))).build(); + } + Integer certifiedVersionsCounter = vsp.getVersionStatusCounters().get(VersionStatus.Certified); if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { if (artifactStorageManager.isEnabled() && !deleteVspFromStorage(vspId)) { -- cgit 1.2.3-korg