PDP Group support

Add support to push all policies to pdp group + log

Issue-ID: CLAMP-303
Change-Id: Ie53b1dfd11ef8db9c2b5ccca0511e6f6343fb70a
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java
index a24d344..83f938d 100644
--- a/src/main/java/org/onap/clamp/loop/Loop.java
+++ b/src/main/java/org/onap/clamp/loop/Loop.java
@@ -23,6 +23,8 @@
 
 package org.onap.clamp.loop;
 
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
 
@@ -257,6 +259,30 @@
         return buffer.toString().replace('.', '_').replaceAll(" ", "");
     }
 
+    public String createPoliciesPayloadPdpGroup() {
+        JsonObject jsonObject = new JsonObject();
+        JsonArray jsonArray = new JsonArray();
+        jsonObject.add("policies", jsonArray);
+
+        for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
+            JsonObject policyNode = new JsonObject();
+            jsonArray.add(policyNode);
+            policyNode.addProperty("policy-id", opPolicy.getName());
+
+            for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
+                JsonObject guardPolicyNode = new JsonObject();
+                jsonArray.add(guardPolicyNode);
+                guardPolicyNode.addProperty("policy-id", guardName);
+            }
+        }
+        for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
+            JsonObject policyNode = new JsonObject();
+            jsonArray.add(policyNode);
+            policyNode.addProperty("policy-id", microServicePolicy.getName());
+        }
+        return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/src/main/resources/application-noaaf.properties b/src/main/resources/application-noaaf.properties
index 84e97ea..5316f81 100644
--- a/src/main/resources/application-noaaf.properties
+++ b/src/main/resources/application-noaaf.properties
@@ -135,7 +135,7 @@
 #
 #
 # Configuration Settings for Policy Engine Components
-clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081/policy/api/v1
+clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081
 clamp.config.policy.userName=test
 clamp.config.policy.password=test
 clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 4792d05..54cd467 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -150,7 +150,7 @@
 #
 #
 # Configuration Settings for Policy Engine Components
-clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081/policy/api/v1
+clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081
 clamp.config.policy.userName=test
 clamp.config.policy.password=test
 clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123
diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml
index 810c9d2..8a39f13 100644
--- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml
+++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml
@@ -175,8 +175,17 @@
 						</setHeader>
 						<to uri="direct:create-guard-policy" />
 					</split>
-
 				</split>
+				<setHeader headerName="HttpQueryExceptionFlag">
+					<simple resultType="java.lang.Boolean">true</simple>
+				</setHeader>
+				<to uri="direct:create-pdp-group-policy" />
+				
+				<log
+					loggingLevel="INFO"
+					message="SUBMIT request successfully executed for loop: ${body}" />
+				<to
+					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('SUBMIT request successfully executed','INFO',${header.LoopObject})" />
 			</route>
 		</put>
 		<put uri="/v2/loop/delete/{loopName}">
diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml
index 33419c2..64af7b2 100644
--- a/src/main/resources/clds/camel/routes/flexible-flow.xml
+++ b/src/main/resources/clds/camel/routes/flexible-flow.xml
@@ -113,7 +113,7 @@
 			<constant>application/json</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies
 			</simple>
 		</setHeader>
 		<log
@@ -136,7 +136,7 @@
 			<constant>DELETE</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies/${header.microServicePolicy.getName()}
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies/${header.microServicePolicy.getName()}
 			</simple>
 		</setHeader>
 		<log
@@ -161,10 +161,10 @@
 			<constant>POST</constant>
 		</setHeader>
 		<setHeader headerName="Content-Type">
-			<constant>application/json</constant>
+			<constant>application/yaml; legacy-version</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies
 			</simple>
 		</setHeader>
 		<log
@@ -187,7 +187,7 @@
 			<constant>DELETE</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies/${header.operationalPolicy.getName()}
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies/${header.operationalPolicy.getName()}
 			</simple>
 		</setHeader>
 		<log
@@ -215,7 +215,7 @@
 			<constant>application/json</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies
 			</simple>
 		</setHeader>
 		<log
@@ -238,7 +238,7 @@
 			<constant>DELETE</constant>
 		</setHeader>
 		<setHeader headerName="CamelHttpUri">
-			<simple>{{clamp.config.policy.url}}/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/${header.guardPolicy.getKey()}
+			<simple>{{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/${header.guardPolicy.getKey()}
 			</simple>
 		</setHeader>
 		<log
@@ -249,5 +249,32 @@
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Guard policy deleted successfully','INFO',${header.LoopObject})" />
 	</route>
-
+	
+	<route id="create-pdp-group-policy">
+		<from uri="direct:create-pdp-group-policy" />
+		<log
+			loggingLevel="INFO"
+			message="Creating PDP Group Policy: ${header.guardPolicy.getKey()}" />
+		<setBody>
+			<simple>${header.LoopObject.createPoliciesPayloadPdpGroup()}
+			</simple>
+		</setBody>
+		<setHeader headerName="CamelHttpMethod">
+			<constant>POST</constant>
+		</setHeader>
+		<setHeader headerName="Content-Type">
+			<constant>application/json</constant>
+		</setHeader>
+		<setHeader headerName="CamelHttpUri">
+			<simple>{{clamp.config.policy.url}}/policy/pap/v1/pdps
+			</simple>
+		</setHeader>
+		<log
+			loggingLevel="INFO"
+			message="Endpoint to create PDP Group policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"></log>
+		<toD
+			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.HttpQueryExceptionFlag}" />
+		<to
+			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Pdp Group created successfully','INFO',${header.LoopObject})" />
+	</route>
 </routes>
\ No newline at end of file
diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
index 0e03e1b..dcad1a5 100644
--- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
+++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java
@@ -29,15 +29,18 @@
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
 
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Random;
 
 import org.junit.Test;
 import org.onap.clamp.clds.util.JsonUtils;
+import org.onap.clamp.clds.util.ResourceFileUtil;
 import org.onap.clamp.loop.log.LogType;
 import org.onap.clamp.loop.log.LoopLog;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
+import org.skyscreamer.jsonassert.JSONAssert;
 
 public class LoopToJsonTest {
 
@@ -74,10 +77,11 @@
     }
 
     @Test
-    public void LoopGsonTest() {
+    public void LoopGsonTest() throws IOException {
         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
             "123456789", "https://dcaetest.org", "UUID-blueprint");
-        OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
+        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",
@@ -103,4 +107,20 @@
         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
             "loop");
     }
+
+    @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"),
+            loopTest.createPoliciesPayloadPdpGroup(), false);
+    }
 }
diff --git a/src/test/resources/tosca/pdp-group-policy-payload.json b/src/test/resources/tosca/pdp-group-policy-payload.json
new file mode 100644
index 0000000..bf941e5
--- /dev/null
+++ b/src/test/resources/tosca/pdp-group-policy-payload.json
@@ -0,0 +1,16 @@
+{
+  "policies": [
+    {
+      "policy-id": "GuardOpPolicyTest"
+    },
+    {
+      "policy-id": "guard2"
+    },
+    {
+      "policy-id": "guard1"
+    },
+    {
+      "policy-id": "configPolicyTest"
+    }
+  ]
+}
\ No newline at end of file