[SDC] rebase 1710 code
[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 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.Map;
32 import java.util.Set;
33
34 import org.openecomp.sdc.ci.tests.datatypes.enums.ToscaKeysEnum;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
36 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
37 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsTopologyTemplateDefinition;
38 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
39 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaParameterConstants;
40 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaSubstitutionMappingsDefinition;
41 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaTopologyTemplateDefinition;
42 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
43 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
44 import org.openecomp.sdc.ci.tests.utils.rest.ImportRestUtils;
45 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
46 import org.openecomp.sdc.common.rest.api.RestResponseAsByteArray;
47 import org.openecomp.sdc.common.util.ZipUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.yaml.snakeyaml.TypeDescription;
51 import org.yaml.snakeyaml.Yaml;
52 import org.yaml.snakeyaml.constructor.Constructor;
53 import org.yaml.snakeyaml.introspector.PropertyUtils;
54
55 public class ToscaParserUtils {
56
57         private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
58
59         
60         /**method get csarUUID and send GET API request toward BE 
61          * @param csarUUID
62          * @return
63          * @throws Exception
64          */
65         public static ToscaDefinition parseToscaYamlToJavaObjectByCsarUuid(String csarUUID) throws Exception {
66                 
67                 ToscaDefinition toscaDefinition = null;
68                 String TOSCAMetaLocation = ToscaParameterConstants.TOSCA_META_PATH;
69                 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
70                 assertNotNull("Tosca Entry-Definitions is null", map);
71                 if (map != null) {
72                         File definitionYamlLocation = (File) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
73                         toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
74                 }
75                 return toscaDefinition;
76
77         }
78
79         /**method read csar from location
80          * @param csarNameLocation - full path with csar name 
81          * @return
82          * @throws Exception
83          */
84         public static ToscaDefinition parseToscaMainYamlToJavaObjectByCsarLocation(File csarNameLocation) throws Exception {
85                 
86                 ToscaDefinition toscaDefinition = null;
87                 String TOSCAMetaLocation = ToscaParameterConstants.TOSCA_META_PATH;
88 //              read file location of main yaml file(location+name) from TOSCA.meta file by 
89                 Map<?, ?> map = getToscaYamlMap(csarNameLocation, TOSCAMetaLocation);
90                 
91                 assertNotNull("Tosca Entry-Definitions is null", map);
92
93                 String definitionYamlLocation = (String) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
94                 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, definitionYamlLocation);
95                 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
96                 return toscaDefinition;
97
98         }
99         
100         public static ToscaDefinition parseToscaAnyYamlToJavaObjectByCsarLocation(File csarNameLocation, String yamlLocation) throws Exception {
101                 
102                 ToscaDefinition toscaDefinition = null;
103                 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, yamlLocation);
104                 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
105                 return toscaDefinition;
106
107         }
108         
109         public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
110
111                 ToscaDefinition toscaDefinition = null;
112                 
113 //        File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
114         FileInputStream fis = null;
115         try {
116                 fis = new FileInputStream(path);
117                         
118                 } catch (Exception e) {
119                         System.out.println("Exception: " + e);
120                 }
121         
122         Constructor constructor = initToscaDefinitionObject();
123         
124         Yaml yaml = new Yaml(constructor);
125         try {
126                 toscaDefinition = (ToscaDefinition) yaml.load(fis);
127                 } catch (Exception e) {
128                         log.debug("Failed to parse tosca yaml file");
129                         System.out.println("Exception: " + e);
130                 } finally {
131                         fis.close();
132                 }
133         return toscaDefinition;
134         
135         }
136
137         public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
138
139                 ToscaDefinition toscaDefinition = null;
140         Constructor constructor = initToscaDefinitionObject();
141         
142         Yaml yaml = new Yaml(constructor);
143         try {
144                 toscaDefinition = (ToscaDefinition) yaml.load(payload);
145                 } catch (Exception e) {
146                         log.debug("Failed to parse tosca yaml file");
147                         System.out.println("Exception: " + e);
148                 }
149         return toscaDefinition;
150         
151         }
152         
153         
154         public static Constructor initToscaDefinitionObject() {
155                 Constructor toscaStructure = new Constructor(ToscaDefinition.class);
156         toscaStructure.addTypeDescription(ToscaDefinition.getTypeDescription());
157         toscaStructure.addTypeDescription(ToscaTopologyTemplateDefinition.getTypeDescription());
158         toscaStructure.addTypeDescription(ToscaNodeTemplatesTopologyTemplateDefinition.getTypeDescription());
159         toscaStructure.addTypeDescription(ToscaGroupsTopologyTemplateDefinition.getTypeDescription());
160         toscaStructure.addTypeDescription(ToscaSubstitutionMappingsDefinition.getTypeDescription());
161         
162 //      Skip properties which are found in YAML, but not found in POJO
163         PropertyUtils propertyUtils = new PropertyUtils();
164         propertyUtils.setSkipMissingProperties(true);
165         toscaStructure.setPropertyUtils(propertyUtils);
166                 return toscaStructure;
167         }
168
169         public static Map<?, ?> getToscaYamlMap(String csarUUID, String yamlFileLocation) throws Exception {
170                 String csarPayload = getCsarPayload(csarUUID, yamlFileLocation);
171                 if (csarPayload != null) {
172                         Yaml yaml = new Yaml();
173                         Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
174                         return map;
175                 }
176                 return null;
177         }
178         
179         public static Map<?, ?> getToscaYamlMap(File csarPath, String yamlFileLocation) throws Exception {
180                 String csarPayload = getYamlPayloadFromCsar(csarPath, yamlFileLocation);
181                 if (csarPayload != null) {
182                         Yaml yaml = new Yaml();
183                         Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
184                         return map;
185                 }
186                 return null;
187         }
188         
189         
190         public static String getCsarPayload(String csarName, String yamlFileLocation) throws Exception {
191
192                 RestResponseAsByteArray csar = ImportRestUtils.getCsar(csarName, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
193                 assertTrue("Return response code different from 200", csar.getHttpStatusCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
194                 byte[] data = csar.getResponse();
195                 return getDataFromZipFileByBytes(yamlFileLocation, data);
196
197         }
198
199         public static String getYamlPayloadFromCsar(File csarName, String fileLocation) throws Exception {
200                 
201                 Path path = csarName.toPath();
202                 byte[] data = Files.readAllBytes(path);
203                 return getDataFromZipFileByBytes(fileLocation, data);
204                 
205         }
206
207         /** method get file data from zip data by file location in the zip structure 
208          * @param fileLocation
209          * @param data
210          * @return
211          */
212         public static String getDataFromZipFileByBytes(String fileLocation, byte[] data) {
213                 Map<String, byte[]> readZip = null;
214                 if (data != null && data.length > 0) {
215                         readZip = ZipUtil.readZip(data);
216
217                 }
218                 byte[] artifactsBs = readZip.get(fileLocation);
219                 String str = new String(artifactsBs, StandardCharsets.UTF_8);
220                 return str;
221         }
222 /*      public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
223                 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
224                 if (csarPayload != null) {
225                         Yaml yaml = new Yaml();
226                         Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
227                         return map;
228                 }
229                 return null;
230         }
231
232         public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
233
234                 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
235                 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
236                 assertNotNull("Tosca Entry-Definitions is null", map);
237                 if (map != null) {
238                         String definitionYamlLocation = (String) map.get("Entry-Definitions");
239                         Map<?, ?> toscaMap = getToscaYamlMap(csarUUID, definitionYamlLocation);
240                         assertNotNull("Tosca definition is null", toscaMap);
241                         if (toscaMap != null) {
242                                 ToscaDefinition toscaDefinition = new ToscaDefinition();
243                                 Set<?> keySet = toscaMap.keySet();
244                                 for (Object key : keySet) {
245                                         ToscaKeysEnum toscaKey = ToscaKeysEnum.findToscaKey((String) key);
246                                         switch (toscaKey) {
247                                         case TOSCA_DEFINITION_VERSION:
248                                                 getToscaDefinitionVersion(toscaMap, toscaDefinition);
249                                                 break;
250                                         case NODE_TYPES:
251                                                 getToscaNodeTypes(toscaMap, toscaDefinition);
252                                                 break;
253                                         case TOPOLOGY_TEMPLATE:
254                                                 getToscaTopologyTemplate(toscaMap, toscaDefinition);
255                                                 break;
256                                         case IMPORTS:
257                                                 // toscaMap.get("imports");
258                                                 break;
259                                         default:
260                                                 break;
261                                         }
262                                 }
263                                 return toscaDefinition;
264                         }
265                 }
266                 return null;
267
268         }
269
270         public static void getToscaDefinitionVersion(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
271                 if (toscaMap.get("tosca_definitions_version") != null) {
272                         toscaDefinition.setTosca_definitions_version((String) toscaMap.get("tosca_definitions_version"));
273                 }
274         }
275
276         // spec 90 page
277         public static void getToscaNodeTypes(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
278                 @SuppressWarnings("unchecked")
279                 Map<String, Map<String, String>> nodeTypes = (Map<String, Map<String, String>>) toscaMap.get("node_types");
280                 Map<String, ToscaNodeTypesDefinition> listToscaNodeTypes = new HashMap<String, ToscaNodeTypesDefinition>();
281                 if (nodeTypes != null) {
282                         for (Map.Entry<String, Map<String, String>> entry : nodeTypes.entrySet()) {
283                                 ToscaNodeTypesDefinition toscaNodeTypes = new ToscaNodeTypesDefinition();
284                                 String toscaNodeName = entry.getKey();
285                                 toscaNodeTypes.setName(toscaNodeName);
286
287                                 Map<String, String> toscaNodeType = entry.getValue();
288                                 if (toscaNodeType != null) {
289                                         Set<Entry<String, String>> entrySet = toscaNodeType.entrySet();
290                                         if (entrySet != null) {
291                                                 // boolean found = false;
292                                                 for (Entry<String, String> toscaNodeTypeMap : entrySet) {
293                                                         String key = toscaNodeTypeMap.getKey();
294                                                         if (key.equals("derived_from")) {
295                                                                 String derivedFrom = toscaNodeTypeMap.getValue();
296                                                                 toscaNodeTypes.setDerived_from(derivedFrom);
297                                                                 // found = true;
298                                                                 break;
299                                                         } else {
300                                                                 continue;
301                                                         }
302
303                                                 }
304                                                 // if (found == false) {
305                                                 // System.out.println("Tosca file not valid,
306                                                 // derived_from not found");
307                                                 // }
308                                         }
309
310                                 }
311 //                              listToscaNodeTypes.add(toscaNodeTypes);
312                                 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
313                         }
314                         toscaDefinition.setNode_types(listToscaNodeTypes);
315                 }
316         }
317
318         public static void getToscaTopologyTemplate(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
319                 ToscaTopologyTemplateDefinition toscaTopologyTemplate = new ToscaTopologyTemplateDefinition();
320                 @SuppressWarnings("unchecked")
321                 Map<String, Map<String, Object>> topologyTemplateMap = (Map<String, Map<String, Object>>) toscaMap.get("topology_template");
322 //              List<ToscaNodeTemplatesTopologyTemplateDefinition> listToscaNodeTemplates = new ArrayList<>();
323                 Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates = new HashMap<String, ToscaNodeTemplatesTopologyTemplateDefinition>();
324
325                 if (topologyTemplateMap != null) {
326                         getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
327                 }
328 //              toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
329                 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
330                 toscaDefinition.setTopology_template(toscaTopologyTemplate);
331         }
332
333         public static void getToscaNodeTemplates(Map<String, Map<String, Object>> topologyTemplateMap, Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates) {
334                 Map<String, Object> nodeTemplatesMap = topologyTemplateMap.get("node_templates");
335                 if (nodeTemplatesMap != null) {
336
337                         for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
338                                 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
339                                 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
340
341                                 @SuppressWarnings("unchecked")
342                                 Map<String, Object> node = (Map<String, Object>) nodeTemplates.getValue();
343                                 getNodeTemplatesType(toscaNodeTemplates, node);
344                                 getToscaNodeTemplateProperties(toscaNodeTemplates, node);
345                                 getToscaNodeTemplateRequirements(toscaNodeTemplates, node);
346                                 mapToscaNodeTemplates.putAll(mapToscaNodeTemplates);
347                         }
348                 }
349         }
350
351         public static void getToscaNodeTemplateRequirements(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates, Map<String, Object> node) {
352 ////            List<ToscaRequirementsNodeTemplatesDefinition> toscaRequirements = new ArrayList<>();
353 //              List<Map<String, ToscaRequirementsNodeTemplatesDefinition>> toscaRequirements = new ArrayList<>();
354 //              if (node.get("requirements") != null) {
355 //                      @SuppressWarnings("unchecked")
356 //                      List<Map<String, Object>> requirementList = (List<Map<String, Object>>) node.get("requirements");
357 //                      for (int i = 0; i < requirementList.size(); i++) {
358 //                              for (Map.Entry<String, Object> requirement : requirementList.get(i).entrySet()) {
359 //                                      ToscaRequirementsNodeTemplatesDefinition toscaRequirement = new ToscaRequirementsNodeTemplatesDefinition();
360 //                                      if (requirement.getKey() != null) {
361 //                                              String requirementName = requirement.getKey();
362 //                                              toscaRequirement.setName(requirementName);
363 //                                      } else {
364 //                                              log.debug("Tosca file not valid, requirements should contain name");
365 //                                      }
366 //
367 //                                      @SuppressWarnings("unchecked")
368 //                                      Map<String, String> requirementMap = (Map<String, String>) requirement.getValue();
369 //                                      Set<Entry<String, String>> entrySet = requirementMap.entrySet();
370 //                                      if (entrySet != null) {
371 //                                              for (Entry<String, String> requirementField : entrySet) {
372 //                                                      String key = requirementField.getKey();
373 //                                                      switch (key) {
374 //                                                      case "capability":
375 //                                                              if (requirementMap.get(key) != null) {
376 //                                                                      String capability = (String) requirementMap.get(key);
377 //                                                                      toscaRequirement.setCapability(capability);
378 //                                                                      break;
379 //                                                              } else {
380 //                                                                      continue;
381 //                                                              }
382 //                                                      case "node":
383 //                                                              if (requirementMap.get(key) != null) {
384 //                                                                      String requirementNode = (String) requirementMap.get(key);
385 //                                                                      toscaRequirement.setNode(requirementNode);
386 //                                                                      break;
387 //                                                              } else {
388 //                                                                      continue;
389 //                                                              }
390 //                                                      case "relationship":
391 //                                                              if (requirementMap.get(key) != null) {
392 //                                                                      String relationship = (String) requirementMap.get(key);
393 //                                                                      toscaRequirement.setRelationship(relationship);
394 //                                                                      break;
395 //                                                              } else {
396 //                                                                      continue;
397 //                                                              }
398 //                                                      default:
399 //                                                              break;
400 //                                                      }
401 //                                              }
402 //                                      }
403 ////                                    toscaRequirements.add(toscaRequirement);
404 //                                      toscaRequirements.add(requirementMap);
405 //                              }
406 //                      }
407 //              }
408 ////            toscaNodeTemplates.setRequirements(toscaRequirements);
409 //              toscaNodeTemplates.setRequirements(requirements);
410                 
411         }
412
413         public static void getToscaNodeTemplateProperties(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
414                         Map<String, Object> node) {
415 //              List<ToscaPropertiesNodeTemplatesDefinition> listToscaProperties = new ArrayList<>();
416                 Map<String, Object> mapToscaProperties = new HashMap<>();
417                 if (node.get("properties") != null) {
418                         @SuppressWarnings("unchecked")
419                         Map<String, Object> properties = (Map<String, Object>) node.get("properties");
420                         for (Map.Entry<String, Object> property : properties.entrySet()) {
421                                 ToscaPropertiesNodeTemplatesDefinition toscaProperty = new ToscaPropertiesNodeTemplatesDefinition();
422                                 String propertyName = property.getKey();
423                                 Object propertyValue = property.getValue();
424                                 toscaProperty.setName(propertyName);
425                                 toscaProperty.setValue(propertyValue);
426 //                              mapToscaProperties.add(toscaProperty);
427                                 mapToscaProperties.put(propertyName, propertyValue);
428                         }
429                 }
430                 toscaNodeTemplates.setProperties(mapToscaProperties);
431         }
432
433         protected static void getNodeTemplatesType(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
434                         Map<String, Object> node) {
435                 if (node.get("type") != null) {
436                         String type = (String) node.get("type");
437                         toscaNodeTemplates.setType(type);
438                 } else {
439                         log.debug("Tosca file not valid, nodeTemplate should contain type");
440                 }
441         }
442
443         protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
444                         ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
445                 String name = nodeTemplates.getKey();
446                 toscaNodeTemplates.setName(name);
447         }*/
448         
449         
450         
451 }