2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.tosca.services.impl;
19 import org.apache.commons.io.FilenameUtils;
20 import org.onap.sdc.tosca.parser.config.ConfigurationManager;
21 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
22 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
27 import org.openecomp.core.validation.ErrorMessageCode;
28 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.tosca.services.ToscaValidationService;
34 import org.yaml.snakeyaml.Yaml;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.io.BufferedInputStream;
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
45 import java.util.HashMap;
46 import java.util.LinkedHashMap;
47 import java.util.List;
49 import java.util.stream.Collectors;
52 public class ToscaValidationServiceImpl implements ToscaValidationService {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaValidationServiceImpl.class);
55 private static final String SDCPARSER_JTOSCA_VALIDATIONISSUE_CONFIG =
56 "SDCParser_jtosca-validation-issue-configuration.yaml";
57 private static final String SDCPARSER_ERROR_CONFIG = "SDCParser_error-configuration.yaml";
58 private static final String TOSCA_DEFINITION_VERSION = "tosca_definitions_version";
61 // Override default SDC Parser configuration
62 ConfigurationManager configurationManager = ConfigurationManager.getInstance();
63 configurationManager.setJtoscaValidationIssueConfiguration(SDCPARSER_JTOSCA_VALIDATIONISSUE_CONFIG);
64 configurationManager.setErrorConfiguration(SDCPARSER_ERROR_CONFIG);
65 SdcToscaParserFactory.setConfigurationManager(configurationManager);
69 public Map<String, List<ErrorMessage>> validate(FileContentHandler fileContentHandler)
73 Files.createTempDirectory(OnboardingTypesEnum.CSAR + "_" + System.currentTimeMillis());
75 // Write temporary files and folders to File System
76 Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler
77 (fileContentHandler, dir);
78 // Process Tosca Yaml validation
79 return processToscaYamls(filePaths);
81 // Cleanup temporary files and folders from file system
82 org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
86 private Map<String, List<ErrorMessage>> processToscaYamls(Map<String, String> filePaths) {
87 Map<String, String> validFilePaths = getValidFilePaths(filePaths);
88 Map<String, List<ErrorMessage>> validationIssues = new HashMap<>();
91 for (Map.Entry<String, String> fileEntry : validFilePaths.entrySet()) {
93 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
94 factory.getSdcCsarHelper(fileEntry.getValue());
95 processValidationIssues(fileEntry.getKey(), factory, validationIssues);
96 } catch (SdcToscaParserException stpe) {
97 LOGGER.error("SDC Parser Exception from SDC Parser Library : " + stpe);
98 ErrorMessage.ErrorMessageUtil.addMessage(fileEntry.getKey(), validationIssues).add(
99 new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
100 .getErrorWithParameters(new ErrorMessageCode("JE000"), "Unexpected Error "
103 catch (RuntimeException rte) {
104 LOGGER.error("Runtime Exception from SDC Parser Library : " + rte);
105 ErrorMessage.ErrorMessageUtil.addMessage(fileEntry.getKey(), validationIssues).add(
106 new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
107 .getErrorWithParameters(new ErrorMessageCode("JE000"), "Unexpected Error "
111 return validationIssues;
114 private Map<String, String> getValidFilePaths(Map<String, String> filePaths) {
115 return filePaths.entrySet()
117 .filter(map -> FileUtils.isValidYamlExtension(FilenameUtils.getExtension(map.getKey()))
118 && isToscaYaml(map.getValue()))
119 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
122 private boolean isToscaYaml(String filePath) {
123 boolean retValue = false;
125 try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filePath)));) {
126 Yaml yaml = new Yaml();
127 LinkedHashMap<String,Object> data = (LinkedHashMap) yaml.load(input);
128 if(data.get(TOSCA_DEFINITION_VERSION) != null) {
133 LOGGER.info("Ignore the exception as the input file may not be a Tosca Yaml; let the " +
134 "default value return", e);
139 private void processValidationIssues(String fileName, SdcToscaParserFactory factory, Map<String,
140 List<ErrorMessage>> validationIssues) {
142 List<JToscaValidationIssue> criticalsReport = factory.getCriticalExceptions();
143 criticalsReport.stream().forEach(err ->
144 ErrorMessage.ErrorMessageUtil.addMessage(fileName, validationIssues).add(
145 new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
146 .getErrorWithParameters(new ErrorMessageCode(err.getCode()), err.getMessage()))));
148 List<JToscaValidationIssue> warningsReport = factory.getWarningExceptions();
149 warningsReport.stream().forEach(err ->
150 ErrorMessage.ErrorMessageUtil.addMessage(fileName, validationIssues).add(
151 new ErrorMessage(ErrorLevel.WARNING, ErrorMessagesFormatBuilder
152 .getErrorWithParameters(new ErrorMessageCode(err.getCode()), err.getMessage()))));
154 List<JToscaValidationIssue> notAnalyzedReport = factory.getNotAnalyzadExceptions();
155 notAnalyzedReport.stream().forEach(err ->
156 ErrorMessage.ErrorMessageUtil.addMessage(fileName, validationIssues).add(
157 new ErrorMessage(ErrorLevel.WARNING, ErrorMessagesFormatBuilder
158 .getErrorWithParameters(new ErrorMessageCode(err.getCode()), err.getMessage()))));