Unit test fails due to cleanup
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / LegacyOperationalPolicy.java
index dd156d8..a7a344d 100644 (file)
@@ -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.
@@ -46,6 +47,11 @@ import org.yaml.snakeyaml.Yaml;
  */
 public class LegacyOperationalPolicy {
 
+    private static final String ACTOR = "actor";
+    private static final String RECIPE = "recipe";
+    private static final String POLICIES = "policies";
+    private static final String PAYLOAD = "payload";
+
     private LegacyOperationalPolicy() {
 
     }
@@ -79,7 +85,7 @@ public class LegacyOperationalPolicy {
     /**
      * This method rework the payload attribute (yaml) that is normally wrapped in a
      * string when coming from the UI.
-     * 
+     *
      * @param policyJson The operational policy json config
      * @return The same object reference but modified
      */
@@ -112,6 +118,12 @@ public class LegacyOperationalPolicy {
             replacePropertiesIfEmpty(policy, "failure_retries", "final_failure_retries");
             replacePropertiesIfEmpty(policy, "failure_exception", "final_failure_exception");
             replacePropertiesIfEmpty(policy, "failure_guard", "final_failure_guard");
+            // Again special case for payload, should remove it if it's there but empty
+            // otherwise policy crashes
+            JsonElement payloadElem = policy.getAsJsonObject().get("payload");
+            if (payloadElem != null && payloadElem.isJsonPrimitive() && payloadElem.getAsString().isEmpty()) {
+                policy.getAsJsonObject().remove("payload");
+            }
         }
         return policyJson;
     }
@@ -144,32 +156,30 @@ public class LegacyOperationalPolicy {
 
     /**
      * This method transforms the configuration json to a Yaml format.
-     * 
+     *
      * @param operationalPolicyJsonElement The operational policy json config
      * @return The Yaml as string
      */
     public static String createPolicyPayloadYamlLegacy(JsonElement operationalPolicyJsonElement) {
-        JsonElement opPolicy = fulfillPoliciesTreeField(
-                removeAllQuotes(reworkPayloadAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy())));
-        Map<?, ?> jsonMap = createMap(opPolicy);
         DumperOptions options = new DumperOptions();
         options.setDefaultScalarStyle(ScalarStyle.PLAIN);
         options.setIndent(2);
         options.setPrettyFlow(true);
         // Policy can't support { } in the yaml
         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
-        return (new Yaml(options)).dump(jsonMap);
+        return (new Yaml(options)).dump(createMap(fulfillPoliciesTreeField(
+                removeAllQuotes(reworkActorAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy())))));
     }
 
     /**
      * This method load mandatory field in the operational policy configuration
      * JSON.
-     * 
+     *
      * @param configurationsJson The operational policy JSON
      * @param loop               The parent loop object
      */
     public static void preloadConfiguration(JsonObject configurationsJson, Loop loop) {
-        if (configurationsJson.entrySet().isEmpty()) {
+        if (configurationsJson != null && configurationsJson.entrySet().isEmpty()) {
             JsonObject controlLoopName = new JsonObject();
             controlLoopName.addProperty("controlLoopName",
                     loop != null ? loop.getName() : "Empty (NO loop loaded yet)");
@@ -178,4 +188,65 @@ public class LegacyOperationalPolicy {
             configurationsJson.add("operational_policy", controlLoop);
         }
     }
+
+    /**
+     * This method rework on the actor/recipe and payload attribute.
+     *
+     * @param policyJson The operational policy json config
+     * @return The same object reference but modified
+     */
+    public static JsonElement reworkActorAttributes(JsonElement policyJson) {
+        for (JsonElement policy : policyJson.getAsJsonObject().get(POLICIES).getAsJsonArray()) {
+            JsonObject actor = policy.getAsJsonObject().get(ACTOR).getAsJsonObject();
+            policy.getAsJsonObject().remove(ACTOR);
+            String actorStr = actor.getAsJsonObject().get(ACTOR).getAsString();
+            policy.getAsJsonObject().addProperty(ACTOR, actorStr);
+
+            if ("CDS".equalsIgnoreCase(actorStr)) {
+                policy.getAsJsonObject().addProperty(RECIPE, getRecipe(actor));
+                addCdsPayloadAttributes(actor.getAsJsonObject(RECIPE), policy);
+            } else {
+                policy.getAsJsonObject().addProperty(RECIPE,
+                                                     actor.getAsJsonObject().get(RECIPE).getAsString());
+                addPayloadAttributes(actor, policy);
+            }
+        }
+        return policyJson;
+    }
+
+    private static void addPayloadAttributes(JsonObject jsonObject,
+                                             JsonElement policy) {
+        JsonElement payloadElem = jsonObject.getAsJsonObject().get(PAYLOAD);
+        String payloadString = payloadElem != null ? payloadElem.getAsString() : "";
+        if (!payloadString.isEmpty()) {
+            Map<String, String> testMap = new Yaml().load(payloadString);
+            String json = new GsonBuilder().create().toJson(testMap);
+            policy.getAsJsonObject().add(PAYLOAD,
+                                         new GsonBuilder().create().fromJson(json, JsonElement.class));
+        } else {
+            policy.getAsJsonObject().addProperty(PAYLOAD, "");
+        }
+    }
+
+    private static void addCdsPayloadAttributes(JsonObject jsonObject,
+                                             JsonElement policy) {
+        JsonElement payloadElem = jsonObject.getAsJsonObject().get(PAYLOAD);
+        JsonObject payloadObject = payloadElem != null ?
+                payloadElem.getAsJsonObject() : null;
+        if (payloadObject != null) {
+            /* Since policy expects payload to be map of string,
+               converting data object to string. */
+            JsonObject dataObject = payloadObject.get("data").getAsJsonObject();
+            payloadObject.remove("data");
+            payloadObject.addProperty("data", dataObject.toString());
+            policy.getAsJsonObject().add(PAYLOAD,
+                                         payloadObject);
+        } else {
+            policy.getAsJsonObject().addProperty(PAYLOAD, "");
+        }
+    }
+
+    private static String getRecipe(JsonObject actor) {
+        return actor.getAsJsonObject().get("recipe").getAsJsonObject().get("recipe").getAsString();
+    }
 }