2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.policy.clamp.clds.tosca.update.execution.cds;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
31 import org.onap.policy.clamp.clds.tosca.ToscaSchemaConstants;
32 import org.onap.policy.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
33 import org.onap.policy.clamp.loop.service.Service;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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 {
43 private static final Logger logger =
44 LoggerFactory.getLogger(ToscaMetadataCdsProcess.class);
47 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
48 if (serviceModel == null) {
49 logger.info("serviceModel is null, therefore the ToscaMetadataCdsProcess is skipped");
54 JsonArray jsonArray = new JsonArray();
56 addToJsonArray(childObject, "enum", jsonArray);
59 generatePayload(childObject, serviceModel);
62 generateOperation(childObject, serviceModel);
68 private static void generatePayload(JsonObject childObject, Service serviceModel) {
69 JsonArray schemaAnyOf = new JsonArray();
70 schemaAnyOf.addAll(createBlankEntry());
71 schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
72 schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
73 addToJsonArray(childObject, "anyOf", schemaAnyOf);
76 private static void generateOperation(JsonObject childObject, Service serviceModel) {
77 generateOperationPerResource(childObject, "VF", serviceModel);
78 generateOperationPerResource(childObject, "PNF", serviceModel);
81 private static void generateOperationPerResource(JsonObject childObject, String resourceName,
82 Service serviceModel) {
83 JsonArray schemaEnum = new JsonArray();
84 JsonArray schemaTitle = new JsonArray();
85 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
87 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
88 .getAsJsonObject("controllerProperties");
89 if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != null) {
90 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
91 schemaEnum.add(workflowsEntry);
92 schemaTitle.add(workflowsEntry + " (CDS operation)");
96 addToJsonArray(childObject, "enum", schemaEnum);
97 if (childObject.get("options") == null) {
98 JsonObject optionsSection = new JsonObject();
99 childObject.add("options", optionsSection);
101 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
105 private static JsonArray generatePayloadPerResource(String resourceName,
106 Service serviceModel) {
107 JsonArray schemaAnyOf = new JsonArray();
109 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
111 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
112 .getAsJsonObject("controllerProperties");
113 if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != null) {
114 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
116 JsonObject obj = new JsonObject();
117 obj.addProperty("title", workflowsEntry.getKey());
118 obj.add("properties",
119 createInputPropertiesForPayload(workflowsEntry.getValue().getAsJsonObject(),
120 controllerProperties,
121 workflowsEntry.getKey()));
122 schemaAnyOf.add(obj);
129 private static JsonArray createBlankEntry() {
130 JsonArray result = new JsonArray();
131 JsonObject blankObject = new JsonObject();
132 blankObject.addProperty("title", "User defined");
133 blankObject.add("properties", new JsonObject());
134 result.add(blankObject);
138 private static JsonObject createAnyOfJsonProperty(String name,
140 boolean readOnlyFlag) {
141 JsonObject result = new JsonObject();
142 result.addProperty("title", name);
143 result.addProperty("type", "string");
144 result.addProperty("default", defaultValue);
145 result.addProperty("readOnly", readOnlyFlag);
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 * @param workFlowName work flow name
164 * @return returns the properties of payload
166 public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
167 JsonObject controllerProperties,
168 String workFlowName) {
169 String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
170 String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
171 JsonObject inputs = workFlow.getAsJsonObject("inputs");
172 JsonObject jsonObject = new JsonObject();
173 jsonObject.add("artifact_name", createAnyOfJsonProperty(
174 "artifact name", artifactName, true));
175 jsonObject.add("artifact_version", createAnyOfJsonProperty(
176 "artifact version", artifactVersion, true));
177 jsonObject.add("mode", createAnyOfJsonProperty(
178 "mode", "async", false));
179 jsonObject.add("data", createDataProperty(inputs, workFlowName));
183 private static JsonObject createDataProperty(JsonObject inputs, String workFlowName) {
184 JsonObject data = new JsonObject();
185 data.addProperty("title", "data");
186 data.addProperty("type", "string");
187 data.addProperty("format", "textarea");
188 JsonObject defaultValue = new JsonObject();
189 addDefaultValueForData(inputs, defaultValue, workFlowName);
190 data.addProperty("default", defaultValue.toString());
194 private static void addDefaultValueForData(JsonObject inputs,
195 JsonObject defaultValue,
196 String workFlowName) {
197 Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
198 for (Map.Entry<String, JsonElement> entry : entrySet) {
199 String key = entry.getKey();
200 JsonObject inputProperty = inputs.getAsJsonObject(key);
201 if (key.equalsIgnoreCase(workFlowName + "-properties")) {
202 addDefaultValueForData(entry.getValue().getAsJsonObject().get("properties")
203 .getAsJsonObject(), defaultValue, workFlowName);
204 } else if ("object".equalsIgnoreCase(inputProperty.get(ToscaSchemaConstants.TYPE).getAsString())) {
205 JsonObject object = new JsonObject();
206 addDefaultValueForData(entry.getValue().getAsJsonObject().get("properties")
207 .getAsJsonObject(), object, workFlowName);
208 defaultValue.add(entry.getKey(), object);
209 } else if (ToscaSchemaConstants.TYPE_LIST.equalsIgnoreCase(inputProperty.get(ToscaSchemaConstants.TYPE)
211 defaultValue.add(entry.getKey(), handleListType(entry.getValue().getAsJsonObject(), workFlowName));
213 defaultValue.addProperty(entry.getKey(), "");
218 private static JsonArray handleListType(JsonObject inputs,
219 String workFlowName) {
221 JsonObject object = new JsonObject();
222 if (inputs.get("properties") != null) {
223 addDefaultValueForData(inputs.get("properties").getAsJsonObject(), object, workFlowName);
225 JsonArray arr = new JsonArray();