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