Rework the submit operation
[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 import com.google.gson.JsonParser;
31
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.Set;
38
39 import org.json.JSONObject;
40 import org.springframework.stereotype.Component;
41 import org.yaml.snakeyaml.Yaml;
42
43 @Component
44 public class BlueprintParser {
45
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";
60
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();
65
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);
71             }
72         }
73         microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName()));
74         return microServices;
75     }
76
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();
88             }
89         }
90         String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA;
91         return Collections.singletonList(new MicroService(msName, "", "", "", ""));
92     }
93
94     String getName(Entry<String, JsonElement> entry) {
95         String microServiceYamlName = entry.getKey();
96         JsonObject ob = entry.getValue().getAsJsonObject();
97         if (ob.has(PROPERTIES)) {
98             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
99             if (properties.has(NAME)) {
100                 return properties.get(NAME).getAsString();
101             }
102         }
103         return microServiceYamlName;
104     }
105
106     String getInput(Entry<String, JsonElement> entry) {
107         JsonObject ob = entry.getValue().getAsJsonObject();
108         if (ob.has(RELATIONSHIPS)) {
109             JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS);
110             for (JsonElement element : relationships) {
111                 String target = getTarget(element.getAsJsonObject());
112                 if (!target.isEmpty()) {
113                     return target;
114                 }
115             }
116         }
117         return "";
118     }
119
120     String getModelType(Entry<String, JsonElement> entry) {
121         JsonObject ob = entry.getValue().getAsJsonObject();
122         if (ob.has(PROPERTIES)) {
123             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
124             if (properties.has(POLICYID)) {
125                 JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
126                 if (policyIdObj.has(POLICY_TYPEID)) {
127                    return policyIdObj.get(POLICY_TYPEID).getAsString();
128                 }
129             }
130         }
131         return "";
132     }
133
134     String getBlueprintName(Entry<String, JsonElement> entry) {
135         return entry.getKey();
136     }
137
138     MicroService getNodeRepresentation(Entry<String, JsonElement> entry) {
139         String name = getName(entry);
140         String getInputFrom = getInput(entry);
141         String modelType = getModelType(entry);
142         String blueprintName = getBlueprintName(entry);
143         return new MicroService(name, modelType, getInputFrom, "", blueprintName);
144     }
145
146     private String getTarget(JsonObject elementObject) {
147         if (elementObject.has(TYPE) && elementObject.has(TARGET)
148             && elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) {
149             return elementObject.get(TARGET).getAsString();
150         }
151         return "";
152     }
153
154     private static JsonObject convertToJson(String yamlString) {
155         Yaml yaml = new Yaml();
156         Map<String, Object> map = yaml.load(yamlString);
157
158         JSONObject jsonObject = new JSONObject(map);
159         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
160     }
161 }