Merge "Fix legacy policy submit issues"
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicyRepresentationBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.policy.operational;
26
27 import static org.onap.clamp.clds.tosca.update.execution.cds.ToscaMetadataCdsProcess.createInputPropertiesForPayload;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34
35 import java.io.IOException;
36 import java.util.Map.Entry;
37
38 import org.onap.clamp.clds.util.JsonUtils;
39 import org.onap.clamp.clds.util.ResourceFileUtil;
40 import org.onap.clamp.loop.service.Service;
41
42 public class OperationalPolicyRepresentationBuilder {
43
44     private static final EELFLogger logger =
45             EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
46
47     /**
48      * This method generates the operational policy json representation that will be
49      * used by ui for rendering. It uses the model (VF and VFModule) defined in the
50      * loop object to do so, so it's dynamic. It also uses the operational policy
51      * schema template defined in the resource folder.
52      *
53      * @param modelJson The loop model json
54      * @return The json representation
55      */
56     public static JsonObject generateOperationalPolicySchema(Service modelJson) {
57
58         JsonObject jsonSchema = null;
59         try {
60             jsonSchema = JsonUtils.GSON.fromJson(
61                     ResourceFileUtil
62                             .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
63                     JsonObject.class);
64             jsonSchema.get("properties").getAsJsonObject()
65                     .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
66                     .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
67                     .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson, true));
68
69             // update CDS recipe and payload information to schema
70             JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
71                     .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
72                     .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
73                     .getAsJsonObject().get("anyOf").getAsJsonArray();
74
75             for (JsonElement actor : actors) {
76                 if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
77                     actor.getAsJsonObject().get("properties").getAsJsonObject().get("recipe").getAsJsonObject()
78                             .get("anyOf").getAsJsonArray()
79                             .addAll(createAnyOfArrayForCdsRecipe(modelJson));
80                 }
81             }
82             return jsonSchema;
83         } catch (IOException e) {
84             logger.error("Unable to generate the json schema because of an exception", e);
85             return new JsonObject();
86         }
87     }
88
89     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
90                                                    String[] enumArray) {
91         JsonObject property = new JsonObject();
92         property.addProperty("title", title);
93         property.addProperty("type", type);
94         property.addProperty("default", defaultValue);
95         property.addProperty("readOnly", readOnlyFlag);
96
97         if (enumArray != null) {
98             JsonArray jsonArray = new JsonArray();
99             property.add("enum", jsonArray);
100             for (String val : enumArray) {
101                 jsonArray.add(val);
102             }
103         }
104         return property;
105     }
106
107     private static JsonArray createVnfSchema(Service modelService, boolean generateType) {
108         JsonArray vnfSchemaArray = new JsonArray();
109         JsonObject modelVnfs = modelService.getResourceByType("VF");
110
111         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
112             JsonObject vnfOneOfSchema = new JsonObject();
113             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
114             JsonObject properties = new JsonObject();
115             if (generateType) {
116                 properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
117             }
118             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
119                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
120
121             vnfOneOfSchema.add("properties", properties);
122             vnfSchemaArray.add(vnfOneOfSchema);
123         }
124         return vnfSchemaArray;
125     }
126
127     private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) {
128         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
129         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
130
131         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
132             JsonObject vfModuleOneOfSchema = new JsonObject();
133             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
134             JsonObject properties = new JsonObject();
135             if (generateType) {
136                 properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
137             }
138             properties.add("resourceID",
139                     createSchemaProperty("Resource ID", "string",
140                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
141                             "True", null));
142             properties.add("modelInvariantId",
143                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
144                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
145                                     .getAsString(),
146                             "True", null));
147             properties.add("modelVersionId",
148                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
149                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
150                             "True", null));
151             properties.add("modelName",
152                     createSchemaProperty("Model Name", "string",
153                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
154                             "True", null));
155             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
156                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
157                     "True", null));
158             properties
159                     .add("modelCustomizationId",
160                             createSchemaProperty("Customization ID", "string",
161                                     modelVfModules.get(entry.getKey()).getAsJsonObject()
162                                             .get("vfModuleModelCustomizationUUID").getAsString(), "True",
163                                     null));
164
165             vfModuleOneOfSchema.add("properties", properties);
166             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
167         }
168         return vfModuleOneOfSchemaArray;
169     }
170
171     /**
172      * Create an anyOf array of possible structure we may have for Target.
173      *
174      * @param modelJson The service object
175      * @return A JsonArray with everything inside
176      */
177     public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) {
178         JsonArray targetOneOfStructure = new JsonArray();
179         targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType));
180         targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType));
181         return targetOneOfStructure;
182     }
183
184     private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) {
185         JsonArray anyOfStructure = new JsonArray();
186         anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF")));
187         anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF")));
188         return anyOfStructure;
189     }
190
191     private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
192         JsonArray schemaArray = new JsonArray();
193         for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
194             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
195                     .getAsJsonObject("controllerProperties");
196
197             if (controllerProperties != null) {
198                 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
199                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
200                     JsonObject obj = new JsonObject();
201                     obj.addProperty("title", workflowsEntry.getKey());
202                     obj.addProperty("type", "object");
203                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
204                             controllerProperties, workflowsEntry.getKey()));
205                     schemaArray.add(obj);
206                 }
207
208             }
209         }
210         return schemaArray;
211     }
212
213     private static JsonObject createPayloadProperty(JsonObject workFlow,
214                                                     JsonObject controllerProperties, String workFlowName) {
215         JsonObject payload = new JsonObject();
216         payload.addProperty("title", "Payload (YAML)");
217         payload.addProperty("type", "object");
218         payload.add("properties", createInputPropertiesForPayload(workFlow,
219                                                                   controllerProperties));
220         JsonObject properties = new JsonObject();
221         properties.add("recipe", createRecipeForCdsWorkflow(workFlowName));
222         properties.add("payload", payload);
223         return properties;
224     }
225
226     private static JsonObject createRecipeForCdsWorkflow(String workflow) {
227         JsonObject recipe = new JsonObject();
228         recipe.addProperty("title", "recipe");
229         recipe.addProperty("type", "string");
230         recipe.addProperty("default", workflow);
231         JsonObject options = new JsonObject();
232         options.addProperty("hidden", true);
233         recipe.add("options", options);
234         return recipe;
235     }
236 }