2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.tosca.update.parser;
26 import java.util.LinkedHashMap;
27 import java.util.Map.Entry;
28 import org.onap.clamp.clds.tosca.update.elements.ToscaElement;
29 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
30 import org.yaml.snakeyaml.Yaml;
32 public class ToscaElementParser {
36 private ToscaElementParser() {
39 private static LinkedHashMap<String, Object> searchAllDataTypesAndPolicyTypes(String toscaYaml) {
40 LinkedHashMap<String, LinkedHashMap<String, Object>> file =
41 (LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaYaml);
42 LinkedHashMap<String, Object> allDataTypesFound = file.get("data_types");
43 LinkedHashMap<String, Object> allPolicyTypesFound = file.get("policy_types");
44 LinkedHashMap<String, Object> allItemsFound = new LinkedHashMap<>();
45 // Put the policies and datatypes in the same collection
46 allItemsFound = (allDataTypesFound == null) ? (new LinkedHashMap<>()) : allDataTypesFound;
47 allItemsFound.putAll(allPolicyTypesFound == null ? new LinkedHashMap<>() : allPolicyTypesFound);
51 private static LinkedHashMap<String, Object> searchAllNativeToscaDataTypes(String toscaNativeYaml) {
52 return ((LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaNativeYaml))
57 * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
58 * all entities and put them at the same level.
60 * @param toscaYaml the tosca model content
61 * @param nativeToscaYaml the tosca native datatype content
62 * @return a map of Tosca Element containing all tosca elements found (policy types and datatypes)
64 public static LinkedHashMap<String, ToscaElement> searchAllToscaElements(String toscaYaml,
65 String nativeToscaYaml) {
66 LinkedHashMap<String, Object> allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml);
67 allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml));
68 return parseAllItemsFound(allItemsFound);
72 * With all the component, get as Map, Components and Components properties are created.
76 private static LinkedHashMap<String, ToscaElement> parseAllItemsFound(LinkedHashMap<String, Object> allMaps) {
77 LinkedHashMap<String, ToscaElement> allItemsFound = new LinkedHashMap<String, ToscaElement>();
78 //Component creations, from the file maps
79 for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
80 LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) itemToParse.getValue();
81 ToscaElement toscaElement =
82 new ToscaElement(itemToParse.getKey(), (String) componentBody.get("derived_from"),
83 (String) componentBody.get("description"));
84 //If policy, version and type_version :
85 if (componentBody.get("type_version") != null) {
86 toscaElement.setVersion((String) componentBody.get("type_version"));
87 toscaElement.setTypeVersion((String) componentBody.get("type_version"));
89 //Properties creation, from the map
90 if (componentBody.get("properties") != null) {
91 LinkedHashMap<String, Object> properties =
92 (LinkedHashMap<String, Object>) componentBody.get("properties");
93 for (Entry<String, Object> itemToProperty : properties.entrySet()) {
94 ToscaElementProperty toscaElementProperty = new ToscaElementProperty(itemToProperty.getKey(),
95 (LinkedHashMap<String, Object>) itemToProperty.getValue());
96 toscaElement.addProperties(toscaElementProperty);
99 allItemsFound.put(toscaElement.getName(), toscaElement);
101 return allItemsFound;