1
# Copyright (C) 2014 eNovance SAS <licensing@enovance.com>
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
from sqlalchemy import func
16
from sqlalchemy import sql
18
from neutron.db import agents_db
19
from neutron.db import l3_agentschedulers_db as l3_sch_db
20
from neutron.db import l3_attrs_db
21
from neutron.db import l3_db
24
class L3_HA_scheduler_db_mixin(l3_sch_db.L3AgentSchedulerDbMixin):
26
def get_ha_routers_l3_agents_count(self, context):
27
"""Return a map between HA routers and how many agents every
28
router is scheduled to.
31
# Postgres requires every column in the select to be present in
32
# the group by statement when using an aggregate function.
33
# One solution is to generate a subquery and join it with the desired
35
binding_model = l3_sch_db.RouterL3AgentBinding
36
sub_query = (context.session.query(
37
binding_model.router_id,
38
func.count(binding_model.router_id).label('count')).
39
join(l3_attrs_db.RouterExtraAttributes,
40
binding_model.router_id ==
41
l3_attrs_db.RouterExtraAttributes.router_id).
43
filter(l3_attrs_db.RouterExtraAttributes.ha == sql.true()).
44
group_by(binding_model.router_id).subquery())
46
query = (context.session.query(
47
l3_db.Router.id, l3_db.Router.tenant_id, sub_query.c.count).
51
def get_l3_agents_ordered_by_num_routers(self, context, agent_ids):
52
query = (context.session.query(agents_db.Agent, func.count(
53
l3_sch_db.RouterL3AgentBinding.router_id).label('count')).
54
outerjoin(l3_sch_db.RouterL3AgentBinding).
55
group_by(agents_db.Agent.id).
56
filter(agents_db.Agent.id.in_(agent_ids)).
59
return [record[0] for record in query]