2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.tosca.update.execution.cds;
26 import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE;
27 import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE_LIST;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
34 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
35 import org.onap.clamp.loop.service.Service;
38 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
40 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
43 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
46 JsonArray jsonArray = new JsonArray();
48 addToJsonArray(childObject, "enum", jsonArray);
51 generatePayload(childObject, serviceModel);
54 generateOperation(childObject, serviceModel);
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);
68 private static void generateOperation(JsonObject childObject, Service serviceModel) {
69 generateOperationPerResource(childObject, "VF", serviceModel);
70 generateOperationPerResource(childObject, "PNF", serviceModel);
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)
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)");
88 addToJsonArray(childObject, "enum", schemaEnum);
89 if (childObject.get("options") == null) {
90 JsonObject optionsSection = new JsonObject();
91 childObject.add("options", optionsSection);
93 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
97 private static JsonArray generatePayloadPerResource(String resourceName,
98 Service serviceModel) {
99 JsonArray schemaAnyOf = new JsonArray();
101 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
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")
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);
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);
130 private static JsonObject createAnyOfJsonProperty(String name,
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);
141 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
142 if (childObject.getAsJsonArray(section) != null) {
143 childObject.getAsJsonArray(section).addAll(value);
145 childObject.add(section, value);
150 * Returns the properties of payload based on the cds work flows.
152 * @param workFlow cds work flows to update payload
153 * @param controllerProperties cds properties to get blueprint name and
155 * @param workFlowName work flow name
156 * @return returns the properties of payload
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));
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());
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));
204 defaultValue.addProperty(entry.getKey(), "");
209 private static JsonArray handleListType(JsonObject inputs,
210 String workFlowName) {
212 JsonObject object = new JsonObject();
213 if (inputs.get("properties") != null) {
214 addDefaultValueForData(inputs.get("properties").getAsJsonObject(), object, workFlowName);
216 JsonArray arr = new JsonArray();