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