Add pdp group

Add pdp group code to add/remove policies from active PDP group

Issue-ID: CLAMP-303
Change-Id: I3fcffd229bc7703231b813948aac14ce63437127
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 83f938d..0041c58 100644
--- a/src/main/java/org/onap/clamp/loop/Loop.java
+++ b/src/main/java/org/onap/clamp/loop/Loop.java
@@ -29,7 +29,9 @@
 import com.google.gson.annotations.Expose;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.persistence.CascadeType;
@@ -259,28 +261,43 @@
         return buffer.toString().replace('.', '_').replaceAll(" ", "");
     }
 
+    /**
+     * Generates the Json that must be sent to policy to add all policies to Active
+     * PDP group.
+     *
+     * @return The json, payload to send
+     */
     public String createPoliciesPayloadPdpGroup() {
         JsonObject jsonObject = new JsonObject();
         JsonArray jsonArray = new JsonArray();
         jsonObject.add("policies", jsonArray);
 
-        for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
+        for (String policyName : this.listPolicyNamesPdpGroup()) {
             JsonObject policyNode = new JsonObject();
             jsonArray.add(policyNode);
-            policyNode.addProperty("policy-id", opPolicy.getName());
+            policyNode.addProperty("policy-id", policyName);
+        }
+        return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+    }
 
+    /**
+     * Generates the list of policy names that must be send/remove to/from active
+     * PDP group.
+     *
+     * @return A list of policy names
+     */
+    public List<String> listPolicyNamesPdpGroup() {
+        List<String> policyNamesList = new ArrayList<>();
+        for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
+            policyNamesList.add(opPolicy.getName());
             for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
-                JsonObject guardPolicyNode = new JsonObject();
-                jsonArray.add(guardPolicyNode);
-                guardPolicyNode.addProperty("policy-id", guardName);
+                policyNamesList.add(guardName);
             }
         }
         for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
-            JsonObject policyNode = new JsonObject();
-            jsonArray.add(policyNode);
-            policyNode.addProperty("policy-id", microServicePolicy.getName());
+            policyNamesList.add(microServicePolicy.getName());
         }
-        return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+        return policyNamesList;
     }
 
     @Override
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 ffa4719..e6dc27c 100644
--- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml
+++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml
@@ -43,8 +43,14 @@
 			<route>
 				<to
 					uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
+				<setHeader headerName="GlobalPropertiesJson">
+					<simple>${body}</simple>
+				</setHeader>
+				<to uri="direct:load-loop" />
 				<to
-					uri="bean:org.onap.clamp.loop.LoopController?method=updateGlobalPropertiesJson(${header.loopName},${body})" />
+					uri="bean:org.onap.clamp.loop.LoopController?method=updateGlobalPropertiesJson(${header.loopName},${header.GlobalPropertiesJson})" />
+				<to
+					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Global Properties UPDATED','INFO',${header.LoopObject})" />
 			</route>
 		</post>
 		<post
@@ -56,8 +62,14 @@
 			<route>
 				<to
 					uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
+				<setHeader headerName="OperationalPoliciesArray">
+					<simple>${body}</simple>
+				</setHeader>
+				<to uri="direct:load-loop" />
 				<to
-					uri="bean:org.onap.clamp.loop.LoopController?method=updateOperationalPolicies(${header.loopName},${body})" />
+					uri="bean:org.onap.clamp.loop.LoopController?method=updateOperationalPolicies(${header.loopName},${header.OperationalPoliciesArray})" />
+				<to
+					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Operational and Guard policies UPDATED','INFO',${header.LoopObject})" />
 			</route>
 		</post>
 		<post
@@ -69,8 +81,15 @@
 			<route>
 				<to
 					uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
+				<setHeader headerName="MicroServicePolicyObject">
+					<simple>${body}</simple>
+				</setHeader>
+
+				<to uri="direct:load-loop" />
 				<to
-					uri="bean:org.onap.clamp.loop.LoopController?method=updateMicroservicePolicy(${header.loopName},${body})" />
+					uri="bean:org.onap.clamp.loop.LoopController?method=updateMicroservicePolicy(${header.loopName},${header.MicroServicePolicyObject})" />
+				<to
+					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Micro Service policies UPDATED','INFO',${header.LoopObject})" />
 			</route>
 		</post>
 		<put
@@ -95,19 +114,23 @@
 					uri="bean:org.onap.clamp.operation.LoopOperation?method=unDeployLoop(${header.loopName})" />
 			</route>
 		</put>
-		<put uri="/v2/loop/submit/{loopName}">
+		<put
+			uri="/v2/loop/submit/{loopName}"
+			outType="org.onap.clamp.loop.Loop"
+			produces="application/json">
 			<route>
 				<log
 					loggingLevel="INFO"
 					message="Receive SUBMIT request for loop: ${header.loopName}" />
 				<to
 					uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
-				<setBody>
-					<simple>${header.loopName}</simple>
-				</setBody>
 				<to uri="direct:load-loop" />
 				<to
 					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Receive SUBMIT request','INFO',${header.LoopObject})" />
+				<setHeader headerName="RaiseHttpExceptionFlag">
+					<simple resultType="java.lang.Boolean">false</simple>
+				</setHeader>
+				<to uri="direct:remove-all-policy-from-active-pdp-group" />
 				<log
 					loggingLevel="INFO"
 					message="Processing all MICRO-SERVICES policies defined in loop ${header.LoopObject.getName()}" />
@@ -125,9 +148,6 @@
 					</setHeader>
 					<to uri="direct:delete-micro-service-policy" />
 
-					<setHeader headerName="RaiseHttpExceptionFlag">
-						<simple resultType="java.lang.Boolean">true</simple>
-					</setHeader>
 					<to uri="direct:create-micro-service-policy" />
 				</split>
 
@@ -148,9 +168,6 @@
 					</setHeader>
 					<to uri="direct:delete-operational-policy" />
 
-					<setHeader headerName="RaiseHttpExceptionFlag">
-						<simple resultType="java.lang.Boolean">true</simple>
-					</setHeader>
 					<to uri="direct:create-operational-policy" />
 					<log
 						loggingLevel="INFO"
@@ -170,22 +187,18 @@
 						</setHeader>
 						<to uri="direct:delete-guard-policy" />
 
-						<setHeader headerName="RaiseHttpExceptionFlag">
-							<simple resultType="java.lang.Boolean">true</simple>
-						</setHeader>
 						<to uri="direct:create-guard-policy" />
 					</split>
 				</split>
-				<setHeader headerName="RaiseHttpExceptionFlag">
-					<simple resultType="java.lang.Boolean">true</simple>
-				</setHeader>
-				<to uri="direct:create-pdp-group-policy" />
-				
+				<to uri="direct:add-all-to-active-pdp-group" />
+
 				<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})" />
+				<to
+					uri="bean:org.onap.clamp.loop.LoopController?method=getLoop(${header.loopName})" />
 			</route>
 		</put>
 		<put uri="/v2/loop/delete/{loopName}">
@@ -195,12 +208,10 @@
 					message="Receive DELETE request for loop: ${header.loopName}" />
 				<to
 					uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
-				<setBody>
-					<simple>${header.loopName}</simple>
-				</setBody>
 				<to uri="direct:load-loop" />
 				<to
 					uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Receive DELETE request','INFO',${header.LoopObject})" />
+				<to uri="direct:remove-all-policy-from-active-pdp-group" />
 				<split>
 					<simple>${header.LoopObject.getMicroServicePolicies()}
 					</simple>
@@ -210,9 +221,7 @@
 					<log
 						loggingLevel="INFO"
 						message="Processing Micro Service Policy: ${header.microServicePolicy.getName()}" />
-					<setHeader headerName="RaiseHttpExceptionFlag">
-						<simple resultType="java.lang.Boolean">true</simple>
-					</setHeader>
+
 					<to uri="direct:delete-micro-service-policy" />
 				</split>
 
@@ -228,9 +237,7 @@
 					<log
 						loggingLevel="INFO"
 						message="Processing Operational Policy: ${header.operationalPolicy.getName()}" />
-					<setHeader headerName="RaiseHttpExceptionFlag">
-						<simple resultType="java.lang.Boolean">true</simple>
-					</setHeader>
+
 					<to uri="direct:delete-operational-policy" />
 					<log
 						loggingLevel="INFO"
@@ -245,13 +252,11 @@
 							loggingLevel="INFO"
 							message="Processing Guard Policy: ${header.guardPolicy.getKey()}" />
 
-						<setHeader headerName="RaiseHttpExceptionFlag">
-							<simple resultType="java.lang.Boolean">true</simple>
-						</setHeader>
 						<to uri="direct:delete-guard-policy" />
 					</split>
 				</split>
-				<to  uri="bean:org.onap.clamp.loop.log.LoopService?method=deleteLoop(${header.loopName})" />
+				<to
+					uri="bean:org.onap.clamp.loop.log.LoopService?method=deleteLoop(${header.loopName})" />
 				<log
 					loggingLevel="INFO"
 					message="DELETE request successfully executed for loop: ${body}" />
diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml
index 1f3e01e..3e69fa4 100644
--- a/src/main/resources/clds/camel/routes/flexible-flow.xml
+++ b/src/main/resources/clds/camel/routes/flexible-flow.xml
@@ -78,6 +78,9 @@
 
 	<route id="load-loop">
 		<from uri="direct:load-loop" />
+		<setBody>
+			<simple>${header.loopName}</simple>
+		</setBody>
 		<setHeader headerName="LoopObject">
 			<method
 				ref="org.onap.clamp.loop.LoopService"
@@ -94,11 +97,11 @@
 				message="Loop not found in database: ${body}" />
 			<stop />
 		</when>
-
 	</route>
 
 	<route id="create-micro-service-policy">
 		<from uri="direct:create-micro-service-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Creating Micro Service Policy: ${header.microServicePolicy.getName()}" />
@@ -123,9 +126,11 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('MicroService policy created successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
 	<route id="delete-micro-service-policy">
 		<from uri="direct:delete-micro-service-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Deleting Micro Service Policy: ${header.microServicePolicy.getName()}" />
@@ -146,10 +151,12 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;deleteWithBody=false&amp;mapHttpMessageBody=false&amp;mapHttpMessageFormUrlEncodedBody=false&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('MicroService policy deleted successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
 
 	<route id="create-operational-policy">
 		<from uri="direct:create-operational-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Creating Operational Policy: ${header.operationalPolicy.getName()}" />
@@ -174,9 +181,11 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Operational policy created successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
 	<route id="delete-operational-policy">
 		<from uri="direct:delete-operational-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Deleting Operational Policy: ${header.operationalPolicy.getName()}" />
@@ -197,10 +206,12 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;deleteWithBody=false&amp;mapHttpMessageBody=false&amp;mapHttpMessageFormUrlEncodedBody=false&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Operational policy deleted successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
 
 	<route id="create-guard-policy">
 		<from uri="direct:create-guard-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Creating Guard Policy: ${header.guardPolicy.getKey()}" />
@@ -225,9 +236,11 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Guard policy created successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
 	<route id="delete-guard-policy">
 		<from uri="direct:delete-guard-policy" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
 			message="Deleting Guard Policy: ${header.guardPolicy.getKey()}" />
@@ -248,13 +261,14 @@
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;deleteWithBody=false&amp;mapHttpMessageBody=false&amp;mapHttpMessageFormUrlEncodedBody=false&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
 			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Guard policy deleted successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
 	</route>
-	
-	<route id="create-pdp-group-policy">
-		<from uri="direct:create-pdp-group-policy" />
+	<route id="add-all-to-active-pdp-group">
+		<from uri="direct:add-all-to-active-pdp-group" />
+		<to uri="direct:check-raise-http-exception-flag" />
 		<log
 			loggingLevel="INFO"
-			message="Creating PDP Group Policy: ${header.guardPolicy.getKey()}" />
+			message="Adding loop policies to PDP Group: ${header.LoopObject.getName()}" />
 		<setBody>
 			<simple>${header.LoopObject.createPoliciesPayloadPdpGroup()}
 			</simple>
@@ -271,10 +285,60 @@
 		</setHeader>
 		<log
 			loggingLevel="INFO"
-			message="Endpoint to create PDP Group policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"></log>
+			message="Endpoint to add policies to PDP Group: ${header.CamelHttpMethod} ${header.CamelHttpUri}"></log>
 		<toD
 			uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
 		<to
-			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Pdp Group created successfully','INFO',${header.LoopObject})" />
+			uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Policies pushed to PDP Group successfully','INFO',${header.LoopObject})" />
+		<to uri="direct:reset-raise-http-exception-flag" />
+	</route>
+
+	<route id="remove-all-policy-from-active-pdp-group">
+		<from uri="direct:remove-all-policy-from-active-pdp-group" />
+		<to uri="direct:check-raise-http-exception-flag" />
+		<log
+			loggingLevel="INFO"
+			message="Removing policies from active PDP group for loop: ${header.LoopObject.getName()}" />
+		<split>
+			<simple>${header.LoopObject.listPolicyNamesPdpGroup()}</simple>
+			<setHeader headerName="PolicyName">
+				<simple>${body}</simple>
+			</setHeader>
+			<setBody>
+				<constant>null</constant>
+			</setBody>
+			<setHeader headerName="CamelHttpMethod">
+				<constant>DELETE</constant>
+			</setHeader>
+			<setHeader headerName="CamelHttpUri">
+				<simple>{{clamp.config.policy.url}}/pdps/policies/${header.PolicyName}/versions/1.0.0
+				</simple>
+			</setHeader>
+			<log
+				loggingLevel="INFO"
+				message="Endpoint to delete policy from PDP Group: ${header.CamelHttpMethod} ${header.CamelHttpUri}"></log>
+			<toD
+				uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&amp;httpClient.connectTimeout=10000&amp;authUsername={{clamp.config.policy.userName}}&amp;authPassword={{clamp.config.policy.password}}" />
+			<to
+				uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog(${header.PolicyName}' Policy removed from PDP Group successfully','INFO',${header.LoopObject})" />
+		</split>
+		<to uri="direct:reset-raise-http-exception-flag" />
+	</route>
+
+	<route id="check-raise-http-exception-flag">
+		<from uri="direct:check-raise-http-exception-flag" />
+		<when>
+			<simple>${header.RaiseHttpExceptionFlag} == null</simple>
+			<setHeader headerName="RaiseHttpExceptionFlag">
+				<simple resultType="java.lang.Boolean">true</simple>
+			</setHeader>
+		</when>
+	</route>
+	
+	<route id="reset-raise-http-exception-flag">
+		<from uri="direct:reset-raise-http-exception-flag" />
+			<setHeader headerName="RaiseHttpExceptionFlag">
+				<constant>null</constant>
+			</setHeader>
 	</route>
 </routes>
\ No newline at end of file