ceee5facd0361c32b2f9d42d7ff22add82b07377
[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
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
22
23 import java.util.Set;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.tosca.csar.Manifest;
32 import org.openecomp.sdc.tosca.csar.ONAPManifestOnboarding;
33 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
34 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.stream.Collectors;
42
43 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
44 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGBLE_FOLDERS;
45 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGIBLE_FILES;
46 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
47 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;
48 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
49 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
50
51 class ONAPCsarValidator implements Validator {
52
53     private static Logger logger = LoggerFactory.getLogger(ONAPCsarValidator.class);
54
55     private List<ErrorMessage> uploadFileErrors = new ArrayList<>();
56
57     @Override
58     public Map<String, List<ErrorMessage>> validateContent(final FileContentHandler contentHandler) {
59
60         Map<String, List<ErrorMessage>> errors = new HashMap<>();
61         validateManifest(contentHandler);
62         validateMetadata(contentHandler);
63         validateNoExtraFiles(contentHandler);
64         validateFolders(contentHandler.getFolderList());
65
66         if(uploadFileErrors == null || uploadFileErrors.isEmpty()){
67             return errors;
68         }
69         errors.put(SdcCommon.UPLOAD_FILE, uploadFileErrors);
70         return errors;
71     }
72
73     private void validateMetadata(FileContentHandler contentMap){
74         if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) {
75             try (InputStream metaFileContent = contentMap.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)) {
76
77                 ToscaMetadata onboardingToscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metaFileContent);
78                 String entryDefinitionsPath = onboardingToscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS);
79                 if (entryDefinitionsPath != null) {
80                     validateFileExist(contentMap, entryDefinitionsPath);
81                 } else {
82                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
83                             Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
84                 }
85             } catch (IOException exception) {
86                 logger.error(exception.getMessage(), exception);
87                 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
88                         Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
89             }
90         } else {
91             validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
92         }
93     }
94
95     private void validateManifest(FileContentHandler contentMap) {
96
97         if (!validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
98             return;
99         }
100
101         try (InputStream fileContent = contentMap.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
102
103             Manifest onboardingManifest = new ONAPManifestOnboarding();
104             onboardingManifest.parse(fileContent);
105             if (!onboardingManifest.isValid()) {
106                 onboardingManifest.getErrors().forEach(error -> uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
107                         error)));
108             }
109
110         } catch (IOException e) {
111             // convert to runtime to keep the throws unchanged
112             throw new RuntimeException("Failed to validateContent manifest", e);
113         }
114     }
115
116     private void validateNoExtraFiles(FileContentHandler contentMap) {
117         List<String> unwantedFiles = contentMap.getFileList().stream()
118                 .filter(this::filterFiles).collect(Collectors.toList());
119         if (!unwantedFiles.isEmpty()) {
120             unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
121                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
122                             getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), unwantedFile))));
123         }
124     }
125
126     private void validateFolders(Set<String> folderList) {
127         List<String> filterResult =
128                 folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
129         if (!filterResult.isEmpty()) {
130             folderList.stream().filter(this::filterFolders).forEach(unwantedFolder ->
131                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
132                             getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
133                                     unwantedFolder))));
134         }
135     }
136
137     private boolean filterFiles(String inFileName) {
138         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
139         return !valid && filterFolders(inFileName);
140     }
141
142     private boolean filterFolders(String fileName) {
143         return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
144     }
145
146     private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
147         return contentMap.containsFile(fileName);
148     }
149
150     private boolean validateFileExist(FileContentHandler contentMap, String fileName) {
151
152         boolean containsFile = contentMap.containsFile(fileName);
153         if (!containsFile) {
154             uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
155                     getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
156         }
157         return containsFile;
158     }
159 }