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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdcrests.vsp.rest.services;
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;
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;
54 * Manages the package upload process status.
57 public class OrchestrationTemplateCandidateUploadManagerImpl implements OrchestrationTemplateCandidateUploadManager {
59 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateUploadManagerImpl.class);
61 private final VspUploadStatusRecordDao uploadManagerDao;
62 private final VspUploadStatusRecordMapper vspUploadStatusRecordMapper;
63 private final VendorSoftwareProductManager vendorSoftwareProductManager;
64 private final Lock startUploadLock;
67 public OrchestrationTemplateCandidateUploadManagerImpl(
68 @Qualifier("vsp-upload-status-record-dao-impl") final VspUploadStatusRecordDao uploadManagerDao) {
70 this.uploadManagerDao = uploadManagerDao;
71 this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
72 this.vspUploadStatusRecordMapper = new VspUploadStatusRecordMapper();
73 startUploadLock = new ReentrantLock();
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();
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);
90 final VspUploadStatusRecord vspUploadStatusRecord;
91 startUploadLock.lock();
93 final List<VspUploadStatusRecord> uploadInProgressList = uploadManagerDao.findAllInProgress(vspId, vspVersionId);
94 if (!uploadInProgressList.isEmpty()) {
95 throw vspUploadAlreadyInProgress(vspId, vspVersionId).get();
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());
105 uploadManagerDao.create(vspUploadStatusRecord);
106 LOGGER.debug("Upload lock '{}' created for VSP id '{}', version '{}'", vspUploadStatusRecord.getLockId(), vspId, vspVersionId);
107 } catch (final CoreException e) {
109 } catch (final Exception e) {
110 throw couldNotCreateLock(vspId, vspVersionId, e).get();
112 startUploadLock.unlock();
115 return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
119 public VspUploadStatusDto putUploadAsFinished(final String vspId, final String vspVersionId, final UUID lockId, final VspUploadStatus completionStatus,
122 if (!completionStatus.isCompleteStatus()) {
123 throw OrchestrationTemplateCandidateUploadManagerExceptionSupplier.invalidCompleteStatus(completionStatus).get();
125 final Optional<VspUploadStatusRecord> vspUploadStatusOptional =
126 uploadManagerDao.findByVspIdAndVersionIdAndLockId(vspId, vspVersionId, lockId);
127 if (vspUploadStatusOptional.isEmpty()) {
128 throw couldNotFindLock(lockId, vspId, vspVersionId).get();
130 final VspUploadStatusRecord vspUploadStatusRecord = vspUploadStatusOptional.get();
131 if (vspUploadStatusRecord.getIsComplete()) {
132 throw uploadAlreadyFinished(lockId, vspId, vspVersionId).get();
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);
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)
149 return vspUploadStatusRecordMapper.applyMapping(vspUploadStatusRecord, VspUploadStatusDto.class);
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();
160 public Optional<VspUploadStatusDto> findLatestStatus(final String vspId, final String vspVersionId, final String user) {
161 checkVspExists(vspId, vspVersionId);
163 final Optional<VspUploadStatusRecord> vspUploadStatus = uploadManagerDao.findLatest(vspId, vspVersionId);
164 if (vspUploadStatus.isEmpty()) {
165 return Optional.empty();
168 return Optional.of(vspUploadStatusRecordMapper.applyMapping(vspUploadStatus.get(), VspUploadStatusDto.class));