Avoid parallelizing node initialization due to state contention

Change-Id: Iadf3bf85d614c1b6b111f107e3d3d55a1f55a53c
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSyncRulesCommand.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSyncRulesCommand.java
index 88b9a71..33f1a03 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSyncRulesCommand.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSyncRulesCommand.java
@@ -29,6 +29,8 @@
         description = "Re-installs flow rules for OpenStack networking")
 public class OpenstackSyncRulesCommand extends AbstractShellCommand {
 
+    private static final long TIMEOUT_MS = 10000; // we wait 10s for init each node
+
     @Override
     protected void execute() {
         // All handlers in this application reacts the node complete event and
@@ -42,6 +44,21 @@
         osNodeService.completeNodes().forEach(osNode -> {
             OpenstackNode updated = osNode.updateState(NodeState.INIT);
             osNodeAdminService.updateNode(updated);
+
+            long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
+            while (updated.state() != NodeState.COMPLETE) {
+                long  waitMs = timeoutExpiredMs - System.currentTimeMillis();
+
+                if (updated.state() == NodeState.COMPLETE) {
+                    print("Finished sync rules for node {}", updated.hostname());
+                    break;
+                }
+
+                if (waitMs <= 0) {
+                    error("Failed to sync rules for node {}", updated.hostname());
+                    break;
+                }
+            }
         });
         print("Successfully requested re-installing flow rules.");
     }
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
index 077fc4b..ddb130f 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/web/OpenstackManagementWebResource.java
@@ -68,6 +68,8 @@
     private static final String FLOATINGIPS = "floatingips";
     private static final String ARP_MODE_NAME = "arpMode";
 
+    private static final long TIMEOUT_MS = 10000; // we wait 10s for init each node
+
     private static final String DEVICE_OWNER_IFACE = "network:router_interface";
 
     private static final String ARP_MODE_REQUIRED = "ARP mode is not specified";
@@ -296,6 +298,21 @@
         osNodeService.completeNodes().forEach(osNode -> {
             OpenstackNode updated = osNode.updateState(NodeState.INIT);
             osNodeAdminService.updateNode(updated);
+
+            long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
+            while (updated.state() != NodeState.COMPLETE) {
+                long  waitMs = timeoutExpiredMs - System.currentTimeMillis();
+
+                if (updated.state() == NodeState.COMPLETE) {
+                    log.info("Finished sync rules for node {}", updated.hostname());
+                    break;
+                }
+
+                if (waitMs <= 0) {
+                    log.warn("Failed to sync rules for node {}", updated.hostname());
+                    break;
+                }
+            }
         });
     }