d856cfc6e51f59b145783d3b7c8feae4521e1267
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdcrests.vsp.rest.services;
18
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;
47
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;
54
55 import static org.openecomp.core.utilities.file.FileUtils.getFileExtension;
56 import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName;
57
58 @Named
59 @Service("orchestrationTemplateCandidate")
60 @Scope(value = "prototype")
61 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
62
63   private OrchestrationTemplateCandidateManager candidateManager =
64       OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
65   private VendorSoftwareProductManager vendorSoftwareProductManager = VspManagerFactory
66       .getInstance().createInterface();
67   private ActivityLogManager activityLogManager =
68       ActivityLogManagerFactory.getInstance().createInterface();
69
70   @Override
71   public Response upload(String vspId, String versionId, Attachment fileToUpload, String user) {
72
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));
77
78     UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
79         .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
80
81     return Response.ok(uploadFileResponseDto).build();
82   }
83
84   @Override
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
88     if (!zipFile.isPresent()) {
89       return Response.status(Response.Status.NOT_FOUND).build();
90     }
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();
95   }
96
97   @Override
98   public Response abort(String vspId, String versionId) throws Exception {
99     candidateManager.abort(vspId, new Version(versionId));
100     return Response.ok().build();
101   }
102
103   @Override
104   public Response process(String vspId, String versionId, String user)
105       throws InvocationTargetException, IllegalAccessException {
106
107     Version version = new Version(versionId);
108     OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
109
110     activityLogManager.logActivity(new ActivityLogEntity(vspId, version,
111         ActivityType.Upload_Network_Package, user, true, "", ""));
112
113     OrchestrationTemplateActionResponseDto responseDto =
114         new OrchestrationTemplateActionResponseDto();
115     BeanUtils.copyProperties(responseDto, response);
116
117     return Response.ok(responseDto).build();
118   }
119
120   @Override
121   public Response updateFilesDataStructure(
122       String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user)
123       throws Exception {
124
125     FilesDataStructure fileDataStructure = new FilesDataStructure();
126     try {
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);
133     }
134     ValidationResponse response = candidateManager
135         .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
136
137     if (!response.isValid()) {
138       return Response.status(Response.Status.EXPECTATION_FAILED).entity(
139           new MapValidationResponseToDto()
140               .applyMapping(response, ValidationResponseDto.class)).build();
141     }
142     return Response.ok(fileDataStructureDto).build();
143   }
144
145   @Override
146   public Response getFilesDataStructure(String vspId, String versionId, String user)
147       throws Exception {
148     Optional<FilesDataStructure> filesDataStructure =
149         candidateManager.getFilesDataStructure(vspId, new Version(versionId));
150     if (!filesDataStructure.isPresent()) {
151       filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId,
152           new Version(versionId));
153     }
154
155     FileDataStructureDto fileDataStructureDto =
156         filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
157             .applyMapping(dataStructure, FileDataStructureDto.class))
158             .orElse(new FileDataStructureDto());
159     return Response.ok(fileDataStructureDto).build();
160   }
161
162 }