1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
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;
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;
26 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
27 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
29 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
30 implements OrchestrationTemplateFileHandler {
34 public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
35 byte[] uploadedFileData) {
36 FileContentHandler contentMap = null;
37 List<String> folderList = new ArrayList<>();
39 Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip =
40 CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
41 contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
42 folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
43 } catch (IOException exception) {
44 uploadFileResponse.addStructureError(
45 SdcCommon.UPLOAD_FILE,
46 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
47 } catch (CoreException coreException) {
48 uploadFileResponse.addStructureError(
49 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
51 validateContent(uploadFileResponse, contentMap, folderList);
52 return Optional.ofNullable(contentMap);
55 private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap,
56 List<String> folderList) {
57 validateManifest(uploadFileResponse, contentMap);
58 validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
59 validateNoExtraFiles(uploadFileResponse, contentMap);
60 validateFolders(uploadFileResponse, folderList);
63 private void validateManifest(UploadFileResponse uploadFileResponse,
64 FileContentHandler contentMap) {
66 if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
70 try (InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
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)));
78 } catch (IOException e) {
79 // convert to runtime to keep the throws unchanged
80 throw new RuntimeException("Failed to validate manifest", e);
84 private void validateNoExtraFiles(UploadFileResponse uploadFileResponse,
85 FileContentHandler contentMap) {
86 List<String> unwantedFiles = contentMap.getFileList().stream()
87 .filter(this::filterFiles).collect(Collectors.toList());
88 if (!unwantedFiles.isEmpty()) {
89 unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
90 uploadFileResponse.addStructureError(
91 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
92 getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(),
97 private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) {
98 List<String> filterResult =
99 folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
100 if (!filterResult.isEmpty()) {
101 folderList.stream().filter(this::filterFolders).forEach(unwantedFolder ->
102 uploadFileResponse.addStructureError(
103 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
104 getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
110 private boolean filterFiles(String inFileName) {
111 boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
112 return !valid && filterFolders(inFileName);
115 private boolean filterFolders(String fileName) {
116 return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
119 private boolean validateFileExist(UploadFileResponse uploadFileResponse,
120 FileContentHandler contentMap, String fileName) {
122 boolean containsFile = contentMap.containsFile(fileName);
124 uploadFileResponse.addStructureError(
125 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
126 getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
132 protected boolean updateCandidateData(VspDetails vspDetails, byte[] uploadedFileData,
133 FileContentHandler contentMap,
134 String fileSuffix, String networkPackageName,
135 CandidateService candidateService,
136 UploadFileResponse uploadFileResponse) {
138 candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
139 new OrchestrationTemplateCandidateData(ByteBuffer.wrap(uploadedFileData), "", fileSuffix,
140 networkPackageName));
141 } catch (Exception exception) {
142 logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
143 getHandlerType().toString()), exception);
144 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
145 new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
153 protected OnboardingTypesEnum getHandlerType() {
154 return OnboardingTypesEnum.CSAR;
158 protected boolean isInvalidRawZipData(String fileSuffix,
159 UploadFileResponse uploadFileResponse,
160 byte[] uploadedFileData,
161 CandidateService candidateService) {
162 return super.isInvalidRawZipData(fileSuffix, uploadFileResponse, uploadedFileData, candidateService);