bdc422d580159879c83bac4d0353588f7d066092
[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.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
43 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
45 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
46 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
47 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
48 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
49 import org.springframework.context.annotation.Scope;
50 import org.springframework.stereotype.Service;
51
52 import javax.inject.Named;
53 import javax.ws.rs.core.Response;
54 import java.io.IOException;
55 import java.io.InputStream;
56 import java.lang.reflect.InvocationTargetException;
57 import java.util.Optional;
58
59 import static org.openecomp.core.utilities.file.FileUtils.getFileExtension;
60 import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName;
61 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
62
63 @Named
64 @Service("orchestrationTemplateCandidate")
65 @Scope(value = "prototype")
66 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
67   private static final Logger LOGGER =
68       LoggerFactory.getLogger(OrchestrationTemplateCandidateImpl.class);
69   private OrchestrationTemplateCandidateManager candidateManager =
70       OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
71   private VendorSoftwareProductManager vendorSoftwareProductManager = VspManagerFactory
72       .getInstance().createInterface();
73   private ActivityLogManager activityLogManager =
74           ActivityLogManagerFactory.getInstance().createInterface();
75
76   @Override
77   public Response upload(String vspId, String versionId, Attachment fileToUpload, String user) {
78
79     String filename = fileToUpload.getContentDisposition().getParameter("filename");
80     UploadFileResponse uploadFileResponse = candidateManager
81         .upload(vspId, new Version(versionId), fileToUpload.getObject(InputStream.class),
82             getFileExtension(filename), getNetworkPackageName(filename));
83
84     UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
85         .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
86
87     return Response.ok(uploadFileResponseDto).build();
88   }
89
90   @Override
91   public Response get(String vspId, String versionId, String user) throws IOException {
92     Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
93     String fileName = null;
94     if (zipFile.isPresent()) {
95       fileName = "Candidate." + zipFile.get().getLeft();
96     } else {
97       zipFile = vendorSoftwareProductManager.get(vspId, new Version((versionId)));
98
99       if (!zipFile.isPresent()) {
100         ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
101             getErrorWithParameters(
102                 Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
103                 ""));
104         LOGGER.error(errorMessage.getMessage());
105         return Response.status(Response.Status.NOT_FOUND).build();
106       }
107       fileName = "Processed." + zipFile.get().getLeft();
108     }
109     Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
110     response.header("Content-Disposition", "attachment; filename=" + fileName);
111     return response.build();
112   }
113
114   @Override
115   public Response abort(String vspId, String versionId) throws Exception {
116     candidateManager.abort(vspId, new Version(versionId));
117     return Response.ok().build();
118   }
119
120   @Override
121   public Response process(String vspId, String versionId, String user)
122       throws InvocationTargetException, IllegalAccessException {
123
124     Version version = new Version(versionId);
125     OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
126
127     activityLogManager.logActivity(new ActivityLogEntity(vspId, version,
128             ActivityType.Upload_Network_Package, user, true, "", ""));
129
130     OrchestrationTemplateActionResponseDto responseDto =
131         new OrchestrationTemplateActionResponseDto();
132     BeanUtils.copyProperties(responseDto, response);
133
134     return Response.ok(responseDto).build();
135   }
136
137   @Override
138   public Response updateFilesDataStructure(
139       String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user)
140       throws Exception {
141
142     FilesDataStructure fileDataStructure = new FilesDataStructure();
143     try {
144       BeanUtils.copyProperties(fileDataStructure, fileDataStructureDto);
145     } catch (IllegalAccessException | InvocationTargetException exception) {
146       String errorWithParameters = ErrorMessagesFormatBuilder
147           .getErrorWithParameters(Messages.MAPPING_OBJECTS_FAILURE.getErrorMessage(),
148               fileDataStructureDto.toString(), fileDataStructure.toString());
149       throw new Exception(errorWithParameters, exception);
150     }
151     ValidationResponse response = candidateManager
152         .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
153
154     if (!response.isValid()) {
155       return Response.status(Response.Status.EXPECTATION_FAILED).entity(
156           new MapValidationResponseToDto()
157               .applyMapping(response, ValidationResponseDto.class)).build();
158     }
159     return Response.ok(fileDataStructureDto).build();
160   }
161
162   @Override
163   public Response getFilesDataStructure(String vspId, String versionId, String user)
164       throws Exception {
165     Optional<FilesDataStructure> filesDataStructure =
166         candidateManager.getFilesDataStructure(vspId, new Version(versionId));
167     if (!filesDataStructure.isPresent()) {
168       filesDataStructure = vendorSoftwareProductManager.getOrchestrationTemplateStructure(vspId,
169           new Version(versionId));
170     }
171
172     FileDataStructureDto fileDataStructureDto =
173         filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
174             .applyMapping(dataStructure, FileDataStructureDto.class))
175             .orElse(new FileDataStructureDto());
176     return Response.ok(fileDataStructureDto).build();
177   }
178
179 }