X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fpolicy%2Foperational%2FOperationalPolicyRepresentationBuilder.java;h=c0e1c019eb3e32015360943384831744a05858bd;hb=2d9f989e0d37002c758bdf66a3f25fcfdac35b62;hp=6577069ec0ee95c39acd7137c3a179c865adf41c;hpb=98bade58e0ab9b74378e4e3214700e7b00e10d7d;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 6577069e..c0e1c019 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -29,8 +29,12 @@ import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; + import java.io.IOException; +import java.util.Map; import java.util.Map.Entry; +import java.util.Set; + import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.loop.service.Service; @@ -40,6 +44,22 @@ public class OperationalPolicyRepresentationBuilder { private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class); + public static final String PROPERTIES = "properties"; + public static final String ITEMS = "items"; + public static final String ANY_OF = "anyOf"; + public static final String TITLE = "title"; + public static final String RECIPE = "recipe"; + public static final String DEFAULT = "default"; + public static final String STRING = "string"; + public static final String TYPE = "type"; + public static final String TYPE_LIST = "list"; + public static final String TYPE_OBJECT = "object"; + public static final String TYPE_ARRAY = "array"; + + private OperationalPolicyRepresentationBuilder() { + throw new IllegalStateException("This is Utility class, not supposed to be initiated."); + } + /** * This method generates the operational policy json representation that will be * used by ui for rendering. It uses the model (VF and VFModule) defined in the @@ -57,21 +77,21 @@ public class OperationalPolicyRepresentationBuilder { ResourceFileUtil .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"), JsonObject.class); - jsonSchema.get("properties").getAsJsonObject() - .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, true)); + jsonSchema.get(PROPERTIES).getAsJsonObject() + .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies") + .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("target") + .getAsJsonObject().get(ANY_OF).getAsJsonArray().addAll(createAnyOfArray(modelJson, true)); // update CDS recipe and payload information to schema - JsonArray actors = jsonSchema.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(); + JsonArray actors = jsonSchema.get(PROPERTIES).getAsJsonObject() + .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies") + .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("actor") + .getAsJsonObject().get(ANY_OF).getAsJsonArray(); for (JsonElement actor : actors) { - if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) { - actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject() - .get("anyOf").getAsJsonArray() + if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get(TITLE).getAsString())) { + actor.getAsJsonObject().get(PROPERTIES).getAsJsonObject().get(RECIPE).getAsJsonObject() + .get(ANY_OF).getAsJsonArray() .addAll(createAnyOfArrayForCdsRecipe(modelJson)); } } @@ -85,9 +105,9 @@ public class OperationalPolicyRepresentationBuilder { private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag, String[] enumArray) { JsonObject property = new JsonObject(); - property.addProperty("title", title); - property.addProperty("type", type); - property.addProperty("default", defaultValue); + property.addProperty(TITLE, title); + property.addProperty(TYPE, type); + property.addProperty(DEFAULT, defaultValue); property.addProperty("readOnly", readOnlyFlag); if (enumArray != null) { @@ -106,78 +126,89 @@ public class OperationalPolicyRepresentationBuilder { for (Entry entry : modelVnfs.entrySet()) { JsonObject vnfOneOfSchema = new JsonObject(); - vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey()); + vnfOneOfSchema.addProperty(TITLE, "VNF" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); if (generateType) { - properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null)); + properties.add(TYPE, createSchemaProperty("Type", STRING, "VNF", "True", null)); } - properties.add("resourceID", createSchemaProperty("Resource ID", "string", - modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null)); + properties.add("resourceID", createSchemaProperty("Resource ID", STRING, + modelVnfs.get(entry.getKey()).getAsJsonObject().get("invariantUUID").getAsString(), "True", null)); - vnfOneOfSchema.add("properties", properties); + vnfOneOfSchema.add(PROPERTIES, properties); vnfSchemaArray.add(vnfOneOfSchema); } return vnfSchemaArray; } + private static JsonArray createBlankEntry() { + JsonArray result = new JsonArray(); + JsonObject blankObject = new JsonObject(); + blankObject.addProperty("title", "User defined"); + blankObject.add("properties", new JsonObject()); + result.add(blankObject); + return result; + } + private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) { JsonArray vfModuleOneOfSchemaArray = new JsonArray(); JsonObject modelVfModules = modelService.getResourceByType("VFModule"); for (Entry entry : modelVfModules.entrySet()) { JsonObject vfModuleOneOfSchema = new JsonObject(); - vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey()); + vfModuleOneOfSchema.addProperty(TITLE, "VFMODULE" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); if (generateType) { - properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null)); + properties.add(TYPE, createSchemaProperty("Type", STRING, "VFMODULE", "True", null)); } properties.add("resourceID", - createSchemaProperty("Resource ID", "string", + createSchemaProperty("Resource ID", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); properties.add("modelInvariantId", - createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", + 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", + createSchemaProperty("Model Version Id (ModelUUID)", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(), "True", null)); properties.add("modelName", - createSchemaProperty("Model Name", "string", + createSchemaProperty("Model Name", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); - properties.add("modelVersion", createSchemaProperty("Model Version", "string", + properties.add("modelVersion", createSchemaProperty("Model Version", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(), "True", null)); properties .add("modelCustomizationId", - createSchemaProperty("Customization ID", "string", + createSchemaProperty("Customization ID", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject() .get("vfModuleModelCustomizationUUID").getAsString(), "True", null)); - vfModuleOneOfSchema.add("properties", properties); + vfModuleOneOfSchema.add(PROPERTIES, properties); vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema); } return vfModuleOneOfSchemaArray; } /** - * Create an anyOf array of possible structure we may have for Target + * Create an anyOf array of possible structure we may have for Target. * * @param modelJson The service object * @return A JsonArray with everything inside */ public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) { JsonArray targetOneOfStructure = new JsonArray(); + // First entry must be user defined + targetOneOfStructure.addAll(createBlankEntry()); targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType)); targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType)); return targetOneOfStructure; } - public static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) { + private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) { JsonArray anyOfStructure = new JsonArray(); anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF"))); anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF"))); @@ -190,13 +221,14 @@ public class OperationalPolicyRepresentationBuilder { JsonObject controllerProperties = entry.getValue().getAsJsonObject() .getAsJsonObject("controllerProperties"); - if (controllerProperties != null) { + if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != 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)); + obj.addProperty(TITLE, workflowsEntry.getKey()); + obj.addProperty(TYPE, "object"); + obj.add(PROPERTIES, createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), + controllerProperties, workflowsEntry.getKey())); schemaArray.add(obj); } @@ -205,26 +237,116 @@ public class OperationalPolicyRepresentationBuilder { 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"); + private static JsonObject createPayloadProperty(JsonObject workFlow, + JsonObject controllerProperties, String workFlowName) { + JsonObject payload = new JsonObject(); + payload.addProperty(TITLE, "Payload"); + payload.addProperty(TYPE, "object"); + payload.add(PROPERTIES, createInputPropertiesForPayload(workFlow, controllerProperties, + workFlowName)); JsonObject properties = new JsonObject(); - properties.add("type", type); + properties.add(RECIPE, createRecipeForCdsWorkflow(workFlowName)); + properties.add("payload", payload); 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(); + private static JsonObject createRecipeForCdsWorkflow(String workflow) { + JsonObject recipe = new JsonObject(); + recipe.addProperty(TITLE, RECIPE); + recipe.addProperty(TYPE, STRING); + recipe.addProperty(DEFAULT, workflow); + JsonObject options = new JsonObject(); + options.addProperty("hidden", true); + recipe.add("options", options); + return recipe; + } + + /** + * Returns the properties of payload based on the cds work flows. + * + * @param workFlow cds work flows to update payload + * @param controllerProperties cds properties to get blueprint name and + * version + * @param workFlowName work flow name + * @return returns the properties of payload + */ + public static JsonObject createInputPropertiesForPayload(JsonObject workFlow, + JsonObject controllerProperties, + String workFlowName) { + String artifactName = controllerProperties.get("sdnc_model_name").getAsString(); + String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString(); + JsonObject inputs = workFlow.getAsJsonObject("inputs"); + JsonObject jsonObject = new JsonObject(); + jsonObject.add("artifact_name", createSchemaProperty( + "artifact name", STRING, artifactName, "True", null)); + jsonObject.add("artifact_version", createSchemaProperty( + "artifact version", STRING, artifactVersion, "True", null)); + jsonObject.add("mode", createCdsInputProperty( + "mode", STRING, "async" ,null)); + jsonObject.add("data", createDataProperty(inputs, workFlowName)); + return jsonObject; + } + + private static JsonObject createDataProperty(JsonObject inputs, String workflowName) { + JsonObject data = new JsonObject(); + data.addProperty(TITLE, "data"); + JsonObject dataObj = new JsonObject(); + addDataFields(inputs, dataObj, workflowName); + data.add(PROPERTIES, dataObj); + return data; + } + + private static void addDataFields(JsonObject inputs, + JsonObject dataObj, + String workFlowName) { + Set> entrySet = inputs.entrySet(); + for (Map.Entry entry : entrySet) { + String key = entry.getKey(); + JsonObject inputProperty = inputs.getAsJsonObject(key); + if (key.equalsIgnoreCase(workFlowName + "-properties")) { + addDataFields(entry.getValue().getAsJsonObject().get("properties").getAsJsonObject(), + dataObj, workFlowName); + } else { + dataObj.add(entry.getKey(), + createCdsInputProperty(key, inputProperty.get(TYPE).getAsString(),null, + entry.getValue().getAsJsonObject())); + } + } + } + + private static JsonObject createCdsInputProperty(String title, + String type, + String defaultValue, + JsonObject cdsProperty) { + JsonObject property = new JsonObject(); + property.addProperty(TITLE, title); + + if (TYPE_LIST.equalsIgnoreCase(type)) { + property.addProperty(TYPE, TYPE_ARRAY); + if (cdsProperty != null && cdsProperty.get(PROPERTIES) != null) { + JsonObject listProperties = new JsonObject(); + listProperties.add(PROPERTIES, getProperties(cdsProperty.get(PROPERTIES).getAsJsonObject())); + property.add(ITEMS, listProperties); + } + } else if (TYPE_OBJECT.equalsIgnoreCase(type)) { + property.addProperty(TYPE, TYPE_OBJECT); + property.add(PROPERTIES, getProperties(cdsProperty.get(PROPERTIES).getAsJsonObject())); + } else { + property.addProperty(TYPE, type); + } + + if (defaultValue != null) { + property.addProperty(DEFAULT, defaultValue); + } + return property; + } + + private static JsonObject getProperties(JsonObject inputProperties) { + if (inputProperties == null) { + return null; + } + JsonObject dataObject = new JsonObject(); + addDataFields(inputProperties, dataObject, null); + return dataObject; } }