'Ensure that a VLAN bridge exists.'

def _ensure_vlan_bridge(self, network):

    vlan_num = network.get_meta('vlan')
    bridge = network['bridge']
    bridge_interface = (CONF.vlan_interface or network.get_meta('bridge_interface'))
    network_ref = network_utils.find_network_with_name_label(self._session, bridge)
    if (network_ref is None):
        description = ('network    for    nova    bridge    %s' % bridge)
        network_rec = {'name_label': bridge, 'name_description': description, 'other_config': {}}
        network_ref = self._session.call_xenapi('network.create', network_rec)
        expr = ('field    "device"    =    "%s"    and    field    "VLAN"    =    "-1"' % bridge_interface)
        pifs = self._session.call_xenapi('PIF.get_all_records_where', expr)
        if (len(pifs) == 0):
            raise Exception((_('Found    no    PIF    for    device    %s') % bridge_interface))
        for pif_ref in pifs.keys():
            self._session.call_xenapi('VLAN.create', pif_ref, str(vlan_num), network_ref)
    else:
        network_rec = self._session.call_xenapi('network.get_record', network_ref)
        for pif_ref in network_rec['PIFs']:
            pif_rec = self._session.call_xenapi('PIF.get_record', pif_ref)
            pif_vlan = int(pif_rec['VLAN'])
            if (pif_vlan != vlan_num):
                raise Exception(_('PIF    %(pif_uuid)s    for    network    %(bridge)s    has    VLAN    id    %(pif_vlan)d.    Expected    %(vlan_num)d'), {'pif_uuid': pif_rec['uuid'], 'bridge': bridge, 'pif_vlan': pif_vlan, 'vlan_num': vlan_num})
    return network_ref
