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.DistributionEngineConfiguration;
26 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.ComponentArtifactTypesConfig;
27 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.CreateTopicConfig;
28 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
29 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionStatusTopicConfig;
30 import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration;
31 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
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);
88 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
89 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
90 ArtifactTypeConfig.class);
91 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
92 ArtifactTypeConfig.class);
95 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
98 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
100 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
102 // ApplicationL1Cache
103 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
105 // ApplicationL2Cache
106 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
108 // tosca validators config
109 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
111 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
113 // HEAT deployment artifact
114 org.yaml.snakeyaml.constructor.Constructor depArtHeatConstructor = new org.yaml.snakeyaml.constructor.Constructor(
115 DeploymentArtifactHeatConfiguration.class);
116 PropertyUtils propertyUtils = new PropertyUtils();
117 // Skip properties which are found in YAML but not found in POJO
118 propertyUtils.setSkipMissingProperties(true);
119 depArtHeatConstructor.setPropertyUtils(propertyUtils);
120 yamls.put(org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration.class.getName(),
121 new Yaml(depArtHeatConstructor));
125 private static <T> Yaml getYamlByClassName(Class<T> className) {
127 Yaml yaml = yamls.get(className.getName());
135 public <T> T convert(String dirPath, Class<T> className, String configFileName) {
141 String fullFileName = dirPath + File.separator + configFileName;
143 config = convert(fullFileName, className);
145 } catch (Exception e) {
146 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file {} to object.", configFileName,e);
152 public class MyYamlConstructor extends org.yaml.snakeyaml.constructor.Constructor {
153 private HashMap<String, Class<?>> classMap = new HashMap<>();
155 public MyYamlConstructor(Class<? extends Object> theRoot) {
157 classMap.put(DistributionEngineConfiguration.class.getName(), DistributionEngineConfiguration.class);
158 classMap.put(DistributionStatusTopicConfig.class.getName(), DistributionStatusTopicConfig.class);
162 * This is a modified version of the Constructor. Rather than using a
163 * class loader to get external classes, they are already predefined
164 * above. This approach works similar to the typeTags structure in the
165 * original constructor, except that class information is pre-populated
166 * during initialization rather than runtime.
169 * org.yaml.snakeyaml.constructor.Constructor#getClassForNode(org.yaml.
170 * snakeyaml.nodes.Node)
172 protected Class<?> getClassForNode(Node node) {
173 String name = node.getTag().getClassName();
174 Class<?> cl = classMap.get(name);
176 throw new YAMLException("Class not found: " + name);
182 public <T> T convert(String fullFileName, Class<T> className) {
186 Yaml yaml = getYamlByClassName(className);
188 InputStream in = null;
191 File f = new File(fullFileName);
193 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","The file {} cannot be found. Ignore reading configuration.",fullFileName);
196 in = Files.newInputStream(Paths.get(fullFileName));
198 config = yaml.loadAs(in, className);
200 // System.out.println(config.toString());
201 } catch (Exception e) {
202 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file {} to object.", fullFileName, e);
207 } catch (IOException e) {
208 log.debug("Failed to close input stream", e);
217 public <T> T convert(byte[] fileContents, Class<T> className) {
221 Yaml yaml = getYamlByClassName(className);
223 InputStream in = null;
226 in = new ByteArrayInputStream(fileContents);
228 config = yaml.loadAs(in, className);
230 } catch (Exception e) {
231 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file to object", e);
236 } catch (IOException e) {
237 log.debug("Failed to close input stream", e);
246 public boolean isValidYamlEncoded64(byte[] fileContents) {
247 log.trace("Received Base64 data - decoding before validating...");
248 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
250 return isValidYaml(decodedFileContents);
252 @SuppressWarnings("unchecked")
253 public boolean isValidYaml(byte[] fileContents) {
256 Iterable<Object> mappedToscaTemplateIt = defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
258 for (Object o : mappedToscaTemplateIt) {
259 System.out.println("Loaded object type:" + o.getClass());
260 Map<String, Object> map = (Map<String, Object>) o;
263 } catch (Exception e) {
264 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","Failed to convert yaml file to object - yaml is invalid", e);