2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdcrests.vsp.rest.services;
19 import org.apache.commons.beanutils.BeanUtils;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
29 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
31 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
41 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
43 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
44 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
45 import org.springframework.context.annotation.Scope;
46 import org.springframework.stereotype.Service;
48 import javax.inject.Named;
49 import javax.ws.rs.core.Response;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.lang.reflect.InvocationTargetException;
53 import java.util.Optional;
55 import static org.openecomp.core.utilities.file.FileUtils.getFileExtension;
56 import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName;
57 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
60 @Service("orchestrationTemplateCandidate")
61 @Scope(value = "prototype")
62 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
63 private static final Logger LOGGER =
64 LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
65 private OrchestrationTemplateCandidateManager candidateManager =
66 OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
67 private VendorSoftwareProductManager vendorSoftwareProductManager = VspManagerFactory
68 .getInstance().createInterface();
71 public Response upload(String vspId, String versionId, Attachment fileToUpload, String user) {
73 String filename = fileToUpload.getContentDisposition().getParameter("filename");
74 UploadFileResponse uploadFileResponse = candidateManager
75 .upload(vspId, new Version(versionId), fileToUpload.getObject(InputStream.class),
76 getFileExtension(filename), getNetworkPackageName(filename));
78 UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
79 .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
81 return Response.ok(uploadFileResponseDto).build();
85 public Response get(String vspId, String versionId, String user) throws IOException {
86 Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
87 String fileName = null;
88 if (zipFile.isPresent()) {
89 fileName = "Candidate." + zipFile.get().getLeft();
91 zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
93 if (!zipFile.isPresent()) {
94 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
95 getErrorWithParameters(
96 Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
98 LOGGER.error(errorMessage.getMessage());
99 return Response.status(Response.Status.NOT_FOUND).build();
101 fileName = "Processed." + zipFile.get().getLeft();
103 Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
104 response.header("Content-Disposition", "attachment; filename=" + fileName);
105 return response.build();
109 public Response abort(String vspId, String versionId) throws Exception {
110 candidateManager.abort(vspId, new Version(versionId));
111 return Response.ok().build();
115 public Response process(String vspId, String versionId, String user)
116 throws InvocationTargetException, IllegalAccessException {
118 Version version = new Version(versionId);
119 OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
121 OrchestrationTemplateActionResponseDto responseDto =
122 new OrchestrationTemplateActionResponseDto();
123 BeanUtils.copyProperties(responseDto, response);
125 return Response.ok(responseDto).build();
129 public Response updateFilesDataStructure(
130 String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user)
133 FilesDataStructure fileDataStructure = new FilesDataStructure();
135 BeanUtils.copyProperties(fileDataStructure, fileDataStructureDto);
136 } catch (IllegalAccessException | InvocationTargetException exception) {
137 String errorWithParameters = ErrorMessagesFormatBuilder
138 .getErrorWithParameters(Messages.MAPPING_OBJECTS_FAILURE.getErrorMessage(),
139 fileDataStructureDto.toString(), fileDataStructure.toString());
140 throw new Exception(errorWithParameters, exception);
142 ValidationResponse response = candidateManager
143 .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
145 if (!response.isValid()) {
146 return Response.status(Response.Status.EXPECTATION_FAILED).entity(
147 new MapValidationResponseToDto()
148 .applyMapping(response, ValidationResponseDto.class)).build();
150 return Response.ok(fileDataStructureDto).build();
154 public Response getFilesDataStructure(String vspId, String versionId, String user)
156 Optional<FilesDataStructure> filesDataStructure =
157 candidateManager.getFilesDataStructure(vspId, new Version(versionId));
158 if (!filesDataStructure.isPresent()) {
159 filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId,
160 new Version(versionId));
163 FileDataStructureDto fileDataStructureDto =
164 filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
165 .applyMapping(dataStructure, FileDataStructureDto.class))
166 .orElse(new FileDataStructureDto());
167 return Response.ok(fileDataStructureDto).build();