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.Collections;
33 import java.util.HashSet;
34 import java.util.List;
36 import java.util.Map.Entry;
39 import org.json.JSONObject;
40 import org.springframework.stereotype.Component;
41 import org.yaml.snakeyaml.Yaml;
44 public class BlueprintParser {
46 static final String TCA = "TCA";
47 static final String HOLMES = "Holmes";
48 private static final String TCA_POLICY = "tca_policy";
49 private static final String HOLMES_PREFIX = "holmes";
50 private static final String NODE_TEMPLATES = "node_templates";
51 private static final String DCAE_NODES = "dcae.nodes.";
52 private static final String TYPE = "type";
53 private static final String PROPERTIES = "properties";
54 private static final String NAME = "name";
55 private static final String POLICYID = "policy_id";
56 private static final String POLICY_TYPEID = "policy_type_id";
57 private static final String RELATIONSHIPS = "relationships";
58 private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
59 private static final String TARGET = "target";
61 public Set<MicroService> getMicroServices(String blueprintString) {
62 Set<MicroService> microServices = new HashSet<>();
63 JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
64 JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
66 for (Entry<String, JsonElement> entry : results.entrySet()) {
67 JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
68 if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
69 MicroService microService = getNodeRepresentation(entry);
70 microServices.add(microService);
73 microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
77 public List<MicroService> fallbackToOneMicroService(String blueprintString) {
78 JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
79 JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
80 String theBiggestMicroServiceContent = "";
81 String theBiggestMicroServiceKey = "";
82 for (Entry<String, JsonElement> entry : results.entrySet()) {
83 String msAsString = entry.getValue().toString();
84 int len = msAsString.length();
85 if (len > theBiggestMicroServiceContent.length()) {
86 theBiggestMicroServiceContent = msAsString;
87 theBiggestMicroServiceKey = entry.getKey();
90 String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
92 .singletonList(new MicroService(msName, "onap.policy.monitoring.cdap.tca.hi.lo.app", "", "", ""));
95 String getName(Entry<String, JsonElement> entry) {
96 String microServiceYamlName = entry.getKey();
97 JsonObject ob = entry.getValue().getAsJsonObject();
98 if (ob.has(PROPERTIES)) {
99 JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
100 if (properties.has(NAME)) {
101 return properties.get(NAME).getAsString();
104 return microServiceYamlName;
107 String getInput(Entry<String, JsonElement> entry) {
108 JsonObject ob = entry.getValue().getAsJsonObject();
109 if (ob.has(RELATIONSHIPS)) {
110 JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
111 for (JsonElement element : relationships) {
112 String target = getTarget(element.getAsJsonObject());
113 if (!target.isEmpty()) {
121 String getModelType(Entry<String, JsonElement> entry) {
122 JsonObject ob = entry.getValue().getAsJsonObject();
123 if (ob.has(PROPERTIES)) {
124 JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
125 if (properties.has(POLICYID)) {
126 JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
127 if (policyIdObj.has(POLICY_TYPEID)) {
128 return policyIdObj.get(POLICY_TYPEID).getAsString();
135 String getBlueprintName(Entry<String, JsonElement> entry) {
136 return entry.getKey();
139 MicroService getNodeRepresentation(Entry<String, JsonElement> entry) {
140 String name = getName(entry);
141 String getInputFrom = getInput(entry);
142 String modelType = getModelType(entry);
143 String blueprintName = getBlueprintName(entry);
144 return new MicroService(name, modelType, getInputFrom, "", blueprintName);
147 private String getTarget(JsonObject elementObject) {
148 if (elementObject.has(TYPE) && elementObject.has(TARGET)
149 && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
150 return elementObject.get(TARGET).getAsString();
155 private static JsonObject convertToJson(String yamlString) {
156 Yaml yaml = new Yaml();
157 Map<String, Object> map = yaml.load(yamlString);
159 JSONObject jsonObject = new JSONObject(map);
160 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);