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