summaryrefslogtreecommitdiffstats
path: root/sdc-tosca-parser/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'sdc-tosca-parser/src/main/java')
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ConformanceLevel.java25
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java4
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/Version.java48
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/Configuration.java16
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java51
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java19
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java105
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/GeneralUtility.java53
-rw-r--r--sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/YamlToObjectConverter.java123
9 files changed, 399 insertions, 45 deletions
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ConformanceLevel.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ConformanceLevel.java
new file mode 100644
index 0000000..a026938
--- /dev/null
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ConformanceLevel.java
@@ -0,0 +1,25 @@
+package org.openecomp.sdc.tosca.parser.api;
+
+public class ConformanceLevel {
+
+ private String minVersion;
+ private String maxVersion;
+
+ public String getMaxVersion() {
+ return maxVersion;
+ }
+
+ public void setMaxVersion(String maxVersion) {
+ this.maxVersion = maxVersion;
+ }
+
+ public String getMinVersion() {
+ return minVersion;
+ }
+
+ public void setMinVersion(String minVersion) {
+ this.minVersion = minVersion;
+ }
+
+
+}
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java
index dd0e96c..7cc9022 100644
--- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java
@@ -246,5 +246,7 @@ public interface ISdcCsarHelper {
* @return - the service inputs list.
*/
public List<Input> getServiceInputs();
-
+
+
+ public String getConformanceLevel();
}
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/Version.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/Version.java
new file mode 100644
index 0000000..473e3a8
--- /dev/null
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/Version.java
@@ -0,0 +1,48 @@
+package org.openecomp.sdc.tosca.parser.api;
+
+public class Version implements Comparable<Version> {
+
+ private String version;
+
+ public final String get() {
+ return this.version;
+ }
+
+ public Version(String version) {
+ if(version == null)
+ throw new IllegalArgumentException("Version can not be null");
+ if(!version.matches("[0-9]+(\\.[0-9]+)*"))
+ throw new IllegalArgumentException("Invalid version format");
+ this.version = version;
+ }
+
+ @Override public int compareTo(Version that) {
+ if(that == null)
+ return 1;
+ String[] thisParts = this.get().split("\\.");
+ String[] thatParts = that.get().split("\\.");
+ int length = Math.max(thisParts.length, thatParts.length);
+ for(int i = 0; i < length; i++) {
+ int thisPart = i < thisParts.length ?
+ Integer.parseInt(thisParts[i]) : 0;
+ int thatPart = i < thatParts.length ?
+ Integer.parseInt(thatParts[i]) : 0;
+ if(thisPart < thatPart)
+ return -1;
+ if(thisPart > thatPart)
+ return 1;
+ }
+ return 0;
+ }
+
+ @Override public boolean equals(Object that) {
+ if(this == that)
+ return true;
+ if(that == null)
+ return false;
+ if(this.getClass() != that.getClass())
+ return false;
+ return this.compareTo((Version) that) == 0;
+ }
+
+}
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/Configuration.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/Configuration.java
new file mode 100644
index 0000000..1d00d14
--- /dev/null
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/Configuration.java
@@ -0,0 +1,16 @@
+package org.openecomp.sdc.tosca.parser.config;
+
+import org.openecomp.sdc.tosca.parser.api.ConformanceLevel;
+
+public class Configuration {
+
+ private ConformanceLevel conformanceLevel;
+
+ public ConformanceLevel getConformanceLevel() {
+ return conformanceLevel;
+ }
+
+ public void setConformanceLevel(ConformanceLevel conformanceLevel) {
+ this.conformanceLevel = conformanceLevel;
+ }
+}
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
new file mode 100644
index 0000000..7cd9ed2
--- /dev/null
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java
@@ -0,0 +1,51 @@
+package org.openecomp.sdc.tosca.parser.config;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import org.openecomp.sdc.tosca.parser.utils.YamlToObjectConverter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class ConfigurationManager {
+
+ private static Logger log = LoggerFactory.getLogger(ConfigurationManager.class.getName());
+
+ private static final String CONFIGURATION_FILE = "config/configuration.yaml";
+ private static volatile ConfigurationManager instance;
+ private Configuration configuration;
+
+
+ private ConfigurationManager() {
+ URL url = Resources.getResource(CONFIGURATION_FILE);
+ String configFileContents = null;
+ try {
+ configFileContents = Resources.toString(url, Charsets.UTF_8);
+ } catch (IOException e) {
+ log.error("ConfigurationManager - Failed to load configuration file");
+ }
+ YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
+ this.configuration = yamlToObjectConverter.convertFromString(configFileContents, Configuration.class);
+ }
+
+ public Configuration getConfiguration() {
+ return configuration;
+ }
+
+ public static ConfigurationManager getInstance() {
+ if (instance == null) {
+ synchronized (ConfigurationManager.class){
+ if (instance == null){
+ instance = new ConfigurationManager();
+ }
+ }
+ }
+ return instance;
+ }
+
+ public void setConfiguration(Configuration configuration) {
+ this.configuration = configuration;
+ }
+}
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 000b381..9280322 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
@@ -407,6 +407,25 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper {
return nodeTemplate.getTypeDefinition().getType();
}
+ @Override
+ public String getConformanceLevel() {
+ LinkedHashMap<String, Object> csarMeta = toscaTemplate.getMetaProperties("csar.meta");
+ if (csarMeta == null){
+ log.warn("No csar.meta file is found in CSAR - this file should hold the conformance level of the CSAR. This might be OK for older CSARs.");
+ return null;
+ }
+
+ Object conformanceLevel = csarMeta.get("SDC-TOSCA-Definitions-Version");
+ if (conformanceLevel != null){
+ String confLevelStr = conformanceLevel.toString();
+ log.debug("CSAR conformance level is {}", confLevelStr);
+ return confLevelStr;
+ } else {
+ log.error("Invalid csar.meta file - no entry found for SDC-TOSCA-Definitions-Version key. This entry should hold the conformance level.");
+ return null;
+ }
+ }
+
/************************************* helper functions ***********************************/
private List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate nodeTemplate, String sdcType){
if (nodeTemplate == 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 3f62e12..62b5acb 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
@@ -1,49 +1,74 @@
package org.openecomp.sdc.tosca.parser.impl;
-import java.io.IOException;
-import java.util.List;
-
+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.exceptions.SdcToscaParserException;
-import org.openecomp.sdc.toscaparser.api.NodeTemplate;
+import org.openecomp.sdc.tosca.parser.utils.GeneralUtility;
import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
import org.openecomp.sdc.toscaparser.api.common.JToscaException;
-public class SdcToscaParserFactory{
-
- private static SdcToscaParserFactory instance;
-
- private SdcToscaParserFactory(){}
-
- /**
- * Get an SdcToscaParserFactory instance.
- * After parsing work is done, it must be closed using the close() method.
- */
- public static SdcToscaParserFactory getInstance() {
- if (instance == null) {
- synchronized (SdcToscaParserFactory.class) {
- if (instance == null) {
- instance = new SdcToscaParserFactory();
- }
- }
- }
- return instance;
- }
-
- /**
- * Get an ISdcCsarHelper object for this CSAR file.
- * @param csarPath - the path to CSAR file.
- * @return ISdcCsarHelper object.
- * @throws SdcToscaParserException - in case the path or CSAR are invalid.
- * @throws JToscaException
- */
- public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException, JToscaException, IOException{
- //TODO add logic to check if legal file and csar
- synchronized (SdcToscaParserFactory.class) {
- ToscaTemplate tosca = new ToscaTemplate(csarPath, null, true, null);
- SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca);
- return sdcCsarHelperImpl;
- }
- }
+import java.io.IOException;
+
+public class SdcToscaParserFactory {
+
+ private static volatile SdcToscaParserFactory instance;
+ private static Configuration configuration;
+
+ private SdcToscaParserFactory() {
+
+ }
+
+ /**
+ * Get an SdcToscaParserFactory instance.
+ * After parsing work is done, it must be closed using the close() method.
+ */
+ public static SdcToscaParserFactory getInstance() throws IOException {
+ if (instance == null) {
+ synchronized (SdcToscaParserFactory.class) {
+ if (instance == null) {
+ instance = new SdcToscaParserFactory();
+ configuration = ConfigurationManager.getInstance().getConfiguration();
+ }
+ }
+ }
+ return instance;
+ }
+
+ /**
+ * 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.
+ * @throws JToscaException
+ */
+ public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws JToscaException, IOException, SdcToscaParserException {
+ //TODO add logic to check if legal file and csar
+ synchronized (SdcToscaParserFactory.class) {
+
+
+ ToscaTemplate tosca = new ToscaTemplate(csarPath, null, true, null);
+ SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca);
+ if (sdcCsarHelperImpl != null) {
+ validateCsarVersion(sdcCsarHelperImpl.getConformanceLevel());
+ }
+ return sdcCsarHelperImpl;
+ }
+ }
+
+ private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
+ ConformanceLevel level = configuration.getConformanceLevel();
+ String minVersion = level.getMinVersion();
+ 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);
+ }
+ } else {
+ throw new SdcToscaParserException("Model is not supported. Parser supports versions " + minVersion + " to " + maxVersion);
+ }
+ }
} \ No newline at end of file
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/GeneralUtility.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/GeneralUtility.java
index e4d92f5..da066fc 100644
--- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/GeneralUtility.java
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/GeneralUtility.java
@@ -1,8 +1,53 @@
package org.openecomp.sdc.tosca.parser.utils;
+import java.util.Arrays;
+
public class GeneralUtility {
-
- public static boolean isEmptyString(String str){
- return str == null || str.trim().isEmpty();
- }
+
+ public static boolean isEmptyString(String str) {
+ return str == null || str.trim().isEmpty();
+ }
+
+
+ /**
+ * Compares two version strings.
+ * <p>
+ * Use this instead of String.compareTo() for a non-lexicographical
+ * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
+ *
+ * @param str1 a string of ordinal numbers separated by decimal points.
+ * @param str2 a string of ordinal numbers separated by decimal points.
+ * @return The result is a negative integer if str1 is _numerically_ less than str2.
+ * The result is a positive integer if str1 is _numerically_ greater than str2.
+ * The result is zero if the strings are _numerically_ equal.
+ * @note It does not work if "1.10" is supposed to be equal to "1.10.0".
+ */
+ public static int conformanceLevelCompare(String str1, String str2) {
+ String[] vals1 = str1.split("\\.");
+ String[] vals2 = str2.split("\\.");
+ int i = 0;
+ // set index to first non-equal ordinal or length of shortest version string
+ while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
+ i++;
+ }
+ // compare first non-equal ordinal number
+ if (i < vals1.length && i < vals2.length) {
+ int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
+ return Integer.signum(diff);
+ }
+ //in case of 0 after the . e.g: "3" = "3.0" or "3.0.0.0" = "3.0"
+ str2 = str2.substring(i).replace(".", "");
+ str1 = str1.substring(i).replace(".", "");
+ if ((!(str1.equals(""))) && Integer.valueOf(str1) == 0){
+ vals1 = Arrays.copyOf(vals1, i);
+ }
+ if ((!(str2.equals(""))) && Integer.valueOf(str2) == 0){
+ vals2 = Arrays.copyOf(vals2, i);
+ }
+
+ // the strings are equal or one string is a substring of the other
+ // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
+ return Integer.signum(vals1.length - vals2.length);
+ }
+
}
diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/YamlToObjectConverter.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/YamlToObjectConverter.java
new file mode 100644
index 0000000..44444d2
--- /dev/null
+++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/utils/YamlToObjectConverter.java
@@ -0,0 +1,123 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * sdc-distribution-client
+ * ================================================================================
+ * 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.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+
+public class YamlToObjectConverter {
+
+ private static Logger log = LoggerFactory
+ .getLogger(YamlToObjectConverter.class.getName());
+
+ private static HashMap<String, Yaml> yamls = new HashMap<String, Yaml>();
+
+ private static Yaml defaultYaml = new Yaml();
+
+ private static <T> Yaml getYamlByClassName(Class<T> className) {
+
+ Yaml yaml = yamls.get(className.getName());
+ if (yaml == null) {
+ yaml = defaultYaml;
+ }
+
+ return yaml;
+ }
+
+ public <T> T convert(String dirPath, Class<T> className,
+ String configFileName) {
+
+ T config = null;
+
+ try {
+
+ String fullFileName = dirPath + File.separator + configFileName;
+
+ config = convert(fullFileName, className);
+
+ } catch (Exception e) {
+ log.error("Failed to convert yaml file " + configFileName
+ + " to object.", e);
+ }
+
+ return config;
+ }
+
+ public <T> T convert(String fullFileName, Class<T> className) {
+
+ T config = null;
+
+ Yaml yaml = getYamlByClassName(className);
+
+ InputStream in = null;
+ try {
+
+ File f = new File(fullFileName);
+ if (false == f.exists()) {
+ log.warn("The file " + fullFileName
+ + " cannot be found. Ignore reading configuration.");
+ return null;
+ }
+ in = Files.newInputStream(Paths.get(fullFileName));
+
+ config = yaml.loadAs(in, className);
+
+ // System.out.println(config.toString());
+ } catch (Exception e) {
+ log.error("Failed to convert yaml file " + fullFileName
+ + " to object.", e);
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return config;
+ }
+
+ public <T> T convertFromString(String yamlContents, Class<T> className) {
+
+ T config = null;
+
+ Yaml yaml = getYamlByClassName(className);
+
+ try {
+ config = yaml.loadAs(yamlContents, className);
+ } catch (Exception e){
+ log.error("Failed to convert YAML {} to object." , yamlContents, e);
+ }
+
+ return config;
+ }
+}