Increase timeout for Selenium tests
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / impl / ToscaValidationServiceImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.tosca.services.impl;
18
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.be.utils.TypeUtils.ToscaTagNamesEnum;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.tosca.services.ToscaValidationService;
35 import org.yaml.snakeyaml.Yaml;
36
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.io.BufferedInputStream;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44
45
46 import java.util.HashMap;
47 import java.util.LinkedHashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.stream.Collectors;
51
52
53 public class ToscaValidationServiceImpl implements ToscaValidationService {
54
55   private static final Logger LOGGER = LoggerFactory.getLogger(ToscaValidationServiceImpl.class);
56   private static final String SDCPARSER_JTOSCA_VALIDATIONISSUE_CONFIG =
57       "SDCParser_jtosca-validation-issue-configuration.yaml";
58   private static final String SDCPARSER_ERROR_CONFIG = "SDCParser_error-configuration.yaml";
59
60   static {
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);
66   }
67
68   @Override
69   public Map<String, List<ErrorMessage>> validate(FileContentHandler fileContentHandler)
70       throws IOException {
71
72     Path dir =
73         Files.createTempDirectory(OnboardingTypesEnum.CSAR + "_" + System.currentTimeMillis());
74     try {
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);
80     } finally {
81       // Cleanup temporary files and folders from file system
82       org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
83     }
84   }
85
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<>();
89
90     // Process Yaml Files
91     for (Map.Entry<String, String> fileEntry : validFilePaths.entrySet()) {
92       try {
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 "
101                     + "occurred")));
102       }
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 "
108                     + "occurred")));
109       }
110     }
111     return validationIssues;
112   }
113
114   private Map<String, String> getValidFilePaths(Map<String, String> filePaths) {
115     return filePaths.entrySet()
116         .stream()
117         .filter(map -> FileUtils.isValidYamlExtension(FilenameUtils.getExtension(map.getKey()))
118             && isToscaYaml(map.getValue()))
119         .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
120   }
121
122   private boolean isToscaYaml(final String filePath) {
123    boolean retValue = false;
124
125     try (final InputStream input = new BufferedInputStream(new FileInputStream(new File(filePath)));) {
126       final Yaml yaml = new Yaml();
127       final LinkedHashMap<String,Object> data = (LinkedHashMap) yaml.load(input);
128       if(data.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName()) != null) {
129         retValue = true;
130       }
131     }
132     catch(final Exception e){
133       LOGGER.info("Ignore the exception as the input file may not be a Tosca Yaml; let the " +
134           "default value return", e);
135     }
136     return retValue;
137   }
138
139   private void processValidationIssues(String fileName, SdcToscaParserFactory factory, Map<String,
140       List<ErrorMessage>> validationIssues) {
141
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()))));
147
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()))));
153
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()))));
159
160   }
161
162 }