3 * Copyright (c) 2018 AT&T Intellectual Property.
5 * Modifications Copyright (c) 2018 Verizon Property.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
23 import org.apache.commons.lang3.tuple.Pair;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.CommonUtil;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
37 import org.openecomp.sdc.tosca.csar.Manifest;
38 import org.openecomp.sdc.tosca.csar.OnboardingManifest;
39 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.exceptions.OrchestrationTemplateHandlerException;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.nio.ByteBuffer;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Optional;
50 import java.util.stream.Collectors;
52 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
53 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGBLE_FOLDERS;
54 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGIBLE_FILES;
55 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
56 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;
57 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
58 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
60 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
61 implements OrchestrationTemplateFileHandler {
62 private static Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCSARHandler.class);
65 public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
66 byte[] uploadedFileData) {
67 FileContentHandler contentMap = null;
68 List<String> folderList = new ArrayList<>();
70 Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip =
71 CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
72 contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
73 folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
74 } catch (IOException exception) {
75 logger.error(exception.getMessage(), exception);
76 uploadFileResponse.addStructureError(
77 SdcCommon.UPLOAD_FILE,
78 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
79 } catch (CoreException coreException) {
80 logger.error(coreException.getMessage(), coreException);
81 uploadFileResponse.addStructureError(
82 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
84 validateContent(uploadFileResponse, contentMap, folderList);
85 return Optional.ofNullable(contentMap);
88 private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap,
89 List<String> folderList) {
90 validateManifest(uploadFileResponse, contentMap);
91 validateMetadata(uploadFileResponse, contentMap);
92 validateNoExtraFiles(uploadFileResponse, contentMap);
93 validateFolders(uploadFileResponse, folderList);
96 private void validateMetadata(UploadFileResponse uploadFileResponse,
97 FileContentHandler contentMap){
98 if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) {
99 try (InputStream metaFileContent = contentMap.getFileContent(TOSCA_META_PATH_FILE_NAME)) {
101 ToscaMetadata onboardingToscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metaFileContent);
102 String entryDefinitionsPath = onboardingToscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS);
103 if (entryDefinitionsPath != null) {
104 validateFileExist(uploadFileResponse, contentMap, entryDefinitionsPath);
106 uploadFileResponse.addStructureError(
107 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
108 Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
110 } catch (IOException exception) {
111 logger.error(exception.getMessage(), exception);
112 uploadFileResponse.addStructureError(
113 SdcCommon.UPLOAD_FILE,
114 new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
117 validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
121 private void validateManifest(UploadFileResponse uploadFileResponse,
122 FileContentHandler contentMap) {
124 if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
128 try (InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
130 Manifest onboardingManifest = OnboardingManifest.parse(fileContent);
131 if (!onboardingManifest.isValid()) {
132 onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError(
133 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error)));
136 } catch (IOException e) {
137 // convert to runtime to keep the throws unchanged
138 throw new OrchestrationTemplateHandlerException("Failed to validate manifest", e);
142 private void validateNoExtraFiles(UploadFileResponse uploadFileResponse,
143 FileContentHandler contentMap) {
144 List<String> unwantedFiles = contentMap.getFileList().stream()
145 .filter(this::filterFiles).collect(Collectors.toList());
146 if (!unwantedFiles.isEmpty()) {
147 unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
148 uploadFileResponse.addStructureError(
149 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
150 getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(),
155 private void validateFolders(UploadFileResponse uploadFileResponse, List<String> folderList) {
156 List<String> filterResult =
157 folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
158 if (!filterResult.isEmpty()) {
159 folderList.stream().filter(this::filterFolders).forEach(unwantedFolder ->
160 uploadFileResponse.addStructureError(
161 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
162 getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
168 private boolean filterFiles(String inFileName) {
169 boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
170 return !valid && filterFolders(inFileName);
173 private boolean filterFolders(String fileName) {
174 return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
177 private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
178 return contentMap.containsFile(fileName);
181 private boolean validateFileExist(UploadFileResponse uploadFileResponse,
182 FileContentHandler contentMap, String fileName) {
184 boolean containsFile = contentMap.containsFile(fileName);
186 uploadFileResponse.addStructureError(
187 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
188 getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
194 protected boolean updateCandidateData(VspDetails vspDetails, byte[] uploadedFileData,
195 FileContentHandler contentMap,
196 String fileSuffix, String networkPackageName,
197 CandidateService candidateService,
198 UploadFileResponse uploadFileResponse) {
200 candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
201 new OrchestrationTemplateCandidateData(ByteBuffer.wrap(uploadedFileData), "", fileSuffix,
202 networkPackageName));
203 } catch (Exception exception) {
204 logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
205 getHandlerType().toString()), exception);
206 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
207 new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
215 protected OnboardingTypesEnum getHandlerType() {
216 return OnboardingTypesEnum.CSAR;