2 * Copyright © 2016-2018 European Support Limited
3 * Copyright © 2021 Nokia
4 * Copyright © 2021 Nordix Foundation
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.
17 * ============LICENSE_END=========================================================
18 * Modifications copyright (c) 2019 Nokia
19 * Modifications copyright (c) 2021 Nordix Foundation
20 * ================================================================================
22 package org.openecomp.sdcrests.vsp.rest.services;
24 import static javax.ws.rs.core.Response.Status.EXPECTATION_FAILED;
25 import static javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE;
26 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
27 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
28 import static org.openecomp.sdc.common.errors.Messages.ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT;
29 import static org.openecomp.sdc.common.errors.Messages.ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE;
30 import static org.openecomp.sdc.common.errors.Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST;
31 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
32 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_REDUCER_NOT_CONFIGURED;
33 import static org.openecomp.sdc.common.errors.Messages.UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING;
34 import static org.openecomp.sdcrests.vsp.rest.exception.OrchestrationTemplateCandidateUploadManagerExceptionSupplier.vspUploadAlreadyInProgress;
36 import java.io.ByteArrayInputStream;
37 import java.io.FileInputStream;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Optional;
49 import java.util.UUID;
50 import javax.activation.DataHandler;
51 import javax.inject.Named;
52 import javax.ws.rs.core.Response;
53 import org.apache.commons.lang3.tuple.Pair;
54 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
55 import org.openecomp.sdc.activitylog.ActivityLogManager;
56 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
57 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
58 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
59 import org.openecomp.sdc.be.csar.storage.ArtifactInfo;
60 import org.openecomp.sdc.be.csar.storage.ArtifactStorageConfig;
61 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
62 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
63 import org.openecomp.sdc.be.csar.storage.StorageFactory;
64 import org.openecomp.sdc.be.csar.storage.exception.ArtifactStorageException;
65 import org.openecomp.sdc.common.util.ValidationUtils;
66 import org.openecomp.sdc.common.utils.SdcCommon;
67 import org.openecomp.sdc.datatypes.error.ErrorLevel;
68 import org.openecomp.sdc.datatypes.error.ErrorMessage;
69 import org.openecomp.sdc.logging.api.Logger;
70 import org.openecomp.sdc.logging.api.LoggerFactory;
71 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
72 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
73 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
74 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
75 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspUploadStatus;
77 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageProcessor;
78 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation.CnfPackageValidator;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
81 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
82 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
83 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
84 import org.openecomp.sdc.versioning.dao.types.Version;
85 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
86 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
87 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
88 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
89 import org.openecomp.sdcrests.vendorsoftwareproducts.types.VspUploadStatusDto;
90 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
91 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
92 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
93 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
94 import org.springframework.beans.factory.annotation.Autowired;
95 import org.springframework.context.annotation.Scope;
96 import org.springframework.stereotype.Service;
99 @Service("orchestrationTemplateCandidate")
100 @Scope(value = "prototype")
101 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
103 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
104 private final OrchestrationTemplateCandidateManager candidateManager;
105 private final VendorSoftwareProductManager vendorSoftwareProductManager;
106 private final ActivityLogManager activityLogManager;
107 private final OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager;
108 private final StorageFactory storageFactory;
111 public OrchestrationTemplateCandidateImpl(final OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager) {
112 this.candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
113 this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
114 this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
115 this.storageFactory = new StorageFactory();
116 this.orchestrationTemplateCandidateUploadManager = orchestrationTemplateCandidateUploadManager;
119 // Constructor used in test to avoid mock static
120 public OrchestrationTemplateCandidateImpl(final OrchestrationTemplateCandidateManager candidateManager,
121 final VendorSoftwareProductManager vendorSoftwareProductManager,
122 final ActivityLogManager activityLogManager,
123 final OrchestrationTemplateCandidateUploadManager orchestrationTemplateCandidateUploadManager,
124 final StorageFactory storageFactory) {
125 this.candidateManager = candidateManager;
126 this.vendorSoftwareProductManager = vendorSoftwareProductManager;
127 this.activityLogManager = activityLogManager;
128 this.storageFactory = storageFactory;
129 this.orchestrationTemplateCandidateUploadManager = orchestrationTemplateCandidateUploadManager;
133 public Response upload(String vspId, String versionId, final Attachment fileToUpload, final String user) {
134 LOGGER.debug("STARTED -> OrchestrationTemplateCandidateImpl.upload");
135 vspId = ValidationUtils.sanitizeInputString(vspId);
136 versionId = ValidationUtils.sanitizeInputString(versionId);
137 final Response response;
138 VspUploadStatusDto vspUploadStatus = null;
140 vspUploadStatus = getVspUploadStatus(vspId, versionId, user);
142 if (vspUploadStatus.getStatus() != VspUploadStatus.UPLOADING) {
143 throw vspUploadAlreadyInProgress(vspId, versionId).get();
145 final byte[] fileToUploadBytes;
146 final DataHandler dataHandler = fileToUpload.getDataHandler();
147 final var filename = ValidationUtils.sanitizeInputString(dataHandler.getName());
148 ArtifactInfo artifactInfo = null;
149 final ArtifactStorageManager artifactStorageManager = storageFactory.createArtifactStorageManager();
150 if (artifactStorageManager.isEnabled()) {
151 artifactInfo = handleArtifactStorage(vspId, versionId, filename, dataHandler, artifactStorageManager);
152 fileToUploadBytes = artifactInfo.getBytes();
154 fileToUploadBytes = fileToUpload.getObject(byte[].class);
157 vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInValidation(vspId, versionId, user);
158 final var onboardingPackageProcessor =
159 new OnboardingPackageProcessor(filename, fileToUploadBytes, new CnfPackageValidator(), artifactInfo);
160 final ErrorMessage[] errorMessages = onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]);
161 if (onboardingPackageProcessor.hasErrors()) {
162 orchestrationTemplateCandidateUploadManager
163 .putUploadAsFinished(vspId, versionId, vspUploadStatus.getLockId(), VspUploadStatus.ERROR, user);
164 return Response.status(NOT_ACCEPTABLE).entity(buildUploadResponseWithError(errorMessages)).build();
166 final var onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
167 if (onboardPackageInfo == null) {
168 final UploadFileResponseDto uploadFileResponseDto = buildUploadResponseWithError(
169 new ErrorMessage(ErrorLevel.ERROR, PACKAGE_PROCESS_ERROR.formatMessage(filename)));
170 orchestrationTemplateCandidateUploadManager
171 .putUploadAsFinished(vspId, versionId, vspUploadStatus.getLockId(), VspUploadStatus.ERROR, user);
172 return Response.ok(uploadFileResponseDto).build();
174 final var version = new Version(versionId);
175 final var vspDetails = vendorSoftwareProductManager.getVsp(vspId, version);
176 vspUploadStatus = orchestrationTemplateCandidateUploadManager.putUploadInProcessing(vspId, versionId, user);
177 response = processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages);
178 final UploadFileResponseDto entity = (UploadFileResponseDto) response.getEntity();
179 if (artifactStorageManager.isEnabled()) {
180 if (entity.getErrors().isEmpty()) {
181 artifactStorageManager.put(vspId, versionId + ".reduced", new ByteArrayInputStream(fileToUploadBytes));
183 artifactStorageManager.delete(artifactInfo);
186 orchestrationTemplateCandidateUploadManager
187 .putUploadAsFinished(vspId, versionId, vspUploadStatus.getLockId(), VspUploadStatus.SUCCESS, user);
188 } catch (final Exception ex) {
189 if (vspUploadStatus != null) {
190 orchestrationTemplateCandidateUploadManager
191 .putUploadAsFinished(vspId, versionId, vspUploadStatus.getLockId(), VspUploadStatus.ERROR, user);
195 LOGGER.debug("FINISHED -> OrchestrationTemplateCandidateImpl.upload");
199 private VspUploadStatusDto getVspUploadStatus(final String vspId, final String versionId, final String user) {
200 final Optional<VspUploadStatusDto> vspUploadStatusOpt =
201 orchestrationTemplateCandidateUploadManager.findLatestStatus(vspId, versionId, user);
202 if (vspUploadStatusOpt.isEmpty() || vspUploadStatusOpt.get().isComplete()) {
203 return orchestrationTemplateCandidateUploadManager.putUploadInProgress(vspId, versionId, user);
206 return vspUploadStatusOpt.get();
209 private ArtifactInfo handleArtifactStorage(final String vspId, final String versionId, final String filename,
210 final DataHandler artifactDataHandler,
211 final ArtifactStorageManager artifactStorageManager) {
212 final PackageSizeReducer packageSizeReducer = storageFactory.createPackageSizeReducer().orElse(null);
213 if (packageSizeReducer == null) {
214 throw new ArtifactStorageException(PACKAGE_REDUCER_NOT_CONFIGURED.getErrorMessage());
217 final Path tempArtifactPath;
219 final ArtifactStorageConfig storageConfiguration = artifactStorageManager.getStorageConfiguration();
221 final Path folder = Path.of(storageConfiguration.getTempPath()).resolve(vspId).resolve(versionId);
222 tempArtifactPath = folder.resolve(UUID.randomUUID().toString());
223 Files.createDirectories(folder);
224 LOGGER.debug("STARTED -> Transfer to '{}'", tempArtifactPath.toString());
225 try (final InputStream packageInputStream = artifactDataHandler.getInputStream();
226 final var fileOutputStream = new FileOutputStream(tempArtifactPath.toFile())) {
227 packageInputStream.transferTo(fileOutputStream);
229 LOGGER.debug("FINISHED -> Transfer to '{}'", tempArtifactPath.toString());
230 } catch (final Exception e) {
231 throw new ArtifactStorageException(UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename));
233 final ArtifactInfo artifactInfo;
234 try (final InputStream inputStream = new FileInputStream(tempArtifactPath.toFile())) {
235 artifactInfo = artifactStorageManager.upload(vspId, versionId, inputStream);
236 } catch (final Exception e) {
237 LOGGER.error("Package Size Reducer not configured", e);
238 throw new ArtifactStorageException(ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT.formatMessage(filename));
242 LOGGER.debug("STARTED -> reducing '{}'", tempArtifactPath);
243 artifactInfo.setBytes(packageSizeReducer.reduce(tempArtifactPath));
244 LOGGER.debug("FINISHED -> reducing '{}'", tempArtifactPath);
245 } catch (final Exception e) {
246 LOGGER.debug("ERROR -> reducing '{}'", tempArtifactPath, e);
247 throw new ArtifactStorageException(ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE.formatMessage(filename), e);
251 Files.delete(tempArtifactPath);
252 } catch (final Exception e) {
253 LOGGER.warn("Could not delete temporary package at '{}'", tempArtifactPath, e);
259 private Response processOnboardPackage(final OnboardPackageInfo onboardPackageInfo, final VspDetails vspDetails,
260 final ErrorMessage... errorMessages) {
261 final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
262 final UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
263 .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
264 if (errorMessages.length > 0) {
265 uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
267 return Response.ok(uploadFileResponseDto).build();
270 private Map<String, List<ErrorMessage>> getErrorMap(ErrorMessage[] errorMessages) {
271 final Map<String, List<ErrorMessage>> errorMap = new HashMap<>();
272 final List<ErrorMessage> errorMessageList = new ArrayList<>();
273 Collections.addAll(errorMessageList, errorMessages);
274 errorMap.put(SdcCommon.UPLOAD_FILE, errorMessageList);
278 private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
279 final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
280 uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
281 return uploadFileResponseDto;
285 public Response get(String vspId, String versionId, String user) throws IOException {
286 Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
288 if (zipFile.isPresent()) {
289 fileName = "Candidate." + zipFile.get().getLeft();
291 zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
292 if (zipFile.isEmpty()) {
293 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
294 getErrorWithParameters(NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), ""));
295 LOGGER.error(errorMessage.getMessage());
296 return Response.status(NOT_FOUND).build();
298 fileName = "Processed." + zipFile.get().getLeft();
300 Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
301 response.header("Content-Disposition", "attachment; filename=" + fileName);
302 return response.build();
306 public Response abort(String vspId, String versionId) {
307 candidateManager.abort(vspId, new Version(versionId));
308 return Response.ok().build();
312 public Response process(String vspId, String versionId, String user) {
313 Version version = new Version(versionId);
314 OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
315 activityLogManager.logActivity(new ActivityLogEntity(vspId, version, ActivityType.Upload_Network_Package, user, true, "", ""));
316 OrchestrationTemplateActionResponseDto responseDto = copyOrchestrationTemplateActionResponseToDto(response);
317 return Response.ok(responseDto).build();
321 public Response updateFilesDataStructure(String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user) {
322 FilesDataStructure fileDataStructure = copyFilesDataStructureDtoToFilesDataStructure(fileDataStructureDto);
323 ValidationResponse response = candidateManager.updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
324 if (!response.isValid()) {
325 return Response.status(EXPECTATION_FAILED)
326 .entity(new MapValidationResponseToDto().applyMapping(response, ValidationResponseDto.class))
329 return Response.ok(fileDataStructureDto).build();
333 public Response getFilesDataStructure(String vspId, String versionId, String user) {
334 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(vspId, new Version(versionId));
335 if (filesDataStructure.isEmpty()) {
336 filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId, new Version(versionId));
338 FileDataStructureDto fileDataStructureDto = filesDataStructure
339 .map(dataStructure -> new MapFilesDataStructureToDto().applyMapping(dataStructure, FileDataStructureDto.class))
340 .orElse(new FileDataStructureDto());
341 return Response.ok(fileDataStructureDto).build();
344 private OrchestrationTemplateActionResponseDto copyOrchestrationTemplateActionResponseToDto(OrchestrationTemplateActionResponse response) {
345 OrchestrationTemplateActionResponseDto result = new OrchestrationTemplateActionResponseDto();
346 result.setErrors(response.getErrors());
347 result.setFileNames(response.getFileNames());
348 result.setStatus(response.getStatus());
352 private FilesDataStructure copyFilesDataStructureDtoToFilesDataStructure(FileDataStructureDto fileDataStructureDto) {
353 FilesDataStructure filesDataStructure = new FilesDataStructure();
354 filesDataStructure.setArtifacts(fileDataStructureDto.getArtifacts());
355 filesDataStructure.setModules(fileDataStructureDto.getModules());
356 filesDataStructure.setNested(fileDataStructureDto.getNested());
357 filesDataStructure.setUnassigned(fileDataStructureDto.getUnassigned());
358 return filesDataStructure;