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;
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;
41 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
43 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
46 private DictionaryService dictionaryService;
49 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
52 JsonArray jsonArray = new JsonArray();
54 addToJsonArray(childObject, "enum", jsonArray);
57 generatePayload(childObject, serviceModel);
60 generateOperation(childObject, serviceModel);
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);
73 private static void generateOperation(JsonObject childObject, Service serviceModel) {
74 generateOperationPerResource(childObject, "VF", serviceModel);
75 generateOperationPerResource(childObject, "PNF", serviceModel);
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)
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)");
93 addToJsonArray(childObject, "enum", schemaEnum);
94 if (childObject.get("options") == null) {
95 JsonObject optionsSection = new JsonObject();
96 childObject.add("options", optionsSection);
98 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
102 private static JsonArray generatePayloadPerResource(String resourceName,
103 Service serviceModel) {
104 JsonArray schemaAnyOf = new JsonArray();
106 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
108 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
109 .getAsJsonObject("controllerProperties");
110 if (controllerProperties != null) {
111 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
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);
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);
134 private static JsonObject createAnyOfJsonProperty(String name,
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);
145 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
146 if (childObject.getAsJsonArray(section) != null) {
147 childObject.getAsJsonArray(section).addAll(value);
149 childObject.add(section, value);
154 * Returns the properties of payload based on the cds work flows.
156 * @param workFlow cds work flows to update payload
157 * @param controllerProperties cds properties to get blueprint name and
159 * @return returns the properties of payload
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));
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());
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);
197 defaultValue.addProperty(entry.getKey(), "");