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;
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;
54 public class ToscaParserUtils {
56 private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
59 /**method get csarUUID and send GET API request toward BE
64 public static ToscaDefinition parseToscaYamlToJavaObjectByCsarUuid(String csarUUID) throws Exception {
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);
71 File definitionYamlLocation = (File) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
72 toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
74 return toscaDefinition;
78 /**method read csar from location
79 * @param csarNameLocation - full path with csar name
83 public static ToscaDefinition parseToscaMainYamlToJavaObjectByCsarLocation(File csarNameLocation) throws Exception {
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);
90 assertNotNull("Tosca Entry-Definitions is null", map);
92 String definitionYamlLocation = (String) map.get(ToscaParameterConstants.ENTRY_DEFINITION);
93 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, definitionYamlLocation);
94 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
95 return toscaDefinition;
99 public static ToscaDefinition parseToscaAnyYamlToJavaObjectByCsarLocation(File csarNameLocation, String yamlLocation) throws Exception {
101 ToscaDefinition toscaDefinition = null;
102 String csarPayload = getYamlPayloadFromCsar(csarNameLocation, yamlLocation);
103 toscaDefinition = parseToscaYamlPayloadToJavaObject(csarPayload);
104 return toscaDefinition;
108 public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
110 ToscaDefinition toscaDefinition = null;
112 // File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
113 FileInputStream fis = null;
115 fis = new FileInputStream(path);
117 } catch (Exception e) {
118 System.out.println("Exception: " + e);
121 Constructor constructor = initToscaDefinitionObject();
123 Yaml yaml = new Yaml(constructor);
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);
132 return toscaDefinition;
136 public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
138 ToscaDefinition toscaDefinition = null;
139 Constructor constructor = initToscaDefinitionObject();
141 Yaml yaml = new Yaml(constructor);
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);
150 return toscaDefinition;
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;
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);
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);
194 public static String getCsarPayload(String csarName, String yamlFileLocation) throws Exception {
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);
203 public static String getYamlPayloadFromCsar(File csarName, String fileLocation) throws Exception {
205 Path path = csarName.toPath();
206 byte[] data = Files.readAllBytes(path);
207 return getDataFromZipFileByBytes(fileLocation, data);
211 /** method get file data from zip data by file location in the zip structure
212 * @param fileLocation
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);
222 byte[] artifactsBs = readZip.get(fileLocation);
223 String str = new String(artifactsBs, StandardCharsets.UTF_8);
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);
236 public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
238 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
239 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
240 assertNotNull("Tosca Entry-Definitions is null", map);
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);
251 case TOSCA_DEFINITION_VERSION:
252 getToscaDefinitionVersion(toscaMap, toscaDefinition);
255 getToscaNodeTypes(toscaMap, toscaDefinition);
257 case TOPOLOGY_TEMPLATE:
258 getToscaTopologyTemplate(toscaMap, toscaDefinition);
261 // toscaMap.get("imports");
267 return toscaDefinition;
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"));
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);
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);
308 // if (found == false) {
309 // System.out.println("Tosca file not valid,
310 // derived_from not found");
315 // listToscaNodeTypes.add(toscaNodeTypes);
316 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
318 toscaDefinition.setNode_types(listToscaNodeTypes);
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>();
329 if (topologyTemplateMap != null) {
330 getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
332 // toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
333 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
334 toscaDefinition.setTopology_template(toscaTopologyTemplate);
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) {
341 for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
342 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
343 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
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);
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);
368 // log.debug("Tosca file not valid, requirements should contain name");
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();
378 // case "capability":
379 // if (requirementMap.get(key) != null) {
380 // String capability = (String) requirementMap.get(key);
381 // toscaRequirement.setCapability(capability);
387 // if (requirementMap.get(key) != null) {
388 // String requirementNode = (String) requirementMap.get(key);
389 // toscaRequirement.setNode(requirementNode);
394 // case "relationship":
395 // if (requirementMap.get(key) != null) {
396 // String relationship = (String) requirementMap.get(key);
397 // toscaRequirement.setRelationship(relationship);
407 //// toscaRequirements.add(toscaRequirement);
408 // toscaRequirements.add(requirementMap);
412 //// toscaNodeTemplates.setRequirements(toscaRequirements);
413 // toscaNodeTemplates.setRequirements(requirements);
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);
434 toscaNodeTemplates.setProperties(mapToscaProperties);
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);
443 log.debug("Tosca file not valid, nodeTemplate should contain type");
447 protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
448 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
449 String name = nodeTemplates.getKey();
450 toscaNodeTemplates.setName(name);