Rework the activate pdp group payload
[clamp.git] / src / main / java / org / onap / clamp / loop / components / external / PolicyComponent.java
index acd6115..d47bc96 100644 (file)
@@ -35,6 +35,7 @@ import java.util.List;
 import javax.persistence.Transient;
 
 import org.apache.camel.Exchange;
+import org.onap.clamp.clds.util.JsonUtils;
 import org.onap.clamp.loop.Loop;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
@@ -44,17 +45,27 @@ public class PolicyComponent extends ExternalComponent {
     @Transient
     private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class);
 
+    public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
+            "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100);
     public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT",
-        "The policies defined have NOT yet been created on the policy engine");
+            "The policies defined have NOT yet been created on the policy engine", 90);
     public static final ExternalComponentState SENT = new ExternalComponentState("SENT",
-        "The policies defined have been created but NOT deployed on the policy engine");
+            "The policies defined have been created but NOT deployed on the policy engine", 50);
     public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
-        "The policies defined have been created and deployed on the policy engine");
-    public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
-        "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent");
+            "The policies defined have been created and deployed on the policy engine", 10);
+    public static final ExternalComponentState UNKNOWN = new ExternalComponentState("UNKNOWN",
+            "The current status is not clear. Need to regresh the status to get the current status.", 0);
 
+    /**
+     * Default constructor.
+     */
     public PolicyComponent() {
-        super(NOT_SENT);
+        /*
+         * We assume it's good by default as we will receive the state for each policy
+         * on by one, each time we increase the level we can't decrease it anymore.
+         * That's why it starts with the lowest one SENT_AND_DEPLOYED.
+         */
+        super(UNKNOWN);
     }
 
     @Override
@@ -71,18 +82,44 @@ public class PolicyComponent extends ExternalComponent {
     public static String createPoliciesPayloadPdpGroup(Loop loop) {
         JsonObject jsonObject = new JsonObject();
         JsonArray jsonArray = new JsonArray();
-        jsonObject.add("policies", jsonArray);
+        jsonObject.add("groups", jsonArray);
 
-        for (String policyName : PolicyComponent.listPolicyNamesPdpGroup(loop)) {
-            JsonObject policyNode = new JsonObject();
-            jsonArray.add(policyNode);
-            policyNode.addProperty("policy-id", policyName);
+        for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
+            jsonArray.add(createPdpDeploymentPayload(opPolicy.getPdpGroup(), opPolicy.getPdpSubGroup(),
+                    opPolicy.getPolicyModel().getPolicyModelType(), opPolicy.getPolicyModel().getVersion()));
         }
+
+        for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) {
+            jsonArray.add(createPdpDeploymentPayload(msPolicy.getPdpGroup(), msPolicy.getPdpSubGroup(),
+                    msPolicy.getPolicyModel().getPolicyModelType(), msPolicy.getPolicyModel().getVersion()));
+        }
+
         String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
         logger.info("PdpGroup policy payload: " + payload);
         return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
     }
 
+    private static JsonObject createPdpDeploymentPayload(String pdpGroup, String pdpSubGroup,
+            String policyType, String version) {
+        JsonObject pdpGroupNode = new JsonObject();
+        JsonArray subPdpArray = new JsonArray();
+        pdpGroupNode.addProperty("name", pdpGroup);
+        pdpGroupNode.add("deploymentSubgroups", subPdpArray);
+
+        JsonObject pdpSubGroupNode = new JsonObject();
+        subPdpArray.add(pdpSubGroupNode);
+        pdpSubGroupNode.addProperty("pdpType", pdpSubGroup);
+        pdpSubGroupNode.addProperty("action", "POST");
+
+        JsonArray policyArray = new JsonArray();
+        pdpSubGroupNode.add("policies", policyArray);
+        JsonObject policyNode = new JsonObject();
+        policyNode.addProperty("name", policyType);
+        policyNode.addProperty("version", version);
+        policyArray.add(policyNode);
+        return pdpGroupNode;
+    }
+
     /**
      * Generates the list of policy names that must be send/remove to/from active
      * PDP group.
@@ -103,21 +140,38 @@ public class PolicyComponent extends ExternalComponent {
         return policyNamesList;
     }
 
+    private static ExternalComponentState findNewState(boolean found, boolean deployed) {
+
+        ExternalComponentState newState = NOT_SENT;
+        if (found && deployed) {
+            newState = SENT_AND_DEPLOYED;
+        } else if (found) {
+            newState = SENT;
+        } else if (deployed) {
+            newState = IN_ERROR;
+        }
+        return newState;
+    }
+
+    private static ExternalComponentState mergeStates(ExternalComponentState oldState,
+            ExternalComponentState newState) {
+        return (oldState.compareTo(newState) < 0) ? newState : oldState;
+    }
+
+    /**
+     * This is a method that expect the results of the queries getPolicy and
+     * getPolicyDeployed for a unique policy (op,guard, config, etc ...). It
+     * re-computes the global policy state for each policy results given. Therefore
+     * this method is called multiple times from the camel route and must be reset
+     * for a new global policy state retrieval. The state to compute the global
+     * policy state is stored in this class.
+     * 
+     */
     @Override
     public ExternalComponentState computeState(Exchange camelExchange) {
-        boolean oneNotFound = (boolean) camelExchange.getIn().getExchange().getProperty("atLeastOnePolicyNotFound");
-        boolean oneNotDeployed = (boolean) camelExchange.getIn().getExchange()
-            .getProperty("atLeastOnePolicyNotDeployed");
-
-        if (oneNotFound && oneNotDeployed) {
-            this.setState(NOT_SENT);
-        } else if (!oneNotFound && oneNotDeployed) {
-            this.setState(SENT);
-        } else if (!oneNotFound && !oneNotDeployed) {
-            this.setState(SENT_AND_DEPLOYED);
-        } else {
-            this.setState(IN_ERROR);
-        }
+        this.setState(mergeStates(this.getState(),
+                findNewState((boolean) camelExchange.getIn().getExchange().getProperty("policyFound"),
+                        (boolean) camelExchange.getIn().getExchange().getProperty("policyDeployed"))));
         return this.getState();
     }
 }