2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.tosca.parser.impl;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.onap.sdc.tosca.parser.api.ConformanceLevel;
27 import org.onap.sdc.tosca.parser.config.ConfigurationManager;
28 import org.onap.sdc.tosca.parser.config.ErrorInfo;
29 import org.onap.sdc.tosca.parser.config.JToscaValidationIssueInfo;
30 import org.onap.sdc.tosca.parser.config.SdcToscaParserErrors;
31 import org.onap.sdc.tosca.parser.enums.JToscaValidationIssueType;
32 import org.onap.sdc.tosca.parser.utils.GeneralUtility;
33 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
34 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
35 import org.onap.sdc.toscaparser.api.ToscaTemplate;
36 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
37 import org.onap.sdc.toscaparser.api.common.JToscaException;
38 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
39 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class SdcToscaParserFactory {
44 private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());
46 private static ConfigurationManager configurationManager;
47 private static volatile SdcToscaParserFactory instance;
48 private List<JToscaValidationIssue> criticalExceptions = new ArrayList<>();
49 private List<JToscaValidationIssue> warningExceptions = new ArrayList<>();
50 private List<JToscaValidationIssue> notAnalyzadExceptions = new ArrayList<>();
51 private SdcToscaParserFactory() {}
54 * Get an SdcToscaParserFactory instance.
55 * @return SdcToscaParserFactory instance.
57 public static SdcToscaParserFactory getInstance() {
58 if (instance == null) {
59 synchronized (SdcToscaParserFactory.class) {
60 if (instance == null) {
61 instance = new SdcToscaParserFactory();
62 configurationManager = ConfigurationManager.getInstance();
69 public static void setConfigurationManager(ConfigurationManager configurationManager) {
70 SdcToscaParserFactory.configurationManager = configurationManager;
74 * Get an ISdcCsarHelper object for this CSAR file.
76 * @param csarPath - the absolute path to CSAR file.
77 * @return ISdcCsarHelper object.
78 * @throws SdcToscaParserException - in case the path or CSAR are invalid.
80 public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {
81 return init(csarPath, true);
85 * Get an ISdcCsarHelper object for this CSAR file.
87 * @param csarPath - the absolute path to CSAR file.
88 * @param resolveGetInput - resolve get_input properties
89 * @return ISdcCsarHelper object.
90 * @throws SdcToscaParserException - in case the path or CSAR are invalid.
92 public ISdcCsarHelper getSdcCsarHelper(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
93 return init(csarPath, resolveGetInput);
96 private ISdcCsarHelper init(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
97 synchronized (SdcToscaParserFactory.class) {
98 ToscaTemplate tosca = null;
100 tosca = new ToscaTemplate(csarPath, null, true, null, resolveGetInput);
101 } catch (JToscaException e) {
102 throwSdcToscaParserException(e);
104 SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);
105 String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();
106 validateCsarVersion(cSarConformanceLevel);
108 handleErrorsByTypes(csarPath, cSarConformanceLevel);
109 } catch (JToscaException e) {
110 throwSdcToscaParserException(e);
112 return sdcCsarHelperImpl;
116 private void handleErrorsByTypes(String csarPath, String cSarConformanceLevel) throws JToscaException {
117 clearValidationIssuesLists();
118 for(JToscaValidationIssue toscaValidationIssue : ThreadLocalsHolder.getCollector().getValidationIssues().values()){
119 List<JToscaValidationIssueInfo> issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues().get(toscaValidationIssue.getCode());
120 if(issueInfos != null && !issueInfos.isEmpty()){
121 JToscaValidationIssueInfo issueInfo = null;
122 issueInfo = issueInfos.stream()
123 .filter(i-> isMatchConformanceLevel(cSarConformanceLevel,i.getSinceCsarConformanceLevel()))
124 .max((i1,i2) -> GeneralUtility.conformanceLevelCompare(i1.getSinceCsarConformanceLevel(), i2.getSinceCsarConformanceLevel()) )
127 if(issueInfo != null){
128 switch (JToscaValidationIssueType.valueOf(issueInfo.getIssueType())) {
130 criticalExceptions.add(toscaValidationIssue);
133 warningExceptions.add(toscaValidationIssue);
139 notAnalyzadExceptions.add(toscaValidationIssue);
142 notAnalyzadExceptions.add(toscaValidationIssue);
148 private void clearValidationIssuesLists(){
149 notAnalyzadExceptions.clear();
150 criticalExceptions.clear();
151 warningExceptions.clear();
154 private void logErrors(String inputPath) throws JToscaException{
156 int warningsCount = warningExceptions.size();
157 if (warningsCount > 0) {
158 log.warn("####################################################################################################");
159 log.warn("CSAR Warnings found! CSAR name - {}", inputPath);
160 log.warn("ToscaTemplate - verifyTemplate - {} Parsing Warning{} occurred...", warningsCount, (warningsCount > 1 ? "s" : ""));
161 for (JToscaValidationIssue info : warningExceptions) {
162 log.warn("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
164 log.warn("####################################################################################################");
167 int criticalsCount = criticalExceptions.size();
168 if (criticalsCount > 0) {
169 log.error("####################################################################################################");
170 log.error("ToscaTemplate - verifyTemplate - {} Parsing Critical{} occurred...", criticalsCount, (criticalsCount > 1 ? "s" : ""));
171 for (JToscaValidationIssue info : criticalExceptions) {
172 log.error("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
174 throw new JToscaException(String.format("CSAR Validation Failed. CSAR name - {}. Please check logs for details.", inputPath), JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR.getValue());
177 public List<JToscaValidationIssue> getCriticalExceptions() {
178 return criticalExceptions;
181 public List<JToscaValidationIssue> getWarningExceptions() {
182 return warningExceptions;
185 public List<JToscaValidationIssue> getNotAnalyzadExceptions() {
186 return notAnalyzadExceptions;
190 private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
191 ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel();
192 String minVersion = level.getMinVersion();
193 if (cSarVersion != null) {
194 if (GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) {
195 throwConformanceLevelException(minVersion);
198 throwConformanceLevelException(minVersion);
202 private boolean isMatchConformanceLevel(String ValidationIssueVersion, String cSarVersion){
203 if (ValidationIssueVersion != null && cSarVersion != null) {
204 if ((GeneralUtility.conformanceLevelCompare(ValidationIssueVersion, cSarVersion) >= 0)) {
210 private void throwConformanceLevelException(String minVersion) throws SdcToscaParserException {
211 ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());
212 throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion), errorInfo.getCode());
215 private void throwSdcToscaParserException(JToscaException e) throws SdcToscaParserException {
216 ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
217 throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());