6577069ec0ee95c39acd7137c3a179c865adf41c
[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, true));
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));
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, boolean generateType) {
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             if (generateType) {
112                 properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
113             }
114             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
115                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
116
117             vnfOneOfSchema.add("properties", properties);
118             vnfSchemaArray.add(vnfOneOfSchema);
119         }
120         return vnfSchemaArray;
121     }
122
123     private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) {
124         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
125         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
126
127         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
128             JsonObject vfModuleOneOfSchema = new JsonObject();
129             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
130             JsonObject properties = new JsonObject();
131             if (generateType) {
132                 properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
133             }
134             properties.add("resourceID",
135                     createSchemaProperty("Resource ID", "string",
136                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
137                             "True", null));
138             properties.add("modelInvariantId",
139                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
140                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
141                                     .getAsString(),
142                             "True", null));
143             properties.add("modelVersionId",
144                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
145                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
146                             "True", null));
147             properties.add("modelName",
148                     createSchemaProperty("Model Name", "string",
149                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
150                             "True", null));
151             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
152                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
153                     "True", null));
154             properties
155                     .add("modelCustomizationId",
156                             createSchemaProperty("Customization ID", "string",
157                                     modelVfModules.get(entry.getKey()).getAsJsonObject()
158                                             .get("vfModuleModelCustomizationUUID").getAsString(), "True",
159                                     null));
160
161             vfModuleOneOfSchema.add("properties", properties);
162             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
163         }
164         return vfModuleOneOfSchemaArray;
165     }
166
167     /**
168      * Create an anyOf array of possible structure we may have for Target
169      *
170      * @param modelJson The service object
171      * @return A JsonArray with everything inside
172      */
173     public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) {
174         JsonArray targetOneOfStructure = new JsonArray();
175         targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType));
176         targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType));
177         return targetOneOfStructure;
178     }
179
180     public static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) {
181         JsonArray anyOfStructure = new JsonArray();
182         anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF")));
183         anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF")));
184         return anyOfStructure;
185     }
186
187     private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
188         JsonArray schemaArray = new JsonArray();
189         for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
190             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
191                     .getAsJsonObject("controllerProperties");
192
193             if (controllerProperties != null) {
194                 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
195                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
196                     JsonObject obj = new JsonObject();
197                     obj.addProperty("title", workflowsEntry.getKey());
198                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
199                             controllerProperties));
200                     schemaArray.add(obj);
201                 }
202
203             }
204         }
205         return schemaArray;
206     }
207
208     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
209         JsonObject type = new JsonObject();
210         type.addProperty("title", "Payload (YAML)");
211         type.addProperty("type", "string");
212         type.addProperty("default", createDefaultStringForPayload(workFlow, controllerProperties));
213         type.addProperty("format", "textarea");
214         JsonObject properties = new JsonObject();
215         properties.add("type", type);
216         return properties;
217     }
218
219     private static String createDefaultStringForPayload(JsonObject workFlow, JsonObject controllerProperties) {
220         String artifactName = controllerProperties.get("sdnc_model_name").toString();
221         String artifactVersion = controllerProperties.get("sdnc_model_version").toString();
222         String data = workFlow.getAsJsonObject("inputs").toString();
223         StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n")
224                 .append("artifact_version : ").append(artifactVersion).append("\n")
225                 .append("mode : async").append("\n")
226                 .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'")
227                 .append("'");
228         return builder.toString();
229     }
230 }