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