Change the logic of synchronizing flow rules on kubevirt network

Change-Id: Iea95dfc0035e822b207d902ceecc49001ae66bbb
(cherry picked from commit f0d00d5e5dfd9576bdbfa826bc81cd168f0589b1)
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSyncRulesCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSyncRulesCommand.java
index 80db82a..69e90c6 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSyncRulesCommand.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSyncRulesCommand.java
@@ -21,7 +21,7 @@
 import org.onosproject.kubevirtnode.api.KubevirtNode;
 import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
 
-import static java.lang.Thread.sleep;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.waitFor;
 import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
 import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
 
@@ -33,8 +33,8 @@
         description = "Re-installs flow rules for KubeVirt networking")
 public class KubevirtSyncRulesCommand extends AbstractShellCommand {
 
-    private static final long SLEEP_MS = 5000; // we wait 5s for init each node
-    private static final long TIMEOUT_MS = 10000; // we wait 10s
+    private static final int  SLEEP_S = 1;     // we re-check the status on every 1s
+    private static final long TIMEOUT_MS = 15000;
 
     private static final String SUCCESS_MSG = "Successfully synchronize flow rules for node %s!";
     private static final String FAIL_MSG = "Failed to synchronize flow rules for node %s.";
@@ -45,14 +45,14 @@
         // tries to re-configure flow rules for the complete node.
         KubevirtNodeAdminService nodeAdminService = get(KubevirtNodeAdminService.class);
         if (nodeAdminService == null) {
-            error("Failed to re-install flow rules for OpenStack networking.");
+            error("Failed to re-install flow rules for kubevirt networking.");
             return;
         }
 
         nodeAdminService.completeNodes().forEach(node ->
                 syncRulesBaseForNode(nodeAdminService, node));
 
-        print("Successfully requested re-installing flow rules.");
+        print("Done all flow rules synchronization, but some nodes may have issues.");
     }
 
     private void syncRulesBaseForNode(KubevirtNodeAdminService service, KubevirtNode node) {
@@ -65,18 +65,7 @@
         while (service.node(node.hostname()).state() != COMPLETE) {
             long  waitMs = timeoutExpiredMs - System.currentTimeMillis();
 
-            try {
-                sleep(SLEEP_MS);
-            } catch (InterruptedException e) {
-                error("Exception caused during node synchronization...");
-            }
-
-            if (service.node(node.hostname()).state() == COMPLETE) {
-                break;
-            } else {
-                service.updateNode(updated);
-                print("Failed to synchronize flow rules, retrying...");
-            }
+            waitFor(SLEEP_S);
 
             if (waitMs <= 0) {
                 result = false;
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
index 96c71d9..66b1af8 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
@@ -428,4 +428,17 @@
         }
         return null;
     }
+
+    /**
+     * Waits for the given length of time.
+     *
+     * @param timeSecond the amount of time for wait in second unit
+     */
+    public static void waitFor(int timeSecond) {
+        try {
+            Thread.sleep(timeSecond * 1000L);
+        } catch (Exception e) {
+            log.error(e.toString());
+        }
+    }
 }
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
index 0eb371b..c3861f7 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
@@ -45,12 +45,12 @@
 import java.util.Set;
 
 import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
-import static java.lang.Thread.sleep;
 import static javax.ws.rs.core.Response.created;
 import static org.onlab.util.Tools.nullIsIllegal;
 import static org.onlab.util.Tools.readTreeFromStream;
 import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
 import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.waitFor;
 
 /**
  * Handles REST API call of KubeVirt node config.
@@ -72,10 +72,9 @@
     private static final String API_CONFIG = "apiConfig";
     private static final String OK = "ok";
     private static final String ERROR = "error";
-    private static final String RESULT = "Result";
 
-    private static final long SLEEP_MS = 5000; // we wait 5s for init each node
-    private static final long TIMEOUT_MS = 10000; // we wait 10s
+    private static final int SLEEP_S = 1;     // we re-check the status on every 1s
+    private static final long TIMEOUT_MS = 15000;
 
     private static final String HOST_NAME = "hostname";
     private static final String ERROR_MESSAGE = " cannot be null";
@@ -268,7 +267,7 @@
 
         KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
 
-        service.completeNodes().forEach(this::syncRulesBase);
+        service.completeNodes().forEach(node -> syncRulesBase(service, node));
         return ok(mapper().createObjectNode()).build();
     }
 
@@ -291,30 +290,17 @@
         return ok(jsonResult).build();
     }
 
-    private void syncRulesBase(KubevirtNode node) {
+    private void syncRulesBase(KubevirtNodeAdminService service, KubevirtNode node) {
         KubevirtNode updated = node.updateState(INIT);
-        KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
         service.updateNode(updated);
 
         boolean result = true;
         long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
 
         while (service.node(node.hostname()).state() != COMPLETE) {
-
             long  waitMs = timeoutExpiredMs - System.currentTimeMillis();
 
-            try {
-                sleep(SLEEP_MS);
-            } catch (InterruptedException e) {
-                log.error("Exception caused during node synchronization...");
-            }
-
-            if (service.node(node.hostname()).state() == COMPLETE) {
-                break;
-            } else {
-                service.updateNode(updated);
-                log.info("Failed to synchronize flow rules, retrying...");
-            }
+            waitFor(SLEEP_S);
 
             if (waitMs <= 0) {
                 result = false;