neutron/neutron/extensions/l3agentscheduler.py
Armando Migliaccio 17563a802e Adopt neutron-lib plugin directory
Neutron Manager is loaded at the very startup of the neutron
server process and with it plugins are loaded and stored for
lookup purposes as their references are widely used across the
entire neutron codebase.

Rather than holding these references directly in NeutronManager
this patch refactors the code so that these references are held
by a plugin directory.

This allows subprojects and other parts of the Neutron codebase
to use the directory in lieu of the manager. The result is a
leaner, cleaner, and more decoupled code.

Usage pattern [1,2] can be translated to [3,4] respectively.

[1] manager.NeutronManager.get_service_plugins()[FOO]
[2] manager.NeutronManager.get_plugin()
[3] directory.get_plugin(FOO)
[4] directory.get_plugin()

The more entangled part is in the neutron unit tests, where the
use of the manager can be simplified as mocking is typically
replaced by a call to the directory add_plugin() method. This is
safe as each test case gets its own copy of the plugin directory.
That said, unit tests that look more like API tests and that rely on
the entire plugin machinery, need some tweaking to avoid stumbling
into plugin loading failures.

Due to the massive use of the manager, deprecation warnings are
considered impractical as they cause logs to bloat out of proportion.

Follow-up patches that show how to adopt the directory in neutron
subprojects are tagged with topic:plugin-directory.

NeutronLibImpact

Partially-implements: blueprint neutron-lib

Change-Id: I7331e914234c5f0b7abe836604fdd7e4067551cf
2016-11-23 04:45:33 -07:00

220 lines
6.9 KiB
Python

# Copyright (c) 2013 OpenStack Foundation.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import abc
from neutron_lib import constants
from neutron_lib import exceptions
from neutron_lib.plugins import directory
from oslo_log import log as logging
import six
import webob.exc
from neutron._i18n import _, _LE
from neutron.api import extensions
from neutron.api.v2 import base
from neutron.api.v2 import resource
from neutron.common import rpc as n_rpc
from neutron.extensions import agent
from neutron import policy
from neutron import wsgi
LOG = logging.getLogger(__name__)
L3_ROUTER = 'l3-router'
L3_ROUTERS = L3_ROUTER + 's'
L3_AGENT = 'l3-agent'
L3_AGENTS = L3_AGENT + 's'
class RouterSchedulerController(wsgi.Controller):
def get_plugin(self):
plugin = directory.get_plugin(constants.L3)
if not plugin:
LOG.error(_LE('No plugin for L3 routing registered to handle '
'router scheduling'))
msg = _('The resource could not be found.')
raise webob.exc.HTTPNotFound(msg)
return plugin
def index(self, request, **kwargs):
plugin = self.get_plugin()
policy.enforce(request.context,
"get_%s" % L3_ROUTERS,
{})
return plugin.list_routers_on_l3_agent(
request.context, kwargs['agent_id'])
def create(self, request, body, **kwargs):
plugin = self.get_plugin()
policy.enforce(request.context,
"create_%s" % L3_ROUTER,
{})
agent_id = kwargs['agent_id']
router_id = body['router_id']
result = plugin.add_router_to_l3_agent(request.context, agent_id,
router_id)
notify(request.context, 'l3_agent.router.add', router_id, agent_id)
return result
def delete(self, request, id, **kwargs):
plugin = self.get_plugin()
policy.enforce(request.context,
"delete_%s" % L3_ROUTER,
{})
agent_id = kwargs['agent_id']
result = plugin.remove_router_from_l3_agent(request.context, agent_id,
id)
notify(request.context, 'l3_agent.router.remove', id, agent_id)
return result
class L3AgentsHostingRouterController(wsgi.Controller):
def get_plugin(self):
plugin = directory.get_plugin(constants.L3)
if not plugin:
LOG.error(_LE('No plugin for L3 routing registered to handle '
'router scheduling'))
msg = _('The resource could not be found.')
raise webob.exc.HTTPNotFound(msg)
return plugin
def index(self, request, **kwargs):
plugin = self.get_plugin()
policy.enforce(request.context,
"get_%s" % L3_AGENTS,
{})
return plugin.list_l3_agents_hosting_router(
request.context, kwargs['router_id'])
class L3agentscheduler(extensions.ExtensionDescriptor):
"""Extension class supporting l3 agent scheduler.
"""
@classmethod
def get_name(cls):
return "L3 Agent Scheduler"
@classmethod
def get_alias(cls):
return constants.L3_AGENT_SCHEDULER_EXT_ALIAS
@classmethod
def get_description(cls):
return "Schedule routers among l3 agents"
@classmethod
def get_updated(cls):
return "2013-02-07T10:00:00-00:00"
@classmethod
def get_resources(cls):
"""Returns Ext Resources."""
exts = []
parent = dict(member_name="agent",
collection_name="agents")
controller = resource.Resource(RouterSchedulerController(),
base.FAULT_MAP)
exts.append(extensions.ResourceExtension(
L3_ROUTERS, controller, parent))
parent = dict(member_name="router",
collection_name="routers")
controller = resource.Resource(L3AgentsHostingRouterController(),
base.FAULT_MAP)
exts.append(extensions.ResourceExtension(
L3_AGENTS, controller, parent))
return exts
def get_extended_resources(self, version):
return {}
class InvalidL3Agent(agent.AgentNotFound):
message = _("Agent %(id)s is not a L3 Agent or has been disabled")
class RouterHostedByL3Agent(exceptions.Conflict):
message = _("The router %(router_id)s has been already hosted "
"by the L3 Agent %(agent_id)s.")
class RouterSchedulingFailed(exceptions.Conflict):
message = _("Failed scheduling router %(router_id)s to "
"the L3 Agent %(agent_id)s.")
class RouterReschedulingFailed(exceptions.Conflict):
message = _("Failed rescheduling router %(router_id)s: "
"no eligible l3 agent found.")
class RouterL3AgentMismatch(exceptions.Conflict):
message = _("Cannot host distributed router %(router_id)s "
"on legacy L3 agent %(agent_id)s.")
class DVRL3CannotAssignToDvrAgent(exceptions.Conflict):
message = _("Not allowed to manually assign a router to an "
"agent in 'dvr' mode.")
class DVRL3CannotRemoveFromDvrAgent(exceptions.Conflict):
message = _("Not allowed to manually remove a router from "
"an agent in 'dvr' mode.")
class RouterDoesntSupportScheduling(exceptions.Conflict):
message = _("Router %(router_id)s does not support agent scheduling.")
@six.add_metaclass(abc.ABCMeta)
class L3AgentSchedulerPluginBase(object):
"""REST API to operate the l3 agent scheduler.
All of method must be in an admin context.
"""
@abc.abstractmethod
def add_router_to_l3_agent(self, context, id, router_id):
pass
@abc.abstractmethod
def remove_router_from_l3_agent(self, context, id, router_id):
pass
@abc.abstractmethod
def list_routers_on_l3_agent(self, context, id):
pass
@abc.abstractmethod
def list_l3_agents_hosting_router(self, context, router_id):
pass
def router_supports_scheduling(self, context, router_id):
"""Override this method to conditionally schedule routers."""
return True
def notify(context, action, router_id, agent_id):
info = {'id': agent_id, 'router_id': router_id}
notifier = n_rpc.get_notifier('router')
notifier.info(context, action, {'agent': info})