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 static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
27 import java.io.FileInputStream;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
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;
55 public class ToscaParserUtils {
57 private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
60 /**method get csarUUID and send GET API request toward BE
65 public static ToscaDefinition parseToscaYamlToJavaObjectByCsarUuid(String csarUUID) throws Exception {
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);
72 File definitionYamlLocation = (File) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
73 toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
75 return toscaDefinition;
79 /**method read csar from location
80 * @param csarNameLocation - full path with csar name
84 public static ToscaDefinition parseToscaMainYamlToJavaObjectByCsarLocation(File csarNameLocation) throws Exception {
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);
91 assertNotNull("Tosca Entry-Definitions is null", map);
93 String definitionYamlLocation = (String) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
94 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, definitionYamlLocation);
95 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
96 return toscaDefinition;
100 public static ToscaDefinition parseToscaAnyYamlToJavaObjectByCsarLocation(File csarNameLocation, String yamlLocation) throws Exception {
102 ToscaDefinition toscaDefinition = null;
103 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, yamlLocation);
104 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
105 return toscaDefinition;
109 public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
111 ToscaDefinition toscaDefinition = null;
113 // File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
114 FileInputStream fis = null;
116 fis = new FileInputStream(path);
118 } catch (Exception e) {
119 System.out.println("Exception: " + e);
122 Constructor constructor = initToscaDefinitionObject();
124 Yaml yaml = new Yaml(constructor);
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);
133 return toscaDefinition;
137 public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
139 ToscaDefinition toscaDefinition = null;
140 Constructor constructor = initToscaDefinitionObject();
142 Yaml yaml = new Yaml(constructor);
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);
149 return toscaDefinition;
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());
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;
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);
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);
190 public static String getCsarPayload(String csarName, String yamlFileLocation) throws Exception {
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);
199 public static String getYamlPayloadFromCsar(File csarName, String fileLocation) throws Exception {
201 Path path = csarName.toPath();
202 byte[] data = Files.readAllBytes(path);
203 return getDataFromZipFileByBytes(fileLocation, data);
207 /** method get file data from zip data by file location in the zip structure
208 * @param fileLocation
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);
218 byte[] artifactsBs = readZip.get(fileLocation);
219 String str = new String(artifactsBs, StandardCharsets.UTF_8);
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);
232 public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
234 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
235 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
236 assertNotNull("Tosca Entry-Definitions is null", map);
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);
247 case TOSCA_DEFINITION_VERSION:
248 getToscaDefinitionVersion(toscaMap, toscaDefinition);
251 getToscaNodeTypes(toscaMap, toscaDefinition);
253 case TOPOLOGY_TEMPLATE:
254 getToscaTopologyTemplate(toscaMap, toscaDefinition);
257 // toscaMap.get("imports");
263 return toscaDefinition;
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"));
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);
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);
304 // if (found == false) {
305 // System.out.println("Tosca file not valid,
306 // derived_from not found");
311 // listToscaNodeTypes.add(toscaNodeTypes);
312 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
314 toscaDefinition.setNode_types(listToscaNodeTypes);
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>();
325 if (topologyTemplateMap != null) {
326 getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
328 // toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
329 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
330 toscaDefinition.setTopology_template(toscaTopologyTemplate);
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) {
337 for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
338 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
339 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
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);
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);
364 // log.debug("Tosca file not valid, requirements should contain name");
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();
374 // case "capability":
375 // if (requirementMap.get(key) != null) {
376 // String capability = (String) requirementMap.get(key);
377 // toscaRequirement.setCapability(capability);
383 // if (requirementMap.get(key) != null) {
384 // String requirementNode = (String) requirementMap.get(key);
385 // toscaRequirement.setNode(requirementNode);
390 // case "relationship":
391 // if (requirementMap.get(key) != null) {
392 // String relationship = (String) requirementMap.get(key);
393 // toscaRequirement.setRelationship(relationship);
403 //// toscaRequirements.add(toscaRequirement);
404 // toscaRequirements.add(requirementMap);
408 //// toscaNodeTemplates.setRequirements(toscaRequirements);
409 // toscaNodeTemplates.setRequirements(requirements);
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);
430 toscaNodeTemplates.setProperties(mapToscaProperties);
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);
439 log.debug("Tosca file not valid, nodeTemplate should contain type");
443 protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
444 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
445 String name = nodeTemplates.getKey();
446 toscaNodeTemplates.setName(name);