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.enums.EcompLoggerErrorCode;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.fe.config.Configuration.FeMonitoringConfig;
35 import org.yaml.snakeyaml.TypeDescription;
36 import org.yaml.snakeyaml.Yaml;
37 import org.yaml.snakeyaml.error.YAMLException;
38 import org.yaml.snakeyaml.introspector.PropertyUtils;
39 import org.yaml.snakeyaml.nodes.Node;
41 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.nio.file.Files;
46 import java.nio.file.Paths;
47 import java.util.HashMap;
50 public class YamlToObjectConverter {
52 private static Logger log = Logger.getLogger(YamlToObjectConverter.class.getName());
54 private static HashMap<String, Yaml> yamls = new HashMap<>();
56 private static Yaml defaultYaml = new Yaml();
60 org.yaml.snakeyaml.constructor.Constructor deConstructor = new org.yaml.snakeyaml.constructor.Constructor(
61 DistributionEngineConfiguration.class);
62 TypeDescription deDescription = new TypeDescription(DistributionEngineConfiguration.class);
63 deDescription.putListPropertyType("distributionStatusTopic", DistributionStatusTopicConfig.class);
64 deDescription.putListPropertyType("distribNotifServiceArtifactTypes", ComponentArtifactTypesConfig.class);
65 deDescription.putListPropertyType("distribNotifResourceArtifactTypes", ComponentArtifactTypesConfig.class);
66 deDescription.putListPropertyType("createTopic", CreateTopicConfig.class);
67 deDescription.putListPropertyType("distributionNotificationTopic", DistributionNotificationTopicConfig.class);
68 deConstructor.addTypeDescription(deDescription);
69 Yaml yaml = new Yaml(deConstructor);
70 yamls.put(DistributionEngineConfiguration.class.getName(), yaml);
73 org.yaml.snakeyaml.constructor.Constructor feConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
74 org.openecomp.sdc.fe.config.Configuration.class);
75 TypeDescription feConfDescription = new TypeDescription(org.openecomp.sdc.fe.config.Configuration.class);
76 feConfDescription.putListPropertyType("systemMonitoring", FeMonitoringConfig.class);
77 feConfConstructor.addTypeDescription(feConfDescription);
78 yamls.put(org.openecomp.sdc.fe.config.Configuration.class.getName(), new Yaml(feConfConstructor));
81 org.yaml.snakeyaml.constructor.Constructor beConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
82 org.openecomp.sdc.be.config.Configuration.class);
83 TypeDescription beConfDescription = new TypeDescription(org.openecomp.sdc.be.config.Configuration.class);
84 beConfConstructor.addTypeDescription(beConfDescription);
87 beConfDescription.putListPropertyType("systemMonitoring", BeMonitoringConfig.class);
90 beConfDescription.putListPropertyType("elasticSearch", ElasticSearchConfig.class);
91 TypeDescription esDescription = new TypeDescription(ElasticSearchConfig.class);
92 esDescription.putListPropertyType("indicesTimeFrequency", IndicesTimeFrequencyEntry.class);
93 beConfConstructor.addTypeDescription(esDescription);
95 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
96 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
97 ArtifactTypeConfig.class);
98 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
99 ArtifactTypeConfig.class);
102 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
105 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
106 // switchoverDetector
107 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
109 // ApplicationL1Cache
110 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
112 // ApplicationL2Cache
113 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
115 // tosca validators config
116 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
118 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
120 // HEAT deployment artifact
121 org.yaml.snakeyaml.constructor.Constructor depArtHeatConstructor = new org.yaml.snakeyaml.constructor.Constructor(
122 DeploymentArtifactHeatConfiguration.class);
123 PropertyUtils propertyUtils = new PropertyUtils();
124 // Skip properties which are found in YAML but not found in POJO
125 propertyUtils.setSkipMissingProperties(true);
126 depArtHeatConstructor.setPropertyUtils(propertyUtils);
127 yamls.put(org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration.class.getName(),
128 new Yaml(depArtHeatConstructor));
132 private static <T> Yaml getYamlByClassName(Class<T> className) {
134 Yaml yaml = yamls.get(className.getName());
142 public <T> T convert(String dirPath, Class<T> className, String configFileName) {
148 String fullFileName = dirPath + File.separator + configFileName;
150 config = convert(fullFileName, className);
152 } catch (Exception e) {
153 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file {} to object.", configFileName,e);
159 public class MyYamlConstructor extends org.yaml.snakeyaml.constructor.Constructor {
160 private HashMap<String, Class<?>> classMap = new HashMap<>();
162 public MyYamlConstructor(Class<? extends Object> theRoot) {
164 classMap.put(DistributionEngineConfiguration.class.getName(), DistributionEngineConfiguration.class);
165 classMap.put(DistributionStatusTopicConfig.class.getName(), DistributionStatusTopicConfig.class);
169 * This is a modified version of the Constructor. Rather than using a
170 * class loader to get external classes, they are already predefined
171 * above. This approach works similar to the typeTags structure in the
172 * original constructor, except that class information is pre-populated
173 * during initialization rather than runtime.
176 * org.yaml.snakeyaml.constructor.Constructor#getClassForNode(org.yaml.
177 * snakeyaml.nodes.Node)
179 protected Class<?> getClassForNode(Node node) {
180 String name = node.getTag().getClassName();
181 Class<?> cl = classMap.get(name);
183 throw new YAMLException("Class not found: " + name);
189 public <T> T convert(String fullFileName, Class<T> className) {
193 Yaml yaml = getYamlByClassName(className);
195 InputStream in = null;
198 File f = new File(fullFileName);
200 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","The file {} cannot be found. Ignore reading configuration.",fullFileName);
203 in = Files.newInputStream(Paths.get(fullFileName));
205 config = yaml.loadAs(in, className);
207 // System.out.println(config.toString());
208 } catch (Exception e) {
209 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file {} to object.", fullFileName, e);
214 } catch (IOException e) {
215 log.debug("Failed to close input stream", e);
223 public <T> T convert(byte[] fileContents, Class<T> className) {
227 Yaml yaml = getYamlByClassName(className);
229 InputStream in = null;
232 in = new ByteArrayInputStream(fileContents);
234 config = yaml.loadAs(in, className);
236 } catch (Exception e) {
237 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file to object", e);
242 } catch (IOException e) {
243 log.debug("Failed to close input stream", e);
251 public boolean isValidYamlEncoded64(byte[] fileContents) {
252 log.trace("Received Base64 data - decoding before validating...");
253 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
255 return isValidYaml(decodedFileContents);
257 @SuppressWarnings("unchecked")
258 public boolean isValidYaml(byte[] fileContents) {
261 Iterable<Object> mappedToscaTemplateIt = defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
263 for (Object o : mappedToscaTemplateIt) {
264 System.out.println("Loaded object type:" + o.getClass());
265 Map<String, Object> map = (Map<String, Object>) o;
268 } catch (Exception e) {
269 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file to object - yaml is invalid", e);