2 * ============LICENSE_START=======================================================
3 * sdc-distribution-client
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.onap.sdc.tosca.parser.utils;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.yaml.snakeyaml.Yaml;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.HashMap;
35 public class YamlToObjectConverter {
37 private static Logger log = LoggerFactory
38 .getLogger(YamlToObjectConverter.class.getName());
40 private static HashMap<String, Yaml> yamls = new HashMap<String, Yaml>();
42 private static Yaml defaultYaml = new Yaml();
44 private static <T> Yaml getYamlByClassName(Class<T> className) {
46 Yaml yaml = yamls.get(className.getName());
54 public <T> T convert(String dirPath, Class<T> className,
55 String configFileName) {
61 String fullFileName = dirPath + File.separator + configFileName;
63 config = convert(fullFileName, className);
65 } catch (Exception e) {
66 log.error("Failed to convert yaml file " + configFileName
73 public <T> T convert(String fullFileName, Class<T> className) {
77 Yaml yaml = getYamlByClassName(className);
79 InputStream in = null;
82 File f = new File(fullFileName);
83 if (false == f.exists()) {
84 log.warn("The file " + fullFileName
85 + " cannot be found. Ignore reading configuration.");
88 in = Files.newInputStream(Paths.get(fullFileName));
90 config = yaml.loadAs(in, className);
92 // System.out.println(config.toString());
93 } catch (Exception e) {
94 log.error("Failed to convert yaml file " + fullFileName
100 } catch (IOException e) {
101 // TODO Auto-generated catch block
110 public <T> T convertFromString(String yamlContents, Class<T> className) {
113 Yaml yaml = new Yaml();
115 Object data = yaml.load(yamlContents);
116 // convert it manually with jackson instead of using snakeyaml auto converter,
117 // because of problematic complex objects like JtoscaValidationIssueConfiguration
118 ObjectMapper mapper = new ObjectMapper();
119 config = mapper.convertValue(data, className);
120 } catch (Exception e){
121 log.error("Failed to convert YAML {} to object." , yamlContents, e);