2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.utilities.yaml;
23 import org.openecomp.core.utilities.CommonMethods;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.yaml.snakeyaml.DumperOptions;
27 import org.yaml.snakeyaml.TypeDescription;
28 import org.yaml.snakeyaml.Yaml;
29 import org.yaml.snakeyaml.constructor.Constructor;
30 import org.yaml.snakeyaml.introspector.BeanAccess;
31 import org.yaml.snakeyaml.introspector.Property;
32 import org.yaml.snakeyaml.introspector.PropertyUtils;
33 import org.yaml.snakeyaml.nodes.MappingNode;
34 import org.yaml.snakeyaml.nodes.NodeTuple;
35 import org.yaml.snakeyaml.nodes.Tag;
36 import org.yaml.snakeyaml.parser.ParserException;
37 import org.yaml.snakeyaml.representer.Representer;
39 import java.beans.IntrospectionException;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.AbstractMap;
43 import java.util.LinkedHashMap;
44 import java.util.LinkedHashSet;
51 public class YamlUtil {
53 private static Logger logger = LoggerFactory.getLogger(YamlUtil.class);
58 * @param <T> the type parameter
59 * @param yamlContent the yaml content
60 * @param typClass the typ class
63 public <T> T yamlToObject(String yamlContent, Class<T> typClass) {
64 Constructor constructor = getConstructor(typClass);
65 constructor.setPropertyUtils(getPropertyUtils());
66 TypeDescription yamlFileDescription = new TypeDescription(typClass);
67 constructor.addTypeDescription(yamlFileDescription);
68 Yaml yaml = new Yaml(constructor);
69 T yamlObj = (T) yaml.load(yamlContent);
77 * @param <T> the type parameter
78 * @param yamlContent the yaml content
79 * @param typClass the typ class
82 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
84 Constructor constructor = getConstructor(typClass);
85 constructor.setPropertyUtils(getPropertyUtils());
86 TypeDescription yamlFileDescription = new TypeDescription(typClass);
87 constructor.addTypeDescription(yamlFileDescription);
88 Yaml yaml = new Yaml(constructor);
89 T yamlObj = (T) yaml.load(yamlContent);
90 if (yamlObj != null) {
94 throw new RuntimeException();
96 } catch (Exception exception) {
97 logger.error("Error will trying to convert yaml to object:" + exception.getMessage());
98 throw new RuntimeException(exception);
101 if (yamlContent != null) {
104 } catch (IOException ignore) {
114 * @param <T> the type parameter
115 * @param typClass the typ class
116 * @return the constructor
118 public <T> Constructor getConstructor(Class<T> typClass) {
119 return new StrictMapAppenderConstructor(typClass);
123 * Gets property utils.
125 * @return the property utils
127 protected PropertyUtils getPropertyUtils() {
128 return new MyPropertyUtils();
134 * @param yamlContent the yaml content
137 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
138 Yaml yaml = new Yaml();
139 Map<String, LinkedHashMap<String, Object>> yamlData =
140 (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
145 * Object to yaml string.
147 * @param <T> the type parameter
151 public <T> String objectToYaml(Object obj) {
152 DumperOptions options = new DumperOptions();
153 options.setPrettyFlow(true);
154 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
155 Representer representer = new CustomRepresenter();
156 representer.addClassTag(obj.getClass(), Tag.MAP);
157 representer.setPropertyUtils(new MyPropertyUtils());
159 Yaml yaml = new Yaml(representer, options);
160 return yaml.dump(obj);
164 * Load yaml file is input stream.
166 * @param yamlFullFileName the yaml full file name
167 * @return the input stream
169 public InputStream loadYamlFileIs(String yamlFullFileName) {
170 return CommonMethods.class.getResourceAsStream(yamlFullFileName);
174 * Is yaml file content valid boolean.
176 * @param yamlFullFileName the yaml full file name
177 * @return the boolean
179 public boolean isYamlFileContentValid(String yamlFullFileName) {
180 Yaml yaml = new Yaml();
182 Object loadResult = yaml.load(yamlFullFileName);
183 if (loadResult == null) {
187 } catch (Exception exception) {
193 private class CustomRepresenter extends Representer {
195 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
196 Object propertyValue, Tag customTag) {
197 if (propertyValue == null) {
200 NodeTuple defaultNode =
201 super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
203 return property.getName().equals("_default")
204 ? new NodeTuple(representData("default"), defaultNode.getValueNode())
210 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
211 //remove the bean type from the output yaml (!! ...)
212 if (!classTags.containsKey(javaBean.getClass())) {
213 addClassTag(javaBean.getClass(), Tag.MAP);
216 return super.representJavaBean(properties, javaBean);
222 * The type My property utils.
224 public class MyPropertyUtils extends PropertyUtils {
226 public Property getProperty(Class<?> type, String name) throws IntrospectionException {
227 if (name.equals("default")) {
230 return super.getProperty(type, name);
233 //Unsorted properties
235 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess beanAccess)
236 throws IntrospectionException {
237 return new LinkedHashSet<Property>(getPropertiesMap(type,
238 BeanAccess.FIELD).values());
244 * The type Strict map appender constructor.
246 protected class StrictMapAppenderConstructor extends Constructor {
249 * Instantiates a new Strict map appender constructor.
251 * @param theRoot the the root
253 public StrictMapAppenderConstructor(Class<?> theRoot) {
258 protected Map<Object, Object> constructMapping(MappingNode node) {
260 return super.constructMapping(node);
261 } catch (IllegalStateException exception) {
262 throw new ParserException("while parsing MappingNode", node.getStartMark(),
263 exception.getMessage(), node.getEndMark());
268 protected Map<Object, Object> createDefaultMap() {
269 final Map<Object, Object> delegate = super.createDefaultMap();
270 return new AbstractMap<Object, Object>() {
272 public Object put(Object key, Object value) {
273 if (delegate.containsKey(key)) {
274 throw new IllegalStateException("duplicate key: " + key);
276 return delegate.put(key, value);
280 public Set<Entry<Object, Object>> entrySet() {
281 return delegate.entrySet();