Fix broken CSIT
[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 java.io.ByteArrayInputStream;
24 import java.io.File;
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;
30 import java.util.Map;
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;
53
54 public class YamlToObjectConverter {
55
56         private static Logger log = Logger.getLogger(YamlToObjectConverter.class.getName());
57
58         private static HashMap<String, Yaml> yamls = new HashMap<>();
59
60         private static Yaml defaultYaml = new Yaml();
61
62         static {
63
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);
75
76                 // FE conf
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));
83
84                 // BE conf
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);
89
90                 // systemMonitoring
91                 beConfDescription.putListPropertyType("systemMonitoring", BeMonitoringConfig.class);
92
93                 // resourceDeploymentArtifacts and serviceDeploymentArtifacts
94                 beConfDescription.putMapPropertyType("resourceDeploymentArtifacts", String.class,
95                                 ArtifactTypeConfig.class);
96                 beConfDescription.putMapPropertyType("serviceDeploymentArtifacts", String.class,
97                                 ArtifactTypeConfig.class);
98
99                 // onboarding
100                 beConfDescription.putListPropertyType("onboarding", OnboardingConfig.class);
101
102                 // ecompPortal
103                 beConfDescription.putListPropertyType("ecompPortal", EcompPortalConfig.class);
104                 // switchoverDetector
105                 beConfDescription.putListPropertyType("switchoverDetector", SwitchoverDetectorConfig.class);
106
107                 // ApplicationL1Cache
108                 beConfDescription.putListPropertyType("applicationL1Cache", ApplicationL1CacheConfig.class);
109
110                 // ApplicationL2Cache
111                 beConfDescription.putListPropertyType("applicationL2Cache", ApplicationL2CacheConfig.class);
112
113                 // tosca validators config
114                 beConfDescription.putListPropertyType("toscaValidators", ToscaValidatorsConfig.class);
115
116                 yamls.put(org.openecomp.sdc.be.config.Configuration.class.getName(), new Yaml(beConfConstructor));
117
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));
127
128         }
129
130         private static <T> Yaml getYamlByClassName(Class<T> className) {
131
132                 Yaml yaml = yamls.get(className.getName());
133                 if (yaml == null) {
134                         yaml = defaultYaml;
135                 }
136
137                 return yaml;
138         }
139
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");
144                 }
145                 final String fullFileName = dirPath + File.separator + configFileName;
146                 return convert(fullFileName, className);
147         }
148
149         public <T> T convert(String fullFileName, Class<T> className) throws YamlConversionException {
150                 final File file = new File(fullFileName);
151                 if (!file.exists() || file.isDirectory()) {
152                         log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
153                                 "The file {} cannot be found. Ignore reading configuration.", fullFileName);
154                         return null;
155                 }
156
157                 final Yaml yaml = getYamlByClassName(className);
158
159                 try (final InputStream in = Files.newInputStream(Paths.get(fullFileName))) {
160                         return yaml.loadAs(in, className);
161                 } catch (final IOException e) {
162                         log.debug("Failed to open/close input stream", e);
163                 } catch (Exception e) {
164                         log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
165                                 "Failed to convert yaml file {} to object.", fullFileName, e);
166                         final String errorMsg = String.format("Could not parse '%s' to class '%s'", fullFileName, className);
167                         throw new YamlConversionException(errorMsg, e);
168                 }
169
170                 return null;
171         }
172
173         public <T> T convert(byte[] fileContents, Class<T> className) {
174                 final Yaml yaml = getYamlByClassName(className);
175
176                 try (final InputStream in = new ByteArrayInputStream(fileContents)){
177                         return yaml.loadAs(in, className);
178                 } catch (final IOException e) {
179                         log.debug("Failed to open or close input stream", e);
180                 } catch (final Exception e) {
181                         log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,
182                                 "","","Failed to convert yaml file to object", e);
183                 }
184
185                 return null;
186         }
187
188         public boolean isValidYamlEncoded64(byte[] fileContents) {
189                 log.trace("Received Base64 data - decoding before validating...");
190                 byte[] decodedFileContents = Base64.decodeBase64(fileContents);
191                 
192                 return isValidYaml(decodedFileContents);
193         }
194         @SuppressWarnings("unchecked")
195         public boolean isValidYaml(byte[] fileContents) {
196                 try {
197                         
198                         Iterable<Object> mappedToscaTemplateIt =  defaultYaml.loadAll(new ByteArrayInputStream(fileContents));
199                         
200                          for (Object o : mappedToscaTemplateIt) {
201                         System.out.println("Loaded object type:" + o.getClass());
202                         Map<String, Object> map = (Map<String, Object>) o;
203                          }
204                         
205                 } catch (Exception e) {
206                         log.error(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
207                                 "Failed to convert yaml file to object - yaml is invalid", e);
208                         return false;
209                 }
210                 return true;
211         }
212 }