aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org')
-rw-r--r--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.java3
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/exception/VendorSoftwareProductsExceptionSupplier.java91
-rw-r--r--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.java22
-rw-r--r--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.java114
4 files changed, 176 insertions, 54 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
index 22e9b54099..6a1107cbec 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/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
@@ -22,6 +22,7 @@
package org.openecomp.sdcrests.vsp.rest;
import java.util.Optional;
+import org.openecomp.sdc.common.errors.CatalogRestClientException;
public interface CatalogVspClient {
@@ -31,5 +32,5 @@ public interface CatalogVspClient {
* @param vspId the id of the vsp
* @param user the user to perform the action
*/
- Optional<String> findNameOfVfUsingVsp(String vspId, String user) throws Exception;
+ Optional<String> findNameOfVfUsingVsp(String vspId, String user) throws CatalogRestClientException;
}
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/exception/VendorSoftwareProductsExceptionSupplier.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/exception/VendorSoftwareProductsExceptionSupplier.java
new file mode 100644
index 0000000000..10d897a28f
--- /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/exception/VendorSoftwareProductsExceptionSupplier.java
@@ -0,0 +1,91 @@
+/*
+ * -
+ * ============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.exception;
+
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_DELETE_ALREADY_IN_USE_BY_VF;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_DELETE_FROM_DATABASE_ERROR;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_DELETE_FROM_STORAGE_ERROR;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_DELETE_GENERIC_ERROR;
+import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_DELETE_NOT_ARCHIVED;
+
+import java.util.function.Supplier;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.common.errors.ErrorCategory;
+import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
+import org.openecomp.sdc.common.errors.Messages;
+import org.openecomp.sdc.vendorsoftwareproduct.dao.errors.VendorSoftwareProductNotFoundErrorBuilder;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class VendorSoftwareProductsExceptionSupplier {
+
+ public static Supplier<CoreException> vspNotFound(final String vspId) {
+ final VendorSoftwareProductNotFoundErrorBuilder errorBuilder = new VendorSoftwareProductNotFoundErrorBuilder(vspId);
+ return () -> new CoreException((errorBuilder.build()));
+ }
+
+ public static Supplier<CoreException> vspInUseByVf(final String vfName) {
+ final String errorMsg = Messages.DELETE_VSP_ERROR_USED_BY_VF.formatMessage(vfName, vfName);
+ final ErrorCodeBuilder errorBuilder =
+ new ErrorCodeBuilder().withId(VSP_DELETE_ALREADY_IN_USE_BY_VF)
+ .withCategory(ErrorCategory.USER)
+ .withMessage(errorMsg);
+ return () -> new CoreException(errorBuilder.build());
+ }
+
+ public static Supplier<CoreException> deleteGenericError(final String vspId) {
+ final String errorMsg = String.format("An error has occurred while trying to delete the VSP '%s'.", vspId);
+ final ErrorCodeBuilder errorBuilder =
+ new ErrorCodeBuilder().withId(VSP_DELETE_GENERIC_ERROR)
+ .withCategory(ErrorCategory.SYSTEM)
+ .withMessage(errorMsg);
+ return () -> new CoreException(errorBuilder.build());
+ }
+
+ public static Supplier<CoreException> deleteNotArchivedVsp(final String vspId) {
+ final String errorMsg = Messages.DELETE_NOT_ARCHIVED_VSP_ERROR.formatMessage(vspId);
+ final ErrorCodeBuilder errorBuilder =
+ new ErrorCodeBuilder().withId(VSP_DELETE_NOT_ARCHIVED)
+ .withCategory(ErrorCategory.USER)
+ .withMessage(errorMsg);
+ return () -> new CoreException(errorBuilder.build());
+ }
+
+ public static Supplier<CoreException> deleteVspFromStorageFailure(final String vspId) {
+ final String errorMsg = Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId);
+ final ErrorCodeBuilder errorBuilder =
+ new ErrorCodeBuilder().withId(VSP_DELETE_FROM_STORAGE_ERROR)
+ .withCategory(ErrorCategory.SYSTEM)
+ .withMessage(errorMsg);
+ return () -> new CoreException(errorBuilder.build());
+ }
+
+ public static Supplier<CoreException> deleteVspFromDatabaseFailure(final String vspId) {
+ final String errorMsg = Messages.DELETE_VSP_ERROR.formatMessage(vspId);
+ final ErrorCodeBuilder errorBuilder =
+ new ErrorCodeBuilder().withId(VSP_DELETE_FROM_DATABASE_ERROR)
+ .withCategory(ErrorCategory.SYSTEM)
+ .withMessage(errorMsg);
+ return () -> new CoreException(errorBuilder.build());
+ }
+}
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 221c3a7fc5..c9ad46850e 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
@@ -21,9 +21,14 @@
package org.openecomp.sdcrests.vsp.rest.services;
+import static javax.ws.rs.core.HttpHeaders.ACCEPT;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
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.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;
@@ -33,32 +38,17 @@ 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,
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 9bb68f23af..6431db3354 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,7 +42,6 @@ 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;
@@ -101,6 +100,7 @@ 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.exception.VendorSoftwareProductsExceptionSupplier;
import org.openecomp.sdcrests.vsp.rest.mapping.MapComputeEntityToVspComputeDto;
import org.openecomp.sdcrests.vsp.rest.mapping.MapItemToVspDetailsDto;
import org.openecomp.sdcrests.vsp.rest.mapping.MapPackageInfoToPackageInfoDto;
@@ -281,60 +281,100 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts {
}
@Override
- public Response deleteVsp(String vspId, String user) {
- Item vsp = itemManager.get(vspId);
- 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()));
+ public Response deleteVsp(final String vspId, final String user) {
+ final Item vsp = itemManager.get(vspId);
+ if (!ItemType.vsp.getName().equals(vsp.getType())) {
+ throw VendorSoftwareProductsExceptionSupplier.vspNotFound(vspId).get();
}
+
+ checkIfCanDeleteVsp(vsp, user);
+
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();
+ deleteVspFromStorage(vspId, user);
+ } catch (final Exception e) {
+ logDeleteFromStorageFailure(vspId, user);
+ throw VendorSoftwareProductsExceptionSupplier.deleteVspFromStorageFailure(vspId).get();
}
- Integer certifiedVersionsCounter = vsp.getVersionStatusCounters().get(VersionStatus.Certified);
- final ArtifactStorageManager artifactStorageManager = storageFactory.createArtifactStorageManager();
- if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) {
- if (artifactStorageManager.isEnabled() && !deleteVspFromStorage(vspId, artifactStorageManager)) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(new Exception(Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId))).build();
- }
- return deleteVsp(vspId, user, vsp);
- } else {
- final var isVspArchived = getVspList(null, ItemStatus.ARCHIVED.name(), user).stream().anyMatch(item -> item.getId().equals(vspId));
- if (isVspArchived) {
- if (artifactStorageManager.isEnabled() && !deleteVspFromStorage(vspId, artifactStorageManager)) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(new Exception(Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId))).build();
- }
- return deleteVsp(vspId, user, vsp);
- }
- return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VSP_ERROR.getErrorMessage())).build();
+ try {
+ deleteVsp(vspId, user, vsp);
+ } catch (final Exception e) {
+ throw VendorSoftwareProductsExceptionSupplier.deleteVspFromDatabaseFailure(vspId).get();
+ }
+
+ return Response.ok().build();
+ }
+
+ private void checkIfCanDeleteVsp(final Item vsp, final String user) {
+ final String vspId = vsp.getId();
+
+ checkIfVspInUse(user, vspId);
+
+ if (isVspItemNeverCertified(vsp)) {
+ return;
+ }
+ if (!isVspItemArchived(vspId, user)) {
+ throw VendorSoftwareProductsExceptionSupplier.deleteNotArchivedVsp(vspId).get();
}
}
- private boolean deleteVspFromStorage(final String vspId, final ArtifactStorageManager artifactStorageManager) {
+ private void checkIfVspInUse(final String user, final String vspId) {
+ final Optional<String> vfNameThatUsesVspOpt;
try {
+ vfNameThatUsesVspOpt = catalogVspClient.findNameOfVfUsingVsp(vspId, user);
+ } catch (final Exception e) {
+ throw VendorSoftwareProductsExceptionSupplier.deleteGenericError(vspId).get();
+ }
+ if (vfNameThatUsesVspOpt.isPresent()) {
+ final String vfName = vfNameThatUsesVspOpt.get();
+ throw VendorSoftwareProductsExceptionSupplier.vspInUseByVf(vfName).get();
+ }
+ }
+
+ private boolean isVspItemArchived(final String vspId, final String user) {
+ return getVspList(null, ItemStatus.ARCHIVED.name(), user).stream().anyMatch(item -> vspId.equals(item.getId()));
+ }
+
+ private boolean isVspItemNeverCertified(final Item vsp) {
+ final Integer certifiedVersionsCounter = vsp.getVersionStatusCounters().get(VersionStatus.Certified);
+ return certifiedVersionsCounter == null || certifiedVersionsCounter == 0;
+ }
+
+ private void deleteVspFromStorage(final String vspId, final String user) {
+ final ArtifactStorageManager artifactStorageManager = storageFactory.createArtifactStorageManager();
+ if (artifactStorageManager.isEnabled()) {
artifactStorageManager.delete(vspId);
+ logDeleteFromStorageAllSuccess(vspId, user);
+ }
+ }
+
+ private void logDeleteFromStorageFailure(final String vspId, final String user) {
+ final String message = Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId);
+ try {
+ versioningManager.list(vspId).forEach(version -> activityLogManager.logActivity(
+ new ActivityLogEntity(vspId, version, ActivityType.Delete_From_Storage, user, false, message, message)
+ ));
} catch (final Exception e) {
- LOGGER.error("Failed to delete VSP '{}'", vspId, e);
- return false;
+ LOGGER.error("Could not log activity '{}'", message, e);
+ }
+ }
+ private void logDeleteFromStorageAllSuccess(final String vspId, final String user) {
+ final String message = String.format("VSP '%s' fully deleted from the storage", vspId);
+ try {
+ versioningManager.list(vspId).forEach(version -> activityLogManager.logActivity(
+ new ActivityLogEntity(vspId, version, ActivityType.Delete_From_Storage, user, true, message, message)
+ ));
+ } catch (final Exception e) {
+ LOGGER.error("Could not log activity '{}'", message, e);
}
- return true;
}
- private Response deleteVsp(String vspId, String user, Item vsp) {
+ private void deleteVsp(final String vspId, final String user, final Item vsp) {
versioningManager.list(vspId).forEach(version -> vendorSoftwareProductManager.deleteVsp(vspId, version));
itemManager.delete(vsp);
permissionsManager.deleteItemPermissions(vspId);
uniqueValueUtil.deleteUniqueValue(VENDOR_SOFTWARE_PRODUCT_NAME, vsp.getName());
notifyUsers(vspId, vsp.getName(), null, null, user, NotificationEventTypes.DELETE);
- return Response.ok().build();
}
@Override