-package org.openecomp.sdc.tosca.parser.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.openecomp.sdc.tosca.parser.api.ConformanceLevel;\r
-import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;\r
-import org.openecomp.sdc.tosca.parser.config.*;\r
-import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;\r
-import org.openecomp.sdc.tosca.parser.utils.GeneralUtility;\r
-import org.openecomp.sdc.toscaparser.api.ToscaTemplate;\r
-import org.openecomp.sdc.toscaparser.api.common.JToscaValidationIssue;\r
-import org.openecomp.sdc.toscaparser.api.common.JToscaException;\r
-import org.openecomp.sdc.toscaparser.api.utils.JToscaErrorCodes;\r
-import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-public class SdcToscaParserFactory {\r
- private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());\r
-\r
- private static ConfigurationManager configurationManager;\r
- private static volatile SdcToscaParserFactory instance;\r
- private List<JToscaValidationIssue> criticalExceptions = new ArrayList<>();\r
- private List<JToscaValidationIssue> warningExceptions = new ArrayList<>();\r
- private List<JToscaValidationIssue> notAnalyzadExceptions = new ArrayList<>();\r
- private SdcToscaParserFactory() {}\r
-\r
- /**\r
- * Get an SdcToscaParserFactory instance.\r
- * @return SdcToscaParserFactory instance.\r
- */\r
- public static SdcToscaParserFactory getInstance() {\r
- if (instance == null) {\r
- synchronized (SdcToscaParserFactory.class) {\r
- if (instance == null) {\r
- instance = new SdcToscaParserFactory();\r
- configurationManager = ConfigurationManager.getInstance();\r
- }\r
- }\r
- }\r
- return instance;\r
- }\r
-\r
- public static void setConfigurationManager(ConfigurationManager configurationManager) {\r
- SdcToscaParserFactory.configurationManager = configurationManager;\r
- }\r
-\r
- /**\r
- * Get an ISdcCsarHelper object for this CSAR file.\r
- *\r
- * @param csarPath - the absolute path to CSAR file.\r
- * @return ISdcCsarHelper object.\r
- * @throws SdcToscaParserException - in case the path or CSAR are invalid.\r
- */\r
- public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {\r
- return init(csarPath, true);\r
- }\r
-\r
- /**\r
- * Get an ISdcCsarHelper object for this CSAR file.\r
- *\r
- * @param csarPath - the absolute path to CSAR file.\r
- * @param resolveGetInput - resolve get_input properties\r
- * @return ISdcCsarHelper object.\r
- * @throws SdcToscaParserException - in case the path or CSAR are invalid.\r
- */\r
- public ISdcCsarHelper getSdcCsarHelper(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {\r
- return init(csarPath, resolveGetInput);\r
- }\r
-\r
- private ISdcCsarHelper init(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {\r
- synchronized (SdcToscaParserFactory.class) {\r
- ToscaTemplate tosca = null;\r
- try {\r
- tosca = new ToscaTemplate(csarPath, null, true, null, resolveGetInput);\r
- } catch (JToscaException e) {\r
- throwSdcToscaParserException(e);\r
- }\r
- SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);\r
- String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();\r
- validateCsarVersion(cSarConformanceLevel);\r
- try {\r
- handleErrorsByTypes(csarPath, cSarConformanceLevel);\r
- } catch (JToscaException e) {\r
- throwSdcToscaParserException(e);\r
- }\r
- return sdcCsarHelperImpl;\r
- }\r
- }\r
-\r
- private void handleErrorsByTypes(String csarPath, String cSarConformanceLevel) throws JToscaException {\r
- clearValidationIssuesLists();\r
- for(JToscaValidationIssue toscaValidationIssue : ThreadLocalsHolder.getCollector().getValidationIssues().values()){\r
- List<JToscaValidationIssueInfo> issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues().get(toscaValidationIssue.getCode());\r
- if(issueInfos != null && !issueInfos.isEmpty()){\r
- JToscaValidationIssueInfo issueInfo = null;\r
- issueInfo = issueInfos.stream()\r
- .filter(i-> isMatchConformanceLevel(cSarConformanceLevel,i.getSinceCsarConformanceLevel()))\r
- .max((i1,i2) -> GeneralUtility.conformanceLevelCompare(i1.getSinceCsarConformanceLevel(), i2.getSinceCsarConformanceLevel()) )\r
- .orElse(null);\r
-\r
- if(issueInfo != null){\r
- switch (JToscaValidationIssueType.valueOf(issueInfo.getIssueType())) {\r
- case CRITICAL:\r
- criticalExceptions.add(toscaValidationIssue);\r
- break;\r
- case WARNING:\r
- warningExceptions.add(toscaValidationIssue);\r
- break;\r
- default:\r
- break;\r
- }\r
- }else{\r
- notAnalyzadExceptions.add(toscaValidationIssue);\r
- }\r
- }else{//notAnalyzed\r
- notAnalyzadExceptions.add(toscaValidationIssue);\r
- }\r
- }\r
- logErrors(csarPath);\r
- }\r
-\r
- private void clearValidationIssuesLists(){\r
- notAnalyzadExceptions.clear();\r
- criticalExceptions.clear();\r
- warningExceptions.clear();\r
- }\r
-\r
- private void logErrors(String inputPath) throws JToscaException{\r
- //Warnings\r
- int warningsCount = warningExceptions.size();\r
- if (warningsCount > 0) {\r
- log.warn("####################################################################################################");\r
- log.warn("CSAR Warnings found! CSAR name - {}", inputPath);\r
- log.warn("ToscaTemplate - verifyTemplate - {} Parsing Warning{} occurred...", warningsCount, (warningsCount > 1 ? "s" : ""));\r
- for (JToscaValidationIssue info : warningExceptions) {\r
- log.warn("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);\r
- }\r
- log.warn("####################################################################################################");\r
- }\r
- //Criticals\r
- int criticalsCount = criticalExceptions.size();\r
- if (criticalsCount > 0) {\r
- log.error("####################################################################################################");\r
- log.error("ToscaTemplate - verifyTemplate - {} Parsing Critical{} occurred...", criticalsCount, (criticalsCount > 1 ? "s" : ""));\r
- for (JToscaValidationIssue info : criticalExceptions) {\r
- log.error("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);\r
- }\r
- throw new JToscaException(String.format("CSAR Validation Failed. CSAR name - {}. Please check logs for details.", inputPath), JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR.getValue());\r
- }\r
- }\r
- public List<JToscaValidationIssue> getCriticalExceptions() {\r
- return criticalExceptions;\r
- }\r
-\r
- public List<JToscaValidationIssue> getWarningExceptions() {\r
- return warningExceptions;\r
- }\r
-\r
- public List<JToscaValidationIssue> getNotAnalyzadExceptions() {\r
- return notAnalyzadExceptions;\r
- }\r
-\r
-\r
- private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {\r
- ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel();\r
- String minVersion = level.getMinVersion();\r
- String maxVersion = level.getMaxVersion();\r
- if (cSarVersion != null) {\r
- if ((GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) || (GeneralUtility.conformanceLevelCompare(cSarVersion, maxVersion) > 0)) {\r
- throwConformanceLevelException(minVersion, maxVersion);\r
- }\r
- } else {\r
- throwConformanceLevelException(minVersion, maxVersion);\r
- }\r
- }\r
-\r
- private boolean isMatchConformanceLevel(String ValidationIssueVersion, String cSarVersion){\r
- if (ValidationIssueVersion != null && cSarVersion != null) {\r
- if ((GeneralUtility.conformanceLevelCompare(ValidationIssueVersion, cSarVersion) >= 0)) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
- private void throwConformanceLevelException(String minVersion, String maxVersion) throws SdcToscaParserException {\r
- ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());\r
- throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion, maxVersion), errorInfo.getCode());\r
- }\r
-\r
- private void throwSdcToscaParserException(JToscaException e) throws SdcToscaParserException {\r
- ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());\r
- throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());\r
- }\r
-\r
-\r
-\r
+package org.onap.sdc.tosca.parser.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.onap.sdc.tosca.parser.api.ConformanceLevel;
+import org.onap.sdc.tosca.parser.config.ConfigurationManager;
+import org.onap.sdc.tosca.parser.config.ErrorInfo;
+import org.onap.sdc.tosca.parser.config.JToscaValidationIssueInfo;
+import org.onap.sdc.tosca.parser.config.SdcToscaParserErrors;
+import org.onap.sdc.tosca.parser.utils.GeneralUtility;
+import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.onap.sdc.tosca.parser.config.*;
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
+import org.onap.sdc.toscaparser.api.ToscaTemplate;
+import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
+import org.onap.sdc.toscaparser.api.common.JToscaException;
+import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
+import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SdcToscaParserFactory {
+ private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());
+
+ private static ConfigurationManager configurationManager;
+ private static volatile SdcToscaParserFactory instance;
+ private List<JToscaValidationIssue> criticalExceptions = new ArrayList<>();
+ private List<JToscaValidationIssue> warningExceptions = new ArrayList<>();
+ private List<JToscaValidationIssue> notAnalyzadExceptions = new ArrayList<>();
+ private SdcToscaParserFactory() {}
+
+ /**
+ * Get an SdcToscaParserFactory instance.
+ * @return SdcToscaParserFactory instance.
+ */
+ public static SdcToscaParserFactory getInstance() {
+ if (instance == null) {
+ synchronized (SdcToscaParserFactory.class) {
+ if (instance == null) {
+ instance = new SdcToscaParserFactory();
+ configurationManager = ConfigurationManager.getInstance();
+ }
+ }
+ }
+ return instance;
+ }
+
+ public static void setConfigurationManager(ConfigurationManager configurationManager) {
+ SdcToscaParserFactory.configurationManager = configurationManager;
+ }
+
+ /**
+ * Get an ISdcCsarHelper object for this CSAR file.
+ *
+ * @param csarPath - the absolute path to CSAR file.
+ * @return ISdcCsarHelper object.
+ * @throws SdcToscaParserException - in case the path or CSAR are invalid.
+ */
+ public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {
+ return init(csarPath, true);
+ }
+
+ /**
+ * Get an ISdcCsarHelper object for this CSAR file.
+ *
+ * @param csarPath - the absolute path to CSAR file.
+ * @param resolveGetInput - resolve get_input properties
+ * @return ISdcCsarHelper object.
+ * @throws SdcToscaParserException - in case the path or CSAR are invalid.
+ */
+ public ISdcCsarHelper getSdcCsarHelper(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
+ return init(csarPath, resolveGetInput);
+ }
+
+ private ISdcCsarHelper init(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
+ synchronized (SdcToscaParserFactory.class) {
+ ToscaTemplate tosca = null;
+ try {
+ tosca = new ToscaTemplate(csarPath, null, true, null, resolveGetInput);
+ } catch (JToscaException e) {
+ throwSdcToscaParserException(e);
+ }
+ SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);
+ String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();
+ validateCsarVersion(cSarConformanceLevel);
+ try {
+ handleErrorsByTypes(csarPath, cSarConformanceLevel);
+ } catch (JToscaException e) {
+ throwSdcToscaParserException(e);
+ }
+ return sdcCsarHelperImpl;
+ }
+ }
+
+ private void handleErrorsByTypes(String csarPath, String cSarConformanceLevel) throws JToscaException {
+ clearValidationIssuesLists();
+ for(JToscaValidationIssue toscaValidationIssue : ThreadLocalsHolder.getCollector().getValidationIssues().values()){
+ List<JToscaValidationIssueInfo> issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues().get(toscaValidationIssue.getCode());
+ if(issueInfos != null && !issueInfos.isEmpty()){
+ JToscaValidationIssueInfo issueInfo = null;
+ issueInfo = issueInfos.stream()
+ .filter(i-> isMatchConformanceLevel(cSarConformanceLevel,i.getSinceCsarConformanceLevel()))
+ .max((i1,i2) -> GeneralUtility.conformanceLevelCompare(i1.getSinceCsarConformanceLevel(), i2.getSinceCsarConformanceLevel()) )
+ .orElse(null);
+
+ if(issueInfo != null){
+ switch (JToscaValidationIssueType.valueOf(issueInfo.getIssueType())) {
+ case CRITICAL:
+ criticalExceptions.add(toscaValidationIssue);
+ break;
+ case WARNING:
+ warningExceptions.add(toscaValidationIssue);
+ break;
+ default:
+ break;
+ }
+ }else{
+ notAnalyzadExceptions.add(toscaValidationIssue);
+ }
+ }else{//notAnalyzed
+ notAnalyzadExceptions.add(toscaValidationIssue);
+ }
+ }
+ logErrors(csarPath);
+ }
+
+ private void clearValidationIssuesLists(){
+ notAnalyzadExceptions.clear();
+ criticalExceptions.clear();
+ warningExceptions.clear();
+ }
+
+ private void logErrors(String inputPath) throws JToscaException{
+ //Warnings
+ int warningsCount = warningExceptions.size();
+ if (warningsCount > 0) {
+ log.warn("####################################################################################################");
+ log.warn("CSAR Warnings found! CSAR name - {}", inputPath);
+ log.warn("ToscaTemplate - verifyTemplate - {} Parsing Warning{} occurred...", warningsCount, (warningsCount > 1 ? "s" : ""));
+ for (JToscaValidationIssue info : warningExceptions) {
+ log.warn("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
+ }
+ log.warn("####################################################################################################");
+ }
+ //Criticals
+ int criticalsCount = criticalExceptions.size();
+ if (criticalsCount > 0) {
+ log.error("####################################################################################################");
+ log.error("ToscaTemplate - verifyTemplate - {} Parsing Critical{} occurred...", criticalsCount, (criticalsCount > 1 ? "s" : ""));
+ for (JToscaValidationIssue info : criticalExceptions) {
+ log.error("JTosca Exception [{}]: {}. CSAR name - {}", info.getCode(),info.getMessage(), inputPath);
+ }
+ throw new JToscaException(String.format("CSAR Validation Failed. CSAR name - {}. Please check logs for details.", inputPath), JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR.getValue());
+ }
+ }
+ public List<JToscaValidationIssue> getCriticalExceptions() {
+ return criticalExceptions;
+ }
+
+ public List<JToscaValidationIssue> getWarningExceptions() {
+ return warningExceptions;
+ }
+
+ public List<JToscaValidationIssue> getNotAnalyzadExceptions() {
+ return notAnalyzadExceptions;
+ }
+
+
+ private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
+ ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel();
+ String minVersion = level.getMinVersion();
+ String maxVersion = level.getMaxVersion();
+ if (cSarVersion != null) {
+ if ((GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) || (GeneralUtility.conformanceLevelCompare(cSarVersion, maxVersion) > 0)) {
+ throwConformanceLevelException(minVersion, maxVersion);
+ }
+ } else {
+ throwConformanceLevelException(minVersion, maxVersion);
+ }
+ }
+
+ private boolean isMatchConformanceLevel(String ValidationIssueVersion, String cSarVersion){
+ if (ValidationIssueVersion != null && cSarVersion != null) {
+ if ((GeneralUtility.conformanceLevelCompare(ValidationIssueVersion, cSarVersion) >= 0)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ private void throwConformanceLevelException(String minVersion, String maxVersion) throws SdcToscaParserException {
+ ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());
+ throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion, maxVersion), errorInfo.getCode());
+ }
+
+ private void throwSdcToscaParserException(JToscaException e) throws SdcToscaParserException {
+ ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
+ throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());
+ }
+
+
+
}
\ No newline at end of file