[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / ToscaParserUtils.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.ci.tests.utils;
22
23 import static org.testng.AssertJUnit.assertNotNull;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.openecomp.sdc.ci.tests.datatypes.enums.ToscaKeysEnum;
31 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
32 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsTopologyTemplateDefinition;
33 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
34 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaSubstitutionMappingsDefinition;
35 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaTopologyTemplateDefinition;
36 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.yaml.snakeyaml.TypeDescription;
40 import org.yaml.snakeyaml.Yaml;
41 import org.yaml.snakeyaml.constructor.Constructor;
42 import org.yaml.snakeyaml.introspector.PropertyUtils;
43
44 public class ToscaParserUtils {
45
46         private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
47
48         public static ToscaDefinition parseToscaYamlToJavaObject(String csarUUID) throws Exception {
49                 
50                 ToscaDefinition toscaDefinition = null;
51                 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
52                 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
53                 assertNotNull("Tosca Entry-Definitions is null", map);
54                 if (map != null) {
55                         File definitionYamlLocation = (File) map.get("Entry-Definitions");
56                         toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
57                 }
58                 return toscaDefinition;
59
60         }
61
62         public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
63
64                 ToscaDefinition toscaDefinition = null;
65                 
66 //        File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
67         FileInputStream fis = null;
68         try {
69                 fis = new FileInputStream(path);
70                         
71                 } catch (Exception e) {
72                         System.out.println("Exception: " + e);
73                 }
74         
75         Constructor constructor = getConstructor();
76         
77         Yaml yaml = new Yaml(constructor);
78         try {
79                 toscaDefinition = (ToscaDefinition) yaml.load(fis);
80                 } catch (Exception e) {
81                         log.debug("Failed to parse tosca yaml file");
82                         System.out.println("Exception: " + e);
83                 } finally {
84                         fis.close();
85                 }
86         return toscaDefinition;
87         
88         }
89
90         public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
91
92                 ToscaDefinition toscaDefinition = null;
93         Constructor constructor = getConstructor();
94         
95         Yaml yaml = new Yaml(constructor);
96         try {
97                 toscaDefinition = (ToscaDefinition) yaml.load(payload);
98                 } catch (Exception e) {
99                         log.debug("Failed to parse tosca yaml file");
100                         System.out.println("Exception: " + e);
101                 }
102         return toscaDefinition;
103         
104         }
105         
106         
107         public static Constructor getConstructor() {
108                 Constructor constructor = new Constructor(ToscaDefinition.class);
109         constructor.addTypeDescription(ToscaDefinition.getTypeDescription());
110         constructor.addTypeDescription(ToscaTopologyTemplateDefinition.getTypeDescription());
111         constructor.addTypeDescription(ToscaNodeTemplatesTopologyTemplateDefinition.getTypeDescription());
112         constructor.addTypeDescription(ToscaGroupsTopologyTemplateDefinition.getTypeDescription());
113         constructor.addTypeDescription(ToscaSubstitutionMappingsDefinition.getTypeDescription());
114         
115 //      Skip properties which are found in YAML, but not found in POJO
116         PropertyUtils propertyUtils = new PropertyUtils();
117         propertyUtils.setSkipMissingProperties(true);
118         constructor.setPropertyUtils(propertyUtils);
119                 return constructor;
120         }
121
122         public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
123                 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
124                 if (csarPayload != null) {
125                         Yaml yaml = new Yaml();
126                         Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
127                         return map;
128                 }
129                 return null;
130         }
131         
132 /*      public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
133                 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
134                 if (csarPayload != null) {
135                         Yaml yaml = new Yaml();
136                         Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
137                         return map;
138                 }
139                 return null;
140         }
141
142         public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
143
144                 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
145                 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
146                 assertNotNull("Tosca Entry-Definitions is null", map);
147                 if (map != null) {
148                         String definitionYamlLocation = (String) map.get("Entry-Definitions");
149                         Map<?, ?> toscaMap = getToscaYamlMap(csarUUID, definitionYamlLocation);
150                         assertNotNull("Tosca definition is null", toscaMap);
151                         if (toscaMap != null) {
152                                 ToscaDefinition toscaDefinition = new ToscaDefinition();
153                                 Set<?> keySet = toscaMap.keySet();
154                                 for (Object key : keySet) {
155                                         ToscaKeysEnum toscaKey = ToscaKeysEnum.findToscaKey((String) key);
156                                         switch (toscaKey) {
157                                         case TOSCA_DEFINITION_VERSION:
158                                                 getToscaDefinitionVersion(toscaMap, toscaDefinition);
159                                                 break;
160                                         case NODE_TYPES:
161                                                 getToscaNodeTypes(toscaMap, toscaDefinition);
162                                                 break;
163                                         case TOPOLOGY_TEMPLATE:
164                                                 getToscaTopologyTemplate(toscaMap, toscaDefinition);
165                                                 break;
166                                         case IMPORTS:
167                                                 // toscaMap.get("imports");
168                                                 break;
169                                         default:
170                                                 break;
171                                         }
172                                 }
173                                 return toscaDefinition;
174                         }
175                 }
176                 return null;
177
178         }
179
180         public static void getToscaDefinitionVersion(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
181                 if (toscaMap.get("tosca_definitions_version") != null) {
182                         toscaDefinition.setTosca_definitions_version((String) toscaMap.get("tosca_definitions_version"));
183                 }
184         }
185
186         // spec 90 page
187         public static void getToscaNodeTypes(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
188                 @SuppressWarnings("unchecked")
189                 Map<String, Map<String, String>> nodeTypes = (Map<String, Map<String, String>>) toscaMap.get("node_types");
190                 Map<String, ToscaNodeTypesDefinition> listToscaNodeTypes = new HashMap<String, ToscaNodeTypesDefinition>();
191                 if (nodeTypes != null) {
192                         for (Map.Entry<String, Map<String, String>> entry : nodeTypes.entrySet()) {
193                                 ToscaNodeTypesDefinition toscaNodeTypes = new ToscaNodeTypesDefinition();
194                                 String toscaNodeName = entry.getKey();
195                                 toscaNodeTypes.setName(toscaNodeName);
196
197                                 Map<String, String> toscaNodeType = entry.getValue();
198                                 if (toscaNodeType != null) {
199                                         Set<Entry<String, String>> entrySet = toscaNodeType.entrySet();
200                                         if (entrySet != null) {
201                                                 // boolean found = false;
202                                                 for (Entry<String, String> toscaNodeTypeMap : entrySet) {
203                                                         String key = toscaNodeTypeMap.getKey();
204                                                         if (key.equals("derived_from")) {
205                                                                 String derivedFrom = toscaNodeTypeMap.getValue();
206                                                                 toscaNodeTypes.setDerived_from(derivedFrom);
207                                                                 // found = true;
208                                                                 break;
209                                                         } else {
210                                                                 continue;
211                                                         }
212
213                                                 }
214                                                 // if (found == false) {
215                                                 // System.out.println("Tosca file not valid,
216                                                 // derived_from not found");
217                                                 // }
218                                         }
219
220                                 }
221 //                              listToscaNodeTypes.add(toscaNodeTypes);
222                                 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
223                         }
224                         toscaDefinition.setNode_types(listToscaNodeTypes);
225                 }
226         }
227
228         public static void getToscaTopologyTemplate(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
229                 ToscaTopologyTemplateDefinition toscaTopologyTemplate = new ToscaTopologyTemplateDefinition();
230                 @SuppressWarnings("unchecked")
231                 Map<String, Map<String, Object>> topologyTemplateMap = (Map<String, Map<String, Object>>) toscaMap.get("topology_template");
232 //              List<ToscaNodeTemplatesTopologyTemplateDefinition> listToscaNodeTemplates = new ArrayList<>();
233                 Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates = new HashMap<String, ToscaNodeTemplatesTopologyTemplateDefinition>();
234
235                 if (topologyTemplateMap != null) {
236                         getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
237                 }
238 //              toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
239                 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
240                 toscaDefinition.setTopology_template(toscaTopologyTemplate);
241         }
242
243         public static void getToscaNodeTemplates(Map<String, Map<String, Object>> topologyTemplateMap, Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates) {
244                 Map<String, Object> nodeTemplatesMap = topologyTemplateMap.get("node_templates");
245                 if (nodeTemplatesMap != null) {
246
247                         for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
248                                 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
249                                 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
250
251                                 @SuppressWarnings("unchecked")
252                                 Map<String, Object> node = (Map<String, Object>) nodeTemplates.getValue();
253                                 getNodeTemplatesType(toscaNodeTemplates, node);
254                                 getToscaNodeTemplateProperties(toscaNodeTemplates, node);
255                                 getToscaNodeTemplateRequirements(toscaNodeTemplates, node);
256                                 mapToscaNodeTemplates.putAll(mapToscaNodeTemplates);
257                         }
258                 }
259         }
260
261         public static void getToscaNodeTemplateRequirements(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates, Map<String, Object> node) {
262 ////            List<ToscaRequirementsNodeTemplatesDefinition> toscaRequirements = new ArrayList<>();
263 //              List<Map<String, ToscaRequirementsNodeTemplatesDefinition>> toscaRequirements = new ArrayList<>();
264 //              if (node.get("requirements") != null) {
265 //                      @SuppressWarnings("unchecked")
266 //                      List<Map<String, Object>> requirementList = (List<Map<String, Object>>) node.get("requirements");
267 //                      for (int i = 0; i < requirementList.size(); i++) {
268 //                              for (Map.Entry<String, Object> requirement : requirementList.get(i).entrySet()) {
269 //                                      ToscaRequirementsNodeTemplatesDefinition toscaRequirement = new ToscaRequirementsNodeTemplatesDefinition();
270 //                                      if (requirement.getKey() != null) {
271 //                                              String requirementName = requirement.getKey();
272 //                                              toscaRequirement.setName(requirementName);
273 //                                      } else {
274 //                                              log.debug("Tosca file not valid, requirements should contain name");
275 //                                      }
276 //
277 //                                      @SuppressWarnings("unchecked")
278 //                                      Map<String, String> requirementMap = (Map<String, String>) requirement.getValue();
279 //                                      Set<Entry<String, String>> entrySet = requirementMap.entrySet();
280 //                                      if (entrySet != null) {
281 //                                              for (Entry<String, String> requirementField : entrySet) {
282 //                                                      String key = requirementField.getKey();
283 //                                                      switch (key) {
284 //                                                      case "capability":
285 //                                                              if (requirementMap.get(key) != null) {
286 //                                                                      String capability = (String) requirementMap.get(key);
287 //                                                                      toscaRequirement.setCapability(capability);
288 //                                                                      break;
289 //                                                              } else {
290 //                                                                      continue;
291 //                                                              }
292 //                                                      case "node":
293 //                                                              if (requirementMap.get(key) != null) {
294 //                                                                      String requirementNode = (String) requirementMap.get(key);
295 //                                                                      toscaRequirement.setNode(requirementNode);
296 //                                                                      break;
297 //                                                              } else {
298 //                                                                      continue;
299 //                                                              }
300 //                                                      case "relationship":
301 //                                                              if (requirementMap.get(key) != null) {
302 //                                                                      String relationship = (String) requirementMap.get(key);
303 //                                                                      toscaRequirement.setRelationship(relationship);
304 //                                                                      break;
305 //                                                              } else {
306 //                                                                      continue;
307 //                                                              }
308 //                                                      default:
309 //                                                              break;
310 //                                                      }
311 //                                              }
312 //                                      }
313 ////                                    toscaRequirements.add(toscaRequirement);
314 //                                      toscaRequirements.add(requirementMap);
315 //                              }
316 //                      }
317 //              }
318 ////            toscaNodeTemplates.setRequirements(toscaRequirements);
319 //              toscaNodeTemplates.setRequirements(requirements);
320                 
321         }
322
323         public static void getToscaNodeTemplateProperties(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
324                         Map<String, Object> node) {
325 //              List<ToscaPropertiesNodeTemplatesDefinition> listToscaProperties = new ArrayList<>();
326                 Map<String, Object> mapToscaProperties = new HashMap<>();
327                 if (node.get("properties") != null) {
328                         @SuppressWarnings("unchecked")
329                         Map<String, Object> properties = (Map<String, Object>) node.get("properties");
330                         for (Map.Entry<String, Object> property : properties.entrySet()) {
331                                 ToscaPropertiesNodeTemplatesDefinition toscaProperty = new ToscaPropertiesNodeTemplatesDefinition();
332                                 String propertyName = property.getKey();
333                                 Object propertyValue = property.getValue();
334                                 toscaProperty.setName(propertyName);
335                                 toscaProperty.setValue(propertyValue);
336 //                              mapToscaProperties.add(toscaProperty);
337                                 mapToscaProperties.put(propertyName, propertyValue);
338                         }
339                 }
340                 toscaNodeTemplates.setProperties(mapToscaProperties);
341         }
342
343         protected static void getNodeTemplatesType(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
344                         Map<String, Object> node) {
345                 if (node.get("type") != null) {
346                         String type = (String) node.get("type");
347                         toscaNodeTemplates.setType(type);
348                 } else {
349                         log.debug("Tosca file not valid, nodeTemplate should contain type");
350                 }
351         }
352
353         protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
354                         ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
355                 String name = nodeTemplates.getKey();
356                 toscaNodeTemplates.setName(name);
357         }*/
358         
359         
360         
361 }