b0bf827800067f25688d4d06ae90732c77ba3d1a
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / Extractor.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 Extractor {
32     private LinkedHashMap<String, Component> allItems;
33     private String source;
34     private String nativeComponent;
35
36     @SuppressWarnings("unchecked")
37     public Extractor(String toParse, String nativeComponent) throws IOException {
38
39         this.source = toParse;
40         this.nativeComponent = nativeComponent;
41         allItems = new LinkedHashMap<String, Component>();
42         getAllAsMaps();
43
44     }
45
46     public LinkedHashMap<String, Component> getAllItems() {
47         return allItems;
48     }
49
50     public String getSource() {
51         return source;
52     }
53
54     /**
55      * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
56      * all entities and put them at the same level.
57      *
58      * @return an object
59      */
60     @SuppressWarnings("unchecked")
61     public LinkedHashMap<String, Object> getAllAsMaps() {
62         Yaml yaml = new Yaml();
63         Object contentFile = yaml.load(source);
64         LinkedHashMap<String, LinkedHashMap<String, Object>> file =
65                 (LinkedHashMap<String, LinkedHashMap<String, Object>>) contentFile;
66         // Get DataTypes
67         LinkedHashMap<String, Object> dataTypes = file.get("data_types");
68         dataTypes = (dataTypes == null) ? (new LinkedHashMap<>()) : dataTypes;
69         // Get Policies : first, get topology and after extract policies from it
70         LinkedHashMap<String, Object> policyTypes = file.get("policy_types");
71         // Put the policies and datatypes in the same collection
72         dataTypes.putAll(policyTypes);
73
74         Object contentNativeFile = yaml.load(nativeComponent);
75         LinkedHashMap<String, Object> dataTypesEmbedded =
76                 ((LinkedHashMap<String, LinkedHashMap<String, Object>>) contentNativeFile).get("data_types");
77         dataTypes.putAll(dataTypesEmbedded);
78
79         parseInComponent(dataTypes);
80         return dataTypes;
81     }
82
83     /**
84      * With all the component, get as Map, Components and Components properties are created.
85      *
86      * @param allMaps maps
87      */
88     @SuppressWarnings("unchecked")
89     public void parseInComponent(LinkedHashMap<String, Object> allMaps) {
90         //Component creations, from the file maps
91         for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
92             LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) itemToParse.getValue();
93             Component component = new Component(itemToParse.getKey(), (String) componentBody.get("derived_from"),
94                     (String) componentBody.get("description"));
95             //If policy, version and type_version :
96             if (componentBody.get("type_version") != null) {
97                 component.setVersion((String) componentBody.get("type_version"));
98                 component.setTypeVersion((String) componentBody.get("type_version"));
99             }
100             //Properties creation, from the map
101             if (componentBody.get("properties") != null) {
102                 LinkedHashMap<String, Object> properties =
103                         (LinkedHashMap<String, Object>) componentBody.get("properties");
104                 for (Entry<String, Object> itemToProperty : properties.entrySet()) {
105                     Property property = new Property(itemToProperty.getKey(),
106                             (LinkedHashMap<String, Object>) itemToProperty.getValue());
107                     component.addProperties(property);
108                 }
109             }
110             this.allItems.put(component.getName(), component);
111         }
112     }
113 }