Fixes a bridge and ovsdb connection is removed in case openstack node is removed.

Change-Id: Ib84a76f91c86c24a192ec4cfe64c1f37b84cc6b2
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/util/OpenstackNodeUtil.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/util/OpenstackNodeUtil.java
index f19b8d5..b520b17 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/util/OpenstackNodeUtil.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/util/OpenstackNodeUtil.java
@@ -48,7 +48,11 @@
 import java.io.IOException;
 import java.security.cert.X509Certificate;
 import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 
 import static org.onlab.util.Tools.get;
 
@@ -69,6 +73,7 @@
     private static final String ZERO = "0";
 
     private static final String DPDK_DEVARGS = "dpdk-devargs";
+    private static final String NOT_AVAILABLE = "N/A";
 
     /**
      * Prevents object installation from external.
@@ -290,6 +295,56 @@
     }
 
     /**
+     * Obtains the gateway node by openstack node. Note that the gateway
+     * node is determined by device's device identifier.
+     *
+     * @param gws                a collection of gateway nodes
+     * @param openstackNode      device identifier
+     * @return the hostname of selected gateway node
+     */
+    public static String getGwByComputeNode(Set<OpenstackNode> gws, OpenstackNode openstackNode) {
+        int numOfGw = gws.size();
+
+        if (numOfGw == 0) {
+            return NOT_AVAILABLE;
+        }
+
+        if (!openstackNode.type().equals(OpenstackNode.NodeType.COMPUTE)) {
+            return NOT_AVAILABLE;
+        }
+
+        int gwIndex = Math.abs(openstackNode.intgBridge().hashCode()) % numOfGw;
+
+        return getGwByIndex(gws, gwIndex).hostname();
+    }
+
+    /**
+     * Obtains gateway instance by giving index number.
+     *
+     * @param gws       a collection of gateway nodes
+     * @param index     index number
+     * @return gateway instance
+     */
+    private static OpenstackNode getGwByIndex(Set<OpenstackNode> gws, int index) {
+        Map<String, OpenstackNode> hashMap = new HashMap<>();
+        gws.forEach(gw -> hashMap.put(gw.hostname(), gw));
+        TreeMap<String, OpenstackNode> treeMap = new TreeMap<>(hashMap);
+        Iterator<String> iteratorKey = treeMap.keySet().iterator();
+
+        int intIndex = 0;
+        OpenstackNode gw = null;
+        while (iteratorKey.hasNext()) {
+            String key = iteratorKey.next();
+
+            if (intIndex == index) {
+                gw = treeMap.get(key);
+            }
+            intIndex++;
+        }
+        return gw;
+    }
+
+    /**
      * Builds up and a complete endpoint URL from gateway node.
      *
      * @param node gateway node