re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / YamlToObjectConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.common.util;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.openecomp.sdc.be.config.Configuration.*;
25 import org.openecomp.sdc.be.config.Configuration.ElasticSearchConfig.IndicesTimeFrequencyEntry;
26 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
27 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.ComponentArtifactTypesConfig;
28 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.CreateTopicConfig;
29 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
30 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionStatusTopicConfig;
31 import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.fe.config.Configuration.FeMonitoringConfig;
34 import org.yaml.snakeyaml.TypeDescription;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.error.YAMLException;
37 import org.yaml.snakeyaml.introspector.PropertyUtils;
38 import org.yaml.snakeyaml.nodes.Node;
39
40 import java.io.ByteArrayInputStream;
41 import java.io.File;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 public class YamlToObjectConverter {
50
51         private static Logger log = Logger.getLogger(YamlToObjectConverter.class.getName());
52
53         private static HashMap<String, Yaml> yamls = new HashMap<>();
54
55         private static Yaml defaultYaml = new Yaml();
56
57         static {
58
59                 org.yaml.snakeyaml.constructor.Constructor deConstructor = new org.yaml.snakeyaml.constructor.Constructor(
60                                 DistributionEngineConfiguration.class);
61                 TypeDescription deDescription = new TypeDescription(DistributionEngineConfiguration.class);
62                 deDescription.putListPropertyType("distributionStatusTopic", DistributionStatusTopicConfig.class);
63                 deDescription.putListPropertyType("distribNotifServiceArtifactTypes", ComponentArtifactTypesConfig.class);
64                 deDescription.putListPropertyType("distribNotifResourceArtifactTypes", ComponentArtifactTypesConfig.class);
65                 deDescription.putListPropertyType("createTopic", CreateTopicConfig.class);
66                 deDescription.putListPropertyType("distributionNotificationTopic", DistributionNotificationTopicConfig.class);
67                 deConstructor.addTypeDescription(deDescription);
68                 Yaml yaml = new Yaml(deConstructor);
69                 yamls.put(DistributionEngineConfiguration.class.getName(), yaml);
70
71                 // FE conf
72                 org.yaml.snakeyaml.constructor.Constructor feConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
73                                 org.openecomp.sdc.fe.config.Configuration.class);
74                 TypeDescription feConfDescription = new TypeDescription(org.openecomp.sdc.fe.config.Configuration.class);
75                 feConfDescription.putListPropertyType("systemMonitoring", FeMonitoringConfig.class);
76                 feConfConstructor.addTypeDescription(feConfDescription);
77                 yamls.put(org.openecomp.sdc.fe.config.Configuration.class.getName(), new Yaml(feConfConstructor));
78
79                 // BE conf
80                 org.yaml.snakeyaml.constructor.Constructor beConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
81                                 org.openecomp.sdc.be.config.Configuration.class);
82                 TypeDescription beConfDescription = new TypeDescription(org.openecomp.sdc.be.config.Configuration.class);
83                 beConfConstructor.addTypeDescription(beConfDescription);
84
85                 // systemMonitoring
86                 beConfDescription.putListPropertyType("systemMonitoring", BeMonitoringConfig.class);
87
88                 // elasticSearch
89                 beConfDescription.putListPropertyType("elasticSearch", ElasticSearchConfig.class);
90                 TypeDescription esDescription = new TypeDescription(ElasticSearchConfig.class);
91                 esDescription.putListPropertyType("indicesTimeFrequency", IndicesTimeFrequencyEntry.class);
92                 beConfConstructor.addTypeDescription(esDescription);
93
94                 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
95                 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
96                                 ArtifactTypeConfig.class);
97                 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
98                                 ArtifactTypeConfig.class);
99
100                 // onboarding
101                 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
102
103                 // ecompPortal
104                 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
105                 // switchoverDetector
106                 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
107
108                 // ApplicationL1Cache
109                 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
110
111                 // ApplicationL2Cache
112                 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
113
114                 // tosca validators config
115                 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
116
117                 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
118
119                 // HEAT deployment artifact
120                 org.yaml.snakeyaml.constructor.Constructor depArtHeatConstructor = new org.yaml.snakeyaml.constructor.Constructor(
121                                 DeploymentArtifactHeatConfiguration.class);
122                 PropertyUtils propertyUtils = new PropertyUtils();
123                 // Skip properties which are found in YAML but not found in POJO
124                 propertyUtils.setSkipMissingProperties(true);
125                 depArtHeatConstructor.setPropertyUtils(propertyUtils);
126                 yamls.put(org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration.class.getName(),
127                                 new Yaml(depArtHeatConstructor));
128
129         }
130
131         private static <T> Yaml getYamlByClassName(Class<T> className) {
132
133                 Yaml yaml = yamls.get(className.getName());
134                 if (yaml == null) {
135                         yaml = defaultYaml;
136                 }
137
138                 return yaml;
139         }
140
141         public <T> T convert(String dirPath, Class<T> className, String configFileName) {
142
143                 T config = null;
144
145                 try {
146
147                         String fullFileName = dirPath + File.separator + configFileName;
148
149                         config = convert(fullFileName, className);
150
151                 } catch (Exception e) {
152                         log.error("Failed to convert yaml file {} to object.", configFileName,e);
153                 }
154
155                 return config;
156         }
157
158         public class MyYamlConstructor extends org.yaml.snakeyaml.constructor.Constructor {
159                 private HashMap<String, Class<?>> classMap = new HashMap<>();
160
161                 public MyYamlConstructor(Class<? extends Object> theRoot) {
162                         super(theRoot);
163                         classMap.put(DistributionEngineConfiguration.class.getName(), DistributionEngineConfiguration.class);
164                         classMap.put(DistributionStatusTopicConfig.class.getName(), DistributionStatusTopicConfig.class);
165                 }
166
167                 /*
168                  * This is a modified version of the Constructor. Rather than using a
169                  * class loader to get external classes, they are already predefined
170                  * above. This approach works similar to the typeTags structure in the
171                  * original constructor, except that class information is pre-populated
172                  * during initialization rather than runtime.
173                  *
174                  * @see
175                  * org.yaml.snakeyaml.constructor.Constructor#getClassForNode(org.yaml.
176                  * snakeyaml.nodes.Node)
177                  */
178                 protected Class<?> getClassForNode(Node node) {
179                         String name = node.getTag().getClassName();
180                         Class<?> cl = classMap.get(name);
181                         if (cl == null)
182                                 throw new YAMLException("Class not found: " + name);
183                         else
184                                 return cl;
185                 }
186         }
187
188         public <T> T convert(String fullFileName, Class<T> className) {
189
190                 T config = null;
191
192                 Yaml yaml = getYamlByClassName(className);
193
194                 InputStream in = null;
195                 try {
196
197                         File f = new File(fullFileName);
198                         if (!f.exists()) {
199                                 log.warn("The file " + fullFileName + " cannot be found. Ignore reading configuration.");
200                                 return null;
201                         }
202                         in = Files.newInputStream(Paths.get(fullFileName));
203
204                         config = yaml.loadAs(in, className);
205
206                         // System.out.println(config.toString());
207                 } catch (Exception e) {
208                         log.error("Failed to convert yaml file {} to object.", fullFileName, e);
209                 } finally {
210                         if (in != null) {
211                                 try {
212                                         in.close();
213                                 } catch (IOException e) {
214                                         log.debug("Failed to close input stream", e);
215                                 }
216                         }
217                 }
218
219                 return config;
220         }
221
222         public <T> T convert(byte[] fileContents, Class<T> className) {
223
224                 T config = null;
225
226                 Yaml yaml = getYamlByClassName(className);
227
228                 InputStream in = null;
229                 try {
230
231                         in = new ByteArrayInputStream(fileContents);
232
233                         config = yaml.loadAs(in, className);
234
235                 } catch (Exception e) {
236                         log.error("Failed to convert yaml file to object", e);
237                 } finally {
238                         if (in != null) {
239                                 try {
240                                         in.close();
241                                 } catch (IOException e) {
242                                         log.debug("Failed to close input stream", e);
243                                 }
244                         }
245                 }
246
247                 return config;
248         }
249
250         public boolean isValidYamlEncoded64(byte[] fileContents) {
251                 log.trace("Received Base64 data - decoding before validating...");
252                 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
253                 
254                 return isValidYaml(decodedFileContents);
255         }
256         
257         public boolean isValidYaml(byte[] fileContents) {
258                 try {
259                         
260                         Iterable<Object> mappedToscaTemplateIt =  defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
261                         
262                          for (Object o : mappedToscaTemplateIt) {
263                         System.out.println("Loaded object type:" + o.getClass());
264                         Map<String, Object> map = (Map<String, Object>) o;
265                          }
266                         
267                 } catch (Exception e) {
268                         log.error("Failed to convert yaml file to object - yaml is invalid", e);
269                         return false;
270                 }
271                 return true;
272         }
273 }