| 1 |
# (c) Copyright 2016 IBM Corporation, All Rights Reserved. |
| 2 |
# |
| 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 |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 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 |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import abc |
| 16 |
import random |
| 17 |
|
| 18 |
from neutron.extensions import availability_zone as az_ext |
| 19 |
from neutron_lib.plugins import constants as plugin_constants |
| 20 |
from neutron_lib.plugins import directory |
| 21 |
from oslo_config import cfg |
| 22 |
from oslo_log import log as logging |
| 23 |
|
| 24 |
from neutron_vpnaas.extensions import vpn_agentschedulers |
| 25 |
|
| 26 |
LOG = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
class VPNScheduler(metaclass=abc.ABCMeta): |
| 30 |
@property |
| 31 |
def l3_plugin(self): |
| 32 |
return directory.get_plugin(plugin_constants.L3) |
| 33 |
|
| 34 |
@abc.abstractmethod |
| 35 |
def schedule(self, plugin, context, router_id, |
| 36 |
candidates=None, hints=None): |
| 37 |
"""Schedule the router to an active VPN agent. |
| 38 |
|
| 39 |
Schedule the router only if it is not already scheduled. |
| 40 |
""" |
| 41 |
pass |
| 42 |
|
| 43 |
def _get_unscheduled_routers(self, context, plugin, router_ids=None): |
| 44 |
"""Get the list of routers with VPN services to be scheduled. |
| 45 |
|
| 46 |
If router IDs are omitted, look for all unscheduled routers. |
| 47 |
|
| 48 |
:param context: the context |
| 49 |
:param plugin: the core plugin |
| 50 |
:param router_ids: the list of routers to be checked for scheduling |
| 51 |
:returns: the list of routers to be scheduled |
| 52 |
""" |
| 53 |
unscheduled_router_ids = plugin.get_unscheduled_vpn_routers( |
| 54 |
context, router_ids=router_ids) |
| 55 |
if unscheduled_router_ids: |
| 56 |
return self.l3_plugin.get_routers( |
| 57 |
context, filters={'id': unscheduled_router_ids}) |
| 58 |
return [] |
| 59 |
|
| 60 |
def _get_routers_can_schedule(self, context, plugin, routers, vpn_agent): |
| 61 |
"""Get the subset of routers whose VPN services can be scheduled on |
| 62 |
the VPN agent. |
| 63 |
""" |
| 64 |
# Assuming that only an active, enabled VPN agent is passed in, |
| 65 |
# all routers can be scheduled to it |
| 66 |
return routers |
| 67 |
|
| 68 |
def auto_schedule_routers(self, plugin, context, vpn_agent): |
| 69 |
"""Schedule non-hosted routers to a VPN agent. |
| 70 |
|
| 71 |
:returns: True if routers have been successfully assigned to the agent |
| 72 |
""" |
| 73 |
unscheduled_routers = self._get_unscheduled_routers(context, plugin) |
| 74 |
|
| 75 |
target_routers = self._get_routers_can_schedule( |
| 76 |
context, plugin, unscheduled_routers, vpn_agent) |
| 77 |
if not target_routers: |
| 78 |
if unscheduled_routers: |
| 79 |
LOG.warning('No unscheduled routers compatible with VPN agent ' |
| 80 |
'configuration on host %s', vpn_agent['host']) |
| 81 |
return [] |
| 82 |
|
| 83 |
self._bind_routers(context, plugin, target_routers, vpn_agent) |
| 84 |
return [router['id'] for router in target_routers] |
| 85 |
|
| 86 |
def _get_candidates(self, plugin, context, sync_router): |
| 87 |
"""Return VPN agents where a router could be scheduled.""" |
| 88 |
active_vpn_agents = plugin.get_vpn_agents(context, active=True) |
| 89 |
if not active_vpn_agents: |
| 90 |
LOG.warning('No active VPN agents') |
| 91 |
return active_vpn_agents |
| 92 |
|
| 93 |
def _bind_routers(self, context, plugin, routers, vpn_agent): |
| 94 |
for router in routers: |
| 95 |
plugin.create_router_to_agent_binding( |
| 96 |
context, router['id'], vpn_agent['id']) |
| 97 |
|
| 98 |
def _schedule_router(self, plugin, context, router_id, |
| 99 |
candidates=None): |
| 100 |
current_vpn_agents = plugin.get_vpn_agents_hosting_routers( |
| 101 |
context, [router_id]) |
| 102 |
if current_vpn_agents: |
| 103 |
chosen_agent = current_vpn_agents[0] |
| 104 |
LOG.debug('VPN service of router %(router_id)s has already ' |
| 105 |
'been hosted by VPN agent %(agent_id)s', |
| 106 |
{'router_id': router_id, |
| 107 |
'agent_id': chosen_agent}) |
| 108 |
return chosen_agent |
| 109 |
|
| 110 |
sync_router = self.l3_plugin.get_router(context, router_id) |
| 111 |
candidates = candidates or self._get_candidates( |
| 112 |
plugin, context, sync_router) |
| 113 |
if not candidates: |
| 114 |
raise vpn_agentschedulers.RouterReschedulingFailed( |
| 115 |
router_id=router_id) |
| 116 |
|
| 117 |
chosen_agent = self._choose_vpn_agent(plugin, context, candidates) |
| 118 |
if plugin.create_router_to_agent_binding(context, router_id, |
| 119 |
chosen_agent['id']): |
| 120 |
return chosen_agent |
| 121 |
|
| 122 |
@abc.abstractmethod |
| 123 |
def _choose_vpn_agent(self, plugin, context, candidates): |
| 124 |
"""Choose an agent from candidates based on a specific policy.""" |
| 125 |
pass |
| 126 |
|
| 127 |
|
| 128 |
class ChanceScheduler(VPNScheduler): |
| 129 |
"""Randomly allocate an VPN agent for a router.""" |
| 130 |
|
| 131 |
def schedule(self, plugin, context, router_id, |
| 132 |
candidates=None): |
| 133 |
return self._schedule_router( |
| 134 |
plugin, context, router_id, candidates=candidates) |
| 135 |
|
| 136 |
def _choose_vpn_agent(self, plugin, context, candidates): |
| 137 |
return random.choice(candidates) |
| 138 |
|
| 139 |
|
| 140 |
class LeastRoutersScheduler(VPNScheduler): |
| 141 |
"""Allocate to an VPN agent with the least number of routers bound.""" |
| 142 |
|
| 143 |
def schedule(self, plugin, context, router_id, |
| 144 |
candidates=None): |
| 145 |
return self._schedule_router( |
| 146 |
plugin, context, router_id, candidates=candidates) |
| 147 |
|
| 148 |
def _choose_vpn_agent(self, plugin, context, candidates): |
| 149 |
candidates_dict = {c['id']: c for c in candidates} |
| 150 |
chosen_agent_id = plugin.get_vpn_agent_with_min_routers( |
| 151 |
context, candidates_dict.keys()) |
| 152 |
return candidates_dict[chosen_agent_id] |
| 153 |
|
| 154 |
|
| 155 |
class AZLeastRoutersScheduler(LeastRoutersScheduler): |
| 156 |
"""Availability zone aware scheduler.""" |
| 157 |
def _get_az_hints(self, router): |
| 158 |
return (router.get(az_ext.AZ_HINTS) or |
| 159 |
cfg.CONF.default_availability_zones) |
| 160 |
|
| 161 |
def _get_routers_can_schedule(self, context, plugin, routers, vpn_agent): |
| 162 |
"""Overwrite VPNScheduler's method to filter by availability zone.""" |
| 163 |
target_routers = [] |
| 164 |
for r in routers: |
| 165 |
az_hints = self._get_az_hints(r) |
| 166 |
if not az_hints or vpn_agent['availability_zone'] in az_hints: |
| 167 |
target_routers.append(r) |
| 168 |
|
| 169 |
if not target_routers: |
| 170 |
return |
| 171 |
|
| 172 |
return super()._get_routers_can_schedule( |
| 173 |
context, plugin, target_routers, vpn_agent) |
| 174 |
|
| 175 |
def _get_candidates(self, plugin, context, sync_router): |
| 176 |
"""Overwrite VPNScheduler's method to filter by availability zone.""" |
| 177 |
all_candidates = super()._get_candidates(plugin, context, sync_router) |
| 178 |
|
| 179 |
candidates = [] |
| 180 |
az_hints = self._get_az_hints(sync_router) |
| 181 |
for agent in all_candidates: |
| 182 |
if not az_hints or agent['availability_zone'] in az_hints: |
| 183 |
candidates.append(agent) |
| 184 |
|
| 185 |
return candidates |