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;
26 import java.io.IOException;
27 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
29 import org.yaml.snakeyaml.Yaml;
31 public class ToscaItemsParser {
32 private LinkedHashMap<String, ToscaElement> allItemsFound;
37 * @param toscaYaml The tosca to parse
38 * @param toscaNativeDataTypeYaml THe name of the policy type to search
40 public ToscaItemsParser(String toscaYaml, String toscaNativeDataTypeYaml) {
41 this.allItemsFound = searchAllToscaElements(toscaYaml, toscaNativeDataTypeYaml);
44 public LinkedHashMap<String, ToscaElement> getAllItemsFound() {
48 private static LinkedHashMap<String, Object> searchAllDataTypesAndPolicyTypes(String toscaYaml) {
49 LinkedHashMap<String, LinkedHashMap<String, Object>> file =
50 (LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaYaml);
52 LinkedHashMap<String, Object> allItemsFound = file.get("data_types");
53 allItemsFound = (allItemsFound == null) ? (new LinkedHashMap<>()) : allItemsFound;
54 // Put the policies and datatypes in the same collection
55 allItemsFound.putAll(file.get("policy_types"));
59 private static LinkedHashMap<String, Object> searchAllNativeToscaDataTypes(String toscaNativeYaml) {
60 return ((LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaNativeYaml))
65 * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
66 * all entities and put them at the same level.
70 private static LinkedHashMap<String, ToscaElement> searchAllToscaElements(String toscaYaml,
71 String nativeToscaYaml) {
72 LinkedHashMap<String, Object> allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml);
73 allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml));
74 return parseAllItemsFound(allItemsFound);
78 * With all the component, get as Map, Components and Components properties are created.
82 private static LinkedHashMap<String, ToscaElement> parseAllItemsFound(LinkedHashMap<String, Object> allMaps) {
83 LinkedHashMap<String, ToscaElement> allItemsFound = new LinkedHashMap<String, ToscaElement>();
84 //Component creations, from the file maps
85 for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
86 LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) itemToParse.getValue();
87 ToscaElement toscaElement =
88 new ToscaElement(itemToParse.getKey(), (String) componentBody.get("derived_from"),
89 (String) componentBody.get("description"));
90 //If policy, version and type_version :
91 if (componentBody.get("type_version") != null) {
92 toscaElement.setVersion((String) componentBody.get("type_version"));
93 toscaElement.setTypeVersion((String) componentBody.get("type_version"));
95 //Properties creation, from the map
96 if (componentBody.get("properties") != null) {
97 LinkedHashMap<String, Object> properties =
98 (LinkedHashMap<String, Object>) componentBody.get("properties");
99 for (Entry<String, Object> itemToProperty : properties.entrySet()) {
100 Property property = new Property(itemToProperty.getKey(),
101 (LinkedHashMap<String, Object>) itemToProperty.getValue());
102 toscaElement.addProperties(property);
105 allItemsFound.put(toscaElement.getName(), toscaElement);
107 return allItemsFound;