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