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;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
33 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
34 import org.onap.clamp.loop.service.Service;
37 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
39 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
42 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
45 JsonArray jsonArray = new JsonArray();
47 addToJsonArray(childObject, "enum", jsonArray);
50 generatePayload(childObject, serviceModel);
53 generateOperation(childObject, serviceModel);
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);
67 private static void generateOperation(JsonObject childObject, Service serviceModel) {
68 generateOperationPerResource(childObject, "VF", serviceModel);
69 generateOperationPerResource(childObject, "PNF", serviceModel);
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)
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)");
87 addToJsonArray(childObject, "enum", schemaEnum);
88 if (childObject.get("options") == null) {
89 JsonObject optionsSection = new JsonObject();
90 childObject.add("options", optionsSection);
92 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
96 private static JsonArray generatePayloadPerResource(String resourceName,
97 Service serviceModel) {
98 JsonArray schemaAnyOf = new JsonArray();
100 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
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")
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);
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);
128 private static JsonObject createAnyOfJsonProperty(String name,
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);
139 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
140 if (childObject.getAsJsonArray(section) != null) {
141 childObject.getAsJsonArray(section).addAll(value);
143 childObject.add(section, value);
148 * Returns the properties of payload based on the cds work flows.
150 * @param workFlow cds work flows to update payload
151 * @param controllerProperties cds properties to get blueprint name and
153 * @return returns the properties of payload
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));
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());
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);
191 defaultValue.addProperty(entry.getKey(), "");