Rework the policy refresh
[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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import java.util.Map.Entry;
34 import org.onap.clamp.clds.util.JsonUtils;
35 import org.onap.clamp.clds.util.ResourceFileUtil;
36 import org.onap.clamp.loop.service.Service;
37
38 public class OperationalPolicyRepresentationBuilder {
39
40     private static final EELFLogger logger =
41             EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
42
43     /**
44      * This method generates the operational policy json representation that will be
45      * used by ui for rendering. It uses the model (VF and VFModule) defined in the
46      * loop object to do so, so it's dynamic. It also uses the operational policy
47      * schema template defined in the resource folder.
48      *
49      * @param modelJson The loop model json
50      * @return The json representation
51      */
52     public static JsonObject generateOperationalPolicySchema(Service modelJson) {
53
54         JsonObject jsonSchema = null;
55         try {
56             jsonSchema = JsonUtils.GSON.fromJson(
57                     ResourceFileUtil
58                             .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
59                     JsonObject.class);
60             jsonSchema.get("properties").getAsJsonObject()
61                     .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
62                     .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
63                     .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
64
65             // update CDS recipe and payload information to schema
66             JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
67                     .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
68                     .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
69                     .getAsJsonObject().get("anyOf").getAsJsonArray();
70
71             for (JsonElement actor : actors) {
72                 if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
73                     actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject()
74                             .get("anyOf").getAsJsonArray()
75                             .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails()));
76                 }
77             }
78             return jsonSchema;
79         } catch (IOException e) {
80             logger.error("Unable to generate the json schema because of an exception",e);
81             return new JsonObject();
82         }
83     }
84
85     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
86                                                    String[] enumArray) {
87         JsonObject property = new JsonObject();
88         property.addProperty("title", title);
89         property.addProperty("type", type);
90         property.addProperty("default", defaultValue);
91         property.addProperty("readOnly", readOnlyFlag);
92
93         if (enumArray != null) {
94             JsonArray jsonArray = new JsonArray();
95             property.add("enum", jsonArray);
96             for (String val : enumArray) {
97                 jsonArray.add(val);
98             }
99         }
100         return property;
101     }
102
103     private static JsonArray createVnfSchema(Service modelService) {
104         JsonArray vnfSchemaArray = new JsonArray();
105         JsonObject modelVnfs = modelService.getResourceByType("VF");
106
107         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
108             JsonObject vnfOneOfSchema = new JsonObject();
109             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
110             JsonObject properties = new JsonObject();
111             properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
112             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
113                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
114
115             vnfOneOfSchema.add("properties", properties);
116             vnfSchemaArray.add(vnfOneOfSchema);
117         }
118         return vnfSchemaArray;
119     }
120
121     private static JsonArray createVfModuleSchema(Service modelService) {
122         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
123         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
124
125         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
126             JsonObject vfModuleOneOfSchema = new JsonObject();
127             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
128             JsonObject properties = new JsonObject();
129             properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
130             properties.add("resourceID",
131                     createSchemaProperty("Resource ID", "string",
132                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
133                             "True", null));
134             properties.add("modelInvariantId",
135                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
136                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
137                                     .getAsString(),
138                             "True", null));
139             properties.add("modelVersionId",
140                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
141                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
142                             "True", null));
143             properties.add("modelName",
144                     createSchemaProperty("Model Name", "string",
145                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
146                             "True", null));
147             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
148                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
149                     "True", null));
150             properties
151                     .add("modelCustomizationId",
152                             createSchemaProperty("Customization ID", "string",
153                                     modelVfModules.get(entry.getKey()).getAsJsonObject()
154                                             .get("vfModuleModelCustomizationUUID").getAsString(), "True",
155                                     null));
156
157             vfModuleOneOfSchema.add("properties", properties);
158             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
159         }
160         return vfModuleOneOfSchemaArray;
161     }
162
163     private static JsonArray createAnyOfArray(Service modelJson) {
164         JsonArray targetOneOfStructure = new JsonArray();
165         targetOneOfStructure.addAll(createVnfSchema(modelJson));
166         targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
167         return targetOneOfStructure;
168     }
169
170     private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) {
171         JsonArray anyOfStructure = new JsonArray();
172         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF")));
173         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF")));
174         return anyOfStructure;
175     }
176
177     private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
178         JsonArray schemaArray = new JsonArray();
179         for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
180             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
181                     .getAsJsonObject("controllerProperties");
182
183             if (controllerProperties != null) {
184                 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
185                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
186                     JsonObject obj = new JsonObject();
187                     obj.addProperty("title", workflowsEntry.getKey());
188                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
189                             controllerProperties));
190                     schemaArray.add(obj);
191                 }
192
193             }
194         }
195         return schemaArray;
196     }
197
198     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
199         JsonObject type = new JsonObject();
200         type.addProperty("title", "Payload (YAML)");
201         type.addProperty("type", "string");
202         type.addProperty("default", createDefaultStringForPayload(workFlow, controllerProperties));
203         type.addProperty("format", "textarea");
204         JsonObject properties = new JsonObject();
205         properties.add("type", type);
206         return properties;
207     }
208
209     private static String createDefaultStringForPayload(JsonObject workFlow, JsonObject controllerProperties) {
210         String artifactName = controllerProperties.get("sdnc_model_name").toString();
211         String artifactVersion = controllerProperties.get("sdnc_model_version").toString();
212         String data = workFlow.getAsJsonObject("inputs").toString();
213         StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n")
214                 .append("artifact_version : ").append(artifactVersion).append("\n")
215                 .append("mode : async").append("\n")
216                 .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'")
217                 .append("'");
218         return builder.toString();
219     }
220 }