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 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";
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();
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);
74 microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
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();
91 String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
93 .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", ""));
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();
105 return microServiceYamlName;
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()) {
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()),
128 if (!modelType.isEmpty()) {
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();
147 // Then it's may be a relationship
148 if (ob.has(RELATIONSHIPS)) {
149 return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList);
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, "");
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();
169 private static JsonObject convertToJson(String yamlString) {
170 Yaml yaml = new Yaml();
171 Map<String, Object> map = yaml.load(yamlString);
173 JSONObject jsonObject = new JSONObject(map);
174 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);