diff options
author | Pavel Aharoni <pa0916@att.com> | 2017-05-24 13:23:12 +0300 |
---|---|---|
committer | Pavel Aharoni <pa0916@att.com> | 2017-05-24 13:23:12 +0300 |
commit | 9e430cd9d4722c3e614c8a2fd822cff1604be1f2 (patch) | |
tree | 565af8e16f17ae46bc901093aded0e19ac1b4c1f /sdc-tosca-parser/src/main/java | |
parent | 67a7f1d57b6b1d7c07dc3da4db51a387f90fef28 (diff) |
[SDC-24] error handling
Change-Id: Iac97052fab32f638d4cf52b094caad31f6d76902
Signed-off-by: Pavel Aharoni <pa0916@att.com>
Diffstat (limited to 'sdc-tosca-parser/src/main/java')
7 files changed, 266 insertions, 46 deletions
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java index 7cd9ed2..e83e6aa 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java @@ -8,18 +8,44 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URL; +import java.util.HashMap; +import java.util.Map; + public class ConfigurationManager { private static Logger log = LoggerFactory.getLogger(ConfigurationManager.class.getName()); - private static final String CONFIGURATION_FILE = "config/configuration.yaml"; + private static final String CONFIGURATION_DIR = "config/"; private static volatile ConfigurationManager instance; - private Configuration configuration; +// private Configuration configuration; +// private ErrorConfiguration errorConfiguration; + + Map<String, Object> configurations = new HashMap<String, Object>(); + + + private ConfigurationManager() { + initialConfigObjectsFromFiles(); + } + + private void initialConfigObjectsFromFiles() { + loadConfigurationClass(ErrorConfiguration.class); + loadConfigurationClass(Configuration.class); + } - private ConfigurationManager() { - URL url = Resources.getResource(CONFIGURATION_FILE); + private <T> void loadConfigurationClass(Class<T> clazz) { + T object = getObjectFromYaml(clazz); + configurations.put(clazz.getSimpleName(), object); + } + + + public <T> T getObjectFromYaml(Class<T> className) { + + + String configFileName = calculateFileName(className); + + URL url = Resources.getResource(CONFIGURATION_DIR + configFileName); String configFileContents = null; try { configFileContents = Resources.toString(url, Charsets.UTF_8); @@ -27,17 +53,16 @@ public class ConfigurationManager { log.error("ConfigurationManager - Failed to load configuration file"); } YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); - this.configuration = yamlToObjectConverter.convertFromString(configFileContents, Configuration.class); - } + T object = yamlToObjectConverter.convertFromString(configFileContents, className); - public Configuration getConfiguration() { - return configuration; + return object; } + public static ConfigurationManager getInstance() { if (instance == null) { - synchronized (ConfigurationManager.class){ - if (instance == null){ + synchronized (ConfigurationManager.class) { + if (instance == null) { instance = new ConfigurationManager(); } } @@ -45,7 +70,41 @@ public class ConfigurationManager { return instance; } - public void setConfiguration(Configuration configuration) { - this.configuration = configuration; + private static <T> String calculateFileName(Class<T> className) { + + String[] words = className.getSimpleName().split("(?=\\p{Upper})"); + + StringBuilder builder = new StringBuilder(); + + // There cannot be a null value returned from "split" - words != null is + // redundant + // if (words != null) { + boolean isFirst = true; + for (int i = 0; i < words.length; i++) { + + String word = words[i]; + if (word != null && !word.isEmpty()) { + if (!isFirst) { + builder.append("-"); + } else { + isFirst = false; + } + builder.append(words[i].toLowerCase()); + } + } + return builder.toString() + ".yaml"; + + /* + * } else { return className.getSimpleName().toLowerCase() + Constants.YAML_SUFFIX; } + */ + + } + + public ErrorConfiguration getErrorConfiguration() { + return (ErrorConfiguration) configurations.get((ErrorConfiguration.class.getSimpleName())); + } + + public Configuration getConfiguration() { + return (Configuration) configurations.get((Configuration.class.getSimpleName())); } } diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorConfiguration.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorConfiguration.java new file mode 100644 index 0000000..59e8c6d --- /dev/null +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorConfiguration.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdc.tosca.parser.config; + +import java.util.Map; + +public class ErrorConfiguration { + + private Map<String, ErrorInfo> errors; + + public Map<String, ErrorInfo> getErrors() { + return errors; + } + + public void setErrors(Map<String, ErrorInfo> errors) { + this.errors = errors; + } + + public ErrorInfo getErrorInfo(String key) { + ErrorInfo clone = null; + ErrorInfo other = errors.get(key); + if (other != null) { + clone = new ErrorInfo(); + clone.cloneData(other); + } + return clone; + } +} diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorInfo.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorInfo.java new file mode 100644 index 0000000..01df115 --- /dev/null +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorInfo.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdc.tosca.parser.config; + +public class ErrorInfo { + + private String code; + private String message; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public void cloneData(ErrorInfo other) { + this.code = other.getCode(); + this.message = other.getMessage(); + } + +} diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/SdcToscaParserErrors.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/SdcToscaParserErrors.java new file mode 100644 index 0000000..ce84a9c --- /dev/null +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/SdcToscaParserErrors.java @@ -0,0 +1,31 @@ +package org.openecomp.sdc.tosca.parser.config; + +import java.util.EnumMap; +import java.util.Map; + +import org.openecomp.sdc.toscaparser.api.utils.JToscaErrorCodes; + +public enum SdcToscaParserErrors { + + BAD_FORMAT, CONFORMANCE_LEVEL_ERROR, FILE_NOT_FOUND, GENERAL_ERROR; + + private static final Map<JToscaErrorCodes, SdcToscaParserErrors> JTOSCA_ERRORS = + new EnumMap<JToscaErrorCodes, SdcToscaParserErrors>(JToscaErrorCodes.class) {{ + + put(JToscaErrorCodes.GENERAL_ERROR, GENERAL_ERROR); + + put(JToscaErrorCodes.PATH_NOT_VALID, FILE_NOT_FOUND); + //CSAR contents problems + put(JToscaErrorCodes.MISSING_META_FILE, BAD_FORMAT); + put(JToscaErrorCodes.INVALID_META_YAML_CONTENT, BAD_FORMAT); + put(JToscaErrorCodes.ENTRY_DEFINITION_NOT_DEFINED, BAD_FORMAT); + put(JToscaErrorCodes.MISSING_ENTRY_DEFINITION_FILE, BAD_FORMAT); + put(JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR, BAD_FORMAT); + put(JToscaErrorCodes.INVALID_CSAR_FORMAT, BAD_FORMAT); + }}; + + public static SdcToscaParserErrors getSdcErrorByJToscaError(JToscaErrorCodes jToscaErrorCode) { + return JTOSCA_ERRORS.get(jToscaErrorCode); + } + +} diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java index f41141f..a7fd99d 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java @@ -1,12 +1,20 @@ package org.openecomp.sdc.tosca.parser.exceptions;
-public class SdcToscaParserException extends Exception{
- /**
- *
- */
- private static final long serialVersionUID = 626014844866501196L;
-
- public SdcToscaParserException(String string) {
- super(string);
- }
+public class SdcToscaParserException extends Exception {
+
+ private static final long serialVersionUID = 626014844866501196L;
+ private String code;
+
+ public SdcToscaParserException(String string, String code) {
+ super(string);
+ this.code = code;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public void setCode(String code) {
+ this.code = code;
+ }
}
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java index df1d470..c53dcec 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java @@ -22,6 +22,8 @@ package org.openecomp.sdc.tosca.parser.impl; import java.util.*; import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -49,7 +51,8 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { private static final String PATH_DELIMITER = "#"; private static final String PREFIX = "port_"; - private static final String[] SUFFIX = new String[]{"_network_role_tag", "_ip_requirements", "_subnetpoolid"}; + Pattern SUFFIX = Pattern.compile("(_network_role_tag|_ip_requirements|_subnetpoolid)$"); +// private static final String[] SUFFIX = new String[]{"_network_role_tag", "_ip_requirements", "_subnetpoolid"}; private ToscaTemplate toscaTemplate; private static Logger log = LoggerFactory.getLogger(SdcCsarHelperImpl.class.getName()); @@ -91,6 +94,11 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { public Map<String, Map<String, Object>> getCpPropertiesFromVfc(NodeTemplate vfc) { + if (vfc == null) { + log.error("getCpPropertiesFromVfc - vfc is null"); + return new HashMap<>(); + } + List<String> paths = new ArrayList<>(); paths.add("network_role_tag"); paths.add("ip_requirements#ip_count_required#count"); @@ -102,22 +110,25 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { Map<String, Map<String, Object>> cps = new HashMap<>(); - for (Map.Entry<String, Property> entry : props.entrySet()) { - String fullCpName = entry.getKey(); - - if (fullCpName.startsWith(PREFIX) && - Arrays.stream(SUFFIX).parallel().anyMatch(fullCpName::endsWith)) - { - //this is CP - get all it's properties according to paths list - String cpName = fullCpName.replaceAll("^("+PREFIX+")", "").replaceAll("("+String.join("|", SUFFIX)+")$", ""); - cps.put(cpName, new HashMap<>()); - for (String path: paths) { - String fullPathToSearch = PREFIX + cpName + "_" + path; - String value = getNodeTemplatePropertyLeafValue(vfc, fullPathToSearch); - if (value != null) { - value = StringUtils.stripStart(value, "["); - value = StringUtils.stripEnd(value, "]"); - cps.get(cpName).put(path, value); + if (props != null) { + for (Map.Entry<String, Property> entry : props.entrySet()) { + String fullCpName = entry.getKey(); + Matcher matcher = SUFFIX.matcher(fullCpName); + + if (fullCpName.startsWith(PREFIX) && matcher.find()) { + //this is CP - get all it's properties according to paths list + String cpName = fullCpName.replaceAll("^(" + PREFIX + ")", "").replaceAll(matcher.group(1), ""); + + List<String> propertiesToSearch = paths.stream().filter(i -> i.contains(StringUtils.stripStart(matcher.group(1), "_"))).collect(Collectors.toList()); + for (String item : propertiesToSearch) { + String fullPathToSearch = PREFIX + cpName + "_" + item; + Object value = getNodeTemplatePropertyAsObject(vfc, fullPathToSearch); + if (value != null) { + if (!cps.containsKey(cpName)) + cps.put(cpName, new HashMap<>()); + } + + cps.get(cpName).put(item, value); } } } @@ -569,7 +580,8 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { Object current = property.getValue(); return iterateProcessPath(1, current, split); } - log.error("processProperties - property not found"); + String propName = (split != null && split.length > 0 ? split[0] : null); + log.error("processProperties - property {} not found", propName); return null; } } diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java index 14c332c..2ddde1f 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java @@ -2,17 +2,18 @@ package org.openecomp.sdc.tosca.parser.impl; import org.openecomp.sdc.tosca.parser.api.ConformanceLevel;
import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
-import org.openecomp.sdc.tosca.parser.config.Configuration;
-import org.openecomp.sdc.tosca.parser.config.ConfigurationManager;
+import org.openecomp.sdc.tosca.parser.config.*;
import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.openecomp.sdc.tosca.parser.utils.GeneralUtility;
import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
import org.openecomp.sdc.toscaparser.api.common.JToscaException;
+import org.openecomp.sdc.toscaparser.api.utils.JToscaErrorCodes;
public class SdcToscaParserFactory {
private static volatile SdcToscaParserFactory instance;
private static Configuration configuration;
+ private static ErrorConfiguration errorConfiguration;
private SdcToscaParserFactory() {
@@ -28,6 +29,7 @@ public class SdcToscaParserFactory { if (instance == null) {
instance = new SdcToscaParserFactory();
configuration = ConfigurationManager.getInstance().getConfiguration();
+ errorConfiguration = ConfigurationManager.getInstance().getErrorConfiguration();
}
}
}
@@ -40,11 +42,15 @@ public class SdcToscaParserFactory { * @param csarPath - the absolute path to CSAR file.
* @return ISdcCsarHelper object.
* @throws SdcToscaParserException - in case the path or CSAR are invalid.
- * @throws JToscaException - in case the path or CSAR are invalid.
*/
- public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws JToscaException, SdcToscaParserException {
+ public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException {
synchronized (SdcToscaParserFactory.class) {
- ToscaTemplate tosca = new ToscaTemplate(csarPath, null, true, null);
+ ToscaTemplate tosca = null;
+ try {
+ tosca = new ToscaTemplate(csarPath, null, true, null);
+ } catch (JToscaException e) {
+ throwSdcToscaParserException(e);
+ }
SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca);
validateCsarVersion(sdcCsarHelperImpl.getConformanceLevel());
return sdcCsarHelperImpl;
@@ -57,11 +63,20 @@ public class SdcToscaParserFactory { String maxVersion = level.getMaxVersion();
if (cSarVersion != null) {
if ((GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) || (GeneralUtility.conformanceLevelCompare(cSarVersion, maxVersion) > 0)) {
- throw new SdcToscaParserException("Model is not supported. Parser supports versions " + minVersion + " to " + maxVersion);
+ throwConformanceLevelException(minVersion, maxVersion);
}
} else {
- throw new SdcToscaParserException("Model is not supported. Parser supports versions " + minVersion + " to " + maxVersion);
+ throwConformanceLevelException(minVersion, maxVersion);
}
}
+ private void throwConformanceLevelException(String minVersion, String maxVersion) throws SdcToscaParserException {
+ ErrorInfo errorInfo = errorConfiguration.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 = errorConfiguration.getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
+ throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());
+ }
}
\ No newline at end of file |