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 * ===================================================================
23 package org.onap.clamp.clds.sdc.controller.installer;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Map.Entry;
37 import org.json.JSONObject;
38 import org.springframework.stereotype.Component;
39 import org.yaml.snakeyaml.Yaml;
42 public class BlueprintParser {
44 static final String TCA = "TCA";
45 static final String HOLMES = "Holmes";
46 private static final String TCA_POLICY = "tca_policy";
47 private static final String HOLMES_PREFIX = "holmes";
48 private static final String NODE_TEMPLATES = "node_templates";
49 private static final String DCAE_NODES = "dcae.nodes.";
50 private static final String TYPE = "type";
51 private static final String PROPERTIES = "properties";
52 private static final String NAME = "name";
53 private static final String RELATIONSHIPS = "relationships";
54 private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
55 private static final String TARGET = "target";
57 public Set<MicroService> getMicroServices(String blueprintString) {
58 Set<MicroService> microServices = new HashSet<>();
59 JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
60 JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
62 for (Entry<String, JsonElement> entry : results.entrySet()) {
63 JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
64 if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
65 MicroService microService = getNodeRepresentation(entry);
66 microServices.add(microService);
69 microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
73 public List<MicroService> fallbackToOneMicroService(String blueprintString) {
74 JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
75 JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
76 String theBiggestMicroServiceContent = "";
77 String theBiggestMicroServiceKey = "";
78 for (Entry<String, JsonElement> entry : results.entrySet()) {
79 String msAsString = entry.getValue().toString();
80 int len = msAsString.length();
81 if (len > theBiggestMicroServiceContent.length()) {
82 theBiggestMicroServiceContent = msAsString;
83 theBiggestMicroServiceKey = entry.getKey();
86 String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
87 return Collections.singletonList(new MicroService(msName, ""));
90 String getName(Entry<String, JsonElement> entry) {
91 String microServiceYamlName = entry.getKey();
92 JsonObject ob = entry.getValue().getAsJsonObject();
93 if (ob.has(PROPERTIES)) {
94 JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
95 if (properties.has(NAME)) {
96 return properties.get(NAME).getAsString();
99 return microServiceYamlName;
102 String getInput(Entry<String, JsonElement> entry) {
103 JsonObject ob = entry.getValue().getAsJsonObject();
104 if (ob.has(RELATIONSHIPS)) {
105 JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
106 for (JsonElement element : relationships) {
107 String target = getTarget(element.getAsJsonObject());
108 if (!target.isEmpty()) {
116 MicroService getNodeRepresentation(Entry<String, JsonElement> entry) {
117 String name = getName(entry);
118 String getInputFrom = getInput(entry);
119 return new MicroService(name, getInputFrom);
122 private String getTarget(JsonObject elementObject) {
123 if (elementObject.has(TYPE) && elementObject.has(TARGET)
124 && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
125 return elementObject.get(TARGET).getAsString();
130 private static JsonObject convertToJson(String yamlString) {
131 Yaml yaml = new Yaml();
132 Map<String, Object> map = yaml.load(yamlString);
134 JSONObject jsonObject = new JSONObject(map);
135 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);