Onboarding upload control
[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 / services / OrchestrationTemplateCandidateUploadManagerImpl.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.services;
23
24 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotCreateLock;
25 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotFindLock;
26 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.couldNotUpdateLock;
27 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.uploadAlreadyFinished;
28 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress;
29
30 import java.util.Date;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.UUID;
34 import java.util.concurrent.locks.Lock;
35 import java.util.concurrent.locks.ReentrantLock;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
38 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspUploadStatusRecordDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatusRecord;
43 import org.openecomp.sdc.versioning.dao.types.Version;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspUploadStatusDto;
45 import org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier;
46 import org.openecomp.sdcrests.vsp.rest.mapping.VspUploadStatusRecordMapper;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Qualifier;
51 import org.springframework.stereotype.Service;
52
53 /**
54  * Manages the package upload process status.
55  */
56 @Service
57 public class OrchestrationTemplateCandidateUploadManagerImpl implements OrchestrationTemplateCandidateUploadManager {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateUploadManagerImpl.class);
60
61     private final VspUploadStatusRecordDao uploadManagerDao;
62     private final VspUploadStatusRecordMapper vspUploadStatusRecordMapper;
63     private final VendorSoftwareProductManager vendorSoftwareProductManager;
64     private final Lock startUploadLock;
65
66     @Autowired
67     public OrchestrationTemplateCandidateUploadManagerImpl(
68         @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao uploadManagerDao) {
69
70         this.uploadManagerDao = uploadManagerDao;
71         this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
72         this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper();
73         startUploadLock = new ReentrantLock();
74     }
75
76     //for tests purpose
77     OrchestrationTemplateCandidateUploadManagerImpl(final VspUploadStatusRecordDao uploadManagerDao,
78                                                     final VendorSoftwareProductManager vendorSoftwareProductManager) {
79         this.uploadManagerDao = uploadManagerDao;
80         this.vendorSoftwareProductManager = vendorSoftwareProductManager;
81         this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper();
82         startUploadLock = new ReentrantLock();
83     }
84
85     @Override
86     public VspUploadStatusDto putUploadInProgress(final String vspId, final String vspVersionId, final String user) {
87         checkVspExists(vspId, vspVersionId);
88         LOGGER.debug("Start uploading for VSP id '{}', version '{}', triggered by user '{}'", vspId, vspVersionId, user);
89
90         final VspUploadStatusRecord vspUploadStatusRecord;
91         startUploadLock.lock();
92         try {
93             final List<VspUploadStatusRecord> uploadInProgressList = uploadManagerDao.findAllInProgress(vspId, vspVersionId);
94             if (!uploadInProgressList.isEmpty()) {
95                 throw vspUploadAlreadyInProgress(vspId, vspVersionId).get();
96             }
97
98             vspUploadStatusRecord = new VspUploadStatusRecord();
99             vspUploadStatusRecord.setStatus(VspUploadStatus.UPLOADING);
100             vspUploadStatusRecord.setVspId(vspId);
101             vspUploadStatusRecord.setVspVersionId(vspVersionId);
102             vspUploadStatusRecord.setLockId(UUID.randomUUID());
103             vspUploadStatusRecord.setCreated(new Date());
104
105             uploadManagerDao.create(vspUploadStatusRecord);
106             LOGGER.debug("Upload lock '{}' created for VSP id '{}', version '{}'", vspUploadStatusRecord.getLockId(), vspId, vspVersionId);
107         } catch (final CoreException e) {
108             throw e;
109         } catch (final Exception e) {
110             throw couldNotCreateLock(vspId, vspVersionId, e).get();
111         } finally {
112             startUploadLock.unlock();
113         }
114
115         return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
116     }
117
118     @Override
119     public VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus,
120                                                   final String user) {
121
122         if (!completionStatus.isCompleteStatus()) {
123             throw OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompleteStatus(completionStatus).get();
124         }
125         final Optional<VspUploadStatusRecord> vspUploadStatusOptional =
126             uploadManagerDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId);
127         if (vspUploadStatusOptional.isEmpty()) {
128             throw couldNotFindLock(lockId, vspId, vspVersionId).get();
129         }
130         final VspUploadStatusRecord vspUploadStatusRecord = vspUploadStatusOptional.get();
131         if (vspUploadStatusRecord.getIsComplete()) {
132             throw uploadAlreadyFinished(lockId, vspId, vspVersionId).get();
133         }
134         LOGGER.debug("Finishing the upload for VSP id '{}', version '{}', lock '{}', triggered by user '{}'",
135             vspUploadStatusRecord.getVspId(), vspUploadStatusRecord.getVspVersionId(), vspUploadStatusRecord.getLockId(), user);
136         vspUploadStatusRecord.setStatus(completionStatus);
137         vspUploadStatusRecord.setUpdated(new Date());
138         vspUploadStatusRecord.setIsComplete(true);
139
140         try {
141             uploadManagerDao.update(vspUploadStatusRecord);
142             LOGGER.debug("Upload complete for VSP '{}', version '{}', lock '{}'",
143                 vspUploadStatusRecord.getLockId(), vspUploadStatusRecord.getVspId(), vspUploadStatusRecord.getVspVersionId());
144         } catch (final Exception e) {
145             throw couldNotUpdateLock(vspUploadStatusRecord.getLockId(), vspUploadStatusRecord.getVspId(), vspUploadStatusRecord.getVspVersionId(), e)
146                 .get();
147         }
148
149         return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
150     }
151
152     private void checkVspExists(final String vspId, final String vspVersionId) {
153         final VspDetails vspDetails = vendorSoftwareProductManager.getVsp(vspId, new Version(vspVersionId));
154         if (vspDetails == null) {
155             throw OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspNotFound(vspId, vspVersionId).get();
156         }
157     }
158
159     @Override
160     public Optional<VspUploadStatusDto> findLatestStatus(final String vspId, final String vspVersionId, final String user) {
161         checkVspExists(vspId, vspVersionId);
162
163         final Optional<VspUploadStatusRecord> vspUploadStatus = uploadManagerDao.findLatest(vspId, vspVersionId);
164         if (vspUploadStatus.isEmpty()) {
165             return Optional.empty();
166         }
167
168         return Optional.of(vspUploadStatusRecordMapper.applyMapping(vspUploadStatus.get(), VspUploadStatusDto.class));
169     }
170
171 }