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