Merge "Fix model_type missing"
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / BlueprintParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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  * Modifications copyright (c) 2019 AT&T
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.sdc.controller.installer;
26
27 import com.google.gson.Gson;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31
32 import java.util.AbstractMap;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Set;
39
40 import org.json.JSONObject;
41 import org.springframework.stereotype.Component;
42 import org.yaml.snakeyaml.Yaml;
43
44 @Component
45 public class BlueprintParser {
46
47     static final String TCA = "TCA";
48     static final String HOLMES = "Holmes";
49     private static final String TCA_POLICY = "tca_policy";
50     private static final String HOLMES_PREFIX = "holmes";
51     private static final String NODE_TEMPLATES = "node_templates";
52     private static final String DCAE_NODES = "dcae.nodes.";
53     private static final String TYPE = "type";
54     private static final String PROPERTIES = "properties";
55     private static final String NAME = "name";
56     private static final String POLICYID = "policy_id";
57     private static final String POLICY_TYPEID = "policy_type_id";
58     private static final String RELATIONSHIPS = "relationships";
59     private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
60     private static final String TARGET = "target";
61
62     public Set<MicroService> getMicroServices(String blueprintString) {
63         Set<MicroService> microServices = new HashSet<>();
64         JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
65         JsonObject nodeTemplateList = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
66
67         for (Entry<String, JsonElement> entry : nodeTemplateList.entrySet()) {
68             JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
69             if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
70                 MicroService microService = getNodeRepresentation(entry, nodeTemplateList);
71                 microServices.add(microService);
72             }
73         }
74         microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
75         return microServices;
76     }
77
78     public List<MicroService> fallbackToOneMicroService(String blueprintString) {
79         JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
80         JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
81         String theBiggestMicroServiceContent = "";
82         String theBiggestMicroServiceKey = "";
83         for (Entry<String, JsonElement> entry : results.entrySet()) {
84             String msAsString = entry.getValue().toString();
85             int len = msAsString.length();
86             if (len > theBiggestMicroServiceContent.length()) {
87                 theBiggestMicroServiceContent = msAsString;
88                 theBiggestMicroServiceKey = entry.getKey();
89             }
90         }
91         String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
92         return Collections
93             .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", ""));
94     }
95
96     String getName(Entry<String, JsonElement> entry) {
97         String microServiceYamlName = entry.getKey();
98         JsonObject ob = entry.getValue().getAsJsonObject();
99         if (ob.has(PROPERTIES)) {
100             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
101             if (properties.has(NAME)) {
102                 return properties.get(NAME).getAsString();
103             }
104         }
105         return microServiceYamlName;
106     }
107
108     String getInput(Entry<String, JsonElement> entry) {
109         JsonObject ob = entry.getValue().getAsJsonObject();
110         if (ob.has(RELATIONSHIPS)) {
111             JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
112             for (JsonElement element : relationships) {
113                 String target = getTarget(element.getAsJsonObject());
114                 if (!target.isEmpty()) {
115                     return target;
116                 }
117             }
118         }
119         return "";
120     }
121
122     String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList) {
123         for (JsonElement elem : jsonArray) {
124             String modelType = getModelType(
125                 new AbstractMap.SimpleEntry<String, JsonElement>(elem.getAsJsonObject().get(TARGET).getAsString(),
126                     nodeTemplateList.get(elem.getAsJsonObject().get(TARGET).getAsString()).getAsJsonObject()),
127                 nodeTemplateList);
128             if (!modelType.isEmpty()) {
129                 return modelType;
130             }
131         }
132         return "";
133     }
134
135     String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList) {
136         JsonObject ob = entry.getValue().getAsJsonObject();
137         // Search first in this node template
138         if (ob.has(PROPERTIES)) {
139             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
140             if (properties.has(POLICYID)) {
141                 JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
142                 if (policyIdObj.has(POLICY_TYPEID)) {
143                     return policyIdObj.get(POLICY_TYPEID).getAsString();
144                 }
145             }
146         }
147         // Then it's may be a relationship
148         if (ob.has(RELATIONSHIPS)) {
149             return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList);
150         }
151         return "";
152     }
153
154     MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList) {
155         String name = getName(entry);
156         String getInputFrom = getInput(entry);
157         String modelType = getModelType(entry, nodeTemplateList);
158         return new MicroService(name, modelType, getInputFrom, "");
159     }
160
161     private String getTarget(JsonObject elementObject) {
162         if (elementObject.has(TYPE) && elementObject.has(TARGET)
163             && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
164             return elementObject.get(TARGET).getAsString();
165         }
166         return "";
167     }
168
169     private static JsonObject convertToJson(String yamlString) {
170         Yaml yaml = new Yaml();
171         Map<String, Object> map = yaml.load(yamlString);
172
173         JSONObject jsonObject = new JSONObject(map);
174         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
175     }
176 }