aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java
blob: 5dcffd61c464653b29f14e1e8b2155d70607310b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2019 Nokia 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============================================
 * Modifications copyright (c) 2019 AT&T
 * ===================================================================
 *
 */

package org.onap.clamp.clds.sdc.controller.installer;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONObject;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

@Component
public class BlueprintParser {

    static final String TCA = "TCA";
    static final String HOLMES = "Holmes";
    private static final String TCA_POLICY = "tca_policy";
    private static final String HOLMES_PREFIX = "holmes";
    private static final String NODE_TEMPLATES = "node_templates";
    private static final String DCAE_NODES = "dcae.nodes.";
    private static final String TYPE = "type";
    private static final String PROPERTIES = "properties";
    private static final String NAME = "name";
    private static final String POLICYID = "policy_id";
    private static final String POLICY_TYPEID = "policy_type_id";
    private static final String RELATIONSHIPS = "relationships";
    private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
    private static final String TARGET = "target";

    public Set<MicroService> getMicroServices(String blueprintString) {
        Set<MicroService> microServices = new HashSet<>();
        JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
        JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();

        for (Entry<String, JsonElement> entry : results.entrySet()) {
            JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
            if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
                MicroService microService = getNodeRepresentation(entry);
                microServices.add(microService);
            }
        }
        microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
        return microServices;
    }

    public List<MicroService> fallbackToOneMicroService(String blueprintString) {
        JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
        JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
        String theBiggestMicroServiceContent = "";
        String theBiggestMicroServiceKey = "";
        for (Entry<String, JsonElement> entry : results.entrySet()) {
            String msAsString = entry.getValue().toString();
            int len = msAsString.length();
            if (len > theBiggestMicroServiceContent.length()) {
                theBiggestMicroServiceContent = msAsString;
                theBiggestMicroServiceKey = entry.getKey();
            }
        }
        String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
        return Collections
            .singletonList(new MicroService(msName, "onap.policy.monitoring.cdap.tca.hi.lo.app", "", "", ""));
    }

    String getName(Entry<String, JsonElement> entry) {
        String microServiceYamlName = entry.getKey();
        JsonObject ob = entry.getValue().getAsJsonObject();
        if (ob.has(PROPERTIES)) {
            JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
            if (properties.has(NAME)) {
                return properties.get(NAME).getAsString();
            }
        }
        return microServiceYamlName;
    }

    String getInput(Entry<String, JsonElement> entry) {
        JsonObject ob = entry.getValue().getAsJsonObject();
        if (ob.has(RELATIONSHIPS)) {
            JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
            for (JsonElement element : relationships) {
                String target = getTarget(element.getAsJsonObject());
                if (!target.isEmpty()) {
                    return target;
                }
            }
        }
        return "";
    }

    String getModelType(Entry<String, JsonElement> entry) {
        JsonObject ob = entry.getValue().getAsJsonObject();
        if (ob.has(PROPERTIES)) {
            JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
            if (properties.has(POLICYID)) {
                JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
                if (policyIdObj.has(POLICY_TYPEID)) {
                    return policyIdObj.get(POLICY_TYPEID).getAsString();
                }
            }
        }
        return "";
    }

    String getBlueprintName(Entry<String, JsonElement> entry) {
        return entry.getKey();
    }

    MicroService getNodeRepresentation(Entry<String, JsonElement> entry) {
        String name = getName(entry);
        String getInputFrom = getInput(entry);
        String modelType = getModelType(entry);
        String blueprintName = getBlueprintName(entry);
        return new MicroService(name, modelType, getInputFrom, "", blueprintName);
    }

    private String getTarget(JsonObject elementObject) {
        if (elementObject.has(TYPE) && elementObject.has(TARGET)
            && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
            return elementObject.get(TARGET).getAsString();
        }
        return "";
    }

    private static JsonObject convertToJson(String yamlString) {
        Yaml yaml = new Yaml();
        Map<String, Object> map = yaml.load(yamlString);

        JSONObject jsonObject = new JSONObject(map);
        return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
    }
}