2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.policy.operational;
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;
33 import java.io.IOException;
35 import java.util.Map.Entry;
38 import org.onap.clamp.clds.util.JsonUtils;
39 import org.onap.clamp.clds.util.ResourceFileUtil;
40 import org.onap.clamp.loop.service.Service;
42 public class OperationalPolicyRepresentationBuilder {
44 private static final EELFLogger logger =
45 EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
47 public static final String PROPERTIES = "properties";
48 public static final String ITEMS = "items";
49 public static final String ANY_OF = "anyOf";
50 public static final String TITLE = "title";
51 public static final String RECIPE = "recipe";
52 public static final String DEFAULT = "default";
53 public static final String STRING = "string";
54 public static final String TYPE = "type";
55 public static final String TYPE_LIST = "list";
57 private OperationalPolicyRepresentationBuilder() {
58 throw new IllegalStateException("This is Utility class, not supposed to be initiated.");
62 * This method generates the operational policy json representation that will be
63 * used by ui for rendering. It uses the model (VF and VFModule) defined in the
64 * loop object to do so, so it's dynamic. It also uses the operational policy
65 * schema template defined in the resource folder.
67 * @param modelJson The loop model json
68 * @return The json representation
70 public static JsonObject generateOperationalPolicySchema(Service modelJson) {
72 JsonObject jsonSchema = null;
74 jsonSchema = JsonUtils.GSON.fromJson(
76 .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
78 jsonSchema.get(PROPERTIES).getAsJsonObject()
79 .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies")
80 .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("target")
81 .getAsJsonObject().get(ANY_OF).getAsJsonArray().addAll(createAnyOfArray(modelJson, true));
83 // update CDS recipe and payload information to schema
84 JsonArray actors = jsonSchema.get(PROPERTIES).getAsJsonObject()
85 .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies")
86 .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("actor")
87 .getAsJsonObject().get(ANY_OF).getAsJsonArray();
89 for (JsonElement actor : actors) {
90 if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get(TITLE).getAsString())) {
91 actor.getAsJsonObject().get(PROPERTIES).getAsJsonObject().get(RECIPE).getAsJsonObject()
92 .get(ANY_OF).getAsJsonArray()
93 .addAll(createAnyOfArrayForCdsRecipe(modelJson));
97 } catch (IOException e) {
98 logger.error("Unable to generate the json schema because of an exception", e);
99 return new JsonObject();
103 private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
104 String[] enumArray) {
105 JsonObject property = new JsonObject();
106 property.addProperty(TITLE, title);
107 property.addProperty(TYPE, type);
108 property.addProperty(DEFAULT, defaultValue);
109 property.addProperty("readOnly", readOnlyFlag);
111 if (enumArray != null) {
112 JsonArray jsonArray = new JsonArray();
113 property.add("enum", jsonArray);
114 for (String val : enumArray) {
121 private static JsonArray createVnfSchema(Service modelService, boolean generateType) {
122 JsonArray vnfSchemaArray = new JsonArray();
123 JsonObject modelVnfs = modelService.getResourceByType("VF");
125 for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
126 JsonObject vnfOneOfSchema = new JsonObject();
127 vnfOneOfSchema.addProperty(TITLE, "VNF" + "-" + entry.getKey());
128 JsonObject properties = new JsonObject();
130 properties.add(TYPE, createSchemaProperty("Type", STRING, "VNF", "True", null));
132 properties.add("resourceID", createSchemaProperty("Resource ID", STRING,
133 modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
135 vnfOneOfSchema.add(PROPERTIES, properties);
136 vnfSchemaArray.add(vnfOneOfSchema);
138 return vnfSchemaArray;
141 private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) {
142 JsonArray vfModuleOneOfSchemaArray = new JsonArray();
143 JsonObject modelVfModules = modelService.getResourceByType("VFModule");
145 for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
146 JsonObject vfModuleOneOfSchema = new JsonObject();
147 vfModuleOneOfSchema.addProperty(TITLE, "VFMODULE" + "-" + entry.getKey());
148 JsonObject properties = new JsonObject();
150 properties.add(TYPE, createSchemaProperty("Type", STRING, "VFMODULE", "True", null));
152 properties.add("resourceID",
153 createSchemaProperty("Resource ID", STRING,
154 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
156 properties.add("modelInvariantId",
157 createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", STRING,
158 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
161 properties.add("modelVersionId",
162 createSchemaProperty("Model Version Id (ModelUUID)", STRING,
163 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
165 properties.add("modelName",
166 createSchemaProperty("Model Name", STRING,
167 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
169 properties.add("modelVersion", createSchemaProperty("Model Version", STRING,
170 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
173 .add("modelCustomizationId",
174 createSchemaProperty("Customization ID", STRING,
175 modelVfModules.get(entry.getKey()).getAsJsonObject()
176 .get("vfModuleModelCustomizationUUID").getAsString(), "True",
179 vfModuleOneOfSchema.add(PROPERTIES, properties);
180 vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
182 return vfModuleOneOfSchemaArray;
186 * Create an anyOf array of possible structure we may have for Target.
188 * @param modelJson The service object
189 * @return A JsonArray with everything inside
191 public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) {
192 JsonArray targetOneOfStructure = new JsonArray();
193 targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType));
194 targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType));
195 return targetOneOfStructure;
198 private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) {
199 JsonArray anyOfStructure = new JsonArray();
200 anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF")));
201 anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF")));
202 return anyOfStructure;
205 private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
206 JsonArray schemaArray = new JsonArray();
207 for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
208 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
209 .getAsJsonObject("controllerProperties");
211 if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != null) {
212 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
213 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
214 JsonObject obj = new JsonObject();
215 obj.addProperty(TITLE, workflowsEntry.getKey());
216 obj.addProperty(TYPE, "object");
217 obj.add(PROPERTIES, createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
218 controllerProperties, workflowsEntry.getKey()));
219 schemaArray.add(obj);
227 private static JsonObject createPayloadProperty(JsonObject workFlow,
228 JsonObject controllerProperties, String workFlowName) {
229 JsonObject payload = new JsonObject();
230 payload.addProperty(TITLE, "Payload");
231 payload.addProperty(TYPE, "object");
232 payload.add(PROPERTIES, createInputPropertiesForPayload(workFlow,
233 controllerProperties));
234 JsonObject properties = new JsonObject();
235 properties.add(RECIPE, createRecipeForCdsWorkflow(workFlowName));
236 properties.add("payload", payload);
240 private static JsonObject createRecipeForCdsWorkflow(String workflow) {
241 JsonObject recipe = new JsonObject();
242 recipe.addProperty(TITLE, RECIPE);
243 recipe.addProperty(TYPE, STRING);
244 recipe.addProperty(DEFAULT, workflow);
245 JsonObject options = new JsonObject();
246 options.addProperty("hidden", true);
247 recipe.add("options", options);
252 * Returns the properties of payload based on the cds work flows.
254 * @param workFlow cds work flows to update payload
255 * @param controllerProperties cds properties to get blueprint name and
257 * @return returns the properties of payload
259 public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
260 JsonObject controllerProperties) {
261 String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
262 String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
263 JsonObject inputs = workFlow.getAsJsonObject("inputs");
264 JsonObject jsonObject = new JsonObject();
265 jsonObject.add("artifact_name", createSchemaProperty(
266 "artifact name", STRING, artifactName, "True", null));
267 jsonObject.add("artifact_version", createSchemaProperty(
268 "artifact version", STRING, artifactVersion, "True", null));
269 jsonObject.add("mode", createCdsInputProperty(
270 "mode", STRING, "async" ,null));
271 jsonObject.add("data", createDataProperty(inputs));
275 private static JsonObject createDataProperty(JsonObject inputs) {
276 JsonObject data = new JsonObject();
277 data.addProperty(TITLE, "data");
278 JsonObject dataObj = new JsonObject();
279 addDataFields(inputs, dataObj);
280 data.add(PROPERTIES, dataObj);
284 private static void addDataFields(JsonObject inputs,
285 JsonObject dataObj) {
286 Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
287 for (Map.Entry<String, JsonElement> entry : entrySet) {
288 String key = entry.getKey();
289 JsonObject inputProperty = inputs.getAsJsonObject(key);
290 if (inputProperty.get(TYPE) == null) {
291 addDataFields(entry.getValue().getAsJsonObject(), dataObj);
293 dataObj.add(entry.getKey(),
294 createCdsInputProperty(key,
295 inputProperty.get(TYPE).getAsString(),
297 entry.getValue().getAsJsonObject()));
302 private static JsonObject createCdsInputProperty(String title,
305 JsonObject cdsProperty) {
306 JsonObject property = new JsonObject();
307 property.addProperty(TITLE, title);
309 if (TYPE_LIST.equalsIgnoreCase(type)) {
310 property.addProperty(TYPE, "array");
311 if (cdsProperty.get(PROPERTIES) != null) {
312 JsonObject dataObject = new JsonObject();
313 addDataFields(cdsProperty.get(PROPERTIES).getAsJsonObject(),
315 JsonObject listProperties = new JsonObject();
316 listProperties.add(PROPERTIES, dataObject);
317 property.add(ITEMS, listProperties);
320 property.addProperty(TYPE, type);
323 if (defaultValue != null) {
324 property.addProperty(DEFAULT, defaultValue);