6597beb9f5dc4411a552913ee189cf15ffcee6b8
[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 static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
24 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGBLE_FOLDERS;
25 import static org.openecomp.sdc.tosca.csar.CSARConstants.ELIGIBLE_FILES;
26 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
27 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME;
28 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
29 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_PATH_FILE_NAME;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39 import org.openecomp.core.utilities.file.FileContentHandler;
40 import org.openecomp.sdc.common.errors.Messages;
41 import org.openecomp.sdc.common.utils.SdcCommon;
42 import org.openecomp.sdc.datatypes.error.ErrorLevel;
43 import org.openecomp.sdc.datatypes.error.ErrorMessage;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.tosca.csar.Manifest;
47 import org.openecomp.sdc.tosca.csar.ONAPManifestOnboarding;
48 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
49 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
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         Map<String, List<ErrorMessage>> errors = new HashMap<>();
60         validateManifest(contentHandler);
61         validateMetadata(contentHandler);
62         validateNoExtraFiles(contentHandler);
63         validateFolders(contentHandler.getFolderList());
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.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME.getName())) {
75
76                 ToscaMetadata onboardingToscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metaFileContent);
77                 String entryDefinitionsPath = onboardingToscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
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(final FileContentHandler contentMap) {
95         if (!validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
96             return;
97         }
98
99         try (final InputStream fileContent = contentMap.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
100             final Manifest onboardingManifest = new ONAPManifestOnboarding();
101             onboardingManifest.parse(fileContent);
102             if (!onboardingManifest.isValid()) {
103                 onboardingManifest.getErrors()
104                     .forEach(error -> uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, error)));
105             }
106         } catch (final IOException ex) {
107             final String errorMessage =
108                 Messages.MANIFEST_UNEXPECTED_ERROR.formatMessage(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, ex.getMessage());
109             uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, errorMessage));
110             logger.error(errorMessage, ex);
111         }
112     }
113
114     private void validateNoExtraFiles(FileContentHandler contentMap) {
115         List<String> unwantedFiles = contentMap.getFileList().stream()
116                 .filter(this::filterFiles).collect(Collectors.toList());
117         if (!unwantedFiles.isEmpty()) {
118             unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile ->
119                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
120                             getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), unwantedFile))));
121         }
122     }
123
124     private void validateFolders(Set<String> folderList) {
125         List<String> filterResult =
126                 folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
127         if (!filterResult.isEmpty()) {
128             folderList.stream().filter(this::filterFolders).forEach(unwantedFolder ->
129                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
130                             getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(),
131                                     unwantedFolder))));
132         }
133     }
134
135     private boolean filterFiles(String inFileName) {
136         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
137         return !valid && filterFolders(inFileName);
138     }
139
140     private boolean filterFolders(String fileName) {
141         return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
142     }
143
144     private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
145         return contentMap.containsFile(fileName);
146     }
147
148     private boolean validateFileExist(FileContentHandler contentMap, String fileName) {
149
150         boolean containsFile = contentMap.containsFile(fileName);
151         if (!containsFile) {
152             uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR,
153                     getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
154         }
155         return containsFile;
156     }
157 }