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 java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.HashMap;
31 import org.apache.commons.codec.binary.Base64;
32 import org.openecomp.sdc.be.config.Configuration.ApplicationL1CacheConfig;
33 import org.openecomp.sdc.be.config.Configuration.ApplicationL2CacheConfig;
34 import org.openecomp.sdc.be.config.Configuration.ArtifactTypeConfig;
35 import org.openecomp.sdc.be.config.Configuration.BeMonitoringConfig;
36 import org.openecomp.sdc.be.config.Configuration.EcompPortalConfig;
37 import org.openecomp.sdc.be.config.Configuration.OnboardingConfig;
38 import org.openecomp.sdc.be.config.Configuration.SwitchoverDetectorConfig;
39 import org.openecomp.sdc.be.config.Configuration.ToscaValidatorsConfig;
40 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
41 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.ComponentArtifactTypesConfig;
42 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.CreateTopicConfig;
43 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
44 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionStatusTopicConfig;
45 import org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration;
46 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
47 import org.openecomp.sdc.common.log.wrappers.Logger;
48 import org.openecomp.sdc.exception.YamlConversionException;
49 import org.openecomp.sdc.fe.config.Configuration.FeMonitoringConfig;
50 import org.yaml.snakeyaml.TypeDescription;
51 import org.yaml.snakeyaml.Yaml;
52 import org.yaml.snakeyaml.introspector.PropertyUtils;
54 public class YamlToObjectConverter {
56 private static Logger log = Logger.getLogger(YamlToObjectConverter.class.getName());
58 private static HashMap<String, Yaml> yamls = new HashMap<>();
60 private static Yaml defaultYaml = new Yaml();
64 org.yaml.snakeyaml.constructor.Constructor deConstructor = new org.yaml.snakeyaml.constructor.Constructor(
65 DistributionEngineConfiguration.class);
66 TypeDescription deDescription = new TypeDescription(DistributionEngineConfiguration.class);
67 deDescription.putListPropertyType("distributionStatusTopic", DistributionStatusTopicConfig.class);
68 deDescription.putListPropertyType("distribNotifServiceArtifactTypes", ComponentArtifactTypesConfig.class);
69 deDescription.putListPropertyType("distribNotifResourceArtifactTypes", ComponentArtifactTypesConfig.class);
70 deDescription.putListPropertyType("createTopic", CreateTopicConfig.class);
71 deDescription.putListPropertyType("distributionNotificationTopic", DistributionNotificationTopicConfig.class);
72 deConstructor.addTypeDescription(deDescription);
73 Yaml yaml = new Yaml(deConstructor);
74 yamls.put(DistributionEngineConfiguration.class.getName(), yaml);
77 org.yaml.snakeyaml.constructor.Constructor feConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
78 org.openecomp.sdc.fe.config.Configuration.class);
79 TypeDescription feConfDescription = new TypeDescription(org.openecomp.sdc.fe.config.Configuration.class);
80 feConfDescription.putListPropertyType("systemMonitoring", FeMonitoringConfig.class);
81 feConfConstructor.addTypeDescription(feConfDescription);
82 yamls.put(org.openecomp.sdc.fe.config.Configuration.class.getName(), new Yaml(feConfConstructor));
85 org.yaml.snakeyaml.constructor.Constructor beConfConstructor = new org.yaml.snakeyaml.constructor.Constructor(
86 org.openecomp.sdc.be.config.Configuration.class);
87 TypeDescription beConfDescription = new TypeDescription(org.openecomp.sdc.be.config.Configuration.class);
88 beConfConstructor.addTypeDescription(beConfDescription);
91 beConfDescription.putListPropertyType("systemMonitoring", BeMonitoringConfig.class);
93 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
94 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
95 ArtifactTypeConfig.class);
96 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
97 ArtifactTypeConfig.class);
100 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
103 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
104 // switchoverDetector
105 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
107 // ApplicationL1Cache
108 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
110 // ApplicationL2Cache
111 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
113 // tosca validators config
114 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
116 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
118 // HEAT deployment artifact
119 org.yaml.snakeyaml.constructor.Constructor depArtHeatConstructor = new org.yaml.snakeyaml.constructor.Constructor(
120 DeploymentArtifactHeatConfiguration.class);
121 PropertyUtils propertyUtils = new PropertyUtils();
122 // Skip properties which are found in YAML but not found in POJO
123 propertyUtils.setSkipMissingProperties(true);
124 depArtHeatConstructor.setPropertyUtils(propertyUtils);
125 yamls.put(org.openecomp.sdc.be.config.validation.DeploymentArtifactHeatConfiguration.class.getName(),
126 new Yaml(depArtHeatConstructor));
130 private static <T> Yaml getYamlByClassName(Class<T> className) {
132 Yaml yaml = yamls.get(className.getName());
140 public <T> T convert(final String dirPath, final Class<T> className,
141 final String configFileName) throws YamlConversionException {
142 if (className == null) {
143 throw new IllegalArgumentException("className cannot be null");
145 final String fullFileName = dirPath + File.separator + configFileName;
146 return convert(fullFileName, className);
149 public <T> T convert(String fullFileName, Class<T> className) throws YamlConversionException {
150 if (!new File(fullFileName).exists()) {
151 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
152 "The file {} cannot be found. Ignore reading configuration.", fullFileName);
156 final Yaml yaml = getYamlByClassName(className);
158 try (final InputStream in = Files.newInputStream(Paths.get(fullFileName))) {
159 return yaml.loadAs(in, className);
160 } catch (final IOException e) {
161 log.debug("Failed to open/close input stream", e);
162 } catch (Exception e) {
163 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
164 "Failed to convert yaml file {} to object.", fullFileName, e);
165 final String errorMsg = String.format("Could not parse '%s' to class '%s'", fullFileName, className);
166 throw new YamlConversionException(errorMsg, e);
172 public <T> T convert(byte[] fileContents, Class<T> className) {
173 final Yaml yaml = getYamlByClassName(className);
175 try (final InputStream in = new ByteArrayInputStream(fileContents)){
176 return yaml.loadAs(in, className);
177 } catch (final IOException e) {
178 log.debug("Failed to open or close input stream", e);
179 } catch (final Exception e) {
180 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,
181 "","","Failed to convert yaml file to object", e);
187 public boolean isValidYamlEncoded64(byte[] fileContents) {
188 log.trace("Received Base64 data - decoding before validating...");
189 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
191 return isValidYaml(decodedFileContents);
193 @SuppressWarnings("unchecked")
194 public boolean isValidYaml(byte[] fileContents) {
197 Iterable<Object> mappedToscaTemplateIt = defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
199 for (Object o : mappedToscaTemplateIt) {
200 System.out.println("Loaded object type:" + o.getClass());
201 Map<String, Object> map = (Map<String, Object>) o;
204 } catch (Exception e) {
205 log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
206 "Failed to convert yaml file to object - yaml is invalid", e);