Create new VSP, onboard from TOSCA file - UI
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / OrchestrationTemplateCSARHandler.java
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 import java.util.stream.Stream;
26
27 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
28 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
29 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
30         implements OrchestrationTemplateFileHandler {
31
32
33     @Override
34     public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
35                                                           byte[] uploadedFileData) {
36         FileContentHandler contentMap = null;
37         List<String> folderList = new ArrayList<>();
38         try {
39             Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip = CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
40             contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
41             folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
42         } catch (IOException exception) {
43             uploadFileResponse.addStructureError(
44                     SdcCommon.UPLOAD_FILE,
45                     new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
46         } catch (CoreException coreException) {
47             uploadFileResponse.addStructureError(
48                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
49         }
50         validateContent(uploadFileResponse, contentMap, folderList);
51         return Optional.ofNullable(contentMap);
52     }
53
54     private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, List<String> folderList) {
55         validateManifest(uploadFileResponse, contentMap);
56         validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
57         validateNoExtraFiles(uploadFileResponse, contentMap);
58         validateFolders(uploadFileResponse, folderList);
59     }
60
61     private void validateManifest(UploadFileResponse uploadFileResponse, FileContentHandler contentMap) {
62         if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)){
63             return;
64         }
65         InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME);
66         OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent);
67         if (!onboardingManifest.isValid()){
68             onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError(
69                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error)));
70         }
71     }
72
73     private void validateNoExtraFiles(UploadFileResponse uploadFileResponse,  FileContentHandler contentMap) {
74         List<String> unwantedFiles = contentMap.getFileList().stream()
75                 .filter(this::filterFiles).collect(Collectors.toList());
76         if (!unwantedFiles.isEmpty()) {
77             unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
78                     uploadFileResponse.addStructureError(
79                             SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
80                                     getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(),
81                                             unwantedFile))));
82
83           ;
84         }
85     }
86
87     private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) {
88         List<String> filterResult = folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
89         if (!filterResult.isEmpty()) {
90             folderList.stream().filter(this::filterFolders).forEach( unwantedFolder ->
91                     uploadFileResponse.addStructureError(
92                             SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
93                                     getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
94                                             unwantedFolder))));
95
96         }
97     }
98     private boolean filterFiles(String inFileName) {
99         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
100         if (valid){
101             return !valid;
102         }
103         return filterFolders(inFileName);
104     }
105
106     private boolean filterFolders(String fileName) {
107         return !ELIGBLE_FOLDERS.stream().anyMatch(dirName -> fileName.startsWith(dirName));
108     }
109
110     private boolean validateFileExist(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, String fileName) {
111
112         boolean containsFile = contentMap.containsFile(fileName);
113         if (!containsFile) {
114             uploadFileResponse.addStructureError(
115                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
116                             getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
117         }
118         return containsFile;
119     }
120
121     @Override
122     protected boolean updateCandidateData(String vspId, String user, CandidateService candidateService,
123                                           VspDetails vspDetails, UploadFileResponse uploadFileResponse,
124                                           byte[] uploadedFileData, Optional<FileContentHandler> optionalContentMap) {
125         try {
126             candidateService.updateCandidateUploadData(new OrchestrationTemplateCandidateData(
127                     ByteBuffer.wrap(uploadedFileData), ""), vspDetails.getId());
128         } catch (Exception exception) {
129             logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
130                     getHandlerType().toString()), exception);
131             uploadFileResponse
132                     .addStructureError(
133                             SdcCommon.UPLOAD_FILE,
134                             new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
135
136             mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
137             return true;
138         }
139         return false;
140     }
141
142
143     @Override
144     protected OnboardingTypesEnum getHandlerType() {
145         return OnboardingTypesEnum.CSAR;
146     }
147
148     @Override
149     protected boolean isInvalidRawZipData(UploadFileResponse uploadFileResponse,
150                                           byte[] uploadedFileData, CandidateService candidateService) {
151         return super.isInvalidRawZipData(uploadFileResponse, uploadedFileData, candidateService);
152
153     }
154 }