2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.policy.operational;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonSyntaxException;
31 import java.io.IOException;
32 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;
38 public class OperationalPolicyRepresentationBuilder {
41 * This method generates the operational policy json representation that will be
42 * used by ui for rendering. It uses the model (VF and VFModule) defined in the
43 * loop object to do so, so it's dynamic. It also uses the operational policy
44 * schema template defined in the resource folder.
46 * @param modelJson The loop model json
47 * @return The json representation
48 * @throws JsonSyntaxException If the schema template cannot be parsed
49 * @throws IOException In case of issue when opening the schema template
51 public static JsonObject generateOperationalPolicySchema(Service modelJson)
52 throws JsonSyntaxException, IOException {
53 JsonObject jsonSchema = JsonUtils.GSON.fromJson(
54 ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
56 jsonSchema.get("schema").getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject()
57 .get("configurationsJson").getAsJsonObject().get("properties").getAsJsonObject()
58 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
59 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
60 .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
64 private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
66 JsonObject property = new JsonObject();
67 property.addProperty("title", title);
68 property.addProperty("type", type);
69 property.addProperty("default", defaultValue);
70 property.addProperty("readOnly", readOnlyFlag);
72 if (enumArray != null) {
73 JsonArray jsonArray = new JsonArray();
74 property.add("enum", jsonArray);
75 for (String val : enumArray) {
82 private static JsonArray createVnfSchema(Service modelService) {
83 JsonArray vnfSchemaArray = new JsonArray();
84 JsonObject modelVnfs = modelService.getResourceByType("VF");
86 for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
87 JsonObject vnfOneOfSchema = new JsonObject();
88 vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
89 JsonObject properties = new JsonObject();
90 properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
91 properties.add("resourceID", createSchemaProperty("Resource ID", "string",
92 modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
94 vnfOneOfSchema.add("properties", properties);
95 vnfSchemaArray.add(vnfOneOfSchema);
97 return vnfSchemaArray;
100 private static JsonArray createVfModuleSchema(Service modelService) {
101 JsonArray vfModuleOneOfSchemaArray = new JsonArray();
102 JsonObject modelVfModules = modelService.getResourceByType("VFModule");
104 for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
105 JsonObject vfModuleOneOfSchema = new JsonObject();
106 vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
107 JsonObject properties = new JsonObject();
108 properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
109 properties.add("resourceID",
110 createSchemaProperty("Resource ID", "string",
111 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
113 properties.add("modelInvariantId",
114 createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", modelVfModules
115 .get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID").getAsString(),
117 properties.add("modelVersionId",
118 createSchemaProperty("Model Version Id (ModelUUID)", "string",
119 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
121 properties.add("modelName",
122 createSchemaProperty("Model Name", "string",
123 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
125 properties.add("modelVersion", createSchemaProperty("Model Version", "string",
126 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
129 .add("modelCustomizationId",
130 createSchemaProperty("Customization ID", "string", modelVfModules.get(entry.getKey())
131 .getAsJsonObject().get("vfModuleModelCustomizationUUID").getAsString(), "True",
134 vfModuleOneOfSchema.add("properties", properties);
135 vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
137 return vfModuleOneOfSchemaArray;
140 private static JsonArray createAnyOfArray(Service modelJson) {
141 JsonArray targetOneOfStructure = new JsonArray();
142 targetOneOfStructure.addAll(createVnfSchema(modelJson));
143 targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
144 return targetOneOfStructure;