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);
65 private static void generateOperation(JsonObject childObject, Service serviceModel) {
66 generateOperationPerResource(childObject, "VF", serviceModel);
67 generateOperationPerResource(childObject, "PNF", serviceModel);
70 private static void generateOperationPerResource(JsonObject childObject, String resourceName,
71 Service serviceModel) {
72 JsonArray schemaEnum = new JsonArray();
73 JsonArray schemaTitle = new JsonArray();
74 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
76 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
77 .getAsJsonObject("controllerProperties");
78 if (controllerProperties != null) {
79 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
80 schemaEnum.add(workflowsEntry);
81 schemaTitle.add(workflowsEntry + " (CDS operation)");
85 addToJsonArray(childObject, "enum", schemaEnum);
86 if (childObject.get("options") == null) {
87 JsonObject optionsSection = new JsonObject();
88 childObject.add("options", optionsSection);
90 addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
94 private static void generatePayloadPerResource(JsonObject childObject, String resourceName,
95 Service serviceModel) {
96 JsonArray schemaAnyOf = new JsonArray();
98 for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
100 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
101 .getAsJsonObject("controllerProperties");
102 if (controllerProperties != null) {
103 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
105 JsonObject obj = new JsonObject();
106 obj.addProperty("title", workflowsEntry.getKey());
107 obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
108 controllerProperties));
109 schemaAnyOf.add(obj);
113 addToJsonArray(childObject, "enum", schemaAnyOf);
116 private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
117 JsonObject payloadResult = new JsonObject();
119 payloadResult.addProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString());
120 payloadResult.addProperty("artifact_version", controllerProperties.get("sdnc_model_version").getAsString());
121 payloadResult.addProperty("mode", "async");
122 payloadResult.add("data", workFlow.getAsJsonObject("inputs"));
123 return payloadResult;
126 private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
127 if (childObject.getAsJsonArray(section) != null) {
128 childObject.getAsJsonArray(section).addAll(value);
131 childObject.add(section, value);