X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fpolicy%2Foperational%2FOperationalPolicyRepresentationBuilder.java;h=0298cfde74490398c82b8d63645f205813287f85;hb=723de7f63f0951d0cfe7a23956cf9d00128809b1;hp=f6f3f498dcfdd6c115e6bcf6c5d754ba75650888;hpb=f9e2ceeae29504505f631086e612fad4e1e16979;p=clamp.git diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java index f6f3f498..0298cfde 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights * reserved. + * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,12 +28,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; - import java.io.IOException; import java.util.Map.Entry; - import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; +import org.onap.clamp.loop.service.Service; public class OperationalPolicyRepresentationBuilder { @@ -41,13 +41,13 @@ public class OperationalPolicyRepresentationBuilder { * used by ui for rendering. It uses the model (VF and VFModule) defined in the * loop object to do so, so it's dynamic. It also uses the operational policy * schema template defined in the resource folder. - * + * * @param modelJson The loop model json * @return The json representation * @throws JsonSyntaxException If the schema template cannot be parsed * @throws IOException In case of issue when opening the schema template */ - public static JsonObject generateOperationalPolicySchema(JsonObject modelJson) + public static JsonObject generateOperationalPolicySchema(Service modelJson) throws JsonSyntaxException, IOException { JsonObject jsonSchema = JsonUtils.GSON.fromJson( ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"), @@ -57,11 +57,27 @@ public class OperationalPolicyRepresentationBuilder { .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies") .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target") .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson)); + + // update CDS recipe and payload information to schema + JsonArray actors = jsonSchema.get("schema").getAsJsonObject().get("items").getAsJsonObject().get("properties") + .getAsJsonObject().get("configurationsJson").getAsJsonObject().get("properties").getAsJsonObject() + .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies") + .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor") + .getAsJsonObject().get("anyOf").getAsJsonArray(); + + for (JsonElement actor : actors) { + if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) { + actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject() + .get("anyOf").getAsJsonArray() + .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails())); + } + } + return jsonSchema; } private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag, - String[] enumArray) { + String[] enumArray) { JsonObject property = new JsonObject(); property.addProperty("title", title); property.addProperty("type", type); @@ -78,9 +94,9 @@ public class OperationalPolicyRepresentationBuilder { return property; } - private static JsonArray createVnfSchema(JsonObject modelJson) { + private static JsonArray createVnfSchema(Service modelService) { JsonArray vnfSchemaArray = new JsonArray(); - JsonObject modelVnfs = modelJson.get("resourceDetails").getAsJsonObject().get("VF").getAsJsonObject(); + JsonObject modelVnfs = modelService.getResourceByType("VF"); for (Entry entry : modelVnfs.entrySet()) { JsonObject vnfOneOfSchema = new JsonObject(); @@ -96,10 +112,9 @@ public class OperationalPolicyRepresentationBuilder { return vnfSchemaArray; } - private static JsonArray createVfModuleSchema(JsonObject modelJson) { + private static JsonArray createVfModuleSchema(Service modelService) { JsonArray vfModuleOneOfSchemaArray = new JsonArray(); - JsonObject modelVfModules = modelJson.get("resourceDetails").getAsJsonObject().get("VFModule") - .getAsJsonObject(); + JsonObject modelVfModules = modelService.getResourceByType("VFModule"); for (Entry entry : modelVfModules.entrySet()) { JsonObject vfModuleOneOfSchema = new JsonObject(); @@ -111,8 +126,9 @@ public class OperationalPolicyRepresentationBuilder { modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); properties.add("modelInvariantId", - createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", modelVfModules - .get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID").getAsString(), + createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", + modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID") + .getAsString(), "True", null)); properties.add("modelVersionId", createSchemaProperty("Model Version Id (ModelUUID)", "string", @@ -127,8 +143,9 @@ public class OperationalPolicyRepresentationBuilder { "True", null)); properties .add("modelCustomizationId", - createSchemaProperty("Customization ID", "string", modelVfModules.get(entry.getKey()) - .getAsJsonObject().get("vfModuleModelCustomizationUUID").getAsString(), "True", + createSchemaProperty("Customization ID", "string", + modelVfModules.get(entry.getKey()).getAsJsonObject() + .get("vfModuleModelCustomizationUUID").getAsString(), "True", null)); vfModuleOneOfSchema.add("properties", properties); @@ -137,10 +154,61 @@ public class OperationalPolicyRepresentationBuilder { return vfModuleOneOfSchemaArray; } - private static JsonArray createAnyOfArray(JsonObject modelJson) { + private static JsonArray createAnyOfArray(Service modelJson) { JsonArray targetOneOfStructure = new JsonArray(); targetOneOfStructure.addAll(createVnfSchema(modelJson)); targetOneOfStructure.addAll(createVfModuleSchema(modelJson)); return targetOneOfStructure; } + + private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) { + JsonArray anyOfStructure = new JsonArray(); + anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF"))); + anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF"))); + return anyOfStructure; + } + + private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) { + JsonArray schemaArray = new JsonArray(); + for (Entry entry : jsonObject.entrySet()) { + JsonObject controllerProperties = entry.getValue().getAsJsonObject() + .getAsJsonObject("controllerProperties"); + + if (controllerProperties != null) { + JsonObject workflows = controllerProperties.getAsJsonObject("workflows"); + for (Entry workflowsEntry : workflows.entrySet()) { + JsonObject obj = new JsonObject(); + obj.addProperty("title", workflowsEntry.getKey()); + obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), + controllerProperties)); + schemaArray.add(obj); + } + + } + } + return schemaArray; + } + + private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) { + JsonObject type = new JsonObject(); + type.addProperty("title", "Payload (YAML)"); + type.addProperty("type", "string"); + type.addProperty("default", createDefaultStringForPayload(workFlow, controllerProperties)); + type.addProperty("format", "textarea"); + JsonObject properties = new JsonObject(); + properties.add("type", type); + return properties; + } + + private static String createDefaultStringForPayload(JsonObject workFlow, JsonObject controllerProperties) { + String artifactName = controllerProperties.get("sdnc_model_name").toString(); + String artifactVersion = controllerProperties.get("sdnc_model_version").toString(); + String data = workFlow.getAsJsonObject("inputs").toString(); + StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n") + .append("artifact_version : ").append(artifactVersion).append("\n") + .append("mode : async").append("\n") + .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'") + .append("'"); + return builder.toString(); + } }