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;
26 import java.io.FileInputStream;
30 import org.openecomp.sdc.ci.tests.datatypes.enums.ToscaKeysEnum;
31 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
32 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsTopologyTemplateDefinition;
33 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
34 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaSubstitutionMappingsDefinition;
35 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaTopologyTemplateDefinition;
36 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.yaml.snakeyaml.TypeDescription;
40 import org.yaml.snakeyaml.Yaml;
41 import org.yaml.snakeyaml.constructor.Constructor;
42 import org.yaml.snakeyaml.introspector.PropertyUtils;
44 public class ToscaParserUtils {
46 private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
48 public static ToscaDefinition parseToscaYamlToJavaObject(String csarUUID) throws Exception {
50 ToscaDefinition toscaDefinition = null;
51 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
52 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
53 assertNotNull("Tosca Entry-Definitions is null", map);
55 File definitionYamlLocation = (File) map.get("Entry-Definitions");
56 toscaDefinition = parseToscaYamlToJavaObject(definitionYamlLocation);
58 return toscaDefinition;
62 public static ToscaDefinition parseToscaYamlToJavaObject(File path) throws Exception {
64 ToscaDefinition toscaDefinition = null;
66 // File path = new File("C:/Data/D2.0/TOSCA_Ex/Definitions/tosca_definition_version.yaml");
67 FileInputStream fis = null;
69 fis = new FileInputStream(path);
71 } catch (Exception e) {
72 System.out.println("Exception: " + e);
75 Constructor constructor = getConstructor();
77 Yaml yaml = new Yaml(constructor);
79 toscaDefinition = (ToscaDefinition) yaml.load(fis);
80 } catch (Exception e) {
81 log.debug("Failed to parse tosca yaml file");
82 System.out.println("Exception: " + e);
86 return toscaDefinition;
90 public static ToscaDefinition parseToscaYamlPayloadToJavaObject(String payload){
92 ToscaDefinition toscaDefinition = null;
93 Constructor constructor = getConstructor();
95 Yaml yaml = new Yaml(constructor);
97 toscaDefinition = (ToscaDefinition) yaml.load(payload);
98 } catch (Exception e) {
99 log.debug("Failed to parse tosca yaml file");
100 System.out.println("Exception: " + e);
102 return toscaDefinition;
107 public static Constructor getConstructor() {
108 Constructor constructor = new Constructor(ToscaDefinition.class);
109 constructor.addTypeDescription(ToscaDefinition.getTypeDescription());
110 constructor.addTypeDescription(ToscaTopologyTemplateDefinition.getTypeDescription());
111 constructor.addTypeDescription(ToscaNodeTemplatesTopologyTemplateDefinition.getTypeDescription());
112 constructor.addTypeDescription(ToscaGroupsTopologyTemplateDefinition.getTypeDescription());
113 constructor.addTypeDescription(ToscaSubstitutionMappingsDefinition.getTypeDescription());
115 // Skip properties which are found in YAML, but not found in POJO
116 PropertyUtils propertyUtils = new PropertyUtils();
117 propertyUtils.setSkipMissingProperties(true);
118 constructor.setPropertyUtils(propertyUtils);
122 public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
123 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
124 if (csarPayload != null) {
125 Yaml yaml = new Yaml();
126 Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
132 /* public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
133 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
134 if (csarPayload != null) {
135 Yaml yaml = new Yaml();
136 Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
142 public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
144 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
145 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
146 assertNotNull("Tosca Entry-Definitions is null", map);
148 String definitionYamlLocation = (String) map.get("Entry-Definitions");
149 Map<?, ?> toscaMap = getToscaYamlMap(csarUUID, definitionYamlLocation);
150 assertNotNull("Tosca definition is null", toscaMap);
151 if (toscaMap != null) {
152 ToscaDefinition toscaDefinition = new ToscaDefinition();
153 Set<?> keySet = toscaMap.keySet();
154 for (Object key : keySet) {
155 ToscaKeysEnum toscaKey = ToscaKeysEnum.findToscaKey((String) key);
157 case TOSCA_DEFINITION_VERSION:
158 getToscaDefinitionVersion(toscaMap, toscaDefinition);
161 getToscaNodeTypes(toscaMap, toscaDefinition);
163 case TOPOLOGY_TEMPLATE:
164 getToscaTopologyTemplate(toscaMap, toscaDefinition);
167 // toscaMap.get("imports");
173 return toscaDefinition;
180 public static void getToscaDefinitionVersion(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
181 if (toscaMap.get("tosca_definitions_version") != null) {
182 toscaDefinition.setTosca_definitions_version((String) toscaMap.get("tosca_definitions_version"));
187 public static void getToscaNodeTypes(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
188 @SuppressWarnings("unchecked")
189 Map<String, Map<String, String>> nodeTypes = (Map<String, Map<String, String>>) toscaMap.get("node_types");
190 Map<String, ToscaNodeTypesDefinition> listToscaNodeTypes = new HashMap<String, ToscaNodeTypesDefinition>();
191 if (nodeTypes != null) {
192 for (Map.Entry<String, Map<String, String>> entry : nodeTypes.entrySet()) {
193 ToscaNodeTypesDefinition toscaNodeTypes = new ToscaNodeTypesDefinition();
194 String toscaNodeName = entry.getKey();
195 toscaNodeTypes.setName(toscaNodeName);
197 Map<String, String> toscaNodeType = entry.getValue();
198 if (toscaNodeType != null) {
199 Set<Entry<String, String>> entrySet = toscaNodeType.entrySet();
200 if (entrySet != null) {
201 // boolean found = false;
202 for (Entry<String, String> toscaNodeTypeMap : entrySet) {
203 String key = toscaNodeTypeMap.getKey();
204 if (key.equals("derived_from")) {
205 String derivedFrom = toscaNodeTypeMap.getValue();
206 toscaNodeTypes.setDerived_from(derivedFrom);
214 // if (found == false) {
215 // System.out.println("Tosca file not valid,
216 // derived_from not found");
221 // listToscaNodeTypes.add(toscaNodeTypes);
222 listToscaNodeTypes.put(toscaNodeName, toscaNodeTypes);
224 toscaDefinition.setNode_types(listToscaNodeTypes);
228 public static void getToscaTopologyTemplate(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
229 ToscaTopologyTemplateDefinition toscaTopologyTemplate = new ToscaTopologyTemplateDefinition();
230 @SuppressWarnings("unchecked")
231 Map<String, Map<String, Object>> topologyTemplateMap = (Map<String, Map<String, Object>>) toscaMap.get("topology_template");
232 // List<ToscaNodeTemplatesTopologyTemplateDefinition> listToscaNodeTemplates = new ArrayList<>();
233 Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates = new HashMap<String, ToscaNodeTemplatesTopologyTemplateDefinition>();
235 if (topologyTemplateMap != null) {
236 getToscaNodeTemplates(topologyTemplateMap, mapToscaNodeTemplates);
238 // toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
239 toscaTopologyTemplate.setNode_templates(mapToscaNodeTemplates);
240 toscaDefinition.setTopology_template(toscaTopologyTemplate);
243 public static void getToscaNodeTemplates(Map<String, Map<String, Object>> topologyTemplateMap, Map<String,ToscaNodeTemplatesTopologyTemplateDefinition> mapToscaNodeTemplates) {
244 Map<String, Object> nodeTemplatesMap = topologyTemplateMap.get("node_templates");
245 if (nodeTemplatesMap != null) {
247 for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
248 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
249 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
251 @SuppressWarnings("unchecked")
252 Map<String, Object> node = (Map<String, Object>) nodeTemplates.getValue();
253 getNodeTemplatesType(toscaNodeTemplates, node);
254 getToscaNodeTemplateProperties(toscaNodeTemplates, node);
255 getToscaNodeTemplateRequirements(toscaNodeTemplates, node);
256 mapToscaNodeTemplates.putAll(mapToscaNodeTemplates);
261 public static void getToscaNodeTemplateRequirements(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates, Map<String, Object> node) {
262 //// List<ToscaRequirementsNodeTemplatesDefinition> toscaRequirements = new ArrayList<>();
263 // List<Map<String, ToscaRequirementsNodeTemplatesDefinition>> toscaRequirements = new ArrayList<>();
264 // if (node.get("requirements") != null) {
265 // @SuppressWarnings("unchecked")
266 // List<Map<String, Object>> requirementList = (List<Map<String, Object>>) node.get("requirements");
267 // for (int i = 0; i < requirementList.size(); i++) {
268 // for (Map.Entry<String, Object> requirement : requirementList.get(i).entrySet()) {
269 // ToscaRequirementsNodeTemplatesDefinition toscaRequirement = new ToscaRequirementsNodeTemplatesDefinition();
270 // if (requirement.getKey() != null) {
271 // String requirementName = requirement.getKey();
272 // toscaRequirement.setName(requirementName);
274 // log.debug("Tosca file not valid, requirements should contain name");
277 // @SuppressWarnings("unchecked")
278 // Map<String, String> requirementMap = (Map<String, String>) requirement.getValue();
279 // Set<Entry<String, String>> entrySet = requirementMap.entrySet();
280 // if (entrySet != null) {
281 // for (Entry<String, String> requirementField : entrySet) {
282 // String key = requirementField.getKey();
284 // case "capability":
285 // if (requirementMap.get(key) != null) {
286 // String capability = (String) requirementMap.get(key);
287 // toscaRequirement.setCapability(capability);
293 // if (requirementMap.get(key) != null) {
294 // String requirementNode = (String) requirementMap.get(key);
295 // toscaRequirement.setNode(requirementNode);
300 // case "relationship":
301 // if (requirementMap.get(key) != null) {
302 // String relationship = (String) requirementMap.get(key);
303 // toscaRequirement.setRelationship(relationship);
313 //// toscaRequirements.add(toscaRequirement);
314 // toscaRequirements.add(requirementMap);
318 //// toscaNodeTemplates.setRequirements(toscaRequirements);
319 // toscaNodeTemplates.setRequirements(requirements);
323 public static void getToscaNodeTemplateProperties(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
324 Map<String, Object> node) {
325 // List<ToscaPropertiesNodeTemplatesDefinition> listToscaProperties = new ArrayList<>();
326 Map<String, Object> mapToscaProperties = new HashMap<>();
327 if (node.get("properties") != null) {
328 @SuppressWarnings("unchecked")
329 Map<String, Object> properties = (Map<String, Object>) node.get("properties");
330 for (Map.Entry<String, Object> property : properties.entrySet()) {
331 ToscaPropertiesNodeTemplatesDefinition toscaProperty = new ToscaPropertiesNodeTemplatesDefinition();
332 String propertyName = property.getKey();
333 Object propertyValue = property.getValue();
334 toscaProperty.setName(propertyName);
335 toscaProperty.setValue(propertyValue);
336 // mapToscaProperties.add(toscaProperty);
337 mapToscaProperties.put(propertyName, propertyValue);
340 toscaNodeTemplates.setProperties(mapToscaProperties);
343 protected static void getNodeTemplatesType(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
344 Map<String, Object> node) {
345 if (node.get("type") != null) {
346 String type = (String) node.get("type");
347 toscaNodeTemplates.setType(type);
349 log.debug("Tosca file not valid, nodeTemplate should contain type");
353 protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
354 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
355 String name = nodeTemplates.getKey();
356 toscaNodeTemplates.setName(name);