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