Add a CSAR validator for model ETSI SOL001 2.5.1
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / ONAPCsarValidator.java
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.ToscaMetaEntryVersion261.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.List;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36 import org.openecomp.core.utilities.file.FileContentHandler;
37 import org.openecomp.sdc.common.errors.Messages;
38 import org.openecomp.sdc.datatypes.error.ErrorLevel;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 import org.openecomp.sdc.logging.api.Logger;
41 import org.openecomp.sdc.logging.api.LoggerFactory;
42 import org.openecomp.sdc.tosca.csar.Manifest;
43 import org.openecomp.sdc.tosca.csar.ONAPManifestOnboarding;
44 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
45 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
46
47 class ONAPCsarValidator implements Validator {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ONAPCsarValidator.class);
50     private final List<ErrorMessage> uploadFileErrors = new ArrayList<>();
51
52     private void validateMetadata(FileContentHandler contentMap) {
53         if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) {
54             try (InputStream metaFileContent = contentMap.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)) {
55                 ToscaMetadata onboardingToscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metaFileContent);
56                 String entryDefinitionsPath = onboardingToscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
57                 if (entryDefinitionsPath != null) {
58                     validateFileExist(contentMap, entryDefinitionsPath);
59                 } else {
60                     uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
61                 }
62             } catch (IOException exception) {
63                 LOGGER.error(exception.getMessage(), exception);
64                 uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
65             }
66         } else {
67             validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
68         }
69     }
70
71     private void validateManifest(final FileContentHandler contentMap) {
72         if (!validateFileExist(contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
73             return;
74         }
75         try (final InputStream fileContent = contentMap.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) {
76             final Manifest onboardingManifest = new ONAPManifestOnboarding();
77             onboardingManifest.parse(fileContent);
78             if (!onboardingManifest.isValid()) {
79                 onboardingManifest.getErrors().forEach(error -> uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, error)));
80             }
81         } catch (final IOException ex) {
82             final String errorMessage = Messages.MANIFEST_UNEXPECTED_ERROR.formatMessage(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, ex.getMessage());
83             uploadFileErrors.add(new ErrorMessage(ErrorLevel.ERROR, errorMessage));
84             LOGGER.error(errorMessage, ex);
85         }
86     }
87
88     private void validateNoExtraFiles(FileContentHandler contentMap) {
89         List<String> unwantedFiles = contentMap.getFileList().stream().filter(this::filterFiles).collect(Collectors.toList());
90         if (!unwantedFiles.isEmpty()) {
91             unwantedFiles.stream().filter(this::filterFiles).forEach(unwantedFile -> uploadFileErrors
92                 .add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), unwantedFile))));
93         }
94     }
95
96     private void validateFolders(Set<String> folderList) {
97         List<String> filterResult = folderList.stream().filter(this::filterFolders).collect(Collectors.toList());
98         if (!filterResult.isEmpty()) {
99             folderList.stream().filter(this::filterFolders).forEach(unwantedFolder -> uploadFileErrors.add(
100                 new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_DIRECTORIES_NOT_ALLOWED.getErrorMessage(), unwantedFolder))));
101         }
102     }
103
104     private boolean filterFiles(String inFileName) {
105         boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName));
106         return !valid && filterFolders(inFileName);
107     }
108
109     private boolean filterFolders(String fileName) {
110         return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
111     }
112
113     private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
114         return contentMap.containsFile(fileName);
115     }
116
117     private boolean validateFileExist(FileContentHandler contentMap, String fileName) {
118         boolean containsFile = contentMap.containsFile(fileName);
119         if (!containsFile) {
120             uploadFileErrors
121                 .add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage(), fileName)));
122         }
123         return containsFile;
124     }
125
126     @Override
127     public ValidationResult validate(final FileContentHandler csarContent) {
128         validateManifest(csarContent);
129         validateMetadata(csarContent);
130         validateNoExtraFiles(csarContent);
131         validateFolders(csarContent.getFolderList());
132         final var csarValidationResult = new CsarValidationResult();
133         uploadFileErrors.forEach(csarValidationResult::addError);
134         return csarValidationResult;
135     }
136
137     @Override
138     public boolean appliesTo(String model) {
139         return model == null;
140     }
141
142     @Override
143     public int getOrder() {
144         return 0;
145     }
146 }