Update onboarding upload status during processing
[sdc.git] / 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
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdcrests.vsp.rest.exception;
23
24 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_CREATE_UPLOAD_LOCK_ERROR;
25 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_PROCESSING_IN_PROGRESS;
26 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPDATE_UPLOAD_LOCK_ERROR;
27 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_FINISHED_ERROR;
28 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_ALREADY_IN_STATUS_ERROR;
29 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_LOCK_NOT_FOUND_ERROR;
30 import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes.VSP_UPLOAD_STATUS_NOT_FOUND_ERROR;
31
32 import java.util.UUID;
33 import java.util.function.Supplier;
34 import java.util.stream.Collectors;
35 import org.openecomp.sdc.common.errors.CoreException;
36 import org.openecomp.sdc.common.errors.ErrorCode;
37 import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.errors.VendorSoftwareProductNotFoundErrorBuilder;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
40
41 public class OrchestrationTemplateCandidateUploadManagerExceptionSupplier {
42
43     private OrchestrationTemplateCandidateUploadManagerExceptionSupplier() {
44     }
45
46     public static Supplier<CoreException> vspUploadAlreadyInProgress(final String vspId, final String vspVersionId) {
47         final String errorMsg = String.format("There is a processing in progress for the VSP '%s', version '%s'", vspId, vspVersionId);
48         return () -> new CoreException(new ErrorCodeBuilder().withId(VSP_PROCESSING_IN_PROGRESS).withMessage(errorMsg).build());
49     }
50
51     public static Supplier<CoreException> couldNotCreateLock(final String vspId, final String vspVersionId, final Exception exception) {
52         final String errorMsg = String.format("Could not create a lock for the VSP '%s', version '%s'", vspId, vspVersionId);
53         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build();
54         return () -> new CoreException(errorCode, exception);
55     }
56
57     public static Supplier<CoreException> couldNotUpdateStatus(final String vspId, final String vspVersionId, final VspUploadStatus status,
58                                                                final Exception exception) {
59         final String errorMsg = String.format("Could not update upload status for the VSP '%s', version '%s', to '%s'", vspId, vspVersionId, status);
60         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_CREATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build();
61         return () -> new CoreException(errorCode, exception);
62     }
63
64     public static Supplier<CoreException> couldNotUpdateLock(final UUID lockId, final String vspId, final String vspVersionId, final Exception exception) {
65         final String errorMsg = String.format("Could not update the lock %s for the VSP %s, version %s", lockId, vspId, vspVersionId);
66         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPDATE_UPLOAD_LOCK_ERROR).withMessage(errorMsg).build();
67         return () -> new CoreException(errorCode, exception);
68     }
69
70     public static Supplier<CoreException> couldNotFindLock(final UUID lockId, final String vspId, final String vspVersionId) {
71         final String errorMsg = String.format("Could not find lock '%s' for the VSP '%s', version '%s'", lockId, vspId, vspVersionId);
72         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_LOCK_NOT_FOUND_ERROR).withMessage(errorMsg).build();
73         return () -> new CoreException(errorCode);
74     }
75
76     public static Supplier<CoreException> couldNotFindStatus(final String vspId, final String vspVersionId) {
77         final String errorMsg = String.format("Could not find upload status for the VSP '%s', version '%s'", vspId, vspVersionId);
78         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_STATUS_NOT_FOUND_ERROR).withMessage(errorMsg).build();
79         return () -> new CoreException(errorCode);
80     }
81
82     public static Supplier<CoreException> alreadyInStatusBeingUpdated(final String vspId, final String vspVersionId, final VspUploadStatus status) {
83         final String errorMsg = String.format("The upload for the VSP '%s', version '%s' is already in the status '%s'", status, vspId, vspVersionId);
84         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_IN_STATUS_ERROR).withMessage(errorMsg).build();
85         return () -> new CoreException(errorCode);
86     }
87
88     public static Supplier<CoreException> uploadAlreadyFinished(final UUID lockId, final String vspId, final String vspVersionId) {
89         final String errorMsg = String.format("The upload was already finished for lock '%s', VSP '%s', version '%s'", lockId, vspId, vspVersionId);
90         final ErrorCode errorCode = new ErrorCodeBuilder().withId(VSP_UPLOAD_ALREADY_FINISHED_ERROR).withMessage(errorMsg).build();
91         return () -> new CoreException(errorCode);
92     }
93
94     public static Supplier<CoreException> vspNotFound(final String vspId, final String vspVersionId) {
95         return () -> new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId, vspVersionId).build());
96     }
97
98     public static Supplier<IllegalArgumentException> invalidCompleteStatus(final VspUploadStatus status) {
99         final String errorMsg = String.format("Invalid complete status '%s'. Expecting one of: %s",
100             status,
101             VspUploadStatus.getCompleteStatus().stream().map(Enum::name).collect(Collectors.joining(", "))
102         );
103         return () -> new IllegalArgumentException(errorMsg);
104     }
105
106     public static Supplier<IllegalArgumentException> invalidCompletionStatus(final VspUploadStatus status) {
107         final String errorMsg = String.format("Can't update to a status that represents a upload completion as '%s'", status);
108         return () -> new IllegalArgumentException(errorMsg);
109     }
110
111
112 }