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.PROPERTIES;
27 import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE;
28 import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE_LIST;
30 import com.google.gson.JsonArray;
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonObject;
37 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
38 import org.onap.clamp.loop.service.Service;
39 import org.onap.clamp.tosca.DictionaryService;
40 import org.springframework.beans.factory.annotation.Autowired;
43 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
45 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
48 private DictionaryService dictionaryService;
51 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
54 JsonArray jsonArray = new JsonArray();
56 addToJsonArray(childObject, "enum", jsonArray);
59 generatePayload(childObject, serviceModel);
62 generateOperation(childObject, serviceModel);
67 private static void generatePayload(JsonObject childObject, Service serviceModel) {
68 JsonArray schemaAnyOf = new JsonArray();
69 schemaAnyOf.addAll(createBlankEntry());
70 schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
71 schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
72 addToJsonArray(childObject, "anyOf", schemaAnyOf);
75 private static void generateOperation(JsonObject childObject, Service serviceModel) {
76 generateOperationPerResource(childObject, "VF", serviceModel);
77 generateOperationPerResource(childObject, "PNF", serviceModel);
80 private static void generateOperationPerResource(JsonObject childObject, String resourceName,
81 Service serviceModel) {
82 JsonArray schemaEnum = new JsonArray();
83 JsonArray schemaTitle = new JsonArray();
84 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
86 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
87 .getAsJsonObject("controllerProperties");
88 if (controllerProperties != null) {
89 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
90 schemaEnum.add(workflowsEntry);
91 schemaTitle.add(workflowsEntry + " (CDS operation)");
95 addToJsonArray(childObject, "enum", schemaEnum);
96 if (childObject.get("options") == null) {
97 JsonObject optionsSection = new JsonObject();
98 childObject.add("options", optionsSection);
100 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
104 private static JsonArray generatePayloadPerResource(String resourceName,
105 Service serviceModel) {
106 JsonArray schemaAnyOf = new JsonArray();
108 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
110 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
111 .getAsJsonObject("controllerProperties");
112 if (controllerProperties != null) {
113 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
115 JsonObject obj = new JsonObject();
116 obj.addProperty("title", workflowsEntry.getKey());
117 obj.add("properties",
118 createInputPropertiesForPayload(workflowsEntry.getValue().getAsJsonObject(),
119 controllerProperties));
120 schemaAnyOf.add(obj);
127 private static JsonArray createBlankEntry() {
128 JsonArray result = new JsonArray();
129 JsonObject blankObject = new JsonObject();
130 blankObject.addProperty("title", "User defined");
131 blankObject.add("properties", new JsonObject());
132 result.add(blankObject);
136 private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
137 JsonObject result = new JsonObject();
138 result.addProperty("title", name);
139 result.addProperty("type", "string");
140 result.addProperty("default", defaultValue);
141 result.addProperty("readOnly", "True");
145 private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
146 JsonObject result = new JsonObject();
147 result.addProperty("title", name);
148 result.addProperty("type", "object");
149 result.add("properties", allProperties);
153 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
154 if (childObject.getAsJsonArray(section) != null) {
155 childObject.getAsJsonArray(section).addAll(value);
157 childObject.add(section, value);
162 * Returns the properties of payload based on the cds work flows.
164 * @param workFlow cds work flows to update payload
165 * @param controllerProperties cds properties to get blueprint name and
167 * @return returns the properties of payload
169 public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
170 JsonObject controllerProperties) {
171 String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
172 String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
173 JsonObject inputs = workFlow.getAsJsonObject("inputs");
174 JsonObject jsonObject = new JsonObject();
175 jsonObject.add("artifact_name", createAnyOfJsonProperty(
176 "artifact name", artifactName));
177 jsonObject.add("artifact_version", createAnyOfJsonProperty(
178 "artifact version", artifactVersion));
179 jsonObject.add("mode", createCdsInputProperty(
180 "mode", "string", "async", null));
181 jsonObject.add("data", createDataProperty(inputs));
186 private static JsonObject createDataProperty(JsonObject inputs) {
187 JsonObject data = new JsonObject();
188 data.addProperty("title", "data");
189 data.add(PROPERTIES, addDataFields(inputs));
193 private static JsonObject addDataFields(JsonObject inputs) {
194 JsonObject jsonObject = new JsonObject();
195 Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
196 for (Map.Entry<String, JsonElement> entry : entrySet) {
197 String key = entry.getKey();
198 JsonObject inputProperty = inputs.getAsJsonObject(key);
199 if (inputProperty.get(TYPE) == null) {
200 jsonObject.add(entry.getKey(),
201 createAnyOfJsonObject(key,
202 addDataFields(entry.getValue().getAsJsonObject())));
204 jsonObject.add(entry.getKey(),
205 createCdsInputProperty(key,
206 inputProperty.get("type").getAsString(),
208 entry.getValue().getAsJsonObject()));
214 private static JsonObject createCdsInputProperty(String title,
217 JsonObject cdsProperty) {
218 JsonObject property = new JsonObject();
219 property.addProperty("title", title);
221 if (TYPE_LIST.equalsIgnoreCase(type)) {
222 property.addProperty(TYPE, "array");
223 if (cdsProperty.get(PROPERTIES) != null) {
224 JsonObject listProperties = new JsonObject();
225 listProperties.add(PROPERTIES,
226 addDataFields(cdsProperty.get(PROPERTIES).getAsJsonObject()));
227 property.add("items", listProperties);
230 property.addProperty(TYPE, type);
233 if (defaultValue != null) {
234 property.addProperty("default", defaultValue);