2 * ============LICENSE_START=======================================================
3 * Modification Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * 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.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
22 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
23 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGBLE_FOLDERS;
24 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGIBLE_FILES;
25 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
26 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;
27 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
28 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
37 import java.util.stream.Collectors;
38 import org.openecomp.core.utilities.file.FileContentHandler;
39 import org.openecomp.sdc.common.errors.Messages;
40 import org.openecomp.sdc.common.utils.SdcCommon;
41 import org.openecomp.sdc.datatypes.error.ErrorLevel;
42 import org.openecomp.sdc.datatypes.error.ErrorMessage;
43 import org.openecomp.sdc.logging.api.Logger;
44 import org.openecomp.sdc.logging.api.LoggerFactory;
45 import org.openecomp.sdc.tosca.csar.Manifest;
46 import org.openecomp.sdc.tosca.csar.ONAPManifestOnboarding;
47 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
48 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
50 class ONAPCsarValidator implements Validator {
52 private static Logger logger = LoggerFactory.getLogger(ONAPCsarValidator.class);
53 private List<ErrorMessage> uploadFileErrors = new ArrayList<>();
56 public Map<String, List<ErrorMessage>> validateContent(final FileContentHandler contentHandler) {
57 Map<String, List<ErrorMessage>> errors = new HashMap<>();
58 validateManifest(contentHandler);
59 validateMetadata(contentHandler);
60 validateNoExtraFiles(contentHandler);
61 validateFolders(contentHandler.getFolderList());
62 if (uploadFileErrors == null || uploadFileErrors.isEmpty()) {
65 errors.put(SdcCommon.UPLOAD_FILE, uploadFileErrors);
69 private void validateMetadata(FileContentHandler contentMap) {
70 if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) {
71 try (InputStream metaFileContent = contentMap.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)) {
72 ToscaMetadata onboardingToscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metaFileContent);
73 String entryDefinitionsPath = onboardingToscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
74 if (entryDefinitionsPath != null) {
75 validateFileExist(contentMap, entryDefinitionsPath);
77 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
79 } catch (IOException exception) {
80 logger.error(exception.getMessage(), exception);
81 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
84 validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
88 private void validateManifest(final FileContentHandler contentMap) {
89 if (!validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
92 try (final InputStream fileContent = contentMap.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
93 final Manifest onboardingManifest = new ONAPManifestOnboarding();
94 onboardingManifest.parse(fileContent);
95 if (!onboardingManifest.isValid()) {
96 onboardingManifest.getErrors().forEach(error -> uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, error)));
98 } catch (final IOException ex) {
99 final String errorMessage = Messages.MANIFEST_UNEXPECTED_ERROR.formatMessage(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, ex.getMessage());
100 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, errorMessage));
101 logger.error(errorMessage, ex);
105 private void validateNoExtraFiles(FileContentHandler contentMap) {
106 List<String> unwantedFiles = contentMap.getFileList().stream().filter(this::filterFiles).collect(Collectors.toList());
107 if (!unwantedFiles.isEmpty()) {
108 unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile -> uploadFileErrors
109 .add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), unwantedFile))));
113 private void validateFolders(Set<String> folderList) {
114 List<String> filterResult = folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
115 if (!filterResult.isEmpty()) {
116 folderList.stream().filter(this::filterFolders).forEach(unwantedFolder -> uploadFileErrors.add(
117 new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(), unwantedFolder))));
121 private boolean filterFiles(String inFileName) {
122 boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
123 return !valid && filterFolders(inFileName);
126 private boolean filterFolders(String fileName) {
127 return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
130 private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
131 return contentMap.containsFile(fileName);
134 private boolean validateFileExist(FileContentHandler contentMap, String fileName) {
135 boolean containsFile = contentMap.containsFile(fileName);
138 .add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));