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.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.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST;
32 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
33 import static org.openecomp.sdc.common.errors.Messages.UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING;
35 import java.io.ByteArrayInputStream;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.Optional;
47 import java.util.UUID;
48 import javax.activation.DataHandler;
49 import javax.inject.Named;
50 import javax.ws.rs.core.Response;
51 import org.apache.commons.lang3.tuple.Pair;
52 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
53 import org.openecomp.sdc.activitylog.ActivityLogManager;
54 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
55 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
56 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
57 import org.openecomp.sdc.be.csar.storage.ArtifactInfo;
58 import org.openecomp.sdc.be.csar.storage.ArtifactStorageConfig;
59 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
60 import org.openecomp.sdc.be.csar.storage.PackageSizeReducer;
61 import org.openecomp.sdc.be.csar.storage.StorageFactory;
62 import org.openecomp.sdc.common.util.ValidationUtils;
63 import org.openecomp.sdc.common.utils.SdcCommon;
64 import org.openecomp.sdc.datatypes.error.ErrorLevel;
65 import org.openecomp.sdc.datatypes.error.ErrorMessage;
66 import org.openecomp.sdc.logging.api.Logger;
67 import org.openecomp.sdc.logging.api.LoggerFactory;
68 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
69 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
70 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
71 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
73 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageProcessor;
74 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation.CnfPackageValidator;
75 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
76 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
77 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
78 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
80 import org.openecomp.sdc.versioning.dao.types.Version;
81 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
82 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
83 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
84 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
85 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
86 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
87 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
88 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
89 import org.springframework.context.annotation.Scope;
90 import org.springframework.stereotype.Service;
93 @Service("orchestrationTemplateCandidate")
94 @Scope(value = "prototype")
95 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
97 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
98 private final OrchestrationTemplateCandidateManager candidateManager;
99 private final VendorSoftwareProductManager vendorSoftwareProductManager;
100 private final ActivityLogManager activityLogManager;
101 private final ArtifactStorageManager artifactStorageManager;
102 private final PackageSizeReducer packageSizeReducer;
104 public OrchestrationTemplateCandidateImpl() {
105 this.candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
106 this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
107 this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
108 LOGGER.info("Instantiating artifactStorageManager");
109 final StorageFactory storageFactory = new StorageFactory();
110 this.artifactStorageManager = storageFactory.createArtifactStorageManager();
111 LOGGER.info("Instantiating packageSizeReducer");
112 this.packageSizeReducer = storageFactory.createPackageSizeReducer().orElse(null);
115 // Constructor used in test to avoid mock static
116 public OrchestrationTemplateCandidateImpl(final OrchestrationTemplateCandidateManager candidateManager,
117 final VendorSoftwareProductManager vendorSoftwareProductManager,
118 final ActivityLogManager activityLogManager,
119 final ArtifactStorageManager artifactStorageManager,
120 final PackageSizeReducer packageSizeReducer) {
121 this.candidateManager = candidateManager;
122 this.vendorSoftwareProductManager = vendorSoftwareProductManager;
123 this.activityLogManager = activityLogManager;
124 this.artifactStorageManager = artifactStorageManager;
125 this.packageSizeReducer = packageSizeReducer;
129 public Response upload(String vspId, String versionId, final Attachment fileToUpload, final String user) {
130 vspId = ValidationUtils.sanitizeInputString(vspId);
131 versionId = ValidationUtils.sanitizeInputString(versionId);
132 final byte[] fileToUploadBytes;
133 final DataHandler dataHandler = fileToUpload.getDataHandler();
134 final var filename = ValidationUtils.sanitizeInputString(dataHandler.getName());
135 ArtifactInfo artifactInfo = null;
136 if (artifactStorageManager.isEnabled()) {
137 final Path tempArtifactPath;
139 final ArtifactStorageConfig storageConfiguration = artifactStorageManager.getStorageConfiguration();
141 final Path folder = Path.of(storageConfiguration.getTempPath()).resolve(vspId).resolve(versionId);
142 tempArtifactPath = folder.resolve(UUID.randomUUID().toString());
143 Files.createDirectories(folder);
144 try (final InputStream packageInputStream = dataHandler.getInputStream();
145 final var fileOutputStream = new FileOutputStream(tempArtifactPath.toFile())) {
146 packageInputStream.transferTo(fileOutputStream);
148 } catch (final Exception e) {
149 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
150 new ErrorMessage(ErrorLevel.ERROR, UNEXPECTED_PROBLEM_HAPPENED_WHILE_GETTING.formatMessage(filename)))).build();
152 try (final InputStream inputStream = Files.newInputStream(tempArtifactPath)) {
153 artifactInfo = artifactStorageManager.upload(vspId, versionId, inputStream);
154 } catch (final Exception e) {
155 LOGGER.error("Package Size Reducer not configured", e);
156 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
157 new ErrorMessage(ErrorLevel.ERROR, ERROR_HAS_OCCURRED_WHILE_PERSISTING_THE_ARTIFACT.formatMessage(filename)))).build();
160 fileToUploadBytes = packageSizeReducer.reduce(tempArtifactPath);
161 Files.delete(tempArtifactPath);
162 } catch (final Exception e) {
163 LOGGER.error("Package Size Reducer not configured", e);
164 return Response.status(INTERNAL_SERVER_ERROR).entity(buildUploadResponseWithError(
165 new ErrorMessage(ErrorLevel.ERROR,
166 ERROR_HAS_OCCURRED_WHILE_REDUCING_THE_ARTIFACT_SIZE.formatMessage(tempArtifactPath.toString())))).build();
169 fileToUploadBytes = fileToUpload.getObject(byte[].class);
172 final var onboardingPackageProcessor = new OnboardingPackageProcessor(filename, fileToUploadBytes, new CnfPackageValidator(), artifactInfo);
173 final ErrorMessage[] errorMessages = onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]);
174 if (onboardingPackageProcessor.hasErrors()) {
175 return Response.status(NOT_ACCEPTABLE).entity(buildUploadResponseWithError(errorMessages)).build();
177 final var onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
178 if (onboardPackageInfo == null) {
179 final UploadFileResponseDto uploadFileResponseDto = buildUploadResponseWithError(
180 new ErrorMessage(ErrorLevel.ERROR, PACKAGE_PROCESS_ERROR.formatMessage(filename)));
181 return Response.ok(uploadFileResponseDto).build();
183 final var version = new Version(versionId);
184 final var vspDetails = vendorSoftwareProductManager.getVsp(vspId, version);
185 final Response response = processOnboardPackage(onboardPackageInfo, vspDetails, errorMessages);
186 final UploadFileResponseDto entity = (UploadFileResponseDto) response.getEntity();
187 if (artifactStorageManager.isEnabled()) {
188 if (!entity.getErrors().isEmpty()) {
189 artifactStorageManager.delete(artifactInfo);
191 artifactStorageManager.put(vspId, versionId + ".reduced", new ByteArrayInputStream(fileToUploadBytes));
197 private Response processOnboardPackage(final OnboardPackageInfo onboardPackageInfo, final VspDetails vspDetails,
198 final ErrorMessage... errorMessages) {
199 final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
200 final UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
201 .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
202 if (errorMessages.length > 0) {
203 uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
205 return Response.ok(uploadFileResponseDto).build();
208 private Map<String, List<ErrorMessage>> getErrorMap(ErrorMessage[] errorMessages) {
209 final Map<String, List<ErrorMessage>> errorMap = new HashMap<>();
210 final List<ErrorMessage> errorMessageList = new ArrayList<>();
211 Collections.addAll(errorMessageList, errorMessages);
212 errorMap.put(SdcCommon.UPLOAD_FILE, errorMessageList);
216 private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
217 final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
218 uploadFileResponseDto.setErrors(getErrorMap(errorMessages));
219 return uploadFileResponseDto;
223 public Response get(String vspId, String versionId, String user) throws IOException {
224 Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
226 if (zipFile.isPresent()) {
227 fileName = "Candidate." + zipFile.get().getLeft();
229 zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
230 if (!zipFile.isPresent()) {
231 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
232 getErrorWithParameters(NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), ""));
233 LOGGER.error(errorMessage.getMessage());
234 return Response.status(NOT_FOUND).build();
236 fileName = "Processed." + zipFile.get().getLeft();
238 Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
239 response.header("Content-Disposition", "attachment; filename=" + fileName);
240 return response.build();
244 public Response abort(String vspId, String versionId) {
245 candidateManager.abort(vspId, new Version(versionId));
246 return Response.ok().build();
250 public Response process(String vspId, String versionId, String user) {
251 Version version = new Version(versionId);
252 OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
253 activityLogManager.logActivity(new ActivityLogEntity(vspId, version, ActivityType.Upload_Network_Package, user, true, "", ""));
254 OrchestrationTemplateActionResponseDto responseDto = copyOrchestrationTemplateActionResponseToDto(response);
255 return Response.ok(responseDto).build();
259 public Response updateFilesDataStructure(String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user) {
260 FilesDataStructure fileDataStructure = copyFilesDataStructureDtoToFilesDataStructure(fileDataStructureDto);
261 ValidationResponse response = candidateManager.updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
262 if (!response.isValid()) {
263 return Response.status(EXPECTATION_FAILED)
264 .entity(new MapValidationResponseToDto().applyMapping(response, ValidationResponseDto.class))
267 return Response.ok(fileDataStructureDto).build();
271 public Response getFilesDataStructure(String vspId, String versionId, String user) {
272 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(vspId, new Version(versionId));
273 if (!filesDataStructure.isPresent()) {
274 filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId, new Version(versionId));
276 FileDataStructureDto fileDataStructureDto = filesDataStructure
277 .map(dataStructure -> new MapFilesDataStructureToDto().applyMapping(dataStructure, FileDataStructureDto.class))
278 .orElse(new FileDataStructureDto());
279 return Response.ok(fileDataStructureDto).build();
282 private OrchestrationTemplateActionResponseDto copyOrchestrationTemplateActionResponseToDto(OrchestrationTemplateActionResponse response) {
283 OrchestrationTemplateActionResponseDto result = new OrchestrationTemplateActionResponseDto();
284 result.setErrors(response.getErrors());
285 result.setFileNames(response.getFileNames());
286 result.setStatus(response.getStatus());
290 private FilesDataStructure copyFilesDataStructureDtoToFilesDataStructure(FileDataStructureDto fileDataStructureDto) {
291 FilesDataStructure filesDataStructure = new FilesDataStructure();
292 filesDataStructure.setArtifacts(fileDataStructureDto.getArtifacts());
293 filesDataStructure.setModules(fileDataStructureDto.getModules());
294 filesDataStructure.setNested(fileDataStructureDto.getNested());
295 filesDataStructure.setUnassigned(fileDataStructureDto.getUnassigned());
296 return filesDataStructure;