Move check_dvr_serviceable_ports_on_host() to dvr scheduler

After dvr scheduling refactoring this method is only used in
l3_dvrscheduler_db. This patch also makes it private method.

Change-Id: Iac19d1244c63ec1b71360f9dd3b09c3b131e0ec8
This commit is contained in:
Oleg Bondarev 2016-02-09 11:40:10 +03:00
parent 5b7fd5f0f1
commit bf09e2676d
4 changed files with 39 additions and 37 deletions

@ -33,9 +33,7 @@ from neutron.db import agents_db
from neutron.db import agentschedulers_db
from neutron.db import l3_attrs_db
from neutron.db import model_base
from neutron.db import models_v2
from neutron.extensions import l3agentscheduler
from neutron.extensions import portbindings
from neutron.extensions import router_availability_zone as router_az
from neutron import manager
from neutron.plugins.common import constants as service_constants
@ -450,32 +448,6 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase,
if agentschedulers_db.AgentSchedulerDbMixin.is_eligible_agent(
active, l3_agent)]
def check_dvr_serviceable_ports_on_host(self, context, host, subnet_ids):
"""Check for existence of dvr serviceable ports on host
:param context: request context
:param host: host to look ports on
:param subnet_ids: IDs of subnets to look ports on
:return: return True if dvr serviceable port exists on host,
otherwise return False
"""
# db query will return ports for all subnets if subnet_ids is empty,
# so need to check first
if not subnet_ids:
return False
core_plugin = manager.NeutronManager.get_plugin()
filters = {'fixed_ips': {'subnet_id': subnet_ids},
portbindings.HOST_ID: [host]}
ports_query = core_plugin._get_ports_query(context, filters=filters)
owner_filter = or_(
models_v2.Port.device_owner.startswith(
constants.DEVICE_OWNER_COMPUTE_PREFIX),
models_v2.Port.device_owner.in_(
n_utils.get_other_dvr_serviced_device_owners()))
ports_query = ports_query.filter(owner_filter)
return ports_query.first() is not None
def get_l3_agent_candidates(self, context, sync_router, l3_agents,
ignore_admin_state=False):
"""Get the valid l3 agents for the router from a list of l3_agents.

@ -160,7 +160,7 @@ class L3_DVRsch_db_mixin(l3agent_sch_db.L3AgentSchedulerDbMixin):
continue
subnet_ids = self.get_subnet_ids_on_router(admin_context,
router_id)
if self.check_dvr_serviceable_ports_on_host(
if self._check_dvr_serviceable_ports_on_host(
admin_context, port_host, subnet_ids):
continue
filter_rtr = {'device_id': [router_id],
@ -282,12 +282,40 @@ class L3_DVRsch_db_mixin(l3agent_sch_db.L3AgentSchedulerDbMixin):
for router_id in (router_ids - result_set):
subnet_ids = self.get_subnet_ids_on_router(
context, router_id)
if subnet_ids and self.check_dvr_serviceable_ports_on_host(
context, agent_db['host'], list(subnet_ids)):
if (subnet_ids and
self._check_dvr_serviceable_ports_on_host(
context, agent_db['host'],
list(subnet_ids))):
result_set.add(router_id)
return list(result_set)
def _check_dvr_serviceable_ports_on_host(self, context, host, subnet_ids):
"""Check for existence of dvr serviceable ports on host
:param context: request context
:param host: host to look ports on
:param subnet_ids: IDs of subnets to look ports on
:return: return True if dvr serviceable port exists on host,
otherwise return False
"""
# db query will return ports for all subnets if subnet_ids is empty,
# so need to check first
if not subnet_ids:
return False
core_plugin = manager.NeutronManager.get_plugin()
filters = {'fixed_ips': {'subnet_id': subnet_ids},
portbindings.HOST_ID: [host]}
ports_query = core_plugin._get_ports_query(context, filters=filters)
owner_filter = or_(
models_v2.Port.device_owner.startswith(
n_const.DEVICE_OWNER_COMPUTE_PREFIX),
models_v2.Port.device_owner.in_(
n_utils.get_other_dvr_serviced_device_owners()))
ports_query = ports_query.filter(owner_filter)
return ports_query.first() is not None
def _notify_l3_agent_new_port(resource, event, trigger, **kwargs):
LOG.debug('Received %(resource)s %(event)s', {

@ -1056,7 +1056,7 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
{'router': router})
with mock.patch.object(
self.l3plugin,
'check_dvr_serviceable_ports_on_host') as ports_exist:
'_check_dvr_serviceable_ports_on_host') as ports_exist:
# emulating dvr serviceable ports exist on compute node
ports_exist.return_value = True
self.l3plugin.schedule_router(

@ -626,7 +626,8 @@ class L3SchedulerTestBaseMixin(object):
# test dvr agent_mode case no candidates
router['distributed'] = True
self.get_subnet_ids_on_router = mock.Mock()
self.check_dvr_serviceable_ports_on_host = mock.Mock(return_value=True)
self._check_dvr_serviceable_ports_on_host = mock.Mock(
return_value=True)
self._check_get_l3_agent_candidates(router, agent_list, None, count=0)
def test_get_l3_agent_candidates_dvr_no_vms(self):
@ -640,7 +641,7 @@ class L3SchedulerTestBaseMixin(object):
router['distributed'] = True
# Test no VMs present case
self.get_subnet_ids_on_router = mock.Mock()
self.check_dvr_serviceable_ports_on_host = mock.Mock(
self._check_dvr_serviceable_ports_on_host = mock.Mock(
return_value=False)
self._check_get_l3_agent_candidates(
router, agent_list, HOST_DVR, count=0)
@ -656,7 +657,8 @@ class L3SchedulerTestBaseMixin(object):
agent_list = [self.l3_dvr_snat_agent]
self.get_subnet_ids_on_router = mock.Mock()
self.check_dvr_serviceable_ports_on_host = mock.Mock(return_value=True)
self._check_dvr_serviceable_ports_on_host = mock.Mock(
return_value=True)
self._check_get_l3_agent_candidates(router, agent_list, HOST_DVR_SNAT)
def test_get_l3_agent_candidates_dvr_snat_no_vms(self):
@ -669,11 +671,11 @@ class L3SchedulerTestBaseMixin(object):
router['distributed'] = True
agent_list = [self.l3_dvr_snat_agent]
self.check_dvr_serviceable_ports_on_host = mock.Mock(
self._check_dvr_serviceable_ports_on_host = mock.Mock(
return_value=False)
# Test no VMs present case
self.get_subnet_ids_on_router = mock.Mock()
self.check_dvr_serviceable_ports_on_host.return_value = False
self._check_dvr_serviceable_ports_on_host.return_value = False
self._check_get_l3_agent_candidates(
router, agent_list, HOST_DVR_SNAT, count=1)