Removed reference to MDC for logging
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / services / OrchestrationTemplateCandidateImpl.java
1 package org.openecomp.sdcrests.vsp.rest.services;
2
3 import org.apache.commons.beanutils.BeanUtils;
4 import org.apache.commons.lang3.tuple.Pair;
5 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
6 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
7 import org.openecomp.sdc.activitylog.ActivityLogManager;
8 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
9 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
10 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
11 import org.openecomp.sdc.common.errors.Messages;
12 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
13 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
14 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
15 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
20 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
21 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
22 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
23 import org.openecomp.sdcrests.vsp.rest.OrchestrationTemplateCandidate;
24 import org.openecomp.sdcrests.vsp.rest.mapping.MapFilesDataStructureToDto;
25 import org.openecomp.sdcrests.vsp.rest.mapping.MapUploadFileResponseToUploadFileResponseDto;
26 import org.openecomp.sdcrests.vsp.rest.mapping.MapValidationResponseToDto;
27 import org.springframework.context.annotation.Scope;
28 import org.springframework.stereotype.Service;
29
30 import javax.inject.Named;
31 import javax.ws.rs.core.Response;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.lang.reflect.InvocationTargetException;
35 import java.util.Optional;
36
37 import static org.openecomp.core.utilities.file.FileUtils.getFileExtension;
38 import static org.openecomp.core.utilities.file.FileUtils.getNetworkPackageName;
39
40 @Named
41 @Service("orchestrationTemplateCandidate")
42 @Scope(value = "prototype")
43 public class OrchestrationTemplateCandidateImpl implements OrchestrationTemplateCandidate {
44
45   private OrchestrationTemplateCandidateManager candidateManager =
46       OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
47   private ActivityLogManager activityLogManager =
48       ActivityLogManagerFactory.getInstance().createInterface();
49
50   @Override
51   public Response upload(String vspId, String versionId, Attachment fileToUpload, String user) {
52
53     String filename = fileToUpload.getContentDisposition().getParameter("filename");
54     UploadFileResponse uploadFileResponse = candidateManager
55         .upload(vspId, new Version(versionId), fileToUpload.getObject(InputStream.class),
56             getFileExtension(filename), getNetworkPackageName(filename));
57
58     UploadFileResponseDto uploadFileResponseDto = new MapUploadFileResponseToUploadFileResponseDto()
59         .applyMapping(uploadFileResponse, UploadFileResponseDto.class);
60
61     return Response.ok(uploadFileResponseDto).build();
62   }
63
64   @Override
65   public Response get(String vspId, String versionId, String user) throws IOException {
66     Optional<Pair<String, byte[]>> zipFile = candidateManager.get(vspId, new Version(versionId));
67
68     if (!zipFile.isPresent()) {
69       return Response.status(Response.Status.NOT_FOUND).build();
70     }
71     Response.ResponseBuilder response = Response.ok(zipFile.get().getRight());
72     String filename = "Candidate." + zipFile.get().getLeft();
73     response.header("Content-Disposition", "attachment; filename=" + filename);
74     return response.build();
75   }
76
77   @Override
78   public Response process(String vspId, String versionId, String user)
79       throws InvocationTargetException, IllegalAccessException {
80
81     Version version = new Version(versionId);
82     OrchestrationTemplateActionResponse response = candidateManager.process(vspId, version);
83
84     activityLogManager.logActivity(new ActivityLogEntity(vspId, version,
85         ActivityType.Upload_Network_Package, user, true, "", ""));
86
87     OrchestrationTemplateActionResponseDto responseDto =
88         new OrchestrationTemplateActionResponseDto();
89     BeanUtils.copyProperties(responseDto, response);
90
91     return Response.ok(responseDto).build();
92   }
93
94   @Override
95   public Response updateFilesDataStructure(
96       String vspId, String versionId, FileDataStructureDto fileDataStructureDto, String user)
97       throws Exception {
98
99     FilesDataStructure fileDataStructure = new FilesDataStructure();
100     try {
101       BeanUtils.copyProperties(fileDataStructure, fileDataStructureDto);
102     } catch (IllegalAccessException | InvocationTargetException exception) {
103       String errorWithParameters = ErrorMessagesFormatBuilder
104           .getErrorWithParameters(Messages.MAPPING_OBJECTS_FAILURE.getErrorMessage(),
105               fileDataStructureDto.toString(), fileDataStructure.toString());
106       throw new Exception(errorWithParameters, exception);
107     }
108     ValidationResponse response = candidateManager
109         .updateFilesDataStructure(vspId, new Version(versionId), fileDataStructure);
110
111     if (!response.isValid()) {
112       return Response.status(Response.Status.EXPECTATION_FAILED).entity(
113           new MapValidationResponseToDto()
114               .applyMapping(response, ValidationResponseDto.class)).build();
115     }
116     return Response.ok(fileDataStructureDto).build();
117   }
118
119   @Override
120   public Response getFilesDataStructure(String vspId, String versionId, String user)
121       throws Exception {
122     Optional<FilesDataStructure> filesDataStructure =
123         candidateManager.getFilesDataStructure(vspId, new Version(versionId));
124
125     FileDataStructureDto fileDataStructureDto =
126         filesDataStructure.map(dataStructure -> new MapFilesDataStructureToDto()
127             .applyMapping(dataStructure, FileDataStructureDto.class))
128             .orElse(new FileDataStructureDto());
129     return Response.ok(fileDataStructureDto).build();
130   }
131 }