8ecdc1900a47d5bd6ab185766061e2e78241e2e1
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.policy.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 java.util.Set;
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;
36
37
38 /**
39  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
40  */
41 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
42
43     private static final Logger logger =
44             LoggerFactory.getLogger(ToscaMetadataCdsProcess.class);
45
46     @Override
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");
50             return;
51         }
52         switch (parameters) {
53             case "actor":
54                 JsonArray jsonArray = new JsonArray();
55                 jsonArray.add("CDS");
56                 addToJsonArray(childObject, "enum", jsonArray);
57                 break;
58             case "payload":
59                 generatePayload(childObject, serviceModel);
60                 break;
61             case "operation":
62                 generateOperation(childObject, serviceModel);
63                 break;
64             default:
65         }
66     }
67
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);
74     }
75
76     private static void generateOperation(JsonObject childObject, Service serviceModel) {
77         generateOperationPerResource(childObject, "VF", serviceModel);
78         generateOperationPerResource(childObject, "PNF", serviceModel);
79     }
80
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)
86                 .entrySet()) {
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)");
93                 }
94             }
95         }
96         addToJsonArray(childObject, "enum", schemaEnum);
97         if (childObject.get("options") == null) {
98             JsonObject optionsSection = new JsonObject();
99             childObject.add("options", optionsSection);
100         }
101         addToJsonArray(childObject.getAsJsonObject("options"), "enum_titles", schemaTitle);
102
103     }
104
105     private static JsonArray generatePayloadPerResource(String resourceName,
106                                                         Service serviceModel) {
107         JsonArray schemaAnyOf = new JsonArray();
108
109         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
110                 .entrySet()) {
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")
115                         .entrySet()) {
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);
123                 }
124             }
125         }
126         return schemaAnyOf;
127     }
128
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);
135         return result;
136     }
137
138     private static JsonObject createAnyOfJsonProperty(String name,
139                                                       String defaultValue,
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);
146         return result;
147     }
148
149     private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
150         if (childObject.getAsJsonArray(section) != null) {
151             childObject.getAsJsonArray(section).addAll(value);
152         } else {
153             childObject.add(section, value);
154         }
155     }
156
157     /**
158      * Returns the properties of payload based on the cds work flows.
159      *
160      * @param workFlow cds work flows to update payload
161      * @param controllerProperties cds properties to get blueprint name and
162      *                            version
163      * @param workFlowName work flow name
164      * @return returns the properties of payload
165      */
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));
180         return jsonObject;
181     }
182
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());
191         return data;
192     }
193
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)
210                     .getAsString())) {
211                 defaultValue.add(entry.getKey(), handleListType(entry.getValue().getAsJsonObject(), workFlowName));
212             } else {
213                 defaultValue.addProperty(entry.getKey(), "");
214             }
215         }
216     }
217
218     private static JsonArray handleListType(JsonObject inputs,
219                                             String workFlowName) {
220
221         JsonObject object = new JsonObject();
222         if (inputs.get("properties") != null) {
223             addDefaultValueForData(inputs.get("properties").getAsJsonObject(), object, workFlowName);
224         }
225         JsonArray arr = new JsonArray();
226         arr.add(object);
227         return arr;
228     }
229 }