486970451f91127cd44d393528cb2703efa498d1
[sdc.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
21
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;
29
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;
35 import java.util.Map;
36 import java.util.Set;
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;
49
50 class ONAPCsarValidator implements Validator {
51
52     private static Logger logger = LoggerFactory.getLogger(ONAPCsarValidator.class);
53     private List<ErrorMessage> uploadFileErrors = new ArrayList<>();
54
55     @Override
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()) {
63             return errors;
64         }
65         errors.put(SdcCommon.UPLOAD_FILE, uploadFileErrors);
66         return errors;
67     }
68
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);
76                 } else {
77                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
78                 }
79             } catch (IOException exception) {
80                 logger.error(exception.getMessage(), exception);
81                 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
82             }
83         } else {
84             validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
85         }
86     }
87
88     private void validateManifest(final FileContentHandler contentMap) {
89         if (!validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
90             return;
91         }
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)));
97             }
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);
102         }
103     }
104
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))));
110         }
111     }
112
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))));
118         }
119     }
120
121     private boolean filterFiles(String inFileName) {
122         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
123         return !valid && filterFolders(inFileName);
124     }
125
126     private boolean filterFolders(String fileName) {
127         return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
128     }
129
130     private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
131         return contentMap.containsFile(fileName);
132     }
133
134     private boolean validateFileExist(FileContentHandler contentMap, String fileName) {
135         boolean containsFile = contentMap.containsFile(fileName);
136         if (!containsFile) {
137             uploadFileErrors
138                 .add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
139         }
140         return containsFile;
141     }
142 }