summaryrefslogtreecommitdiffstats
path: root/common/onap-tosca-datatype
diff options
context:
space:
mode:
Diffstat (limited to 'common/onap-tosca-datatype')
-rw-r--r--common/onap-tosca-datatype/pom.xml5
-rw-r--r--common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java36
2 files changed, 41 insertions, 0 deletions
diff --git a/common/onap-tosca-datatype/pom.xml b/common/onap-tosca-datatype/pom.xml
index c388652d81..f63bfa63d6 100644
--- a/common/onap-tosca-datatype/pom.xml
+++ b/common/onap-tosca-datatype/pom.xml
@@ -70,5 +70,10 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
</dependencies>
</project>
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
index 4030d3acd1..4ac60d40e4 100644
--- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
@@ -16,6 +16,10 @@
package org.onap.sdc.tosca.services;
+import java.util.List;
+import java.util.Optional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
@@ -29,6 +33,7 @@ import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.representer.Representer;
+
import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.InputStream;
@@ -43,6 +48,7 @@ import java.util.Set;
*/
@SuppressWarnings("unchecked")
public class YamlUtil {
+ private static final Logger LOGGER = LoggerFactory.getLogger(YamlUtil.class.getName());
private static final String DEFAULT = "default";
private static final String DEFAULT_STR = "_default";
@@ -140,6 +146,36 @@ public class YamlUtil {
return (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
}
+
+ /**
+ * Parse a YAML file to List
+ *
+ * @param yamlFileInputStream the YAML file input stream
+ * @return The YAML casted as a list
+ */
+ public static Optional<List<Object>> yamlToList(final InputStream yamlFileInputStream) {
+ List<Object> yamlList = null;
+ try {
+ yamlList = (List<Object>) read(yamlFileInputStream);
+ } catch (final ClassCastException ex) {
+ if (LOGGER.isWarnEnabled()) {
+ LOGGER.warn("Could not parse YAML to List.", ex);
+ }
+ }
+ return Optional.ofNullable(yamlList);
+ }
+
+ /**
+ * Parse a YAML file to Object
+ *
+ * @param yamlFileInputStream the YAML file input stream
+ * @return The YAML Object
+ */
+ public static Object read(final InputStream yamlFileInputStream) {
+ final Yaml yaml = new Yaml();
+ return yaml.load(yamlFileInputStream);
+ }
+
/**
* Object to yaml string.
* @param obj the obj