From 3ba359e291789363ce2670c72da8eaa9caa5f956 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Tue, 18 Jan 2022 10:16:37 +0000 Subject: Update onboarding upload status during processing Updates the onboarding upload status during the VSP package processing Change-Id: Idc705220ad26e62577b52c6f9126aeae51a33fe0 Issue-ID: SDC-3848 Signed-off-by: andre.schmid --- ...ateCandidateUploadManagerExceptionSupplier.java | 36 ++++++++-- .../OrchestrationTemplateCandidateImpl.java | 9 ++- ...rchestrationTemplateCandidateUploadManager.java | 20 ++++++ ...strationTemplateCandidateUploadManagerImpl.java | 78 +++++++++++++++++++--- 4 files changed, 126 insertions(+), 17 deletions(-) (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests') 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/OrchestrationTemplateCandidateUploadManagerExceptionSupplier.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/OrchestrationTemplateCandidateUploadManagerExceptionSupplier.java index 2eb0261a1b..643f59bffb 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/exception/OrchestrationTemplateCandidateUploadManagerExceptionSupplier.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/OrchestrationTemplateCandidateUploadManagerExceptionSupplier.java @@ -25,7 +25,9 @@ import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProdu import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_PROCESSING_IN_PROGRESS; import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPDATE_UPLOAD_LOCK_ERROR; import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_FINISHED_ERROR; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_IN_STATUS_ERROR; import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_LOCK_NOT_FOUND_ERROR; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_STATUS_NOT_FOUND_ERROR; import java.util.UUID; import java.util.function.Supplier; @@ -42,12 +44,19 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier { } public static Supplier vspUploadAlreadyInProgress(final String vspId, final String vspVersionId) { - final String errorMsg = String.format("There is a processing in progress for the VSP %s, version %s", vspId, vspVersionId); + final String errorMsg = String.format("There is a processing in progress for the VSP '%s', version '%s'", vspId, vspVersionId); return () -> new CoreException(new ErrorCodeBuilder().withId(VSP_PROCESSING_IN_PROGRESS).withMessage(errorMsg).build()); } public static Supplier couldNotCreateLock(final String vspId, final String vspVersionId, final Exception exception) { - final String errorMsg = String.format("Could not create a lock for the VSP %s, version %s", vspId, vspVersionId); + final String errorMsg = String.format("Could not create a lock for the VSP '%s', version '%s'", vspId, vspVersionId); + final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build(); + return () -> new CoreException(errorCode, exception); + } + + public static Supplier couldNotUpdateStatus(final String vspId, final String vspVersionId, final VspUploadStatus status, + final Exception exception) { + final String errorMsg = String.format("Could not update upload status for the VSP '%s', version '%s', to '%s'", vspId, vspVersionId, status); final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build(); return () -> new CoreException(errorCode, exception); } @@ -59,11 +68,23 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier { } public static Supplier couldNotFindLock(final UUID lockId, final String vspId, final String vspVersionId) { - final String errorMsg = String.format("Could not find lock '%s' for the VSP %s, version %s", lockId, vspId, vspVersionId); + final String errorMsg = String.format("Could not find lock '%s' for the VSP '%s', version '%s'", lockId, vspId, vspVersionId); final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_LOCK_NOT_FOUND_ERROR).withMessage(errorMsg).build(); return () -> new CoreException(errorCode); } + public static Supplier couldNotFindStatus(final String vspId, final String vspVersionId) { + final String errorMsg = String.format("Could not find upload status for the VSP '%s', version '%s'", vspId, vspVersionId); + final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_STATUS_NOT_FOUND_ERROR).withMessage(errorMsg).build(); + return () -> new CoreException(errorCode); + } + + public static Supplier alreadyInStatusBeingUpdated(final String vspId, final String vspVersionId, final VspUploadStatus status) { + final String errorMsg = String.format("The upload for the VSP '%s', version '%s' is already in the status '%s'", status, vspId, vspVersionId); + final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_IN_STATUS_ERROR).withMessage(errorMsg).build(); + return () -> new CoreException(errorCode); + } + public static Supplier uploadAlreadyFinished(final UUID lockId, final String vspId, final String vspVersionId) { final String errorMsg = String.format("The upload was already finished for lock '%s', VSP '%s', version '%s'", lockId, vspId, vspVersionId); final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_FINISHED_ERROR).withMessage(errorMsg).build(); @@ -75,10 +96,17 @@ public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier { } public static Supplier invalidCompleteStatus(final VspUploadStatus status) { - String errorMsg = String.format("Invalid complete status '%s'. Expecting one of: %s", + final String errorMsg = String.format("Invalid complete status '%s'. Expecting one of: %s", status, VspUploadStatus.getCompleteStatus().stream().map(Enum::name).collect(Collectors.joining(", ")) ); return () -> new IllegalArgumentException(errorMsg); } + + public static Supplier invalidCompletionStatus(final VspUploadStatus status) { + final String errorMsg = String.format("Can't update to a status that represents a upload completion as '%s'", status); + return () -> new IllegalArgumentException(errorMsg); + } + + } 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/OrchestrationTemplateCandidateImpl.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/OrchestrationTemplateCandidateImpl.java index 1477ce1414..6615447c6f 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/OrchestrationTemplateCandidateImpl.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/OrchestrationTemplateCandidateImpl.java @@ -152,6 +152,8 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate fileToUploadBytes = fileToUpload.getObject(byte[].class); } + + vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user); final var onboardingPackageProcessor = new OnboardingPackageProcessor(filename, fileToUploadBytes, new CnfPackageValidator(), artifactInfo); final ErrorMessage[] errorMessages = onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]); @@ -166,13 +168,14 @@ public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplate } final var version = new Version(versionId); final var vspDetails = vendorSoftwareProductManager.getVsp(vspId, version); + vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user); response = processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages); final UploadFileResponseDto entity = (UploadFileResponseDto) response.getEntity(); if (artifactStorageManager.isEnabled()) { - if (!entity.getErrors().isEmpty()) { - artifactStorageManager.delete(artifactInfo); - } else { + if (entity.getErrors().isEmpty()) { artifactStorageManager.put(vspId, versionId + ".reduced", new ByteArrayInputStream(fileToUploadBytes)); + } else { + artifactStorageManager.delete(artifactInfo); } } orchestrationTemplateCandidateUploadManager 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/OrchestrationTemplateCandidateUploadManager.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/OrchestrationTemplateCandidateUploadManager.java index 0f33580099..c4b3e2ef9b 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/OrchestrationTemplateCandidateUploadManager.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/OrchestrationTemplateCandidateUploadManager.java @@ -51,6 +51,26 @@ public interface OrchestrationTemplateCandidateUploadManager { VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus, final String user); + /** + * Updates the upload status to a validation state. + * + * @param vspId the Vendor Software Product id + * @param vspVersionId the Vendor Software Product version id + * @param user the current user + * @return the updated upload status + */ + VspUploadStatusDto putUploadInValidation(final String vspId, final String vspVersionId, final String user); + + /** + * Updates the upload status to a processing state. + * + * @param vspId the Vendor Software Product id + * @param vspVersionId the Vendor Software Product version id + * @param user the current user + * @return the updated upload status + */ + VspUploadStatusDto putUploadInProcessing(String vspId, String vspVersionId, String user); + /** * Finds the latest upload status for a given Vendor Software Product version. * 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/OrchestrationTemplateCandidateUploadManagerImpl.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/OrchestrationTemplateCandidateUploadManagerImpl.java index d7cfe041c2..df280bd20f 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/OrchestrationTemplateCandidateUploadManagerImpl.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/OrchestrationTemplateCandidateUploadManagerImpl.java @@ -21,9 +21,13 @@ package org.openecomp.sdcrests.vsp.rest.services; +import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.alreadyInStatusBeingUpdated; import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotCreateLock; import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindLock; +import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindStatus; import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateLock; +import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateStatus; +import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompletionStatus; import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.uploadAlreadyFinished; import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress; @@ -58,28 +62,31 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateUploadManagerImpl.class); - private final VspUploadStatusRecordDao uploadManagerDao; + private final VspUploadStatusRecordDao vspUploadStatusRecordDao; private final VspUploadStatusRecordMapper vspUploadStatusRecordMapper; private final VendorSoftwareProductManager vendorSoftwareProductManager; private final Lock startUploadLock; + private final Lock updateStatusLock; @Autowired public OrchestrationTemplateCandidateUploadManagerImpl( - @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao uploadManagerDao) { + @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao vspUploadStatusRecordDao) { - this.uploadManagerDao = uploadManagerDao; + this.vspUploadStatusRecordDao = vspUploadStatusRecordDao; this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface(); this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper(); startUploadLock = new ReentrantLock(); + updateStatusLock = new ReentrantLock(); } //for tests purpose - OrchestrationTemplateCandidateUploadManagerImpl(final VspUploadStatusRecordDao uploadManagerDao, + OrchestrationTemplateCandidateUploadManagerImpl(final VspUploadStatusRecordDao vspUploadStatusRecordDao, final VendorSoftwareProductManager vendorSoftwareProductManager) { - this.uploadManagerDao = uploadManagerDao; + this.vspUploadStatusRecordDao = vspUploadStatusRecordDao; this.vendorSoftwareProductManager = vendorSoftwareProductManager; this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper(); startUploadLock = new ReentrantLock(); + updateStatusLock = new ReentrantLock(); } @Override @@ -90,7 +97,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr final VspUploadStatusRecord vspUploadStatusRecord; startUploadLock.lock(); try { - final List uploadInProgressList = uploadManagerDao.findAllInProgress(vspId, vspVersionId); + final List uploadInProgressList = vspUploadStatusRecordDao.findAllInProgress(vspId, vspVersionId); if (!uploadInProgressList.isEmpty()) { throw vspUploadAlreadyInProgress(vspId, vspVersionId).get(); } @@ -102,7 +109,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr vspUploadStatusRecord.setLockId(UUID.randomUUID()); vspUploadStatusRecord.setCreated(new Date()); - uploadManagerDao.create(vspUploadStatusRecord); + vspUploadStatusRecordDao.create(vspUploadStatusRecord); LOGGER.debug("Upload lock '{}' created for VSP id '{}', version '{}'", vspUploadStatusRecord.getLockId(), vspId, vspVersionId); } catch (final CoreException e) { throw e; @@ -115,6 +122,57 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class); } + @Override + public VspUploadStatusDto putUploadInValidation(final String vspId, final String vspVersionId, final String user) { + return updateToNotFinalStatus(vspId, vspVersionId, VspUploadStatus.VALIDATING, user); + } + + @Override + public VspUploadStatusDto putUploadInProcessing(final String vspId, final String vspVersionId, final String user) { + return updateToNotFinalStatus(vspId, vspVersionId, VspUploadStatus.PROCESSING, user); + } + + private VspUploadStatusDto updateToNotFinalStatus(final String vspId, final String vspVersionId, final VspUploadStatus status, final String user) { + LOGGER.debug("Updating upload status to '{}' for VSP id '{}', version '{}', triggered by user '{}'", status, vspId, vspVersionId, user); + if (status.isCompleteStatus()) { + throw invalidCompletionStatus(status).get(); + } + updateStatusLock.lock(); + try { + final Optional vspUploadStatusRecordOptional = vspUploadStatusRecordDao.findLatest(vspId, vspVersionId); + if (vspUploadStatusRecordOptional.isEmpty()) { + throw couldNotFindStatus(vspId, vspVersionId).get(); + } + + final VspUploadStatusRecord vspUploadStatusRecord = vspUploadStatusRecordOptional.get(); + final VspUploadStatus currentStatus = vspUploadStatusRecord.getStatus(); + if (currentStatus == status) { + throw alreadyInStatusBeingUpdated(vspId, vspVersionId, status).get(); + } + return updateStatus(vspUploadStatusRecord, status); + } finally { + updateStatusLock.unlock(); + } + } + + private VspUploadStatusDto updateStatus(final VspUploadStatusRecord vspUploadStatusRecord, final VspUploadStatus status) { + final VspUploadStatus currentStatus = vspUploadStatusRecord.getStatus(); + vspUploadStatusRecord.setStatus(status); + vspUploadStatusRecord.setUpdated(new Date()); + + final String vspId = vspUploadStatusRecord.getVspId(); + final String vspVersionId = vspUploadStatusRecord.getVspVersionId(); + try { + vspUploadStatusRecordDao.update(vspUploadStatusRecord); + LOGGER.debug("Upload lock '{}' status updated from '{}' to '{}' for VSP id '{}', version '{}'", + vspUploadStatusRecord.getLockId(), currentStatus, status, vspId, vspVersionId); + } catch (final Exception e) { + throw couldNotUpdateStatus(vspId, vspVersionId, status, e).get(); + } + + return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class); + } + @Override public VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus, final String user) { @@ -123,7 +181,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr throw OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompleteStatus(completionStatus).get(); } final Optional vspUploadStatusOptional = - uploadManagerDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId); + vspUploadStatusRecordDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId); if (vspUploadStatusOptional.isEmpty()) { throw couldNotFindLock(lockId, vspId, vspVersionId).get(); } @@ -138,7 +196,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr vspUploadStatusRecord.setIsComplete(true); try { - uploadManagerDao.update(vspUploadStatusRecord); + vspUploadStatusRecordDao.update(vspUploadStatusRecord); LOGGER.debug("Upload complete for VSP '{}', version '{}', lock '{}'", vspUploadStatusRecord.getLockId(), vspUploadStatusRecord.getVspId(), vspUploadStatusRecord.getVspVersionId()); } catch (final Exception e) { @@ -160,7 +218,7 @@ public class OrchestrationTemplateCandidateUploadManagerImpl implements Orchestr public Optional findLatestStatus(final String vspId, final String vspVersionId, final String user) { checkVspExists(vspId, vspVersionId); - final Optional vspUploadStatus = uploadManagerDao.findLatest(vspId, vspVersionId); + final Optional vspUploadStatus = vspUploadStatusRecordDao.findLatest(vspId, vspVersionId); if (vspUploadStatus.isEmpty()) { return Optional.empty(); } -- cgit 1.2.3-korg