diff options
Diffstat (limited to 'openecomp-be/api')
5 files changed, 253 insertions, 3 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/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<String> 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<String> 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<String> httpResponse; + httpResponse = HttpRequest.get(url, headers); + ObjectMapper mapper = new ObjectMapper(); + Map<String, Object> 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<String> 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)) { diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java index 6340d0819a..f8af8df1ed 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java @@ -21,6 +21,7 @@ package org.openecomp.sdcrests.vsp.rest.services; +import static ch.qos.logback.classic.util.ContextInitializer.CONFIG_FILE_PROPERTY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; @@ -29,9 +30,15 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.openMocks; import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR; +import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR_USED_BY_VF; import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR; +import java.io.FileNotFoundException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; +import java.util.Optional; import java.util.UUID; import javax.ws.rs.core.Response; import org.apache.http.HttpStatus; @@ -42,6 +49,7 @@ import org.mockito.Mock; import org.openecomp.core.util.UniqueValueUtil; import org.openecomp.sdc.activitylog.ActivityLogManager; import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager; +import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.itempermissions.PermissionsManager; import org.openecomp.sdc.notification.services.NotificationPropagationManager; import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; @@ -50,12 +58,17 @@ import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.VersionStatus; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdc.versioning.types.ItemStatus; +import org.openecomp.sdcrests.vsp.rest.CatalogVspClient; class VendorSoftwareProductsImplTest { + public static final String SOME_INTERNAL_ERROR = "Some internal error"; + public static final String VF_NAME = "Vf_name"; private final String vspId = UUID.randomUUID().toString(); private final String user = "cs0008"; + private final Path testResourcesPath = Paths.get("src", "test", "resources"); + @Mock private AsdcItemManager itemManager; @Mock @@ -72,6 +85,8 @@ class VendorSoftwareProductsImplTest { private UniqueValueUtil uniqueValueUtil; @Mock private ArtifactStorageManager artifactStorageManager; + @Mock + private CatalogVspClient catalogVspClient; @InjectMocks private VendorSoftwareProductsImpl vendorSoftwareProducts; @@ -82,6 +97,8 @@ class VendorSoftwareProductsImplTest { public void setUp() { openMocks(this); + System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString()); + item = new Item(); item.setType("vsp"); item.setId(vspId); @@ -89,7 +106,7 @@ class VendorSoftwareProductsImplTest { } @Test - void deleteVspOk() { + void deleteNotCertifiedVspOk() { Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); assertEquals(HttpStatus.SC_OK, rsp.getStatus()); assertNull(rsp.getEntity()); @@ -125,7 +142,9 @@ class VendorSoftwareProductsImplTest { } @Test - void deleteCertifiedArchivedVsp() { + void deleteCertifiedArchivedVsp() throws FileNotFoundException { + String configPath = getConfigPath("configuration.yaml"); + System.setProperty(CONFIG_FILE_PROPERTY, configPath); item.setStatus(ItemStatus.ARCHIVED); item.addVersionStatus(VersionStatus.Certified); when(itemManager.get(vspId)).thenReturn(item); @@ -160,4 +179,61 @@ class VendorSoftwareProductsImplTest { assertEquals(rsp.getEntity().getClass(), Exception.class); assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId)); } + + @Test + void deleteVspUsedInVfKo() throws Exception { + Item item = new Item(); + item.setType("vsp"); + item.setId(vspId); + item.addVersionStatus(VersionStatus.Certified); + when(itemManager.get(vspId)).thenReturn(item); + when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME)); + + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus()); + assertEquals(rsp.getEntity().getClass(), Exception.class); + assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format(DELETE_VSP_ERROR_USED_BY_VF.getErrorMessage(), VF_NAME, VF_NAME)); + } + + @Test + void deleteVspUsedInVfThrowsExceptionKo() throws Exception { + Item item = new Item(); + item.setType("vsp"); + item.setId(vspId); + item.addVersionStatus(VersionStatus.Certified); + when(itemManager.get(vspId)).thenReturn(item); + final String vf_name = "Vf_name"; + when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new Exception(SOME_INTERNAL_ERROR)); + + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus()); + assertEquals(rsp.getEntity().getClass(), CoreException.class); + assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format("Vsp with id %s cannot be deleted due to error %s.", vspId, SOME_INTERNAL_ERROR)); + } + + @Test + void deleteCertifiedArchivedVspNotInVfOk() throws Exception { + String configPath = getConfigPath("configuration.yaml"); + System.setProperty(CONFIG_FILE_PROPERTY, configPath); + Item item = new Item(); + item.setType("vsp"); + item.setId(vspId); + item.setStatus(ItemStatus.ARCHIVED); + item.addVersionStatus(VersionStatus.Certified); + when(itemManager.get(vspId)).thenReturn(item); + when(itemManager.list(any())).thenReturn(List.of(item)); + when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty()); + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_OK, rsp.getStatus()); + assertNull(rsp.getEntity()); + } + + private String getConfigPath(String classpathFile) throws FileNotFoundException { + + URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile); + if (resource == null) { + throw new FileNotFoundException("Cannot find resource: " + classpathFile); + } + return resource.getPath(); + } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/resources/configuration.yaml b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/resources/configuration.yaml new file mode 100644 index 0000000000..736e4073fd --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/resources/configuration.yaml @@ -0,0 +1,6 @@ +catalogNotificationsConfig: + # catalog backend protocol + + catalogBeProtocol: http + catalogBeHttpPort: 8080 + catalogBeFqdn: localhost |