db0851cd5fd749e2a4b0ac98a73661e2a3aa5b3d
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
2
3 import org.apache.commons.lang3.tuple.Pair;
4 import org.openecomp.core.utilities.file.FileContentHandler;
5 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
6 import org.openecomp.sdc.common.errors.CoreException;
7 import org.openecomp.sdc.common.errors.Messages;
8 import org.openecomp.sdc.common.utils.CommonUtil;
9 import org.openecomp.sdc.common.utils.SdcCommon;
10 import org.openecomp.sdc.datatypes.error.ErrorLevel;
11 import org.openecomp.sdc.datatypes.error.ErrorMessage;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
14 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingManifest;
15 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25
26 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
27 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGBLE_FOLDERS;
28 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGIBLE_FILES;
29 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
30 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;
31
32 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
33         implements OrchestrationTemplateFileHandler {
34
35
36     @Override
37     public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
38                                                           byte[] uploadedFileData) {
39         FileContentHandler contentMap = null;
40         List<String> folderList = new ArrayList<>();
41         try {
42             Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip = CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
43             contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
44             folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
45         } catch (IOException exception) {
46             uploadFileResponse.addStructureError(
47                     SdcCommon.UPLOAD_FILE,
48                     new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
49         } catch (CoreException coreException) {
50             uploadFileResponse.addStructureError(
51                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
52         }
53         validateContent(uploadFileResponse, contentMap, folderList);
54         return Optional.ofNullable(contentMap);
55     }
56
57     private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, List<String> folderList) {
58         validateManifest(uploadFileResponse, contentMap);
59         validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
60         validateNoExtraFiles(uploadFileResponse, contentMap);
61         validateFolders(uploadFileResponse, folderList);
62     }
63
64     private void validateManifest(UploadFileResponse uploadFileResponse, FileContentHandler contentMap) {
65
66         if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)){
67             return;
68         }
69
70         try (InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
71
72             OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent);
73             if (!onboardingManifest.isValid()) {
74                 onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError(
75                         SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error)));
76             }
77
78         } catch (IOException e) {
79             // convert to runtime to keep the throws unchanged
80             throw new RuntimeException("Failed to validate manifest", e);
81         }
82     }
83
84     private void validateNoExtraFiles(UploadFileResponse uploadFileResponse,  FileContentHandler contentMap) {
85         List<String> unwantedFiles = contentMap.getFileList().stream()
86                 .filter(this::filterFiles).collect(Collectors.toList());
87         if (!unwantedFiles.isEmpty()) {
88             unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
89                     uploadFileResponse.addStructureError(
90                             SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
91                                     getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(),
92                                             unwantedFile))));
93         }
94     }
95
96     private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) {
97         List<String> filterResult = folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
98         if (!filterResult.isEmpty()) {
99             folderList.stream().filter(this::filterFolders).forEach( unwantedFolder ->
100                     uploadFileResponse.addStructureError(
101                             SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
102                                     getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
103                                             unwantedFolder))));
104
105         }
106     }
107     private boolean filterFiles(String inFileName) {
108         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
109         return !valid && filterFolders(inFileName);
110     }
111
112     private boolean filterFolders(String fileName) {
113         return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
114     }
115
116     private boolean validateFileExist(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, String fileName) {
117
118         boolean containsFile = contentMap.containsFile(fileName);
119         if (!containsFile) {
120             uploadFileResponse.addStructureError(
121                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
122                             getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
123         }
124         return containsFile;
125     }
126
127     @Override
128     protected boolean updateCandidateData(String vspId, String user, CandidateService candidateService,
129                                           VspDetails vspDetails, UploadFileResponse uploadFileResponse,
130                                           byte[] uploadedFileData, Optional<FileContentHandler> optionalContentMap) {
131         try {
132             candidateService.updateCandidateUploadData(new OrchestrationTemplateCandidateData(
133                     ByteBuffer.wrap(uploadedFileData), ""), vspDetails.getId());
134         } catch (Exception exception) {
135             logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
136                     getHandlerType().toString()), exception);
137             uploadFileResponse
138                     .addStructureError(
139                             SdcCommon.UPLOAD_FILE,
140                             new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
141
142             mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
143             return true;
144         }
145         return false;
146     }
147
148
149     @Override
150     protected OnboardingTypesEnum getHandlerType() {
151         return OnboardingTypesEnum.CSAR;
152     }
153
154     @Override
155     protected boolean isInvalidRawZipData(UploadFileResponse uploadFileResponse,
156                                           byte[] uploadedFileData, CandidateService candidateService) {
157         return super.isInvalidRawZipData(uploadFileResponse, uploadedFileData, candidateService);
158
159     }
160 }