acbb0cdd036f4f074688a44deee2a031733dde11
[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 / OrchestrationTemplateCandidateImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  * Copyright © 2021 Nokia
4  * Copyright © 2021 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  * ============LICENSE_END=========================================================
18  * Modifications copyright (c) 2019 Nokia
19  * Modifications copyright (c) 2021 Nordix Foundation
20  * ================================================================================
21  */
22 package org.openecomp.sdcrests.vsp.rest.services;
23
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;
35
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;
47 import java.util.Map;
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;
97
98 @Named
99 @Service("orchestrationTemplateCandidate")
100 @Scope(value = "prototype")
101 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
102
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;
109
110     @Autowired
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;
117     }
118
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;
130     }
131
132     @Override
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;
139         try {
140             vspUploadStatus = getVspUploadStatus(vspId, versionId, user);
141
142             if (vspUploadStatus.getStatus() != VspUploadStatus.UPLOADING) {
143                 throw vspUploadAlreadyInProgress(vspId, versionId).get();
144             }
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();
153             } else {
154                 fileToUploadBytes = fileToUpload.getObject(byte[].class);
155             }
156
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();
165             }
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();
173             }
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));
182                 } else {
183                     artifactStorageManager.delete(artifactInfo);
184                 }
185             }
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);
192             }
193             throw ex;
194         }
195         LOGGER.debug("FINISHED -> OrchestrationTemplateCandidateImpl.upload");
196         return response;
197     }
198
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);
204         }
205
206         return vspUploadStatusOpt.get();
207     }
208
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());
215         }
216
217         Path tempArtifactPath = null;
218         try {
219             final ArtifactStorageConfig storageConfiguration = artifactStorageManager.getStorageConfiguration();
220
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);
228             }
229             LOGGER.debug("FINISHED -> Transfer to '{}'", tempArtifactPath.toString());
230         } catch (final Exception e) {
231             deleteTempFile(tempArtifactPath);
232             throw new ArtifactStorageException(UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename));
233         }
234         final ArtifactInfo artifactInfo;
235         try (final InputStream inputStream = new FileInputStream(tempArtifactPath.toFile())) {
236             artifactInfo = artifactStorageManager.upload(vspId, versionId, inputStream);
237         } catch (final Exception e) {
238             deleteTempFile(tempArtifactPath);
239             LOGGER.error("Failed to upload artifact", e);
240             throw new ArtifactStorageException(ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT.formatMessage(filename));
241         }
242
243         try {
244             LOGGER.debug("STARTED -> reducing '{}'", tempArtifactPath);
245             artifactInfo.setBytes(packageSizeReducer.reduce(tempArtifactPath));
246             LOGGER.debug("FINISHED -> reducing '{}'", tempArtifactPath);
247         } catch (final Exception e) {
248             deleteTempFile(tempArtifactPath);
249             LOGGER.debug("ERROR -> reducing '{}'", tempArtifactPath, e);
250             throw new ArtifactStorageException(ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE.formatMessage(filename), e);
251         }
252
253         deleteTempFile(tempArtifactPath);
254
255         return artifactInfo;
256     }
257
258     private void deleteTempFile(final Path tempArtifactPath) {
259         if (Files.exists(tempArtifactPath)) {
260             try {
261                 Files.delete(tempArtifactPath);
262             } catch (final Exception e) {
263                 LOGGER.warn("Could not delete temporary package at '{}'", tempArtifactPath, e);
264             }
265         }
266     }
267
268     private Response processOnboardPackage(final OnboardPackageInfo onboardPackageInfo, final VspDetails vspDetails,
269                                            final ErrorMessage... errorMessages) {
270         final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
271         final UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
272             .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
273         if (errorMessages.length > 0) {
274             uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
275         }
276         return Response.ok(uploadFileResponseDto).build();
277     }
278
279     private Map<String, List<ErrorMessage>> getErrorMap(ErrorMessage[] errorMessages) {
280         final Map<String, List<ErrorMessage>> errorMap = new HashMap<>();
281         final List<ErrorMessage> errorMessageList = new ArrayList<>();
282         Collections.addAll(errorMessageList, errorMessages);
283         errorMap.put(SdcCommon.UPLOAD_FILE, errorMessageList);
284         return errorMap;
285     }
286
287     private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
288         final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
289         uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
290         return uploadFileResponseDto;
291     }
292
293     @Override
294     public Response get(String vspId, String versionId, String user) throws IOException {
295         Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
296         String fileName;
297         if (zipFile.isPresent()) {
298             fileName = "Candidate." + zipFile.get().getLeft();
299         } else {
300             zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
301             if (zipFile.isEmpty()) {
302                 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
303                     getErrorWithParameters(NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), ""));
304                 LOGGER.error(errorMessage.getMessage());
305                 return Response.status(NOT_FOUND).build();
306             }
307             fileName = "Processed." + zipFile.get().getLeft();
308         }
309         Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
310         response.header("Content-Disposition", "attachment; filename=" + fileName);
311         return response.build();
312     }
313
314     @Override
315     public Response abort(String vspId, String versionId) {
316         candidateManager.abort(vspId, new Version(versionId));
317         return Response.ok().build();
318     }
319
320     @Override
321     public Response process(String vspId, String versionId, String user) {
322         Version version = new Version(versionId);
323         OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
324         activityLogManager.logActivity(new ActivityLogEntity(vspId, version, ActivityType.Upload_Network_Package, user, true, "", ""));
325         OrchestrationTemplateActionResponseDto responseDto = copyOrchestrationTemplateActionResponseToDto(response);
326         return Response.ok(responseDto).build();
327     }
328
329     @Override
330     public Response updateFilesDataStructure(String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user) {
331         FilesDataStructure fileDataStructure = copyFilesDataStructureDtoToFilesDataStructure(fileDataStructureDto);
332         ValidationResponse response = candidateManager.updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
333         if (!response.isValid()) {
334             return Response.status(EXPECTATION_FAILED)
335                 .entity(new MapValidationResponseToDto().applyMapping(response, ValidationResponseDto.class))
336                 .build();
337         }
338         return Response.ok(fileDataStructureDto).build();
339     }
340
341     @Override
342     public Response getFilesDataStructure(String vspId, String versionId, String user) {
343         Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(vspId, new Version(versionId));
344         if (filesDataStructure.isEmpty()) {
345             filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId, new Version(versionId));
346         }
347         FileDataStructureDto fileDataStructureDto = filesDataStructure
348             .map(dataStructure -> new MapFilesDataStructureToDto().applyMapping(dataStructure, FileDataStructureDto.class))
349             .orElse(new FileDataStructureDto());
350         return Response.ok(fileDataStructureDto).build();
351     }
352
353     private OrchestrationTemplateActionResponseDto copyOrchestrationTemplateActionResponseToDto(OrchestrationTemplateActionResponse response) {
354         OrchestrationTemplateActionResponseDto result = new OrchestrationTemplateActionResponseDto();
355         result.setErrors(response.getErrors());
356         result.setFileNames(response.getFileNames());
357         result.setStatus(response.getStatus());
358         return result;
359     }
360
361     private FilesDataStructure copyFilesDataStructureDtoToFilesDataStructure(FileDataStructureDto fileDataStructureDto) {
362         FilesDataStructure filesDataStructure = new FilesDataStructure();
363         filesDataStructure.setArtifacts(fileDataStructureDto.getArtifacts());
364         filesDataStructure.setModules(fileDataStructureDto.getModules());
365         filesDataStructure.setNested(fileDataStructureDto.getNested());
366         filesDataStructure.setUnassigned(fileDataStructureDto.getUnassigned());
367         return filesDataStructure;
368     }
369 }