Fix package storage and reducer config reload
[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         final Path tempArtifactPath;
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             throw new ArtifactStorageException(UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename));
232         }
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));
239         }
240
241         try {
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);
248         }
249
250         try {
251             Files.delete(tempArtifactPath);
252         } catch (final Exception e) {
253             LOGGER.warn("Could not delete temporary package at '{}'", tempArtifactPath, e);
254         }
255
256         return artifactInfo;
257     }
258
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));
266         }
267         return Response.ok(uploadFileResponseDto).build();
268     }
269
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);
275         return errorMap;
276     }
277
278     private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
279         final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
280         uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
281         return uploadFileResponseDto;
282     }
283
284     @Override
285     public Response get(String vspId, String versionId, String user) throws IOException {
286         Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
287         String fileName;
288         if (zipFile.isPresent()) {
289             fileName = "Candidate." + zipFile.get().getLeft();
290         } else {
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();
297             }
298             fileName = "Processed." + zipFile.get().getLeft();
299         }
300         Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
301         response.header("Content-Disposition", "attachment; filename=" + fileName);
302         return response.build();
303     }
304
305     @Override
306     public Response abort(String vspId, String versionId) {
307         candidateManager.abort(vspId, new Version(versionId));
308         return Response.ok().build();
309     }
310
311     @Override
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();
318     }
319
320     @Override
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))
327                 .build();
328         }
329         return Response.ok(fileDataStructureDto).build();
330     }
331
332     @Override
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));
337         }
338         FileDataStructureDto fileDataStructureDto = filesDataStructure
339             .map(dataStructure -> new MapFilesDataStructureToDto().applyMapping(dataStructure, FileDataStructureDto.class))
340             .orElse(new FileDataStructureDto());
341         return Response.ok(fileDataStructureDto).build();
342     }
343
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());
349         return result;
350     }
351
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;
359     }
360 }