2 * Copyright © 2016-2018 European Support Limited
3 * Copyright © 2021 Nokia
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
17 * Modifications copyright (c) 2019 Nokia
18 * ================================================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
23 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Optional;
32 import javax.activation.DataHandler;
33 import javax.inject.Named;
34 import javax.ws.rs.core.Response;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
37 import org.openecomp.sdc.activitylog.ActivityLogManager;
38 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
39 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
40 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
41 import org.openecomp.sdc.common.errors.Messages;
42 import org.openecomp.sdc.common.utils.SdcCommon;
43 import org.openecomp.sdc.datatypes.error.ErrorLevel;
44 import org.openecomp.sdc.datatypes.error.ErrorMessage;
45 import org.openecomp.sdc.logging.api.Logger;
46 import org.openecomp.sdc.logging.api.LoggerFactory;
47 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
48 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
49 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
50 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
52 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageProcessor;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
58 import org.openecomp.sdc.versioning.dao.types.Version;
59 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
60 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
61 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
62 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
63 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
64 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
65 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
66 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
67 import org.springframework.context.annotation.Scope;
68 import org.springframework.stereotype.Service;
71 @Service("orchestrationTemplateCandidate")
72 @Scope(value = "prototype")
73 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
74 private static final Logger LOGGER =
75 LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
76 private final OrchestrationTemplateCandidateManager candidateManager;
78 private final VendorSoftwareProductManager vendorSoftwareProductManager;
79 private final ActivityLogManager activityLogManager;
82 public OrchestrationTemplateCandidateImpl() {
83 this.candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
84 this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
85 this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
88 // Constructor used in test to avoid mock static
89 public OrchestrationTemplateCandidateImpl(
90 OrchestrationTemplateCandidateManager candidateManager,
91 VendorSoftwareProductManager vendorSoftwareProductManager,
92 ActivityLogManager activityLogManager) {
93 this.candidateManager = candidateManager;
94 this.vendorSoftwareProductManager = vendorSoftwareProductManager;
95 this.activityLogManager = activityLogManager;
99 public Response upload(final String vspId, final String versionId,
100 final Attachment fileToUpload, final String user) {
101 final byte[] fileToUploadBytes = fileToUpload.getObject(byte[].class);
102 final DataHandler dataHandler = fileToUpload.getDataHandler();
103 final String filename = dataHandler.getName();
105 final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(filename, fileToUploadBytes);
106 if (onboardingPackageProcessor.hasErrors()) {
107 final UploadFileResponseDto uploadFileResponseDto =
108 buildUploadResponseWithError(onboardingPackageProcessor.getErrorMessages().toArray(new ErrorMessage[0]));
109 return Response.ok(uploadFileResponseDto).build();
112 final OnboardPackageInfo onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
114 if (onboardPackageInfo == null) {
115 final UploadFileResponseDto uploadFileResponseDto = buildUploadResponseWithError(
116 new ErrorMessage(ErrorLevel.ERROR, Messages.PACKAGE_PROCESS_ERROR.formatMessage(filename)));
117 return Response.ok(uploadFileResponseDto).build();
120 final VspDetails vspDetails = new VspDetails(vspId, new Version(versionId));
121 return processOnboardPackage(onboardPackageInfo, vspDetails);
124 private Response processOnboardPackage(final OnboardPackageInfo onboardPackageInfo, final VspDetails vspDetails) {
125 final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
126 final UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
127 .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
128 return Response.ok(uploadFileResponseDto).build();
131 private UploadFileResponseDto buildUploadResponseWithError(final ErrorMessage... errorMessages) {
132 final UploadFileResponseDto uploadFileResponseDto = new UploadFileResponseDto();
133 final Map<String, List<ErrorMessage>> errorMap = new HashMap<>();
134 final List<ErrorMessage> errorMessageList = new ArrayList<>();
135 Collections.addAll(errorMessageList, errorMessages);
136 errorMap.put(SdcCommon.UPLOAD_FILE, errorMessageList);
137 uploadFileResponseDto.setErrors(errorMap);
138 return uploadFileResponseDto;
142 public Response get(String vspId, String versionId, String user) throws IOException {
143 Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
145 if (zipFile.isPresent()) {
146 fileName = "Candidate." + zipFile.get().getLeft();
148 zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
150 if (!zipFile.isPresent()) {
151 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
152 getErrorWithParameters(
153 Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
155 LOGGER.error(errorMessage.getMessage());
156 return Response.status(Response.Status.NOT_FOUND).build();
158 fileName = "Processed." + zipFile.get().getLeft();
160 Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
161 response.header("Content-Disposition", "attachment; filename=" + fileName);
162 return response.build();
166 public Response abort(String vspId, String versionId) {
167 candidateManager.abort(vspId, new Version(versionId));
168 return Response.ok().build();
172 public Response process(String vspId, String versionId, String user) {
174 Version version = new Version(versionId);
175 OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
177 activityLogManager.logActivity(new ActivityLogEntity(vspId, version,
178 ActivityType.Upload_Network_Package, user, true, "", ""));
180 OrchestrationTemplateActionResponseDto responseDto = copyOrchestrationTemplateActionResponseToDto(response);
182 return Response.ok(responseDto).build();
186 public Response updateFilesDataStructure(
187 String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user) {
189 FilesDataStructure fileDataStructure = copyFilesDataStructureDtoToFilesDataStructure(fileDataStructureDto);
191 ValidationResponse response = candidateManager
192 .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
194 if (!response.isValid()) {
195 return Response.status(Response.Status.EXPECTATION_FAILED).entity(
196 new MapValidationResponseToDto()
197 .applyMapping(response, ValidationResponseDto.class)).build();
199 return Response.ok(fileDataStructureDto).build();
203 public Response getFilesDataStructure(String vspId, String versionId, String user) {
204 Optional<FilesDataStructure> filesDataStructure =
205 candidateManager.getFilesDataStructure(vspId, new Version(versionId));
206 if (!filesDataStructure.isPresent()) {
207 filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId,
208 new Version(versionId));
211 FileDataStructureDto fileDataStructureDto =
212 filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
213 .applyMapping(dataStructure, FileDataStructureDto.class))
214 .orElse(new FileDataStructureDto());
215 return Response.ok(fileDataStructureDto).build();
218 private OrchestrationTemplateActionResponseDto copyOrchestrationTemplateActionResponseToDto(OrchestrationTemplateActionResponse response){
219 OrchestrationTemplateActionResponseDto result = new OrchestrationTemplateActionResponseDto();
220 result.setErrors(response.getErrors());
221 result.setFileNames(response.getFileNames());
222 result.setStatus(response.getStatus());
226 private FilesDataStructure copyFilesDataStructureDtoToFilesDataStructure(FileDataStructureDto fileDataStructureDto){
227 FilesDataStructure filesDataStructure = new FilesDataStructure();
228 filesDataStructure.setArtifacts(fileDataStructureDto.getArtifacts());
229 filesDataStructure.setModules(fileDataStructureDto.getModules());
230 filesDataStructure.setNested(fileDataStructureDto.getNested());
231 filesDataStructure.setUnassigned(fileDataStructureDto.getUnassigned());
232 return filesDataStructure;