2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.sdc.tosca.services;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.LinkedHashMap;
21 import java.util.List;
23 import java.util.Optional;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.yaml.snakeyaml.DumperOptions;
28 import org.yaml.snakeyaml.LoaderOptions;
29 import org.yaml.snakeyaml.TypeDescription;
30 import org.yaml.snakeyaml.Yaml;
31 import org.yaml.snakeyaml.constructor.Constructor;
32 import org.yaml.snakeyaml.introspector.Property;
33 import org.yaml.snakeyaml.introspector.PropertyUtils;
34 import org.yaml.snakeyaml.nodes.MappingNode;
35 import org.yaml.snakeyaml.nodes.NodeTuple;
36 import org.yaml.snakeyaml.nodes.Tag;
37 import org.yaml.snakeyaml.representer.Representer;
42 @SuppressWarnings("unchecked")
43 public class YamlUtil {
45 static final String DEFAULT = "default";
46 static final String DEFAULT_STR = "_default";
47 private static final Logger LOGGER = LoggerFactory.getLogger(YamlUtil.class.getName());
50 * Parse a YAML file to List
52 * @param yamlFileInputStream the YAML file input stream
53 * @return The YAML casted as a list
55 public static Optional<List<Object>> yamlToList(final InputStream yamlFileInputStream) {
56 List<Object> yamlList = null;
58 yamlList = (List<Object>) read(yamlFileInputStream);
59 } catch (final ClassCastException ex) {
60 if (LOGGER.isWarnEnabled()) {
61 LOGGER.warn("Could not parse YAML to List.", ex);
64 return Optional.ofNullable(yamlList);
68 * Parse a YAML file to Object
70 * @param yamlFileInputStream the YAML file input stream
71 * @return The YAML Object
73 public static Object read(final InputStream yamlFileInputStream) {
74 return new Yaml().load(yamlFileInputStream);
80 * @param <T> the type parameter
81 * @param yamlContent the yaml content
82 * @param typClass the t class
85 public <T> T yamlToObject(String yamlContent, Class<T> typClass) {
86 Constructor constructor = getConstructor(typClass);
87 constructor.setPropertyUtils(getPropertyUtils());
88 TypeDescription yamlFileDescription = new TypeDescription(typClass);
89 constructor.addTypeDescription(yamlFileDescription);
90 T yamlObj = new Yaml(constructor, new Representer(new DumperOptions()), new DumperOptions(), getLoaderOptions()).load(yamlContent);
92 //noinspection ResultOfMethodCallIgnored
97 public InputStream loadYamlFileIs(String yamlFullFileName) {
98 return YamlUtil.class.getResourceAsStream(yamlFullFileName);
104 * @param <T> the type parameter
105 * @param yamlContent the yaml content
106 * @param typClass the t class
109 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
111 Constructor constructor = getConstructor(typClass);
112 constructor.setAllowDuplicateKeys(false);
113 constructor.setPropertyUtils(getPropertyUtils());
114 TypeDescription yamlFileDescription = new TypeDescription(typClass);
115 constructor.addTypeDescription(yamlFileDescription);
116 //No Yaml Constructor takes only Constructor and LoaderOptions, that is why I had to pass anonymous Representer and DumperOptions objects
117 T yamlObj = new Yaml(constructor, new Representer(new DumperOptions()), new DumperOptions(), getLoaderOptions()).load(yamlContent);
118 if (yamlObj != null) {
119 //noinspection ResultOfMethodCallIgnored
123 throw new RuntimeException();
125 } catch (Exception exception) {
126 throw new RuntimeException(exception);
129 if (yamlContent != null) {
132 } catch (IOException ignore) {
138 private LoaderOptions getLoaderOptions() {
139 LoaderOptions options = new LoaderOptions();
140 options.setAllowDuplicateKeys(false);
141 options.setMaxAliasesForCollections(9999);
148 * @param <T> the type parameter
149 * @param typClass the t class
150 * @return the constructor
152 public <T> Constructor getConstructor(Class<T> typClass) {
153 return new StrictMapAppenderConstructor(typClass);
157 * Gets property utils.
159 * @return the property utils
161 protected PropertyUtils getPropertyUtils() {
162 return new MyPropertyUtils();
168 * @param yamlContent the yaml content
171 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
172 return new Yaml().load(yamlContent);
176 * Object to yaml string.
181 public String objectToYaml(Object obj) {
182 DumperOptions options = new DumperOptions();
183 options.setPrettyFlow(true);
184 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
185 Representer representer = new CustomRepresenter();
186 representer.addClassTag(obj.getClass(), Tag.MAP);
187 representer.setPropertyUtils(new MyPropertyUtils());
188 Yaml yaml = new Yaml(representer, options);
189 return yaml.dump(obj);
193 * Is yaml file content valid boolean.
195 * @param yamlFullFileName the yaml full file name
196 * @return the boolean
198 public boolean isYamlFileContentValid(String yamlFullFileName) {
200 return new Yaml().load(yamlFullFileName) != null;
201 } catch (Exception exception) {
206 private class CustomRepresenter extends Representer {
209 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
210 //remove the bean type from the output yaml (!! ...)
211 if (!classTags.containsKey(javaBean.getClass())) {
212 addClassTag(javaBean.getClass(), Tag.MAP);
214 return super.representJavaBean(properties, javaBean);
218 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
219 if (propertyValue == null) {
222 NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
223 return DEFAULT_STR.equals(property.getName()) ? new NodeTuple(representData(DEFAULT), defaultNode.getValueNode()) : defaultNode;