update SDC-TOSCA package names
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / tosca / parser / impl / SdcToscaParserFactory.java
1 package org.onap.sdc.tosca.parser.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
22
23 public class SdcToscaParserFactory {
24         private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());
25
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() {}
32
33     /**
34      * Get an SdcToscaParserFactory instance.
35      * @return SdcToscaParserFactory instance.
36      */
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();
43                 }
44             }
45         }
46         return instance;
47     }
48
49     public static void setConfigurationManager(ConfigurationManager configurationManager) {
50         SdcToscaParserFactory.configurationManager = configurationManager;
51     }
52
53     /**
54      * Get an ISdcCsarHelper object for this CSAR file.
55      *
56      * @param csarPath - the absolute path to CSAR file.
57      * @return ISdcCsarHelper object.
58      * @throws SdcToscaParserException - in case the path or CSAR are invalid.
59      */
60     public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {
61         return init(csarPath, true);
62     }
63
64     /**
65      * Get an ISdcCsarHelper object for this CSAR file.
66      *
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.
71      */
72     public ISdcCsarHelper getSdcCsarHelper(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
73         return init(csarPath, resolveGetInput);
74     }
75
76     private ISdcCsarHelper init(String csarPath, boolean resolveGetInput) throws SdcToscaParserException {
77         synchronized (SdcToscaParserFactory.class) {
78             ToscaTemplate tosca = null;
79             try {
80                 tosca = new ToscaTemplate(csarPath, null, true, null, resolveGetInput);
81             } catch (JToscaException e) {
82                 throwSdcToscaParserException(e);
83             }
84             SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);
85             String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();
86             validateCsarVersion(cSarConformanceLevel);
87             try {
88                 handleErrorsByTypes(csarPath, cSarConformanceLevel);
89                         } catch (JToscaException e) {
90                 throwSdcToscaParserException(e);
91                         }
92             return sdcCsarHelperImpl;
93         }
94     }
95
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()) )
105                     .orElse(null);
106
107                         if(issueInfo != null){
108                     switch (JToscaValidationIssueType.valueOf(issueInfo.getIssueType())) {
109                         case CRITICAL:
110                             criticalExceptions.add(toscaValidationIssue);
111                             break;
112                         case WARNING:
113                             warningExceptions.add(toscaValidationIssue);
114                             break;
115                         default:
116                             break;
117                     }
118                 }else{
119                     notAnalyzadExceptions.add(toscaValidationIssue);
120                 }
121             }else{//notAnalyzed
122                 notAnalyzadExceptions.add(toscaValidationIssue);
123             }
124         }
125         logErrors(csarPath);
126     }
127
128     private void clearValidationIssuesLists(){
129         notAnalyzadExceptions.clear();
130         criticalExceptions.clear();
131         warningExceptions.clear();
132     }
133
134     private void logErrors(String inputPath) throws JToscaException{
135                 //Warnings
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);
143                         }
144                         log.warn("####################################################################################################");
145                 }
146                 //Criticals
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);
153                         }
154                         throw new JToscaException(String.format("CSAR Validation Failed. CSAR name - {}. Please check logs for details.", inputPath), JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR.getValue());
155                 }
156     }
157     public List<JToscaValidationIssue> getCriticalExceptions() {
158                 return criticalExceptions;
159         }
160
161         public List<JToscaValidationIssue> getWarningExceptions() {
162                 return warningExceptions;
163         }
164
165         public List<JToscaValidationIssue> getNotAnalyzadExceptions() {
166                 return notAnalyzadExceptions;
167         }
168
169
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);
177             }
178         } else {
179             throwConformanceLevelException(minVersion, maxVersion);
180         }
181     }
182
183     private boolean isMatchConformanceLevel(String ValidationIssueVersion, String cSarVersion){
184         if (ValidationIssueVersion != null && cSarVersion != null) {
185             if ((GeneralUtility.conformanceLevelCompare(ValidationIssueVersion, cSarVersion) >= 0)) {
186                 return true;
187             }
188         }
189         return false;
190     }
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());
194     }
195
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());
199     }
200
201
202
203 }