From 7f969fa8557cb3054283848e928bded10c2b1afe Mon Sep 17 00:00:00 2001 From: Vu Cong Tuan Date: Fri, 8 Sep 2017 09:09:02 +0700 Subject: [PATCH] Use Agent OVO in l3_agentschedulers_db Agent object has been merged [1]. This patch uses Agent object in l3_agentschedulers_db. [1] https://review.openstack.org/#/c/297887/ Co-Authored-By: Nguyen Phuong An Partially-Implements: blueprint adopt-oslo-versioned-objects-for-db Change-Id: I7c652919a258b2e0b7e068a759ff6bfa996b0c4d --- neutron/db/l3_agentschedulers_db.py | 30 +++++++++---------- neutron/objects/agent.py | 5 ++++ .../unit/scheduler/test_l3_agent_scheduler.py | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/neutron/db/l3_agentschedulers_db.py b/neutron/db/l3_agentschedulers_db.py index 7609b238597..85f1f727054 100644 --- a/neutron/db/l3_agentschedulers_db.py +++ b/neutron/db/l3_agentschedulers_db.py @@ -20,7 +20,6 @@ from oslo_config import cfg from oslo_db import exception as db_exc from oslo_log import log as logging import oslo_messaging -from sqlalchemy import or_ from neutron.agent.common import utils as agent_utils from neutron.common import constants as l_consts @@ -374,30 +373,31 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase, for router_model, agent_count in l3_model_list] def get_l3_agents(self, context, active=None, filters=None): - query = context.session.query(agent_model.Agent) - query = query.filter( - agent_model.Agent.agent_type == constants.AGENT_TYPE_L3) + agent_filters = {'agent_type': constants.AGENT_TYPE_L3} if active is not None: - query = (query.filter(agent_model.Agent.admin_state_up == active)) + agent_filters['admin_state_up'] = active + config_filters = [] if filters: for key, value in filters.items(): column = getattr(agent_model.Agent, key, None) if column: if not value: return [] - query = query.filter(column.in_(value)) - agent_modes = filters.get('agent_modes', []) + agent_modes = filters.pop('agent_modes', []) if agent_modes: - agent_mode_key = '\"agent_mode\": \"' - configuration_filter = ( - [agent_model.Agent.configurations.contains('%s%s\"' % - (agent_mode_key, agent_mode)) - for agent_mode in agent_modes]) - query = query.filter(or_(*configuration_filter)) - + config_filters = set('\"agent_mode\": \"%s\"' % agent_mode + for agent_mode in agent_modes) + agent_filters.update(filters) + agent_objs = [] + if config_filters: + for conf_filter in config_filters: + agent_objs.extend(ag_obj.Agent.get_objects_by_agent_mode( + context, conf_filter, **agent_filters)) + else: + agent_objs = ag_obj.Agent.get_objects(context, **agent_filters) return [l3_agent - for l3_agent in query + for l3_agent in agent_objs if agentschedulers_db.AgentSchedulerDbMixin.is_eligible_agent( active, l3_agent)] diff --git a/neutron/objects/agent.py b/neutron/objects/agent.py index 1c81f02977f..bbae88f39df 100644 --- a/neutron/objects/agent.py +++ b/neutron/objects/agent.py @@ -156,3 +156,8 @@ class Agent(base.NeutronDbObject): agent_model.Agent.availability_zone.in_(availability_zones)).all() agents = [cls._load_object(context, record) for record in query] return agents + + @classmethod + def get_objects_by_agent_mode(cls, context, agent_mode=None, **kwargs): + mode_filter = obj_utils.StringContains(agent_mode) + return cls.get_objects(context, configurations=mode_filter, **kwargs) diff --git a/neutron/tests/unit/scheduler/test_l3_agent_scheduler.py b/neutron/tests/unit/scheduler/test_l3_agent_scheduler.py index 3603e71b129..ef63a7accd6 100644 --- a/neutron/tests/unit/scheduler/test_l3_agent_scheduler.py +++ b/neutron/tests/unit/scheduler/test_l3_agent_scheduler.py @@ -1909,7 +1909,7 @@ class TestGetL3AgentsWithAgentModeFilter(testlib_api.SqlTestCase, self.assertEqual(len(self.expected_agent_modes), len(l3_agents)) returned_agent_modes = [self._get_agent_mode(agent) for agent in l3_agents] - self.assertEqual(self.expected_agent_modes, returned_agent_modes) + self.assertItemsEqual(self.expected_agent_modes, returned_agent_modes) class L3AgentAZLeastRoutersSchedulerTestCase(L3HATestCaseMixin):