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