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.activitylog.ActivityLogManager;
24 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
25 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
26 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
27 import org.openecomp.sdc.common.errors.Messages;
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;
59 @Service("orchestrationTemplateCandidate")
60 @Scope(value = "prototype")
61 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
63 private OrchestrationTemplateCandidateManager candidateManager =
64 OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
65 private VendorSoftwareProductManager vendorSoftwareProductManager = VspManagerFactory
66 .getInstance().createInterface();
67 private ActivityLogManager activityLogManager =
68 ActivityLogManagerFactory.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));
88 if (!zipFile.isPresent()) {
89 return Response.status(Response.Status.NOT_FOUND).build();
91 Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
92 String filename = "Candidate." + zipFile.get().getLeft();
93 response.header("Content-Disposition", "attachment; filename=" + filename);
94 return response.build();
98 public Response abort(String vspId, String versionId) throws Exception {
99 candidateManager.abort(vspId, new Version(versionId));
100 return Response.ok().build();
104 public Response process(String vspId, String versionId, String user)
105 throws InvocationTargetException, IllegalAccessException {
107 Version version = new Version(versionId);
108 OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
110 activityLogManager.logActivity(new ActivityLogEntity(vspId, version,
111 ActivityType.Upload_Network_Package, user, true, "", ""));
113 OrchestrationTemplateActionResponseDto responseDto =
114 new OrchestrationTemplateActionResponseDto();
115 BeanUtils.copyProperties(responseDto, response);
117 return Response.ok(responseDto).build();
121 public Response updateFilesDataStructure(
122 String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user)
125 FilesDataStructure fileDataStructure = new FilesDataStructure();
127 BeanUtils.copyProperties(fileDataStructure, fileDataStructureDto);
128 } catch (IllegalAccessException | InvocationTargetException exception) {
129 String errorWithParameters = ErrorMessagesFormatBuilder
130 .getErrorWithParameters(Messages.MAPPING_OBJECTS_FAILURE.getErrorMessage(),
131 fileDataStructureDto.toString(), fileDataStructure.toString());
132 throw new Exception(errorWithParameters, exception);
134 ValidationResponse response = candidateManager
135 .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
137 if (!response.isValid()) {
138 return Response.status(Response.Status.EXPECTATION_FAILED).entity(
139 new MapValidationResponseToDto()
140 .applyMapping(response, ValidationResponseDto.class)).build();
142 return Response.ok(fileDataStructureDto).build();
146 public Response getFilesDataStructure(String vspId, String versionId, String user)
148 Optional<FilesDataStructure> filesDataStructure =
149 candidateManager.getFilesDataStructure(vspId, new Version(versionId));
150 if (!filesDataStructure.isPresent()) {
151 filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId,
152 new Version(versionId));
155 FileDataStructureDto fileDataStructureDto =
156 filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
157 .applyMapping(dataStructure, FileDataStructureDto.class))
158 .orElse(new FileDataStructureDto());
159 return Response.ok(fileDataStructureDto).build();