2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils;
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;
37 import java.io.FileInputStream;
38 import java.nio.charset.StandardCharsets;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
43 import static org.testng.AssertJUnit.assertNotNull;
44 import static org.testng.AssertJUnit.assertTrue;
46 public class ToscaParserUtils {
48 private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
51 /**method get csarUUID and send GET API request toward BE
56 public static ToscaDefinition parseToscaYamlToJavaObjectByCsarUuid(String csarUUID) throws Exception {
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);
63 File definitionYamlLocation = (File) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
64 toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
66 return toscaDefinition;
70 /**method read csar from location
71 * @param csarNameLocation - full path with csar name
75 public static ToscaDefinition parseToscaMainYamlToJavaObjectByCsarLocation(File csarNameLocation) throws Exception {
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);
82 assertNotNull("Tosca Entry-Definitions is null", map);
84 String definitionYamlLocation = (String) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
85 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, definitionYamlLocation);
86 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
87 return toscaDefinition;
91 public static ToscaDefinition parseToscaAnyYamlToJavaObjectByCsarLocation(File csarNameLocation, String yamlLocation) throws Exception {
93 ToscaDefinition toscaDefinition = null;
94 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, yamlLocation);
95 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
96 return toscaDefinition;
100 public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
102 ToscaDefinition toscaDefinition = null;
104 // File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
105 FileInputStream fis = null;
107 try (FileInputStream fileInputStream = fis = new FileInputStream(path)) {
110 } catch (Exception e) {
111 System.out.println("Exception: " + e);
114 Constructor constructor = initToscaDefinitionObject();
116 Yaml yaml = new Yaml(constructor);
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);
125 return toscaDefinition;
129 public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
131 ToscaDefinition toscaDefinition = null;
132 Constructor constructor = initToscaDefinitionObject();
134 Yaml yaml = new Yaml(constructor);
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);
143 return toscaDefinition;
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;
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);
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);
187 public static String getCsarPayload(String csarName, String yamlFileLocation) throws Exception {
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);
196 public static String getYamlPayloadFromCsar(File csarName, String fileLocation) throws Exception {
198 Path path = csarName.toPath();
199 byte[] data = Files.readAllBytes(path);
200 return getDataFromZipFileByBytes(fileLocation, data);
204 /** method get file data from zip data by file location in the zip structure
205 * @param fileLocation
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);
215 byte[] artifactsBs = readZip.get(fileLocation);
216 String str = new String(artifactsBs, StandardCharsets.UTF_8);
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);
229 public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
231 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
232 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
233 assertNotNull("Tosca Entry-Definitions is null", map);
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);
244 case TOSCA_DEFINITION_VERSION:
245 getToscaDefinitionVersion(toscaMap, toscaDefinition);
248 getToscaNodeTypes(toscaMap, toscaDefinition);
250 case TOPOLOGY_TEMPLATE:
251 getToscaTopologyTemplate(toscaMap, toscaDefinition);
254 // toscaMap.get("imports");
260 return toscaDefinition;
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"));
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);
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);
301 // if (found == false) {
302 // System.out.println("Tosca file not valid,
303 // derived_from not found");
308 // listToscaNodeTypes.add(toscaNodeTypes);
309 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
311 toscaDefinition.setNode_types(listToscaNodeTypes);
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>();
322 if (topologyTemplateMap != null) {
323 getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
325 // toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
326 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
327 toscaDefinition.setTopology_template(toscaTopologyTemplate);
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) {
334 for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
335 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
336 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
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);
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);
361 // log.debug("Tosca file not valid, requirements should contain name");
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();
371 // case "capability":
372 // if (requirementMap.get(key) != null) {
373 // String capability = (String) requirementMap.get(key);
374 // toscaRequirement.setCapability(capability);
380 // if (requirementMap.get(key) != null) {
381 // String requirementNode = (String) requirementMap.get(key);
382 // toscaRequirement.setNode(requirementNode);
387 // case "relationship":
388 // if (requirementMap.get(key) != null) {
389 // String relationship = (String) requirementMap.get(key);
390 // toscaRequirement.setRelationship(relationship);
400 //// toscaRequirements.add(toscaRequirement);
401 // toscaRequirements.add(requirementMap);
405 //// toscaNodeTemplates.setRequirements(toscaRequirements);
406 // toscaNodeTemplates.setRequirements(requirements);
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);
427 toscaNodeTemplates.setProperties(mapToscaProperties);
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);
436 log.debug("Tosca file not valid, nodeTemplate should contain type");
440 protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
441 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
442 String name = nodeTemplates.getKey();
443 toscaNodeTemplates.setName(name);