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;
30 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
31 import org.onap.clamp.loop.service.Service;
32 import org.onap.clamp.tosca.DictionaryService;
33 import org.springframework.beans.factory.annotation.Autowired;
36 * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
38 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
41 private DictionaryService dictionaryService;
44 public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
47 JsonArray jsonArray = new JsonArray();
49 addToJsonArray(childObject, "enum", jsonArray);
52 generatePayload(childObject, serviceModel);
55 generateOperation(childObject, serviceModel);
60 private static void generatePayload(JsonObject childObject, Service serviceModel) {
61 generatePayloadPerResource(childObject, "VF", serviceModel);
62 generatePayloadPerResource(childObject, "PNF", serviceModel);
63 addToJsonArray(childObject, "anyOf", createBlankEntry());
66 private static void generateOperation(JsonObject childObject, Service serviceModel) {
67 generateOperationPerResource(childObject, "VF", serviceModel);
68 generateOperationPerResource(childObject, "PNF", serviceModel);
71 private static void generateOperationPerResource(JsonObject childObject, String resourceName,
72 Service serviceModel) {
73 JsonArray schemaEnum = new JsonArray();
74 JsonArray schemaTitle = new JsonArray();
75 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
77 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
78 .getAsJsonObject("controllerProperties");
79 if (controllerProperties != null) {
80 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
81 schemaEnum.add(workflowsEntry);
82 schemaTitle.add(workflowsEntry + " (CDS operation)");
86 addToJsonArray(childObject, "enum", schemaEnum);
87 if (childObject.get("options") == null) {
88 JsonObject optionsSection = new JsonObject();
89 childObject.add("options", optionsSection);
91 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
95 private static void generatePayloadPerResource(JsonObject childObject, String resourceName,
96 Service serviceModel) {
97 JsonArray schemaAnyOf = new JsonArray();
99 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
101 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
102 .getAsJsonObject("controllerProperties");
103 if (controllerProperties != null) {
104 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
106 JsonObject obj = new JsonObject();
107 obj.addProperty("title", workflowsEntry.getKey());
108 obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
109 controllerProperties));
110 schemaAnyOf.add(obj);
114 addToJsonArray(childObject, "anyOf", schemaAnyOf);
117 private static JsonArray createBlankEntry() {
118 JsonArray result = new JsonArray();
119 JsonObject blankObject = new JsonObject();
120 blankObject.addProperty("title", "User defined");
121 blankObject.add("properties", new JsonObject());
122 result.add(blankObject);
126 private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
127 JsonObject payloadResult = new JsonObject();
129 payloadResult.add("artifact_name",
130 createAnyOfJsonProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString()));
131 payloadResult.add("artifact_version",
132 createAnyOfJsonProperty("artifact_version",
133 controllerProperties.get("sdnc_model_version").getAsString()));
134 payloadResult.add("mode", createAnyOfJsonProperty("mode", "async"));
136 payloadResult.add("data", createAnyOfJsonObject("data", workFlow.getAsJsonObject("inputs")));
137 return payloadResult;
140 private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
141 JsonObject result = new JsonObject();
142 result.addProperty("title", name);
143 result.addProperty("type", "string");
144 result.addProperty("default", defaultValue);
145 result.addProperty("readOnly", "True");
149 private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
150 JsonObject result = new JsonObject();
151 result.addProperty("title", name);
152 result.addProperty("type", "object");
153 result.add("properties", allProperties);
157 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
158 if (childObject.getAsJsonArray(section) != null) {
159 childObject.getAsJsonArray(section).addAll(value);
162 childObject.add(section, value);