Draft of op policy
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicy.java
index 62c5a1e..86f8ac3 100644 (file)
@@ -38,7 +38,6 @@ import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -91,17 +90,16 @@ public class OperationalPolicy implements Serializable, Policy {
     /**
      * The constructor.
      *
-     * @param name
-     *        The name of the operational policy
-     * @param loop
-     *        The loop that uses this operational policy
-     * @param configurationsJson
-     *        The operational policy property in the format of json
+     * @param name               The name of the operational policy
+     * @param loop               The loop that uses this operational policy
+     * @param configurationsJson The operational policy property in the format of
+     *                           json
      */
     public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
         this.name = name;
         this.loop = loop;
         this.configurationsJson = configurationsJson;
+        LegacyOperationalPolicy.preloadConfiguration(this.configurationsJson, loop);
     }
 
     @Override
@@ -117,11 +115,6 @@ public class OperationalPolicy implements Serializable, Policy {
         return loop;
     }
 
-    @Override
-    public JsonObject getJsonRepresentation() {
-        return configurationsJson;
-    }
-
     public JsonObject getConfigurationsJson() {
         return configurationsJson;
     }
@@ -130,6 +123,11 @@ public class OperationalPolicy implements Serializable, Policy {
         this.configurationsJson = configurationsJson;
     }
 
+    @Override
+    public JsonObject getJsonRepresentation() {
+        return null;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -184,7 +182,7 @@ public class OperationalPolicy implements Serializable, Policy {
         metadata.addProperty("policy-id", this.name);
 
         operationalPolicyDetails.add("properties", LegacyOperationalPolicy
-            .reworkPayloadAttributes(this.configurationsJson.get("operational_policy").deepCopy()));
+                .reworkPayloadAttributes(this.configurationsJson.get("operational_policy").deepCopy()));
 
         Gson gson = new GsonBuilder().create();
 
@@ -204,9 +202,8 @@ public class OperationalPolicy implements Serializable, Policy {
         // Now using the legacy payload fo Dublin
         JsonObject payload = new JsonObject();
         payload.addProperty("policy-id", this.getName());
-        payload.addProperty("content", URLEncoder.encode(
-            LegacyOperationalPolicy.createPolicyPayloadYamlLegacy(this.configurationsJson.get("operational_policy")),
-            StandardCharsets.UTF_8.toString()));
+        payload.addProperty("content", URLEncoder.encode(LegacyOperationalPolicy.createPolicyPayloadYamlLegacy(
+                this.configurationsJson.get("operational_policy")), StandardCharsets.UTF_8.toString()));
         String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload);
         logger.info("Operational policy payload: " + opPayload);
         return opPayload;
@@ -222,11 +219,9 @@ public class OperationalPolicy implements Serializable, Policy {
 
         JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
         if (guardsList != null) {
-            for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
-                JsonObject guard = new JsonObject();
-                guard.addProperty("policy-id", guardElem.getKey());
-                guard.add("content", guardElem.getValue());
-                result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));
+            for (JsonElement guardElem : guardsList.getAsJsonArray()) {
+                result.put(guardElem.getAsJsonObject().get("policy-id").getAsString(),
+                        new GsonBuilder().create().toJson(guardElem));
             }
         }
         logger.info("Guard policy payload: " + result);