ovs-xapi-sync: Handle exceptions from XAPI for get_single_bridge_id.
[sliver-openvswitch.git] / xenserver / usr_share_openvswitch_scripts_ovs-xapi-sync
index fc21582..ac56d37 100755 (executable)
@@ -35,6 +35,7 @@ from ovs.db import types
 import ovs.daemon
 import ovs.db.idl
 import ovs.unixctl
+import ovs.unixctl.server
 
 vlog = ovs.vlog.Vlog("ovs-xapi-sync")
 session = None
@@ -81,13 +82,41 @@ def get_network_by_bridge(br_name):
                 " XAPI session could not be initialized" % br_name)
         return None
 
-    for n in session.xenapi.network.get_all():
-        rec = session.xenapi.network.get_record(n)
-        if rec['bridge'] == br_name:
-            return rec
+    recs = session.xenapi.network.get_all_records_where('field "bridge"="%s"' % br_name)
+    if len(recs) > 0:
+        return recs.values()[0]
 
     return None
 
+# There are possibilities when multiple xs-network-uuids are set for a bridge.
+# In cases like that, we should choose the bridge-id whose PIF does not have a
+# VLAN associated with it.
+def get_single_bridge_id(bridge_ids, default=None):
+    if not init_session():
+        vlog.warn("Failed to get single bridge id from %s because"
+                  "XAPI session could not be initialized" % bridge_ids)
+        return default
+
+    for bridge_id in bridge_ids:
+        try:
+            recs = session.xenapi.network.get_all_records_where(\
+                                            'field "uuid"="%s"' % bridge_id)
+            if recs:
+                pifs = recs.values()[0]['PIFs']
+                for pif in pifs:
+                    try:
+                        rec = session.xenapi.PIF.get_record(pif)
+                        if rec['VLAN'] == '-1':
+                            return bridge_id
+                    except XenAPI.Failure:
+                        vlog.warn("Could not find XAPI entry for PIF %s" % pif)
+                        continue
+
+        except XenAPI.Failure:
+            vlog.warn("Could not find XAPI entry for bridge_id %s" % bridge_id)
+            continue
+
+    return default
 
 # By default, the "bridge-id" external id in the Bridge table is the
 # same as "xs-network-uuids".  This may be overridden by defining a
@@ -236,7 +265,7 @@ def main():
     ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, None)
     ovs.unixctl.command_register("flush-cache", "", 0, 0, unixctl_flush_cache,
                                  None)
-    error, unixctl_server = ovs.unixctl.UnixctlServer.create(None)
+    error, unixctl_server = ovs.unixctl.server.UnixctlServer.create(None)
     if error:
         ovs.util.ovs_fatal(error, "could not create unixctl server", vlog)
 
@@ -286,6 +315,9 @@ def main():
             bridge_id = nbd
             if bridge_id is None:
                 bridge_id = row.external_ids.get("xs-network-uuids")
+                if bridge_id and len(bridge_id.split(";")) > 1:
+                    bridge_ids = bridge_id.split(";")
+                    bridge_id = get_single_bridge_id(bridge_ids, bridge_ids[0])
 
             if bridge_id is not None:
                 set_external_id(row, "bridge-id", bridge_id.split(";")[0])