Rework the activate pdp group payload

Rework the payload which triggers the activate pdp group policy API
based on the new requirement.

Issue-ID: CLAMP-646
Change-Id: I9bd7e3f2d1907c7e22c5435c792910b8245780b2
Signed-off-by: xuegao <xg353y@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java
index 44ee522..d47bc96 100644
--- a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java
+++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java
@@ -35,6 +35,7 @@
 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;
@@ -81,18 +82,44 @@
     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.
diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
index 34d524a..6c6fb0c 100644
--- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
+++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
@@ -37,7 +37,6 @@
 import org.junit.Test;
 import org.onap.clamp.clds.util.JsonUtils;
 import org.onap.clamp.clds.util.ResourceFileUtil;
-import org.onap.clamp.loop.components.external.PolicyComponent;
 import org.onap.clamp.loop.log.LogType;
 import org.onap.clamp.loop.log.LoopLog;
 import org.onap.clamp.loop.service.Service;
@@ -46,7 +45,6 @@
 import org.onap.clamp.loop.template.PolicyModel;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
-import org.skyscreamer.jsonassert.JSONAssert;
 
 public class LoopToJsonTest {
 
@@ -173,25 +171,4 @@
         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
                 "blueprint", "components");
     }
-
-    /**
-     * This tests the GSON encode/decode of pdpGroup.
-     *
-     * @throws IOException In case of issues
-     */
-    @Test
-    public void createPoliciesPayloadPdpGroupTest() throws IOException {
-        Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
-                "123456789", "https://dcaetest.org", "UUID-blueprint");
-        OperationalPolicy opPolicy = this.getOperationalPolicy(
-                ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
-        loopTest.addOperationalPolicy(opPolicy);
-        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true);
-        loopTest.addMicroServicePolicy(microServicePolicy);
-
-        JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
-                PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
-    }
 }
diff --git a/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java
index e822dfb..52e10d2 100644
--- a/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java
+++ b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java
@@ -24,15 +24,20 @@
 package org.onap.clamp.loop;
 
 import static org.assertj.core.api.Assertions.assertThat;
-
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
 import java.io.IOException;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.onap.clamp.clds.util.ResourceFileUtil;
 import org.onap.clamp.loop.components.external.ExternalComponentState;
 import org.onap.clamp.loop.components.external.PolicyComponent;
+import org.onap.clamp.loop.template.LoopTemplate;
+import org.onap.clamp.loop.template.PolicyModel;
+import org.onap.clamp.policy.microservice.MicroServicePolicy;
+import org.onap.clamp.policy.operational.OperationalPolicy;
 
 public class PolicyComponentTest {
 
@@ -243,4 +248,36 @@
 
         assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
     }
+
+    /**
+     * Test the create policies payload PdpGroup test.
+     */
+    @Test
+    public void createPoliciesPayloadPdpGroupTest() throws IOException {
+        Loop loopTest = new Loop("ControlLoopTest", "<xml></xml>");
+        PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
+
+        MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", policyModel1, true,
+                new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null);
+        microServicePolicy.setPdpGroup("pdpGroup1");
+        microServicePolicy.setPdpSubGroup("pdpSubgroup1");
+
+        loopTest.addMicroServicePolicy(microServicePolicy);
+
+        PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
+        OperationalPolicy opPolicy = new OperationalPolicy("opPolicy", loopTest,
+                new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), policyModel2, null);
+        opPolicy.setPdpGroup("pdpGroup2");
+        opPolicy.setPdpSubGroup("pdpSubgroup2");
+
+        loopTest.addOperationalPolicy(opPolicy);
+
+        LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null);
+        loopTemplate.setDcaeBlueprintId("UUID-blueprint");
+        loopTest.setLoopTemplate(loopTemplate);
+
+        String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest);
+        String expectedRes = ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json");
+        assertThat(payload).isEqualTo(expectedRes);
+    }
 }
diff --git a/src/test/resources/tosca/pdp-group-policy-payload.json b/src/test/resources/tosca/pdp-group-policy-payload.json
index ad3a5b4..93a8545 100644
--- a/src/test/resources/tosca/pdp-group-policy-payload.json
+++ b/src/test/resources/tosca/pdp-group-policy-payload.json
@@ -1,16 +1,34 @@
 {
-  "policies": [
+  "groups": [
     {
-      "policy-id": "GuardOpPolicyTest"
+      "name": "pdpGroup2",
+      "deploymentSubgroups": [
+        {
+          "pdpType": "pdpSubgroup2",
+          "action": "POST",
+          "policies": [
+            {
+              "name": "onap.policies.controlloop.Operational",
+              "version": "1.0.0"
+            }
+          ]
+        }
+      ]
     },
     {
-      "policy-id": "guard.minmax.new"
-    },
-    {
-      "policy-id": "guard.frequency.new"
-    },
-    {
-      "policy-id": "configPolicyTest"
+      "name": "pdpGroup1",
+      "deploymentSubgroups": [
+        {
+          "pdpType": "pdpSubgroup1",
+          "action": "POST",
+          "policies": [
+            {
+              "name": "onap.policies.monitoring.test",
+              "version": "1.0.0"
+            }
+          ]
+        }
+      ]
     }
   ]
 }
\ No newline at end of file