Fix the CDS calls
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicyRepresentationBuilder.java
index c88c1b9..6577069 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.Entry;
 import org.onap.clamp.clds.util.JsonUtils;
@@ -36,6 +37,9 @@ import org.onap.clamp.loop.service.Service;
 
 public class OperationalPolicyRepresentationBuilder {
 
+    private static final EELFLogger logger =
+            EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class);
+
     /**
      * 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
@@ -44,34 +48,38 @@ 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("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("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("type").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,
@@ -92,7 +100,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");
 
@@ -100,7 +108,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));
 
@@ -110,7 +120,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");
 
@@ -118,7 +128,9 @@ 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(),
@@ -152,17 +164,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) {
+    public 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;
     }