Merge "Fix the tosca converter"
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / parser / ToscaElementParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update.parser;
25
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;
31
32 public class ToscaElementParser {
33     /**
34      * Constructor.
35      */
36     private ToscaElementParser() {
37     }
38
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);
48         return allItemsFound;
49     }
50
51     private static LinkedHashMap<String, Object> searchAllNativeToscaDataTypes(String toscaNativeYaml) {
52         return ((LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaNativeYaml))
53                 .get("data_types");
54     }
55
56     /**
57      * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
58      * all entities and put them at the same level.
59      *
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)
63      */
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);
69     }
70
71     /**
72      * With all the component, get as Map, Components and Components properties are created.
73      *
74      * @param allMaps maps
75      */
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"));
88             }
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);
97                 }
98             }
99             allItemsFound.put(toscaElement.getName(), toscaElement);
100         }
101         return allItemsFound;
102     }
103 }