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