Fix the bugs in loop state logic

Fix the bugs in the logic to get the loop state

Issue-ID: CLAMP-362
Change-Id: I9d931f1a1ceed3d9e173b3ee742f8e349b7e653f
Signed-off-by: xg353y <xg353y@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/loop/LoopOperation.java b/src/main/java/org/onap/clamp/loop/LoopOperation.java
index f38b591..c3eb08b 100644
--- a/src/main/java/org/onap/clamp/loop/LoopOperation.java
+++ b/src/main/java/org/onap/clamp/loop/LoopOperation.java
@@ -34,7 +34,9 @@
 import java.io.IOException;
 import java.lang.reflect.Array;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -42,6 +44,7 @@
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.config.ClampProperties;
+import org.onap.clamp.policy.operational.OperationalPolicy;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.yaml.snakeyaml.Yaml;
@@ -143,10 +146,10 @@
         String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);
 
         // use http4 instead of http, because camel http4 component is used to do the http call
-        statusUrl.replace("http", "http4");
+        String newStatusUrl = statusUrl.replaceAll("http:", "http4:");
 
         loop.setDcaeDeploymentId(deploymentId);
-        loop.setDcaeDeploymentStatusUrl(statusUrl);
+        loop.setDcaeDeploymentStatusUrl(newStatusUrl);
         loopService.saveOrUpdateLoop(loop);
     }
 
@@ -157,7 +160,7 @@
      * @return The state based on policy response
      * @throws ParseException The parse exception
      */
-    public String analysePolicyResponse(int statusCode) throws ParseException {
+    public String analysePolicyResponse(int statusCode) {
         if (statusCode == 200) {
             return TempLoopState.SUBMITTED.toString();
         } else if (statusCode == 404) {
@@ -167,6 +170,22 @@
     }
 
     /**
+     * Get the name of the first Operational policy.
+     *
+     * @param loop The closed loop
+     * @return The name of the first operational policy
+     */
+    public String getOperationalPolicyName(Loop loop) {
+        Set<OperationalPolicy> opSet = (Set<OperationalPolicy>)loop.getOperationalPolicies();
+        Iterator<OperationalPolicy> iterator = opSet.iterator();
+        while (iterator.hasNext()) {
+            OperationalPolicy policy = iterator.next();
+            return policy.getName();
+        }
+        return null;
+    }
+
+    /**
      * Get the Closed Loop status based on the reply from DCAE.
      *
      * @param camelExchange The camel exchange
@@ -189,13 +208,13 @@
             String status = (String) jsonObj.get("status");
 
             // status = processing/successded/failed
-            if (status == "successed") {
-                if (opType == "install") {
+            if (status.equals("succeeded")) {
+                if (opType.equals("install")) {
                     return TempLoopState.DEPLOYED.toString();
-                } else if (status == "successed") {
+                } else if (opType.equals("uninstall")) {
                     return TempLoopState.NOT_DEPLOYED.toString();
                 }
-            } else if (status == "processing") {
+            } else if (status.equals("processing")) {
                 return TempLoopState.PROCESSING.toString();
             }
         } else if (statusCode == 404) {
@@ -205,13 +224,14 @@
     }
 
     /**
-     * Get the status of the closed loop based on the response from Policy and DCAE.
+     * Update the status of the closed loop based on the response from Policy and DCAE.
      *
+     * @param loop The closed loop
      * @param policyState The state get from Policy
      * @param dcaeState The state get from DCAE
      * @throws ParseException The parse exception
      */
-    public LoopState updateLoopStatus(TempLoopState policyState, TempLoopState dcaeState) {
+    public LoopState updateLoopStatus(Loop loop, TempLoopState policyState, TempLoopState dcaeState) {
         LoopState clState = LoopState.IN_ERROR;
         if (policyState == TempLoopState.SUBMITTED) {
             if (dcaeState == TempLoopState.DEPLOYED) {
@@ -226,6 +246,8 @@
                 clState = LoopState.DESIGN;
             }
         }
+        loop.setLastComputedState(clState);
+        loopService.saveOrUpdateLoop(loop);
         return clState;
     }
 
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 2640d6b..45cad03 100644
--- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml
+++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml
@@ -207,24 +207,14 @@
 						uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" />
 					<to
 						uri="direct:load-loop" />
-					<split>
-						<simple>${exchangeProperty[loopObject].getOperationalPolicies()}
-						</simple>
-						<setProperty propertyName="operationalPolicy">
-							<simple>${body}</simple>
-						</setProperty>
-						<to 
-							uri="direct:get-status-from-policy" />
-					</split>
-					<to 
+					<to
+						uri="direct:get-status-from-policy" />
+					<to
 						uri="direct:get-status-from-dcae" />
+					<log
+						loggingLevel="INFO"
+						message="policy status0000: ${exchangeProperty[policyStatus]}"></log>
 					<choice>
-					<log
-						loggingLevel="INFO"
-						message="policy status0: ${exchangeProperty[policyStatus]}"></log>
-					<log
-						loggingLevel="INFO"
-						message="dcae status0: ${exchangeProperty[dcaeStatus]}"></log>
 						<when>
 							<simple> ${exchangeProperty[policyStatus]} == 'SUBMITTED' and
 								${exchangeProperty[dcaeStatus]} == 'NOT_DEPLOYED'
@@ -609,11 +599,10 @@
 					<to uri="direct:get-status-from-policy" />
 					<to uri="direct:get-status-from-dcae" />
 					<to
-						uri="bean:org.onap.clamp.loop.LoopOperation?method=updateLoopStatus(${exchangeProperty[policyStatus], ${exchangeProperty[dcaeStatus])" />
-
+						uri="bean:org.onap.clamp.loop.LoopOperation?method=updateLoopStatus(${exchangeProperty[loopObject]},${exchangeProperty[policyStatus]}, ${exchangeProperty[dcaeStatus]})" />
 					<log
 						loggingLevel="INFO"
-						message="Get Status request successfully executed for loop: ${body}" />
+						message="Get Status request successfully executed. The new state is: ${body}" />
 					<to
 						uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Get Status request successfully executed','INFO',${exchangeProperty[loopObject]})" />
 					<to
diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml
index 6813800..a116c56 100644
--- a/src/main/resources/clds/camel/routes/flexible-flow.xml
+++ b/src/main/resources/clds/camel/routes/flexible-flow.xml
@@ -605,9 +605,10 @@
 				message="Query Closed Loop status from policy DPD: ${exchangeProperty[loopObject].getName()}" />
 			<to
 				uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=invokeLog('Policy', 'Query operational policies to PDP group')" />
-			<setBody>
-				<constant>null</constant>
-			</setBody>
+			<setProperty propertyName="operationalPolicyName">
+				<method ref="org.onap.clamp.loop.LoopOperation"
+					method="getOperationalPolicyName(${exchangeProperty[loopObject]})" />
+			</setProperty>
 			<setHeader headerName="CamelHttpMethod">
 				<constant>GET</constant>
 			</setHeader>
@@ -625,9 +626,9 @@
 			</setHeader>
 			<log
 				loggingLevel="INFO"
-				message="Endpoint to query from Policy DPD: {{clamp.config.policy.pap.url}}/policy/api/v1/policytypes/onap.policies.controlloop.operational/versions/1.0.0/policies/${exchangeProperty[operationalPolicy].getName()}/versions/deployed"></log>
+				message="Endpoint to query from Policy DPD: {{clamp.config.policy.pap.url}}/policy/api/v1/policytypes/onap.policies.controlloop.operational/versions/1.0.0/policies/${exchangeProperty[operationalPolicyName]}/versions/deployed"></log>
 			<toD
-				uri="{{clamp.config.policy.pap.url}}/policy/api/v1/policytypes/onap.policies.controlloop.operational/versions/1.0.0/policies/${exchangeProperty[operationalPolicy].getName()}/versions/deployed?bridgeEndpoint=true&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;httpClient.connectTimeout=10000&amp;authMethod=Basic&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}" />
+				uri="{{clamp.config.policy.pap.url}}/policy/api/v1/policytypes/onap.policies.controlloop.operational/versions/1.0.0/policies/${exchangeProperty[operationalPolicyName]}/versions/deployed?bridgeEndpoint=true&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;httpClient.connectTimeout=10000&amp;authMethod=Basic&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}" />
 			<doFinally>
 				<to uri="direct:reset-raise-http-exception-flag" />
 				<to
@@ -639,9 +640,6 @@
 					<method ref="org.onap.clamp.loop.LoopOperation"
 					method="analysePolicyResponse(${header.CamelHttpResponseCode})" />
 				</setProperty>
-				<log
-						loggingLevel="INFO"
-						message="policy status: ${exchangeProperty[policyStatus]}"></log>
 				<to uri="direct:dump-loop-log-http-response" />
 			</doFinally>
 		</doTry>