def synchronize_network(self, context, neutron_network_data, lswitches=None): """Synchronize a Neutron network with its NVP counterpart. This routine synchronizes a set of switches when a Neutron network is mapped to multiple lswitches. """ if not lswitches: # Try to get logical switches from nvp try: lswitches = nvplib.get_lswitches(self._cluster, neutron_network_data['id']) except exceptions.NetworkNotFound: # TODO(salv-orlando): We should be catching # NvpApiClient.ResourceNotFound here # The logical switch was not found LOG.warning( _("Logical switch for neutron network %s not " "found on NVP."), neutron_network_data['id']) lswitches = [] else: for lswitch in lswitches: self._nvp_cache.update_lswitch(lswitch) # By default assume things go wrong status = constants.NET_STATUS_ERROR # In most cases lswitches will contain a single element for ls in lswitches: if not ls: # Logical switch was deleted break ls_status = ls['_relations']['LogicalSwitchStatus'] if not ls_status['fabric_status']: status = constants.NET_STATUS_DOWN break else: # No switch was down or missing. Set status to ACTIVE unless # there were no switches in the first place! if lswitches: status = constants.NET_STATUS_ACTIVE # Update db object if status == neutron_network_data['status']: # do nothing return with context.session.begin(subtransactions=True): try: network = self._plugin._get_network(context, neutron_network_data['id']) except exc.NoResultFound: pass else: network.status = status LOG.debug( _("Updating status for neutron resource %(q_id)s to:" " %(status)s"), { 'q_id': neutron_network_data['id'], 'status': status })
def get_nsx_switch_ids(session, cluster, neutron_network_id): """Return the NSX switch id for a given neutron network. First lookup for mappings in Neutron database. If no mapping is found, query the NSX backend and add the mappings. """ nsx_switch_ids = nicira_db.get_nsx_switch_ids(session, neutron_network_id) if not nsx_switch_ids: # Find logical switches from backend. # This is a rather expensive query, but it won't be executed # more than once for each network in Neutron's lifetime nsx_switches = nvplib.get_lswitches(cluster, neutron_network_id) if not nsx_switches: LOG.warn(_("Unable to find NSX switches for Neutron network %s"), neutron_network_id) return nsx_switch_ids = [] with session.begin(subtransactions=True): for nsx_switch in nsx_switches: nsx_switch_id = nsx_switch['uuid'] nsx_switch_ids.append(nsx_switch_id) # Create DB mapping nicira_db.add_neutron_nsx_network_mapping( session, neutron_network_id, nsx_switch_id) return nsx_switch_ids
def get_nsx_switch_ids(session, cluster, neutron_network_id): """Return the NSX switch id for a given neutron network. First lookup for mappings in Neutron database. If no mapping is found, query the NSX backend and add the mappings. """ nsx_switch_ids = nicira_db.get_nsx_switch_ids( session, neutron_network_id) if not nsx_switch_ids: # Find logical switches from backend. # This is a rather expensive query, but it won't be executed # more than once for each network in Neutron's lifetime nsx_switches = nvplib.get_lswitches(cluster, neutron_network_id) if not nsx_switches: LOG.warn(_("Unable to find NSX switches for Neutron network %s"), neutron_network_id) return nsx_switch_ids = [] with session.begin(subtransactions=True): for nsx_switch in nsx_switches: nsx_switch_id = nsx_switch['uuid'] nsx_switch_ids.append(nsx_switch_id) # Create DB mapping nicira_db.add_neutron_nsx_network_mapping( session, neutron_network_id, nsx_switch_id) return nsx_switch_ids
def fetch_nsx_switches(session, cluster, neutron_net_id): """Retrieve logical switches for a neutron network. This function is optimized for fetching all the lswitches always with a single NSX query. If there is more than 1 logical switch (chained switches use case) NSX lswitches are queried by 'quantum_net_id' tag. Otherwise the NSX lswitch is directly retrieved by id (more efficient). """ nsx_switch_ids = get_nsx_switch_ids(session, cluster, neutron_net_id) if len(nsx_switch_ids) > 1: lswitches = nvplib.get_lswitches(cluster, neutron_net_id) else: lswitches = [nvplib.get_lswitch_by_id(cluster, nsx_switch_ids[0])] return lswitches
def test_exhaust_ports_bridged_network(self): cfg.CONF.set_override('max_lp_per_bridged_ls', 1, group="NVP") providernet_args = {pnet.NETWORK_TYPE: 'flat', pnet.PHYSICAL_NETWORK: 'tzuuid'} with self.network(name='testnet', providernet_args=providernet_args, arg_list=(pnet.NETWORK_TYPE, pnet.PHYSICAL_NETWORK, pnet.SEGMENTATION_ID)) as net: with self.subnet(network=net) as sub: with self.port(subnet=sub): with self.port(subnet=sub): plugin = manager.NeutronManager.get_plugin() ls = nvplib.get_lswitches(plugin.cluster, net['network']['id']) self.assertEqual(len(ls), 2)
def fetch_nsx_switches(session, cluster, neutron_net_id): """Retrieve logical switches for a neutron network. This function is optimized for fetching all the lswitches always with a single NSX query. If there is more than 1 logical switch (chained switches use case) NSX lswitches are queried by 'quantum_net_id' tag. Otherwise the NSX lswitch is directly retrieved by id (more efficient). """ nsx_switch_ids = get_nsx_switch_ids(session, cluster, neutron_net_id) if len(nsx_switch_ids) > 1: lswitches = nvplib.get_lswitches(cluster, neutron_net_id) else: lswitches = [nvplib.get_lswitch_by_id( cluster, nsx_switch_ids[0])] return lswitches
def synchronize_network(self, context, neutron_network_data, lswitches=None): """Synchronize a Neutron network with its NVP counterpart. This routine synchronizes a set of switches when a Neutron network is mapped to multiple lswitches. """ if not lswitches: # Try to get logical switches from nvp try: lswitches = nvplib.get_lswitches( self._cluster, neutron_network_data['id']) except exceptions.NetworkNotFound: # TODO(salv-orlando): We should be catching # NvpApiClient.ResourceNotFound here # The logical switch was not found LOG.warning(_("Logical switch for neutron network %s not " "found on NVP."), neutron_network_data['id']) lswitches = [] else: for lswitch in lswitches: self._nvp_cache.update_lswitch(lswitch) # By default assume things go wrong status = constants.NET_STATUS_ERROR # In most cases lswitches will contain a single element for ls in lswitches: if not ls: # Logical switch was deleted break ls_status = ls['_relations']['LogicalSwitchStatus'] if not ls_status['fabric_status']: status = constants.NET_STATUS_DOWN break else: # No switch was down or missing. Set status to ACTIVE unless # there were no switches in the first place! if lswitches: status = constants.NET_STATUS_ACTIVE # Update db object self._update_neutron_object(context, neutron_network_data, status)