LoopLog repository
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / BlueprintParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24 package org.onap.clamp.clds.sdc.controller.installer;
25
26 import com.google.gson.Gson;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37
38 import org.json.JSONObject;
39 import org.springframework.stereotype.Component;
40 import org.yaml.snakeyaml.Yaml;
41
42 @Component
43 public class BlueprintParser {
44
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 POLICYID = "policy_id";
55     private static final String POLICY_TYPEID = "policy_type_id";
56     private static final String RELATIONSHIPS = "relationships";
57     private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
58     private static final String TARGET = "target";
59
60     public Set<MicroService> getMicroServices(String blueprintString) {
61         Set<MicroService> microServices = new HashSet<>();
62         JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
63         JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
64
65         for (Entry<String, JsonElement> entry : results.entrySet()) {
66             JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
67             if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
68                 MicroService microService = getNodeRepresentation(entry);
69                 microServices.add(microService);
70             }
71         }
72         microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
73         return microServices;
74     }
75
76     public List<MicroService> fallbackToOneMicroService(String blueprintString) {
77         JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
78         JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
79         String theBiggestMicroServiceContent = "";
80         String theBiggestMicroServiceKey = "";
81         for (Entry<String, JsonElement> entry : results.entrySet()) {
82             String msAsString = entry.getValue().toString();
83             int len = msAsString.length();
84             if (len > theBiggestMicroServiceContent.length()) {
85                 theBiggestMicroServiceContent = msAsString;
86                 theBiggestMicroServiceKey = entry.getKey();
87             }
88         }
89         String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
90         return Collections.singletonList(new MicroService(msName, "", "", "", ""));
91     }
92
93     String getName(Entry<String, JsonElement> entry) {
94         String microServiceYamlName = entry.getKey();
95         JsonObject ob = entry.getValue().getAsJsonObject();
96         if (ob.has(PROPERTIES)) {
97             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
98             if (properties.has(NAME)) {
99                 return properties.get(NAME).getAsString();
100             }
101         }
102         return microServiceYamlName;
103     }
104
105     String getInput(Entry<String, JsonElement> entry) {
106         JsonObject ob = entry.getValue().getAsJsonObject();
107         if (ob.has(RELATIONSHIPS)) {
108             JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
109             for (JsonElement element : relationships) {
110                 String target = getTarget(element.getAsJsonObject());
111                 if (!target.isEmpty()) {
112                     return target;
113                 }
114             }
115         }
116         return "";
117     }
118
119     String getModelType(Entry<String, JsonElement> entry) {
120         JsonObject ob = entry.getValue().getAsJsonObject();
121         if (ob.has(PROPERTIES)) {
122             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
123             if (properties.has(POLICYID)) {
124                 JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
125                 if (policyIdObj.has(POLICY_TYPEID)) {
126                     return policyIdObj.get(POLICY_TYPEID).getAsString();
127                 }
128             }
129         }
130         return "";
131     }
132
133     String getBlueprintName(Entry<String, JsonElement> entry) {
134         return entry.getKey();
135     }
136
137     MicroService getNodeRepresentation(Entry<String, JsonElement> entry) {
138         String name = getName(entry);
139         String getInputFrom = getInput(entry);
140         String modelType = getModelType(entry);
141         String blueprintName = getBlueprintName(entry);
142         return new MicroService(name, modelType, getInputFrom, "", blueprintName);
143     }
144
145     private String getTarget(JsonObject elementObject) {
146         if (elementObject.has(TYPE) && elementObject.has(TARGET)
147             && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
148             return elementObject.get(TARGET).getAsString();
149         }
150         return "";
151     }
152
153     private static JsonObject convertToJson(String yamlString) {
154         Yaml yaml = new Yaml();
155         Map<String, Object> map = yaml.load(yamlString);
156
157         JSONObject jsonObject = new JSONObject(map);
158         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
159     }
160 }