| 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 |
|
| 17 |
from neutron.api import extensions |
| 18 |
from neutron.api.v2 import resource |
| 19 |
from neutron.api import wsgi |
| 20 |
from neutron import policy |
| 21 |
from neutron_lib.api import extensions as lib_extensions |
| 22 |
from neutron_lib.api import faults as base |
| 23 |
from neutron_lib import exceptions |
| 24 |
from neutron_lib.plugins import constants as plugin_const |
| 25 |
from neutron_lib.plugins import directory |
| 26 |
from neutron_lib import rpc as n_rpc |
| 27 |
from oslo_log import log as logging |
| 28 |
import webob.exc |
| 29 |
|
| 30 |
|
| 31 |
LOG = logging.getLogger(__name__) |
| 32 |
|
| 33 |
|
| 34 |
VPN_ROUTER = 'vpn-router' |
| 35 |
VPN_ROUTERS = VPN_ROUTER + 's' |
| 36 |
VPN_AGENT = 'vpn-agent' |
| 37 |
VPN_AGENTS = VPN_AGENT + 's' |
| 38 |
|
| 39 |
|
| 40 |
class VPNRouterSchedulerController(wsgi.Controller): |
| 41 |
def get_plugin(self): |
| 42 |
plugin = directory.get_plugin(plugin_const.VPN) |
| 43 |
if not plugin: |
| 44 |
LOG.error('No plugin for VPN registered to handle VPN ' |
| 45 |
'router scheduling') |
| 46 |
msg = 'The resource could not be found.' |
| 47 |
raise webob.exc.HTTPNotFound(msg) |
| 48 |
return plugin |
| 49 |
|
| 50 |
def index(self, request, **kwargs): |
| 51 |
plugin = self.get_plugin() |
| 52 |
policy.enforce(request.context, |
| 53 |
"get_%s" % VPN_ROUTERS, |
| 54 |
{}) |
| 55 |
return plugin.list_routers_on_vpn_agent( |
| 56 |
request.context, kwargs['agent_id']) |
| 57 |
|
| 58 |
def create(self, request, body, **kwargs): |
| 59 |
plugin = self.get_plugin() |
| 60 |
policy.enforce(request.context, |
| 61 |
"create_%s" % VPN_ROUTER, |
| 62 |
{}) |
| 63 |
agent_id = kwargs['agent_id'] |
| 64 |
router_id = body['router_id'] |
| 65 |
result = plugin.add_router_to_vpn_agent(request.context, agent_id, |
| 66 |
router_id) |
| 67 |
notify(request.context, 'vpn_agent.router.add', router_id, agent_id) |
| 68 |
return result |
| 69 |
|
| 70 |
def delete(self, request, id, **kwargs): |
| 71 |
plugin = self.get_plugin() |
| 72 |
policy.enforce(request.context, |
| 73 |
"delete_%s" % VPN_ROUTER, |
| 74 |
{}) |
| 75 |
agent_id = kwargs['agent_id'] |
| 76 |
result = plugin.remove_router_from_vpn_agent(request.context, agent_id, |
| 77 |
id) |
| 78 |
notify(request.context, 'vpn_agent.router.remove', id, agent_id) |
| 79 |
return result |
| 80 |
|
| 81 |
|
| 82 |
class VPNAgentsHostingRouterController(wsgi.Controller): |
| 83 |
def get_plugin(self): |
| 84 |
plugin = directory.get_plugin(plugin_const.VPN) |
| 85 |
if not plugin: |
| 86 |
LOG.error('VPN plugin not registered to handle agent scheduling') |
| 87 |
msg = 'The resource could not be found.' |
| 88 |
raise webob.exc.HTTPNotFound(msg) |
| 89 |
return plugin |
| 90 |
|
| 91 |
def index(self, request, **kwargs): |
| 92 |
plugin = self.get_plugin() |
| 93 |
policy.enforce(request.context, |
| 94 |
"get_%s" % VPN_AGENTS, |
| 95 |
{}) |
| 96 |
return plugin.list_vpn_agents_hosting_router( |
| 97 |
request.context, kwargs['router_id']) |
| 98 |
|
| 99 |
|
| 100 |
class Vpn_agentschedulers(lib_extensions.ExtensionDescriptor): |
| 101 |
"""Extension class supporting VPN agent scheduler. |
| 102 |
""" |
| 103 |
|
| 104 |
@classmethod |
| 105 |
def get_name(cls): |
| 106 |
return "VPN Agent Scheduler" |
| 107 |
|
| 108 |
@classmethod |
| 109 |
def get_alias(cls): |
| 110 |
return "vpn-agent-scheduler" |
| 111 |
|
| 112 |
@classmethod |
| 113 |
def get_description(cls): |
| 114 |
return "Schedule VPN services of routers among VPN agents" |
| 115 |
|
| 116 |
@classmethod |
| 117 |
def get_updated(cls): |
| 118 |
return "2016-08-15T10:00:00-00:00" |
| 119 |
|
| 120 |
@classmethod |
| 121 |
def get_resources(cls): |
| 122 |
"""Returns Ext Resources.""" |
| 123 |
exts = [] |
| 124 |
parent = dict(member_name="agent", |
| 125 |
collection_name="agents") |
| 126 |
|
| 127 |
controller = resource.Resource(VPNRouterSchedulerController(), |
| 128 |
base.FAULT_MAP) |
| 129 |
exts.append(extensions.ResourceExtension( |
| 130 |
VPN_ROUTERS, controller, parent)) |
| 131 |
|
| 132 |
parent = dict(member_name="router", |
| 133 |
collection_name="routers") |
| 134 |
|
| 135 |
controller = resource.Resource(VPNAgentsHostingRouterController(), |
| 136 |
base.FAULT_MAP) |
| 137 |
exts.append(extensions.ResourceExtension( |
| 138 |
VPN_AGENTS, controller, parent)) |
| 139 |
return exts |
| 140 |
|
| 141 |
def get_extended_resources(self, version): |
| 142 |
return {} |
| 143 |
|
| 144 |
|
| 145 |
class InvalidVPNAgent(exceptions.agent.AgentNotFound): |
| 146 |
message = "Agent %(id)s is not a VPN Agent or has been disabled" |
| 147 |
|
| 148 |
|
| 149 |
class RouterHostedByVPNAgent(exceptions.Conflict): |
| 150 |
message = ("The VPN service of router %(router_id)s has been already " |
| 151 |
"hosted by the VPN Agent %(agent_id)s.") |
| 152 |
|
| 153 |
|
| 154 |
class RouterSchedulingFailed(exceptions.Conflict): |
| 155 |
message = ("Failed scheduling router %(router_id)s to the VPN Agent " |
| 156 |
"%(agent_id)s.") |
| 157 |
|
| 158 |
|
| 159 |
class RouterReschedulingFailed(exceptions.Conflict): |
| 160 |
message = ("Failed rescheduling router %(router_id)s: " |
| 161 |
"No eligible VPN agent found.") |
| 162 |
|
| 163 |
|
| 164 |
class VPNAgentSchedulerPluginBase(metaclass=abc.ABCMeta): |
| 165 |
"""REST API to operate the VPN agent scheduler. |
| 166 |
|
| 167 |
All methods must be in an admin context. |
| 168 |
""" |
| 169 |
|
| 170 |
@abc.abstractmethod |
| 171 |
def add_router_to_vpn_agent(self, context, id, router_id): |
| 172 |
pass |
| 173 |
|
| 174 |
@abc.abstractmethod |
| 175 |
def remove_router_from_vpn_agent(self, context, id, router_id): |
| 176 |
pass |
| 177 |
|
| 178 |
@abc.abstractmethod |
| 179 |
def list_routers_on_vpn_agent(self, context, id): |
| 180 |
pass |
| 181 |
|
| 182 |
@abc.abstractmethod |
| 183 |
def list_vpn_agents_hosting_router(self, context, router_id): |
| 184 |
pass |
| 185 |
|
| 186 |
|
| 187 |
def notify(context, action, router_id, agent_id): |
| 188 |
info = {'id': agent_id, 'router_id': router_id} |
| 189 |
notifier = n_rpc.get_notifier('router') |
| 190 |
notifier.info(context, action, {'agent': info}) |