eb78bf0059920ef9a7dd8d06652a31f6d1968bb7
[sdc.git] /
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.INTERNAL_SERVER_ERROR;
26 import static javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE;
27 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
28 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
29 import static org.openecomp.sdc.common.errors.Messages.ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT;
30 import static org.openecomp.sdc.common.errors.Messages.ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE;
31 import static org.openecomp.sdc.common.errors.Messages.EXTERNAL_CSAR_STORE_CONFIGURATION_FAILURE_MISSING_FULL_PATH;
32 import static org.openecomp.sdc.common.errors.Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST;
33 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
34 import static org.openecomp.sdc.common.errors.Messages.UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING;
35
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.file.Path;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Optional;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47 import javax.inject.Named;
48 import javax.ws.rs.core.Response;
49 import org.apache.commons.lang3.tuple.Pair;
50 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
51 import org.openecomp.sdc.activitylog.ActivityLogManager;
52 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
53 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
54 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
55 import org.openecomp.sdc.be.csar.storage.ArtifactInfo;
56 import org.openecomp.sdc.be.csar.storage.ArtifactStorageConfig;
57 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
58 import org.openecomp.sdc.be.csar.storage.CsarPackageReducerConfiguration;
59 import org.openecomp.sdc.be.csar.storage.CsarSizeReducer;
60 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
61 import org.openecomp.sdc.be.csar.storage.PersistentVolumeArtifactStorageConfig;
62 import org.openecomp.sdc.be.csar.storage.PersistentVolumeArtifactStorageManager;
63 import org.openecomp.sdc.be.exception.BusinessException;
64 import org.openecomp.sdc.common.CommonConfigurationManager;
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.impl.onboarding.OnboardingPackageProcessor;
77 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation.CnfPackageValidator;
78 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
81 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
82 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
83 import org.openecomp.sdc.versioning.dao.types.Version;
84 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
85 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
86 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
87 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
88 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
89 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
90 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
91 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
92 import org.springframework.context.annotation.Scope;
93 import org.springframework.stereotype.Service;
94
95 @Named
96 @Service("orchestrationTemplateCandidate")
97 @Scope(value = "prototype")
98 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
99
100     private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
101     private static final String EXTERNAL_CSAR_STORE = "externalCsarStore";
102     private final OrchestrationTemplateCandidateManager candidateManager;
103     private final VendorSoftwareProductManager vendorSoftwareProductManager;
104     private final ActivityLogManager activityLogManager;
105     private final ArtifactStorageManager artifactStorageManager;
106     private final PackageSizeReducer packageSizeReducer;
107
108     public OrchestrationTemplateCandidateImpl() {
109         this.candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
110         this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
111         this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
112         LOGGER.info("Instantiating artifactStorageManager");
113         this.artifactStorageManager = new PersistentVolumeArtifactStorageManager(readArtifactStorageConfiguration());
114         LOGGER.info("Instantiating packageSizeReducer");
115         this.packageSizeReducer = new CsarSizeReducer(readPackageReducerConfiguration());
116     }
117
118     // Constructor used in test to avoid mock static
119     public OrchestrationTemplateCandidateImpl(final OrchestrationTemplateCandidateManager candidateManager,
120                                               final VendorSoftwareProductManager vendorSoftwareProductManager,
121                                               final ActivityLogManager activityLogManager,
122                                               final ArtifactStorageManager artifactStorageManager,
123                                               final PackageSizeReducer packageSizeReducer) {
124         this.candidateManager = candidateManager;
125         this.vendorSoftwareProductManager = vendorSoftwareProductManager;
126         this.activityLogManager = activityLogManager;
127         this.artifactStorageManager = artifactStorageManager;
128         this.packageSizeReducer = packageSizeReducer;
129     }
130
131     private CsarPackageReducerConfiguration readPackageReducerConfiguration() {
132         final var commonConfigurationManager = CommonConfigurationManager.getInstance();
133         final List<String> foldersToStrip = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "foldersToStrip", new ArrayList<>());
134         final int sizeLimit = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "sizeLimit", 1000000);
135         final int thresholdEntries = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "thresholdEntries", 10000);
136         LOGGER.info("Folders to strip: '{}'", String.join(", ", foldersToStrip));
137         final Set<Path> foldersToStripPathSet = foldersToStrip.stream().map(Path::of).collect(Collectors.toSet());
138         return new CsarPackageReducerConfiguration(foldersToStripPathSet, sizeLimit, thresholdEntries);
139     }
140
141     private ArtifactStorageConfig readArtifactStorageConfiguration() {
142         final var commonConfigurationManager = CommonConfigurationManager.getInstance();
143         final boolean isEnabled = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "storeCsarsExternally", false);
144         LOGGER.info("ArtifactConfig.isEnabled: '{}'", isEnabled);
145         final String storagePathString = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "fullPath", null);
146         LOGGER.info("ArtifactConfig.storagePath: '{}'", storagePathString);
147         if (isEnabled && storagePathString == null) {
148             throw new OrchestrationTemplateCandidateException(EXTERNAL_CSAR_STORE_CONFIGURATION_FAILURE_MISSING_FULL_PATH.getErrorMessage());
149         }
150         final var storagePath = storagePathString == null ? null : Path.of(storagePathString);
151         return new PersistentVolumeArtifactStorageConfig(isEnabled, storagePath);
152     }
153
154     @Override
155     public Response upload(final String vspId, final String versionId, final Attachment fileToUpload, final String user) {
156         final byte[] fileToUploadBytes;
157         final var filename = ValidationUtils.sanitizeInputString(fileToUpload.getDataHandler().getName());
158         ArtifactInfo artifactInfo = null;
159         if (artifactStorageManager.isEnabled()) {
160             final InputStream packageInputStream;
161             try {
162                 packageInputStream = fileToUpload.getDataHandler().getInputStream();
163             } catch (final IOException e) {
164                 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
165                     new ErrorMessage(ErrorLevel.ERROR, UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename)))).build();
166             }
167             try {
168                 artifactInfo = artifactStorageManager.upload(vspId, versionId, packageInputStream);
169             } catch (final BusinessException e) {
170                 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
171                     new ErrorMessage(ErrorLevel.ERROR, ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT.formatMessage(filename)))).build();
172             }
173             try {
174                 fileToUploadBytes = packageSizeReducer.reduce(artifactInfo.getPath());
175             } catch (final BusinessException e) {
176                 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
177                         new ErrorMessage(ErrorLevel.ERROR, ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE.formatMessage(artifactInfo.getPath()))))
178                     .build();
179             }
180         } else {
181             fileToUploadBytes = fileToUpload.getObject(byte[].class);
182         }
183
184         final var onboardingPackageProcessor = new OnboardingPackageProcessor(filename, fileToUploadBytes, new CnfPackageValidator(), artifactInfo);
185         final ErrorMessage[] errorMessages = onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]);
186         if (onboardingPackageProcessor.hasErrors()) {
187             return Response.status(NOT_ACCEPTABLE).entity(buildUploadResponseWithError(errorMessages)).build();
188         }
189         final var onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
190         if (onboardPackageInfo == null) {
191             final UploadFileResponseDto uploadFileResponseDto = buildUploadResponseWithError(
192                 new ErrorMessage(ErrorLevel.ERROR, PACKAGE_PROCESS_ERROR.formatMessage(filename)));
193             return Response.ok(uploadFileResponseDto)
194                 .build();
195         }
196         final var version = new Version(ValidationUtils.sanitizeInputString(versionId));
197         final var vspDetails = new VspDetails(ValidationUtils.sanitizeInputString(vspId), version);
198         return processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages);
199     }
200
201     private Response processOnboardPackage(final OnboardPackageInfo onboardPackageInfo, final VspDetails vspDetails,
202                                            final ErrorMessage... errorMessages) {
203         final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
204         final UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
205             .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
206         if (errorMessages.length > 0) {
207             uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
208         }
209         return Response.ok(uploadFileResponseDto).build();
210     }
211
212     private Map<String, List<ErrorMessage>> getErrorMap(ErrorMessage[] errorMessages) {
213         final Map<String, List<ErrorMessage>> errorMap = new HashMap<>();
214         final List<ErrorMessage> errorMessageList = new ArrayList<>();
215         Collections.addAll(errorMessageList, errorMessages);
216         errorMap.put(SdcCommon.UPLOAD_FILE, errorMessageList);
217         return errorMap;
218     }
219
220     private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
221         final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
222         uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
223         return uploadFileResponseDto;
224     }
225
226     @Override
227     public Response get(String vspId, String versionId, String user) throws IOException {
228         Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
229         String fileName;
230         if (zipFile.isPresent()) {
231             fileName = "Candidate." + zipFile.get().getLeft();
232         } else {
233             zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
234             if (!zipFile.isPresent()) {
235                 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
236                     getErrorWithParameters(NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), ""));
237                 LOGGER.error(errorMessage.getMessage());
238                 return Response.status(NOT_FOUND).build();
239             }
240             fileName = "Processed." + zipFile.get().getLeft();
241         }
242         Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
243         response.header("Content-Disposition", "attachment; filename=" + fileName);
244         return response.build();
245     }
246
247     @Override
248     public Response abort(String vspId, String versionId) {
249         candidateManager.abort(vspId, new Version(versionId));
250         return Response.ok().build();
251     }
252
253     @Override
254     public Response process(String vspId, String versionId, String user) {
255         Version version = new Version(versionId);
256         OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
257         activityLogManager.logActivity(new ActivityLogEntity(vspId, version, ActivityType.Upload_Network_Package, user, true, "", ""));
258         OrchestrationTemplateActionResponseDto responseDto = copyOrchestrationTemplateActionResponseToDto(response);
259         return Response.ok(responseDto).build();
260     }
261
262     @Override
263     public Response updateFilesDataStructure(String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user) {
264         FilesDataStructure fileDataStructure = copyFilesDataStructureDtoToFilesDataStructure(fileDataStructureDto);
265         ValidationResponse response = candidateManager.updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
266         if (!response.isValid()) {
267             return Response.status(EXPECTATION_FAILED).entity(new MapValidationResponseToDto().applyMapping(response, ValidationResponseDto.class))
268                 .build();
269         }
270         return Response.ok(fileDataStructureDto).build();
271     }
272
273     @Override
274     public Response getFilesDataStructure(String vspId, String versionId, String user) {
275         Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(vspId, new Version(versionId));
276         if (!filesDataStructure.isPresent()) {
277             filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId, new Version(versionId));
278         }
279         FileDataStructureDto fileDataStructureDto = filesDataStructure
280             .map(dataStructure -> new MapFilesDataStructureToDto().applyMapping(dataStructure, FileDataStructureDto.class))
281             .orElse(new FileDataStructureDto());
282         return Response.ok(fileDataStructureDto).build();
283     }
284
285     private OrchestrationTemplateActionResponseDto copyOrchestrationTemplateActionResponseToDto(OrchestrationTemplateActionResponse response) {
286         OrchestrationTemplateActionResponseDto result = new OrchestrationTemplateActionResponseDto();
287         result.setErrors(response.getErrors());
288         result.setFileNames(response.getFileNames());
289         result.setStatus(response.getStatus());
290         return result;
291     }
292
293     private FilesDataStructure copyFilesDataStructureDtoToFilesDataStructure(FileDataStructureDto fileDataStructureDto) {
294         FilesDataStructure filesDataStructure = new FilesDataStructure();
295         filesDataStructure.setArtifacts(fileDataStructureDto.getArtifacts());
296         filesDataStructure.setModules(fileDataStructureDto.getModules());
297         filesDataStructure.setNested(fileDataStructureDto.getNested());
298         filesDataStructure.setUnassigned(fileDataStructureDto.getUnassigned());
299         return filesDataStructure;
300     }
301 }