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 static org.onap.clamp.clds.tosca.update.execution.cds.ToscaMetadataCdsProcess.createInputPropertiesForPayload;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
35 import java.io.IOException;
36 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);
48 * This method generates the operational policy json representation that will be
49 * used by ui for rendering. It uses the model (VF and VFModule) defined in the
50 * loop object to do so, so it's dynamic. It also uses the operational policy
51 * schema template defined in the resource folder.
53 * @param modelJson The loop model json
54 * @return The json representation
56 public static JsonObject generateOperationalPolicySchema(Service modelJson) {
58 JsonObject jsonSchema = null;
60 jsonSchema = JsonUtils.GSON.fromJson(
62 .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
64 jsonSchema.get("properties").getAsJsonObject()
65 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
66 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
67 .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson, true));
69 // update CDS recipe and payload information to schema
70 JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
71 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
72 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
73 .getAsJsonObject().get("anyOf").getAsJsonArray();
75 for (JsonElement actor : actors) {
76 if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
77 actor.getAsJsonObject().get("properties").getAsJsonObject().get("recipe").getAsJsonObject()
78 .get("anyOf").getAsJsonArray()
79 .addAll(createAnyOfArrayForCdsRecipe(modelJson));
83 } catch (IOException e) {
84 logger.error("Unable to generate the json schema because of an exception", e);
85 return new JsonObject();
89 private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
91 JsonObject property = new JsonObject();
92 property.addProperty("title", title);
93 property.addProperty("type", type);
94 property.addProperty("default", defaultValue);
95 property.addProperty("readOnly", readOnlyFlag);
97 if (enumArray != null) {
98 JsonArray jsonArray = new JsonArray();
99 property.add("enum", jsonArray);
100 for (String val : enumArray) {
107 private static JsonArray createVnfSchema(Service modelService, boolean generateType) {
108 JsonArray vnfSchemaArray = new JsonArray();
109 JsonObject modelVnfs = modelService.getResourceByType("VF");
111 for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
112 JsonObject vnfOneOfSchema = new JsonObject();
113 vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
114 JsonObject properties = new JsonObject();
116 properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
118 properties.add("resourceID", createSchemaProperty("Resource ID", "string",
119 modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
121 vnfOneOfSchema.add("properties", properties);
122 vnfSchemaArray.add(vnfOneOfSchema);
124 return vnfSchemaArray;
127 private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) {
128 JsonArray vfModuleOneOfSchemaArray = new JsonArray();
129 JsonObject modelVfModules = modelService.getResourceByType("VFModule");
131 for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
132 JsonObject vfModuleOneOfSchema = new JsonObject();
133 vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
134 JsonObject properties = new JsonObject();
136 properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
138 properties.add("resourceID",
139 createSchemaProperty("Resource ID", "string",
140 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
142 properties.add("modelInvariantId",
143 createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
144 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
147 properties.add("modelVersionId",
148 createSchemaProperty("Model Version Id (ModelUUID)", "string",
149 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
151 properties.add("modelName",
152 createSchemaProperty("Model Name", "string",
153 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
155 properties.add("modelVersion", createSchemaProperty("Model Version", "string",
156 modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
159 .add("modelCustomizationId",
160 createSchemaProperty("Customization ID", "string",
161 modelVfModules.get(entry.getKey()).getAsJsonObject()
162 .get("vfModuleModelCustomizationUUID").getAsString(), "True",
165 vfModuleOneOfSchema.add("properties", properties);
166 vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
168 return vfModuleOneOfSchemaArray;
172 * Create an anyOf array of possible structure we may have for Target.
174 * @param modelJson The service object
175 * @return A JsonArray with everything inside
177 public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) {
178 JsonArray targetOneOfStructure = new JsonArray();
179 targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType));
180 targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType));
181 return targetOneOfStructure;
184 private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) {
185 JsonArray anyOfStructure = new JsonArray();
186 anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF")));
187 anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF")));
188 return anyOfStructure;
191 private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
192 JsonArray schemaArray = new JsonArray();
193 for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
194 JsonObject controllerProperties = entry.getValue().getAsJsonObject()
195 .getAsJsonObject("controllerProperties");
197 if (controllerProperties != null) {
198 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
199 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
200 JsonObject obj = new JsonObject();
201 obj.addProperty("title", workflowsEntry.getKey());
202 obj.addProperty("type", "object");
203 obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
204 controllerProperties, workflowsEntry.getKey()));
205 schemaArray.add(obj);
213 private static JsonObject createPayloadProperty(JsonObject workFlow,
214 JsonObject controllerProperties, String workFlowName) {
215 JsonObject payload = new JsonObject();
216 payload.addProperty("title", "Payload (YAML)");
217 payload.addProperty("type", "object");
218 payload.add("properties", createInputPropertiesForPayload(workFlow,
219 controllerProperties));
220 JsonObject properties = new JsonObject();
221 properties.add("recipe", createRecipeForCdsWorkflow(workFlowName));
222 properties.add("payload", payload);
226 private static JsonObject createRecipeForCdsWorkflow(String workflow) {
227 JsonObject recipe = new JsonObject();
228 recipe.addProperty("title", "recipe");
229 recipe.addProperty("type", "string");
230 recipe.addProperty("default", workflow);
231 JsonObject options = new JsonObject();
232 options.addProperty("hidden", true);
233 recipe.add("options", options);