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 com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
33 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
34 import org.onap.clamp.loop.service.Service;
35 import org.onap.clamp.tosca.DictionaryService;
36 import org.springframework.beans.factory.annotation.Autowired;
39 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
41 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
44 private DictionaryService dictionaryService;
47 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
50 JsonArray jsonArray = new JsonArray();
52 addToJsonArray(childObject, "enum", jsonArray);
55 generatePayload(childObject, serviceModel);
58 generateOperation(childObject, serviceModel);
63 private static void generatePayload(JsonObject childObject, Service serviceModel) {
64 JsonArray schemaAnyOf = new JsonArray();
65 schemaAnyOf.addAll(createBlankEntry());
66 schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
67 schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
68 addToJsonArray(childObject, "anyOf", schemaAnyOf);
71 private static void generateOperation(JsonObject childObject, Service serviceModel) {
72 generateOperationPerResource(childObject, "VF", serviceModel);
73 generateOperationPerResource(childObject, "PNF", serviceModel);
76 private static void generateOperationPerResource(JsonObject childObject, String resourceName,
77 Service serviceModel) {
78 JsonArray schemaEnum = new JsonArray();
79 JsonArray schemaTitle = new JsonArray();
80 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
82 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
83 .getAsJsonObject("controllerProperties");
84 if (controllerProperties != null) {
85 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
86 schemaEnum.add(workflowsEntry);
87 schemaTitle.add(workflowsEntry + " (CDS operation)");
91 addToJsonArray(childObject, "enum", schemaEnum);
92 if (childObject.get("options") == null) {
93 JsonObject optionsSection = new JsonObject();
94 childObject.add("options", optionsSection);
96 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
100 private static JsonArray generatePayloadPerResource(String resourceName,
101 Service serviceModel) {
102 JsonArray schemaAnyOf = new JsonArray();
104 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
106 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
107 .getAsJsonObject("controllerProperties");
108 if (controllerProperties != null) {
109 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
111 JsonObject obj = new JsonObject();
112 obj.addProperty("title", workflowsEntry.getKey());
113 obj.add("properties",
114 createInputPropertiesForPayload(workflowsEntry.getValue().getAsJsonObject(),
115 controllerProperties));
116 schemaAnyOf.add(obj);
123 private static JsonArray createBlankEntry() {
124 JsonArray result = new JsonArray();
125 JsonObject blankObject = new JsonObject();
126 blankObject.addProperty("title", "User defined");
127 blankObject.add("properties", new JsonObject());
128 result.add(blankObject);
132 private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
133 JsonObject result = new JsonObject();
134 result.addProperty("title", name);
135 result.addProperty("type", "string");
136 result.addProperty("default", defaultValue);
137 result.addProperty("readOnly", "True");
141 private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
142 JsonObject result = new JsonObject();
143 result.addProperty("title", name);
144 result.addProperty("type", "object");
145 result.add("properties", allProperties);
149 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
150 if (childObject.getAsJsonArray(section) != null) {
151 childObject.getAsJsonArray(section).addAll(value);
153 childObject.add(section, value);
158 * Returns the properties of payload based on the cds work flows.
160 * @param workFlow cds work flows to update payload
161 * @param controllerProperties cds properties to get blueprint name and
163 * @return returns the properties of payload
165 public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
166 JsonObject controllerProperties) {
167 String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
168 String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
169 JsonObject inputs = workFlow.getAsJsonObject("inputs");
170 JsonObject jsonObject = new JsonObject();
171 jsonObject.add("artifact_name", createAnyOfJsonProperty(
172 "artifact name", artifactName));
173 jsonObject.add("artifact_version", createAnyOfJsonProperty(
174 "artifact version", artifactVersion));
175 jsonObject.add("mode", createCdsInputProperty(
176 "mode", "string", "async"));
177 jsonObject.add("data", createDataProperty(inputs));
182 private static JsonObject createDataProperty(JsonObject inputs) {
183 JsonObject data = new JsonObject();
184 data.addProperty("title", "data");
185 data.add("properties", addDataFields(inputs));
189 private static JsonObject addDataFields(JsonObject inputs) {
190 JsonObject jsonObject = new JsonObject();
191 Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
192 for (Map.Entry<String, JsonElement> entry : entrySet) {
193 String key = entry.getKey();
194 JsonObject inputProperty = inputs.getAsJsonObject(key);
195 if (inputProperty.get("type") == null) {
196 jsonObject.add(entry.getKey(),
197 createAnyOfJsonObject(key,
198 addDataFields(entry.getValue().getAsJsonObject())));
200 jsonObject.add(entry.getKey(),
201 createCdsInputProperty(key,
202 inputProperty.get("type").getAsString(),
209 private static JsonObject createCdsInputProperty(String title,
211 String defaultValue) {
212 JsonObject property = new JsonObject();
213 property.addProperty("title", title);
214 property.addProperty("type", type);
215 if (defaultValue != null) {
216 property.addProperty("default", defaultValue);
218 property.addProperty("format", "textarea");