1 package org.onap.sdc.tosca.parser.impl;
3 import java.util.ArrayList;
6 import org.onap.sdc.tosca.parser.api.ConformanceLevel;
7 import org.onap.sdc.tosca.parser.config.ConfigurationManager;
8 import org.onap.sdc.tosca.parser.config.ErrorInfo;
9 import org.onap.sdc.tosca.parser.config.JToscaValidationIssueInfo;
10 import org.onap.sdc.tosca.parser.config.SdcToscaParserErrors;
11 import org.onap.sdc.tosca.parser.utils.GeneralUtility;
12 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
13 import org.onap.sdc.tosca.parser.config.*;
14 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
15 import org.onap.sdc.toscaparser.api.ToscaTemplate;
16 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
17 import org.onap.sdc.toscaparser.api.common.JToscaException;
18 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
19 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class SdcToscaParserFactory {
24 private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());
26 private static ConfigurationManager configurationManager;
27 private static volatile SdcToscaParserFactory instance;
28 private List<JToscaValidationIssue> criticalExceptions = new ArrayList<>();
29 private List<JToscaValidationIssue> warningExceptions = new ArrayList<>();
30 private List<JToscaValidationIssue> notAnalyzadExceptions = new ArrayList<>();
31 private SdcToscaParserFactory() {}
34 * Get an SdcToscaParserFactory instance.
35 * @return SdcToscaParserFactory instance.
37 public static SdcToscaParserFactory getInstance() {
38 if (instance == null) {
39 synchronized (SdcToscaParserFactory.class) {
40 if (instance == null) {
41 instance = new SdcToscaParserFactory();
42 configurationManager = ConfigurationManager.getInstance();
49 public static void setConfigurationManager(ConfigurationManager configurationManager) {
50 SdcToscaParserFactory.configurationManager = configurationManager;
54 * Get an ISdcCsarHelper object for this CSAR file.
56 * @param csarPath - the absolute path to CSAR file.
57 * @return ISdcCsarHelper object.
58 * @throws SdcToscaParserException - in case the path or CSAR are invalid.
60 public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {
61 return init(csarPath, true);
65 * Get an ISdcCsarHelper object for this CSAR file.
67 * @param csarPath - the absolute path to CSAR file.
68 * @param resolveGetInput - resolve get_input properties
69 * @return ISdcCsarHelper object.
70 * @throws SdcToscaParserException - in case the path or CSAR are invalid.
72 public ISdcCsarHelper getSdcCsarHelper(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
73 return init(csarPath, resolveGetInput);
76 private ISdcCsarHelper init(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
77 synchronized (SdcToscaParserFactory.class) {
78 ToscaTemplate tosca = null;
80 tosca = new ToscaTemplate(csarPath, null, true, null, resolveGetInput);
81 } catch (JToscaException e) {
82 throwSdcToscaParserException(e);
84 SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);
85 String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();
86 validateCsarVersion(cSarConformanceLevel);
88 handleErrorsByTypes(csarPath, cSarConformanceLevel);
89 } catch (JToscaException e) {
90 throwSdcToscaParserException(e);
92 return sdcCsarHelperImpl;
96 private void handleErrorsByTypes(String csarPath, String cSarConformanceLevel) throws JToscaException {
97 clearValidationIssuesLists();
98 for(JToscaValidationIssue toscaValidationIssue : ThreadLocalsHolder.getCollector().getValidationIssues().values()){
99 List<JToscaValidationIssueInfo> issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues().get(toscaValidationIssue.getCode());
100 if(issueInfos != null && !issueInfos.isEmpty()){
101 JToscaValidationIssueInfo issueInfo = null;
102 issueInfo = issueInfos.stream()
103 .filter(i-> isMatchConformanceLevel(cSarConformanceLevel,i.getSinceCsarConformanceLevel()))
104 .max((i1,i2) -> GeneralUtility.conformanceLevelCompare(i1.getSinceCsarConformanceLevel(), i2.getSinceCsarConformanceLevel()) )
107 if(issueInfo != null){
108 switch (JToscaValidationIssueType.valueOf(issueInfo.getIssueType())) {
110 criticalExceptions.add(toscaValidationIssue);
113 warningExceptions.add(toscaValidationIssue);
119 notAnalyzadExceptions.add(toscaValidationIssue);
122 notAnalyzadExceptions.add(toscaValidationIssue);
128 private void clearValidationIssuesLists(){
129 notAnalyzadExceptions.clear();
130 criticalExceptions.clear();
131 warningExceptions.clear();
134 private void logErrors(String inputPath) throws JToscaException{
136 int warningsCount = warningExceptions.size();
137 if (warningsCount > 0) {
138 log.warn("####################################################################################################");
139 log.warn("CSAR Warnings found! CSAR name - {}", inputPath);
140 log.warn("ToscaTemplate - verifyTemplate - {} Parsing Warning{} occurred...", warningsCount, (warningsCount > 1 ? "s" : ""));
141 for (JToscaValidationIssue info : warningExceptions) {
142 log.warn("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
144 log.warn("####################################################################################################");
147 int criticalsCount = criticalExceptions.size();
148 if (criticalsCount > 0) {
149 log.error("####################################################################################################");
150 log.error("ToscaTemplate - verifyTemplate - {} Parsing Critical{} occurred...", criticalsCount, (criticalsCount > 1 ? "s" : ""));
151 for (JToscaValidationIssue info : criticalExceptions) {
152 log.error("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
154 throw new JToscaException(String.format("CSAR Validation Failed. CSAR name - {}. Please check logs for details.", inputPath), JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR.getValue());
157 public List<JToscaValidationIssue> getCriticalExceptions() {
158 return criticalExceptions;
161 public List<JToscaValidationIssue> getWarningExceptions() {
162 return warningExceptions;
165 public List<JToscaValidationIssue> getNotAnalyzadExceptions() {
166 return notAnalyzadExceptions;
170 private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
171 ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel();
172 String minVersion = level.getMinVersion();
173 String maxVersion = level.getMaxVersion();
174 if (cSarVersion != null) {
175 if ((GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) || (GeneralUtility.conformanceLevelCompare(cSarVersion, maxVersion) > 0)) {
176 throwConformanceLevelException(minVersion, maxVersion);
179 throwConformanceLevelException(minVersion, maxVersion);
183 private boolean isMatchConformanceLevel(String ValidationIssueVersion, String cSarVersion){
184 if (ValidationIssueVersion != null && cSarVersion != null) {
185 if ((GeneralUtility.conformanceLevelCompare(ValidationIssueVersion, cSarVersion) >= 0)) {
191 private void throwConformanceLevelException(String minVersion, String maxVersion) throws SdcToscaParserException {
192 ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());
193 throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion, maxVersion), errorInfo.getCode());
196 private void throwSdcToscaParserException(JToscaException e) throws SdcToscaParserException {
197 ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
198 throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());