2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.clds.sdc.controller.installer;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
32 import java.util.AbstractMap;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.List;
37 import java.util.Map.Entry;
40 import org.json.JSONObject;
41 import org.springframework.stereotype.Component;
42 import org.yaml.snakeyaml.Yaml;
45 public class BlueprintParser {
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";
64 * Get all micro services from blueprint.
66 * @param blueprintString the blueprint in a String
67 * @return A set of MircoService
69 public Set<MicroService> getMicroServices(String blueprintString) {
70 Set<MicroService> microServices = new HashSet<>();
71 JsonObject blueprintJson = BlueprintParser.convertToJson(blueprintString);
72 JsonObject nodeTemplateList = blueprintJson.get(NODE_TEMPLATES).getAsJsonObject();
73 JsonObject inputList = blueprintJson.get(INPUT).getAsJsonObject();
75 for (Entry<String, JsonElement> entry : nodeTemplateList.entrySet()) {
76 JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
77 if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
78 MicroService microService = getNodeRepresentation(entry, nodeTemplateList, inputList);
79 microServices.add(microService);
82 microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
87 * Does a fallback to TCA or Holmes.
89 * @param blueprintString the blueprint in a String
90 * @return The list of microservices
92 public List<MicroService> fallbackToOneMicroService(String blueprintString) {
93 JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
94 JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
95 String theBiggestMicroServiceContent = "";
96 String theBiggestMicroServiceKey = "";
97 for (Entry<String, JsonElement> entry : results.entrySet()) {
98 String msAsString = entry.getValue().toString();
99 int len = msAsString.length();
100 if (len > theBiggestMicroServiceContent.length()) {
101 theBiggestMicroServiceContent = msAsString;
102 theBiggestMicroServiceKey = entry.getKey();
105 String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
107 .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", ""));
110 String getName(Entry<String, JsonElement> entry) {
111 String microServiceYamlName = entry.getKey();
112 JsonObject ob = entry.getValue().getAsJsonObject();
113 if (ob.has(PROPERTIES)) {
114 JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
115 if (properties.has(NAME)) {
116 return properties.get(NAME).getAsString();
119 return microServiceYamlName;
122 String getInput(Entry<String, JsonElement> entry) {
123 JsonObject ob = entry.getValue().getAsJsonObject();
124 if (ob.has(RELATIONSHIPS)) {
125 JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
126 for (JsonElement element : relationships) {
127 String target = getTarget(element.getAsJsonObject());
128 if (!target.isEmpty()) {
136 String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList, JsonObject inputList) {
137 for (JsonElement elem : jsonArray) {
138 String modelType = getModelType(
139 new AbstractMap.SimpleEntry<String, JsonElement>(elem.getAsJsonObject().get(TARGET).getAsString(),
140 nodeTemplateList.get(elem.getAsJsonObject().get(TARGET).getAsString()).getAsJsonObject()),
141 nodeTemplateList, inputList);
142 if (!modelType.isEmpty()) {
149 String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList, JsonObject inputList) {
150 JsonObject ob = entry.getValue().getAsJsonObject();
151 // Search first in this node template
152 if (ob.has(PROPERTIES)) {
153 JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
154 if (properties.has(POLICY_MODELID)) {
155 if (properties.get(POLICY_MODELID).isJsonObject()) {
156 // it's a blueprint parameter
157 return inputList.get(properties.get(POLICY_MODELID).getAsJsonObject().get(GET_INPUT).getAsString())
158 .getAsJsonObject().get("default").getAsString();
160 // It's a direct value
161 return properties.get(POLICY_MODELID).getAsString();
165 // Or it's may be defined in a relationship
166 if (ob.has(RELATIONSHIPS)) {
167 return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList, inputList);
172 MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList,
173 JsonObject inputList) {
174 String name = getName(entry);
175 String getInputFrom = getInput(entry);
176 String modelType = getModelType(entry, nodeTemplateList, inputList);
177 return new MicroService(name, modelType, getInputFrom, "");
180 private String getTarget(JsonObject elementObject) {
181 if (elementObject.has(TYPE) && elementObject.has(TARGET)
182 && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
183 return elementObject.get(TARGET).getAsString();
188 private static JsonObject convertToJson(String yamlString) {
189 Yaml yaml = new Yaml();
190 Map<String, Object> map = yaml.load(yamlString);
192 JSONObject jsonObject = new JSONObject(map);
193 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);