DVR: Move _get_floatingip_by_port to l3_db

This query can be moved to base l3_db since there isn't
anything DVR-specific about it and the l3_db code can use
a similar helper function.

Related: blueprint multi-l3-backends
Change-Id: Idcc982c0aa19d4685975ddaa974f42ee7cba1b4a
This commit is contained in:
Kevin Benton 2017-04-18 04:08:22 -07:00
parent 6ae1ef2ad3
commit 5111f112c3
4 changed files with 17 additions and 17 deletions

View File

@ -1477,15 +1477,19 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
router_ids = set()
with context.session.begin(subtransactions=True):
fip_qry = context.session.query(l3_models.FloatingIP)
floating_ips = fip_qry.filter_by(fixed_port_id=port_id)
for floating_ip in floating_ips:
for floating_ip in self._get_floatingips_by_port_id(
context, port_id):
router_ids.add(floating_ip['router_id'])
floating_ip.update({'fixed_port_id': None,
'fixed_ip_address': None,
'router_id': None})
return router_ids
def _get_floatingips_by_port_id(self, context, port_id):
"""Helper function to retrieve the fips associated with a port_id."""
fip_qry = context.session.query(l3_models.FloatingIP)
return fip_qry.filter_by(fixed_port_id=port_id).all()
def _build_routers_list(self, context, routers, gw_ports):
"""Subclasses can override this to add extra gateway info"""
return routers

View File

@ -286,12 +286,6 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
self.update_arp_entry_for_dvr_service_port(context,
service_port_dict)
def _get_floatingip_on_port(self, context, port_id=None):
"""Helper function to retrieve the fip associated with port."""
fip_qry = context.session.query(l3_models.FloatingIP)
floating_ip = fip_qry.filter_by(fixed_port_id=port_id)
return floating_ip.first()
@registry.receives(resources.ROUTER_INTERFACE, [events.BEFORE_CREATE])
@db_api.retry_if_session_inactive()
def _add_csnat_on_interface_create(self, resource, event, trigger,
@ -642,9 +636,10 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
def check_for_fip_and_create_agent_gw_port_on_host_if_not_exists(
self, context, port, host):
"""Create fip agent_gw_port on host if not exists"""
fip = self._get_floatingip_on_port(context, port_id=port['id'])
if not fip:
fips = self._get_floatingips_by_port_id(context, port['id'])
if not fips:
return
fip = fips[0]
network_id = fip.get('floating_network_id')
agent_gw_port = self.create_fip_agent_gw_port_if_not_exists(
context.elevated(), network_id, host)

View File

@ -428,8 +428,9 @@ def _notify_l3_agent_port_update(resource, event, trigger, **kwargs):
}
_notify_port_delete(
event, resource, trigger, **removed_router_args)
fip = l3plugin._get_floatingip_on_port(context,
port_id=original_port['id'])
fips = l3plugin._get_floatingips_by_port_id(
context, port_id=original_port['id'])
fip = fips[0] if fips else None
if fip and not (removed_routers and
fip['router_id'] in removed_routers):
l3plugin.l3_rpc_notifier.routers_updated_on_host(

View File

@ -1059,8 +1059,8 @@ class L3DvrSchedulerTestCase(testlib_api.SqlTestCase):
directory.add_plugin(constants.L3, l3plugin)
with mock.patch.object(l3plugin, 'get_dvr_routers_to_remove',
return_value=routers_to_remove),\
mock.patch.object(l3plugin, '_get_floatingip_on_port',
return_value=fip):
mock.patch.object(l3plugin, '_get_floatingips_by_port_id',
return_value=[fip] if fip else []):
l3_dvrscheduler_db._notify_l3_agent_port_update(
'port', 'after_update', mock.ANY, **kwargs)
if routers_to_remove:
@ -1108,8 +1108,8 @@ class L3DvrSchedulerTestCase(testlib_api.SqlTestCase):
return_value=[{'agent_id': 'foo_agent',
'router_id': 'foo_id',
'host': source_host}]),\
mock.patch.object(l3plugin, '_get_floatingip_on_port',
return_value=None):
mock.patch.object(l3plugin, '_get_floatingips_by_port_id',
return_value=[]):
l3_dvrscheduler_db._notify_l3_agent_port_update(
'port', 'after_update', plugin, **kwargs)