Merge "CDS attributes are not shown properly in UI"
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / execution / cds / ToscaMetadataCdsProcess.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T 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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update.execution.cds;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29
30 import java.util.Map;
31 import java.util.Set;
32
33 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
34 import org.onap.clamp.loop.service.Service;
35 import org.onap.clamp.tosca.DictionaryService;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 /**
39  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
40  */
41 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
42
43     @Autowired
44     private DictionaryService dictionaryService;
45
46     @Override
47     public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
48         switch (parameters) {
49             case "actor":
50                 JsonArray jsonArray = new JsonArray();
51                 jsonArray.add("CDS");
52                 addToJsonArray(childObject, "enum", jsonArray);
53                 break;
54             case "payload":
55                 generatePayload(childObject, serviceModel);
56                 break;
57             case "operation":
58                 generateOperation(childObject, serviceModel);
59                 break;
60         }
61     }
62
63     private static void generatePayload(JsonObject childObject, Service serviceModel) {
64         JsonArray schemaAnyOf = new JsonArray();
65         schemaAnyOf.addAll(createBlankEntry());
66         schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
67         schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
68         addToJsonArray(childObject, "anyOf", schemaAnyOf);
69     }
70
71     private static void generateOperation(JsonObject childObject, Service serviceModel) {
72         generateOperationPerResource(childObject, "VF", serviceModel);
73         generateOperationPerResource(childObject, "PNF", serviceModel);
74     }
75
76     private static void generateOperationPerResource(JsonObject childObject, String resourceName,
77                                                      Service serviceModel) {
78         JsonArray schemaEnum = new JsonArray();
79         JsonArray schemaTitle = new JsonArray();
80         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
81                 .entrySet()) {
82             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
83                     .getAsJsonObject("controllerProperties");
84             if (controllerProperties != null) {
85                 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
86                     schemaEnum.add(workflowsEntry);
87                     schemaTitle.add(workflowsEntry + " (CDS operation)");
88                 }
89             }
90         }
91         addToJsonArray(childObject, "enum", schemaEnum);
92         if (childObject.get("options") == null) {
93             JsonObject optionsSection = new JsonObject();
94             childObject.add("options", optionsSection);
95         }
96         addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
97
98     }
99
100     private static JsonArray generatePayloadPerResource(String resourceName,
101                                                         Service serviceModel) {
102         JsonArray schemaAnyOf = new JsonArray();
103
104         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
105                 .entrySet()) {
106             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
107                     .getAsJsonObject("controllerProperties");
108             if (controllerProperties != null) {
109                 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
110                         .entrySet()) {
111                     JsonObject obj = new JsonObject();
112                     obj.addProperty("title", workflowsEntry.getKey());
113                     obj.add("properties",
114                             createInputPropertiesForPayload(workflowsEntry.getValue().getAsJsonObject(),
115                                                             controllerProperties));
116                     schemaAnyOf.add(obj);
117                 }
118             }
119         }
120         return schemaAnyOf;
121     }
122
123     private static JsonArray createBlankEntry() {
124         JsonArray result = new JsonArray();
125         JsonObject blankObject = new JsonObject();
126         blankObject.addProperty("title", "User defined");
127         blankObject.add("properties", new JsonObject());
128         result.add(blankObject);
129         return result;
130     }
131
132     private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
133         JsonObject result = new JsonObject();
134         result.addProperty("title", name);
135         result.addProperty("type", "string");
136         result.addProperty("default", defaultValue);
137         result.addProperty("readOnly", "True");
138         return result;
139     }
140
141     private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
142         JsonObject result = new JsonObject();
143         result.addProperty("title", name);
144         result.addProperty("type", "object");
145         result.add("properties", allProperties);
146         return result;
147     }
148
149     private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
150         if (childObject.getAsJsonArray(section) != null) {
151             childObject.getAsJsonArray(section).addAll(value);
152         } else {
153             childObject.add(section, value);
154         }
155     }
156
157     /**
158      * Returns the properties of payload based on the cds work flows.
159      *
160      * @param workFlow cds work flows to update payload
161      * @param controllerProperties cds properties to get blueprint name and
162      *                            version
163      * @return returns the properties of payload
164      */
165     public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
166                                                              JsonObject controllerProperties) {
167         String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
168         String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
169         JsonObject inputs = workFlow.getAsJsonObject("inputs");
170         JsonObject jsonObject = new JsonObject();
171         jsonObject.add("artifact_name", createAnyOfJsonProperty(
172                 "artifact name", artifactName));
173         jsonObject.add("artifact_version", createAnyOfJsonProperty(
174                 "artifact version", artifactVersion));
175         jsonObject.add("mode", createCdsInputProperty(
176                 "mode", "string", "async"));
177         jsonObject.add("data", createDataProperty(inputs));
178
179         return jsonObject;
180     }
181
182     private static JsonObject createDataProperty(JsonObject inputs) {
183         JsonObject data = new JsonObject();
184         data.addProperty("title", "data");
185         data.add("properties", addDataFields(inputs));
186         return data;
187     }
188
189     private static JsonObject addDataFields(JsonObject inputs) {
190         JsonObject jsonObject = new JsonObject();
191         Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
192         for (Map.Entry<String, JsonElement> entry : entrySet) {
193             String key = entry.getKey();
194             JsonObject inputProperty = inputs.getAsJsonObject(key);
195             if (inputProperty.get("type") == null) {
196                 jsonObject.add(entry.getKey(),
197                                createAnyOfJsonObject(key,
198                                                      addDataFields(entry.getValue().getAsJsonObject())));
199             } else {
200                 jsonObject.add(entry.getKey(),
201                                createCdsInputProperty(key,
202                                                       inputProperty.get("type").getAsString(),
203                                                       null));
204             }
205         }
206         return jsonObject;
207     }
208
209     private static JsonObject createCdsInputProperty(String title,
210                                                      String type,
211                                                      String defaultValue) {
212         JsonObject property = new JsonObject();
213         property.addProperty("title", title);
214         property.addProperty("type", type);
215         if (defaultValue != null) {
216             property.addProperty("default", defaultValue);
217         }
218         property.addProperty("format", "textarea");
219         return property;
220     }
221 }