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.sdc.common.util;
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;
40 import java.io.ByteArrayInputStream;
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;
49 public class YamlToObjectConverter {
51 private static Logger log = Logger.getLogger(YamlToObjectConverter.class.getName());
53 private static HashMap<String, Yaml> yamls = new HashMap<>();
55 private static Yaml defaultYaml = new Yaml();
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);
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));
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);
86 beConfDescription.putListPropertyType("systemMonitoring", BeMonitoringConfig.class);
89 beConfDescription.putListPropertyType("elasticSearch", ElasticSearchConfig.class);
90 TypeDescription esDescription = new TypeDescription(ElasticSearchConfig.class);
91 esDescription.putListPropertyType("indicesTimeFrequency", IndicesTimeFrequencyEntry.class);
92 beConfConstructor.addTypeDescription(esDescription);
94 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
95 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
96 ArtifactTypeConfig.class);
97 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
98 ArtifactTypeConfig.class);
101 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
104 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
105 // switchoverDetector
106 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
108 // ApplicationL1Cache
109 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
111 // ApplicationL2Cache
112 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
114 // tosca validators config
115 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
117 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
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));
131 private static <T> Yaml getYamlByClassName(Class<T> className) {
133 Yaml yaml = yamls.get(className.getName());
141 public <T> T convert(String dirPath, Class<T> className, String configFileName) {
147 String fullFileName = dirPath + File.separator + configFileName;
149 config = convert(fullFileName, className);
151 } catch (Exception e) {
152 log.error("Failed to convert yaml file {} to object.", configFileName,e);
158 public class MyYamlConstructor extends org.yaml.snakeyaml.constructor.Constructor {
159 private HashMap<String, Class<?>> classMap = new HashMap<>();
161 public MyYamlConstructor(Class<? extends Object> theRoot) {
163 classMap.put(DistributionEngineConfiguration.class.getName(), DistributionEngineConfiguration.class);
164 classMap.put(DistributionStatusTopicConfig.class.getName(), DistributionStatusTopicConfig.class);
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.
175 * org.yaml.snakeyaml.constructor.Constructor#getClassForNode(org.yaml.
176 * snakeyaml.nodes.Node)
178 protected Class<?> getClassForNode(Node node) {
179 String name = node.getTag().getClassName();
180 Class<?> cl = classMap.get(name);
182 throw new YAMLException("Class not found: " + name);
188 public <T> T convert(String fullFileName, Class<T> className) {
192 Yaml yaml = getYamlByClassName(className);
194 InputStream in = null;
197 File f = new File(fullFileName);
199 log.warn("The file " + fullFileName + " cannot be found. Ignore reading configuration.");
202 in = Files.newInputStream(Paths.get(fullFileName));
204 config = yaml.loadAs(in, className);
206 // System.out.println(config.toString());
207 } catch (Exception e) {
208 log.error("Failed to convert yaml file {} to object.", fullFileName, e);
213 } catch (IOException e) {
214 log.debug("Failed to close input stream", e);
222 public <T> T convert(byte[] fileContents, Class<T> className) {
226 Yaml yaml = getYamlByClassName(className);
228 InputStream in = null;
231 in = new ByteArrayInputStream(fileContents);
233 config = yaml.loadAs(in, className);
235 } catch (Exception e) {
236 log.error("Failed to convert yaml file to object", e);
241 } catch (IOException e) {
242 log.debug("Failed to close input stream", e);
250 public boolean isValidYamlEncoded64(byte[] fileContents) {
251 log.trace("Received Base64 data - decoding before validating...");
252 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
254 return isValidYaml(decodedFileContents);
257 public boolean isValidYaml(byte[] fileContents) {
260 Iterable<Object> mappedToscaTemplateIt = defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
262 for (Object o : mappedToscaTemplateIt) {
263 System.out.println("Loaded object type:" + o.getClass());
264 Map<String, Object> map = (Map<String, Object>) o;
267 } catch (Exception e) {
268 log.error("Failed to convert yaml file to object - yaml is invalid", e);