+# 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