Fix the tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / ToscaItemsParser.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;
25
26 import java.io.IOException;
27 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
29 import org.yaml.snakeyaml.Yaml;
30
31 public class ToscaItemsParser {
32     private LinkedHashMap<String, ToscaElement> allItemsFound;
33
34     /**
35      * Constructor.
36      *
37      * @param toscaYaml               The tosca to parse
38      * @param toscaNativeDataTypeYaml THe name of the policy type to search
39      */
40     public ToscaItemsParser(String toscaYaml, String toscaNativeDataTypeYaml) {
41         this.allItemsFound = searchAllToscaElements(toscaYaml, toscaNativeDataTypeYaml);
42     }
43
44     public LinkedHashMap<String, ToscaElement> getAllItemsFound() {
45         return allItemsFound;
46     }
47
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);
51         // Get DataTypes
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"));
56         return allItemsFound;
57     }
58
59     private static LinkedHashMap<String, Object> searchAllNativeToscaDataTypes(String toscaNativeYaml) {
60         return ((LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaNativeYaml))
61                 .get("data_types");
62     }
63
64     /**
65      * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
66      * all entities and put them at the same level.
67      *
68      * @return a map
69      */
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);
75     }
76
77     /**
78      * With all the component, get as Map, Components and Components properties are created.
79      *
80      * @param allMaps maps
81      */
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"));
94             }
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);
103                 }
104             }
105             allItemsFound.put(toscaElement.getName(), toscaElement);
106         }
107         return allItemsFound;
108     }
109 }