Change generated json schema
[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 import java.util.Map;
30 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
31 import org.onap.clamp.loop.service.Service;
32 import org.onap.clamp.tosca.DictionaryService;
33 import org.springframework.beans.factory.annotation.Autowired;
34
35 /**
36  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
37  */
38 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
39
40     @Autowired
41     private DictionaryService dictionaryService;
42
43     @Override
44     public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
45         switch (parameters) {
46             case "actor":
47                 JsonArray jsonArray = new JsonArray();
48                 jsonArray.add("CDS");
49                 addToJsonArray(childObject, "enum", jsonArray);
50                 break;
51             case "payload":
52                 generatePayload(childObject, serviceModel);
53                 break;
54             case "operation":
55                 generateOperation(childObject, serviceModel);
56                 break;
57         }
58     }
59
60     private static void generatePayload(JsonObject childObject, Service serviceModel) {
61         generatePayloadPerResource(childObject, "VF", serviceModel);
62         generatePayloadPerResource(childObject, "PNF", serviceModel);
63         addToJsonArray(childObject, "anyOf", createBlankEntry());
64     }
65
66     private static void generateOperation(JsonObject childObject, Service serviceModel) {
67         generateOperationPerResource(childObject, "VF", serviceModel);
68         generateOperationPerResource(childObject, "PNF", serviceModel);
69     }
70
71     private static void generateOperationPerResource(JsonObject childObject, String resourceName,
72                                                      Service serviceModel) {
73         JsonArray schemaEnum = new JsonArray();
74         JsonArray schemaTitle = new JsonArray();
75         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
76                 .entrySet()) {
77             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
78                     .getAsJsonObject("controllerProperties");
79             if (controllerProperties != null) {
80                 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
81                     schemaEnum.add(workflowsEntry);
82                     schemaTitle.add(workflowsEntry + " (CDS operation)");
83                 }
84             }
85         }
86         addToJsonArray(childObject, "enum", schemaEnum);
87         if (childObject.get("options") == null) {
88             JsonObject optionsSection = new JsonObject();
89             childObject.add("options", optionsSection);
90         }
91         addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
92
93     }
94
95     private static void generatePayloadPerResource(JsonObject childObject, String resourceName,
96                                                    Service serviceModel) {
97         JsonArray schemaAnyOf = new JsonArray();
98
99         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
100                 .entrySet()) {
101             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
102                     .getAsJsonObject("controllerProperties");
103             if (controllerProperties != null) {
104                 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
105                         .entrySet()) {
106                     JsonObject obj = new JsonObject();
107                     obj.addProperty("title", workflowsEntry.getKey());
108                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
109                             controllerProperties));
110                     schemaAnyOf.add(obj);
111                 }
112             }
113         }
114         addToJsonArray(childObject, "anyOf", schemaAnyOf);
115     }
116
117     private static JsonArray createBlankEntry() {
118         JsonArray result = new JsonArray();
119         JsonObject blankObject = new JsonObject();
120         blankObject.addProperty("title", "User defined");
121         blankObject.add("properties", new JsonObject());
122         result.add(blankObject);
123         return result;
124     }
125
126     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
127         JsonObject payloadResult = new JsonObject();
128
129         payloadResult.add("artifact_name",
130                 createAnyOfJsonProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString()));
131         payloadResult.add("artifact_version",
132                 createAnyOfJsonProperty("artifact_version",
133                         controllerProperties.get("sdnc_model_version").getAsString()));
134         payloadResult.add("mode", createAnyOfJsonProperty("mode", "async"));
135
136         payloadResult.add("data", createAnyOfJsonObject("data", workFlow.getAsJsonObject("inputs")));
137         return payloadResult;
138     }
139
140     private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
141         JsonObject result = new JsonObject();
142         result.addProperty("title", name);
143         result.addProperty("type", "string");
144         result.addProperty("default", defaultValue);
145         result.addProperty("readOnly", "True");
146         return result;
147     }
148
149     private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
150         JsonObject result = new JsonObject();
151         result.addProperty("title", name);
152         result.addProperty("type", "object");
153         result.add("properties", allProperties);
154         return result;
155     }
156
157     private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
158         if (childObject.getAsJsonArray(section) != null) {
159             childObject.getAsJsonArray(section).addAll(value);
160         }
161         else {
162             childObject.add(section, value);
163         }
164     }
165 }