b1c889031485df28a5fd211da2a758051b33c7ba
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / execution / cds / ToscaMetadataCdsProcess.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update.execution.cds;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import java.util.Map;
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;
34
35 /**
36  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
37  */
38 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
39
40     @Autowired
41     private DictionaryService dictionaryService;
42
43     @Override
44     public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
45         switch (parameters) {
46             case "actor":
47                 JsonArray jsonArray = new JsonArray();
48                 jsonArray.add("CDS");
49                 addToJsonArray(childObject, "enum", jsonArray);
50                 break;
51             case "payload":
52                 generatePayload(childObject, serviceModel);
53                 break;
54             case "operation":
55                 generateOperation(childObject, serviceModel);
56                 break;
57         }
58     }
59
60     private static void generatePayload(JsonObject childObject, Service serviceModel) {
61         generatePayloadPerResource(childObject, "VF", serviceModel);
62         generatePayloadPerResource(childObject, "PNF", serviceModel);
63     }
64
65     private static void generateOperation(JsonObject childObject, Service serviceModel) {
66         generateOperationPerResource(childObject, "VF", serviceModel);
67         generateOperationPerResource(childObject, "PNF", serviceModel);
68     }
69
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)
75                 .entrySet()) {
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)");
82                 }
83             }
84         }
85         addToJsonArray(childObject, "enum", schemaEnum);
86         if (childObject.get("options") == null) {
87             JsonObject optionsSection = new JsonObject();
88             childObject.add("options", optionsSection);
89         }
90         addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
91
92     }
93
94     private static void generatePayloadPerResource(JsonObject childObject, String resourceName,
95                                                    Service serviceModel) {
96         JsonArray schemaAnyOf = new JsonArray();
97
98         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
99                 .entrySet()) {
100             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
101                     .getAsJsonObject("controllerProperties");
102             if (controllerProperties != null) {
103                 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
104                         .entrySet()) {
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);
110                 }
111             }
112         }
113         addToJsonArray(childObject, "enum", schemaAnyOf);
114     }
115
116     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
117         JsonObject payloadResult = new JsonObject();
118
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;
124     }
125
126     private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
127         if (childObject.getAsJsonArray(section) != null) {
128             childObject.getAsJsonArray(section).addAll(value);
129         }
130         else {
131             childObject.add(section, value);
132         }
133     }
134 }