/*- * ============LICENSE_START======================================================= * sdc-tosca * ================================================================================ * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ 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.api.ISdcCsarHelper; 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.enums.JToscaValidationIssueType; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; import org.onap.sdc.tosca.parser.utils.GeneralUtility; import org.onap.sdc.toscaparser.api.ToscaTemplate; import org.onap.sdc.toscaparser.api.common.JToscaException; import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue; 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 criticalExceptions = new ArrayList<>(); private List warningExceptions = new ArrayList<>(); private List 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 issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues() .get(toscaValidationIssue.getCode()); if (issueInfos != null && !issueInfos.isEmpty()) { final JToscaValidationIssueInfo 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 getCriticalExceptions() { return criticalExceptions; } public List getWarningExceptions() { return warningExceptions; } public List getNotAnalyzadExceptions() { return notAnalyzadExceptions; } private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException { ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel(); String minVersion = level.getMinVersion(); if (cSarVersion != null) { if (GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) { throwConformanceLevelException(minVersion); } } else { throwConformanceLevelException(minVersion); } } 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) throws SdcToscaParserException { ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString()); throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion), 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()); } }