Merge "Fix model type searching"
[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 INPUT = "inputs";
57     private static final String GET_INPUT = "get_input";
58     private static final String POLICY_MODELID = "policy_model_id";
59     private static final String RELATIONSHIPS = "relationships";
60     private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
61     private static final String TARGET = "target";
62
63     public Set<MicroService> getMicroServices(String blueprintString) {
64         Set<MicroService> microServices = new HashSet<>();
65         JsonObject blueprintJson = BlueprintParser.convertToJson(blueprintString);
66         JsonObject nodeTemplateList = blueprintJson.get(NODE_TEMPLATES).getAsJsonObject();
67         JsonObject inputList = blueprintJson.get(INPUT).getAsJsonObject();
68
69         for (Entry<String, JsonElement> entry : nodeTemplateList.entrySet()) {
70             JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
71             if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
72                 MicroService microService = getNodeRepresentation(entry, nodeTemplateList, inputList);
73                 microServices.add(microService);
74             }
75         }
76         microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
77         return microServices;
78     }
79
80     public List<MicroService> fallbackToOneMicroService(String blueprintString) {
81         JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
82         JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
83         String theBiggestMicroServiceContent = "";
84         String theBiggestMicroServiceKey = "";
85         for (Entry<String, JsonElement> entry : results.entrySet()) {
86             String msAsString = entry.getValue().toString();
87             int len = msAsString.length();
88             if (len > theBiggestMicroServiceContent.length()) {
89                 theBiggestMicroServiceContent = msAsString;
90                 theBiggestMicroServiceKey = entry.getKey();
91             }
92         }
93         String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
94         return Collections
95             .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", ""));
96     }
97
98     String getName(Entry<String, JsonElement> entry) {
99         String microServiceYamlName = entry.getKey();
100         JsonObject ob = entry.getValue().getAsJsonObject();
101         if (ob.has(PROPERTIES)) {
102             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
103             if (properties.has(NAME)) {
104                 return properties.get(NAME).getAsString();
105             }
106         }
107         return microServiceYamlName;
108     }
109
110     String getInput(Entry<String, JsonElement> entry) {
111         JsonObject ob = entry.getValue().getAsJsonObject();
112         if (ob.has(RELATIONSHIPS)) {
113             JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
114             for (JsonElement element : relationships) {
115                 String target = getTarget(element.getAsJsonObject());
116                 if (!target.isEmpty()) {
117                     return target;
118                 }
119             }
120         }
121         return "";
122     }
123
124     String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList, JsonObject inputList) {
125         for (JsonElement elem : jsonArray) {
126             String modelType = getModelType(
127                 new AbstractMap.SimpleEntry<String, JsonElement>(elem.getAsJsonObject().get(TARGET).getAsString(),
128                     nodeTemplateList.get(elem.getAsJsonObject().get(TARGET).getAsString()).getAsJsonObject()),
129                 nodeTemplateList, inputList);
130             if (!modelType.isEmpty()) {
131                 return modelType;
132             }
133         }
134         return "";
135     }
136
137     String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList, JsonObject inputList) {
138         JsonObject ob = entry.getValue().getAsJsonObject();
139         // Search first in this node template
140         if (ob.has(PROPERTIES)) {
141             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
142             if (properties.has(POLICY_MODELID)) {
143                 if (properties.get(POLICY_MODELID).isJsonObject()) {
144                     // it's a blueprint parameter
145                     return inputList.get(properties.get(POLICY_MODELID).getAsJsonObject().get(GET_INPUT).getAsString())
146                         .getAsJsonObject().get("default").getAsString();
147                 } else {
148                     // It's a direct value
149                     return properties.get(POLICY_MODELID).getAsString();
150                 }
151             }
152         }
153         // Or it's may be defined in a relationship
154         if (ob.has(RELATIONSHIPS)) {
155             return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList, inputList);
156         }
157         return "";
158     }
159
160     MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList,
161         JsonObject inputList) {
162         String name = getName(entry);
163         String getInputFrom = getInput(entry);
164         String modelType = getModelType(entry, nodeTemplateList, inputList);
165         return new MicroService(name, modelType, getInputFrom, "");
166     }
167
168     private String getTarget(JsonObject elementObject) {
169         if (elementObject.has(TYPE) && elementObject.has(TARGET)
170             && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
171             return elementObject.get(TARGET).getAsString();
172         }
173         return "";
174     }
175
176     private static JsonObject convertToJson(String yamlString) {
177         Yaml yaml = new Yaml();
178         Map<String, Object> map = yaml.load(yamlString);
179
180         JSONObject jsonObject = new JSONObject(map);
181         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
182     }
183 }