aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java
blob: c6eabcd34c1906867ef16c298aeacb57ff758abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.clamp.clds.tosca.update;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import org.yaml.snakeyaml.Yaml;

public class Extractor {
    private LinkedHashMap<String, Component> allItems;
    private String source;
    private String nativeComponent;

    /**
     * Constructor.
     *
     * @param toParse Tosca to parse
     * @param nativeComponent The policy type to scan
     */
    public Extractor(String toParse, String nativeComponent) {

        this.source = toParse;
        this.nativeComponent = nativeComponent;
        allItems = new LinkedHashMap<String, Component>();
        getAllAsMaps();
    }

    public LinkedHashMap<String, Component> getAllItems() {
        return allItems;
    }

    public String getSource() {
        return source;
    }

    /**
     * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
     * all entities and put them at the same level.
     *
     * @return an object
     */
    @SuppressWarnings("unchecked")
    public LinkedHashMap<String, Object> getAllAsMaps() {
        Yaml yaml = new Yaml();
        Object contentFile = yaml.load(source);
        LinkedHashMap<String, LinkedHashMap<String, Object>> file =
                (LinkedHashMap<String, LinkedHashMap<String, Object>>) contentFile;
        // Get DataTypes
        LinkedHashMap<String, Object> dataTypes = file.get("data_types");
        dataTypes = (dataTypes == null) ? (new LinkedHashMap<>()) : dataTypes;
        // Get Policies : first, get topology and after extract policies from it
        LinkedHashMap<String, Object> policyTypes = file.get("policy_types");
        // Put the policies and datatypes in the same collection
        dataTypes.putAll(policyTypes);

        Object contentNativeFile = yaml.load(nativeComponent);
        LinkedHashMap<String, Object> dataTypesEmbedded =
                ((LinkedHashMap<String, LinkedHashMap<String, Object>>) contentNativeFile).get("data_types");
        dataTypes.putAll(dataTypesEmbedded);

        parseInComponent(dataTypes);
        return dataTypes;
    }

    /**
     * With all the component, get as Map, Components and Components properties are created.
     *
     * @param allMaps maps
     */
    @SuppressWarnings("unchecked")
    public void parseInComponent(LinkedHashMap<String, Object> allMaps) {
        //Component creations, from the file maps
        for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
            LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) itemToParse.getValue();
            Component component = new Component(itemToParse.getKey(), (String) componentBody.get("derived_from"),
                    (String) componentBody.get("description"));
            //If policy, version and type_version :
            if (componentBody.get("type_version") != null) {
                component.setVersion((String) componentBody.get("type_version"));
                component.setTypeVersion((String) componentBody.get("type_version"));
            }
            //Properties creation, from the map
            if (componentBody.get("properties") != null) {
                LinkedHashMap<String, Object> properties =
                        (LinkedHashMap<String, Object>) componentBody.get("properties");
                for (Entry<String, Object> itemToProperty : properties.entrySet()) {
                    Property property = new Property(itemToProperty.getKey(),
                            (LinkedHashMap<String, Object>) itemToProperty.getValue());
                    component.addProperties(property);
                }
            }
            this.allItems.put(component.getName(), component);
        }
    }
}