Payload is expected as string in operational policy
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicyRepresentationBuilder.java
index 244f4c2..57d13ef 100644 (file)
 
 package org.onap.clamp.policy.operational;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 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;
 import java.util.Map.Entry;
+import java.util.Set;
 
 import org.onap.clamp.clds.util.JsonUtils;
 import org.onap.clamp.clds.util.ResourceFileUtil;
@@ -38,6 +41,13 @@ import org.onap.clamp.loop.service.Service;
 
 public class OperationalPolicyRepresentationBuilder {
 
+    private static final EELFLogger logger =
+            EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
+
+    public static final String PROPERTIES = "properties";
+    public static final String TYPE = "type";
+    public static final String TYPE_LIST = "list";
+
     /**
      * 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
@@ -46,40 +56,42 @@ public class OperationalPolicyRepresentationBuilder {
      *
      * @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(Service modelJson)
-            throws JsonSyntaxException, IOException {
-        JsonObject jsonSchema = JsonUtils.GSON.fromJson(
-                ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
-                JsonObject.class);
-        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("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()));
+    public static JsonObject generateOperationalPolicySchema(Service modelJson) {
+
+        JsonObject jsonSchema = null;
+        try {
+            jsonSchema = JsonUtils.GSON.fromJson(
+                    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));
+
+            // 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();
+
+            for (JsonElement actor : actors) {
+                if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
+                    actor.getAsJsonObject().get("properties").getAsJsonObject().get("recipe").getAsJsonObject()
+                            .get("anyOf").getAsJsonArray()
+                            .addAll(createAnyOfArrayForCdsRecipe(modelJson));
+                }
             }
+            return jsonSchema;
+        } catch (IOException e) {
+            logger.error("Unable to generate the json schema because of an exception", e);
+            return new JsonObject();
         }
-
-        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);
@@ -96,7 +108,7 @@ public class OperationalPolicyRepresentationBuilder {
         return property;
     }
 
-    private static JsonArray createVnfSchema(Service modelService) {
+    private static JsonArray createVnfSchema(Service modelService, boolean generateType) {
         JsonArray vnfSchemaArray = new JsonArray();
         JsonObject modelVnfs = modelService.getResourceByType("VF");
 
@@ -104,7 +116,9 @@ public class OperationalPolicyRepresentationBuilder {
             JsonObject vnfOneOfSchema = new JsonObject();
             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
             JsonObject properties = new JsonObject();
-            properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
+            if (generateType) {
+                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));
 
@@ -114,7 +128,7 @@ public class OperationalPolicyRepresentationBuilder {
         return vnfSchemaArray;
     }
 
-    private static JsonArray createVfModuleSchema(Service modelService) {
+    private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) {
         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
 
@@ -122,14 +136,17 @@ public class OperationalPolicyRepresentationBuilder {
             JsonObject vfModuleOneOfSchema = new JsonObject();
             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
             JsonObject properties = new JsonObject();
-            properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
+            if (generateType) {
+                properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
+            }
             properties.add("resourceID",
                     createSchemaProperty("Resource ID", "string",
                             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",
@@ -144,8 +161,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);
@@ -154,17 +172,23 @@ public class OperationalPolicyRepresentationBuilder {
         return vfModuleOneOfSchemaArray;
     }
 
-    private static JsonArray createAnyOfArray(Service modelJson) {
+    /**
+     * 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();
-        targetOneOfStructure.addAll(createVnfSchema(modelJson));
-        targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
+        targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType));
+        targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType));
         return targetOneOfStructure;
     }
 
-    private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) {
+    private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) {
         JsonArray anyOfStructure = new JsonArray();
-        anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF")));
-        anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF")));
+        anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF")));
+        anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF")));
         return anyOfStructure;
     }
 
@@ -179,8 +203,9 @@ public class OperationalPolicyRepresentationBuilder {
                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
                     JsonObject obj = new JsonObject();
                     obj.addProperty("title", workflowsEntry.getKey());
+                    obj.addProperty("type", "object");
                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
-                                                                controllerProperties));
+                            controllerProperties, workflowsEntry.getKey()));
                     schemaArray.add(obj);
                 }
 
@@ -189,25 +214,105 @@ 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));
         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
+     * @return returns the properties of payload
+     */
+    public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
+                                                             JsonObject controllerProperties) {
+        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));
+        return jsonObject;
+    }
+
+    private static JsonObject createDataProperty(JsonObject inputs) {
+        JsonObject data = new JsonObject();
+        data.addProperty("title", "data");
+        JsonObject dataObj = new JsonObject();
+        addDataFields(inputs, dataObj);
+        data.add(PROPERTIES, dataObj);
+        return data;
+    }
+
+    private static void addDataFields(JsonObject inputs,
+                                      JsonObject dataObj) {
+        Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
+        for (Map.Entry<String, JsonElement> entry : entrySet) {
+            String key = entry.getKey();
+            JsonObject inputProperty = inputs.getAsJsonObject(key);
+            if (inputProperty.get(TYPE) == null) {
+                addDataFields(entry.getValue().getAsJsonObject(), dataObj);
+            } 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, "array");
+            if (cdsProperty.get(PROPERTIES) != null) {
+                JsonObject dataObject = new JsonObject();
+                addDataFields(cdsProperty.get(PROPERTIES).getAsJsonObject(),
+                              dataObject);
+                JsonObject listProperties = new JsonObject();
+                listProperties.add(PROPERTIES, dataObject);
+                property.add("items", listProperties);
+            }
+        } else {
+            property.addProperty(TYPE, type);
+        }
+
+        if (defaultValue != null) {
+            property.addProperty("default", defaultValue);
+        }
+        return property;
     }
 }