Revert "vLAN . Support Policies."
[sdc/sdc-tosca.git] / src / main / java / org / openecomp / sdc / tosca / parser / utils / YamlToObjectConverter.java
1 /*-
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.parser.utils;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.yaml.snakeyaml.Yaml;
27
28 import java.io.File;
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;
34
35 public class YamlToObjectConverter {
36
37         private static Logger log = LoggerFactory
38                         .getLogger(YamlToObjectConverter.class.getName());
39
40         private static HashMap<String, Yaml> yamls = new HashMap<String, Yaml>();
41
42         private static Yaml defaultYaml = new Yaml();
43
44         private static <T> Yaml getYamlByClassName(Class<T> className) {
45
46                 Yaml yaml = yamls.get(className.getName());
47                 if (yaml == null) {
48                         yaml = defaultYaml;
49                 }
50
51                 return yaml;
52         }
53
54         public <T> T convert(String dirPath, Class<T> className,
55                         String configFileName) {
56
57                 T config = null;
58
59                 try {
60
61                         String fullFileName = dirPath + File.separator + configFileName;
62
63                         config = convert(fullFileName, className);
64
65                 } catch (Exception e) {
66                         log.error("Failed to convert yaml file " + configFileName
67                                         + " to object.", e);
68                 } 
69
70                 return config;
71         }
72
73         public <T> T convert(String fullFileName, Class<T> className) {
74
75                 T config = null;
76
77                 Yaml yaml = getYamlByClassName(className);
78
79                 InputStream in = null;
80                 try {
81
82                         File f = new File(fullFileName);
83                         if (false == f.exists()) {
84                                 log.warn("The file " + fullFileName
85                                                 + " cannot be found. Ignore reading configuration.");
86                                 return null;
87                         }
88                         in = Files.newInputStream(Paths.get(fullFileName));
89
90                         config = yaml.loadAs(in, className);
91
92                         // System.out.println(config.toString());
93                 } catch (Exception e) {
94                         log.error("Failed to convert yaml file " + fullFileName
95                                         + " to object.", e);
96                 } finally {
97                         if (in != null) {
98                                 try {
99                                         in.close();
100                                 } catch (IOException e) {
101                                         // TODO Auto-generated catch block
102                                         e.printStackTrace();
103                                 }
104                         }
105                 }
106
107                 return config;
108         }
109
110         public <T> T convertFromString(String yamlContents, Class<T> className) {
111
112                 T config = null;
113                 Yaml yaml = new Yaml();
114                 try {
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);
122                 }
123
124                 return config;
125         }
126 }