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
This commit is contained in:
Armando Migliaccio 2016-10-14 15:56:36 -07:00 committed by Kevin Benton
parent f2235b7994
commit 17563a802e
120 changed files with 795 additions and 950 deletions

View File

@ -55,14 +55,14 @@ Calling the Core Plugin from Services
There are many cases where a service may want to create a resource
managed by the core plugin (e.g. ports, networks, subnets). This
can be achieved by importing the Neutron Manager and getting a direct
can be achieved by importing the plugins directory and getting a direct
reference to the core plugin:
.. code:: python
from neutron import manager
from neutron_lib.plugins import directory
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
plugin.create_port(context, port_dict)

View File

@ -18,6 +18,7 @@ import collections
import imp
import os
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import log as logging
from oslo_middleware import base
@ -29,7 +30,6 @@ import webob.exc
from neutron._i18n import _, _LE, _LI, _LW
from neutron.common import exceptions
import neutron.extensions
from neutron import manager
from neutron.plugins.common import constants as const
from neutron.services import provider_configuration
from neutron import wsgi
@ -665,7 +665,7 @@ class PluginAwareExtensionManager(ExtensionManager):
@classmethod
def get_instance(cls):
if cls._instance is None:
service_plugins = manager.NeutronManager.get_service_plugins()
service_plugins = directory.get_plugins()
cls._instance = cls(get_extensions_path(service_plugins),
service_plugins)
return cls._instance

View File

@ -14,6 +14,7 @@
# limitations under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
@ -26,7 +27,6 @@ from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.common import utils
from neutron import manager
LOG = logging.getLogger(__name__)
@ -90,7 +90,7 @@ class DhcpAgentNotifyAPI(object):
@property
def plugin(self):
if self._plugin is None:
self._plugin = manager.NeutronManager.get_plugin()
self._plugin = directory.get_plugin()
return self._plugin
def _schedule_network(self, context, network, existing_agents):
@ -166,8 +166,7 @@ class DhcpAgentNotifyAPI(object):
if 'subnet' in payload and payload['subnet'].get('segment_id'):
# if segment_id exists then the segment service plugin
# must be loaded
nm = manager.NeutronManager
segment_plugin = nm.get_service_plugins()['segments']
segment_plugin = directory.get_plugin('segments')
segment = segment_plugin.get_segment(
context, payload['subnet']['segment_id'])
network['candidate_hosts'] = segment['hosts']

View File

@ -16,6 +16,7 @@
import random
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_log import log as logging
import oslo_messaging
@ -24,8 +25,6 @@ from neutron.api.rpc.agentnotifiers import utils as ag_utils
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.common import utils
from neutron import manager
from neutron.plugins.common import constants as service_constants
LOG = logging.getLogger(__name__)
@ -57,8 +56,7 @@ class L3AgentNotifyAPI(object):
shuffle_agents):
"""Notify changed routers to hosting l3 agents."""
adminContext = context if context.is_admin else context.elevated()
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
for router_id in router_ids:
hosts = plugin.get_hosts_to_notify(adminContext, router_id)
if shuffle_agents:
@ -87,8 +85,7 @@ class L3AgentNotifyAPI(object):
def _notification(self, context, method, router_ids, operation,
shuffle_agents, schedule_routers=True):
"""Notify all the agents that are hosting the routers."""
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
if not plugin:
LOG.error(_LE('No plugin for L3 routing registered. Cannot notify '
'agents with the message %s'), method)

View File

@ -13,6 +13,7 @@
# under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_log import log as logging
import oslo_messaging
import six
@ -21,8 +22,6 @@ from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.common import utils
from neutron.db import agentschedulers_db
from neutron import manager
from neutron.plugins.common import constants as service_constants
LOG = logging.getLogger(__name__)
@ -38,8 +37,7 @@ class MeteringAgentNotifyAPI(object):
def _agent_notification(self, context, method, routers):
"""Notify l3 metering agents hosted by l3 agent hosts."""
adminContext = context if context.is_admin else context.elevated()
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
l3_routers = {}
state = agentschedulers_db.get_admin_state_up_filter()
@ -74,8 +72,7 @@ class MeteringAgentNotifyAPI(object):
def _notification(self, context, method, routers):
"""Notify all the agents that are hosting the routers."""
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
if utils.is_extension_supported(
plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS):
self._agent_notification(context, method, routers)

View File

@ -15,11 +15,11 @@ import copy
import pprint
import time
from neutron_lib.plugins import directory
from oslo_log import log as logging
from oslo_utils import importutils
from neutron.api.rpc.callbacks import exceptions
from neutron import manager
LOG = logging.getLogger(__name__)
@ -196,7 +196,7 @@ class CachedResourceConsumerTracker(object):
def _update_consumer_versions(self):
new_tracker = ResourceConsumerTracker()
neutron_plugin = manager.NeutronManager.get_plugin()
neutron_plugin = directory.get_plugin()
agents_db = _import_agents_db()
# If you use RPC callbacks, your plugin needs to implement
# AgentsDbMixin so that we know which resource versions your

View File

@ -19,6 +19,7 @@ import operator
from neutron_lib import constants
from neutron_lib import exceptions
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import log as logging
@ -34,7 +35,6 @@ from neutron.db import api as db_api
from neutron.db import provisioning_blocks
from neutron.extensions import portbindings
from neutron.extensions import segment as segment_ext
from neutron import manager
from neutron.plugins.common import utils as p_utils
from neutron.quota import resource_registry
@ -79,7 +79,7 @@ class DhcpRpcCallback(object):
def _get_active_networks(self, context, **kwargs):
"""Retrieve and return a list of the active networks."""
host = kwargs.get('host')
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
if utils.is_extension_supported(
plugin, constants.DHCP_AGENT_SCHEDULER_EXT_ALIAS):
if cfg.CONF.network_auto_schedule:
@ -140,7 +140,7 @@ class DhcpRpcCallback(object):
host = kwargs.get('host')
LOG.debug('get_active_networks_info from %s', host)
networks = self._get_active_networks(context, **kwargs)
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
filters = {'network_id': [network['id'] for network in networks]}
ports = plugin.get_ports(context, filters=filters)
filters['enable_dhcp'] = [True]
@ -153,7 +153,7 @@ class DhcpRpcCallback(object):
# inside a segment. If the segment service plugin is loaded and
# there are active dhcp enabled subnets, then filter out the subnets
# that are not on the host's segment.
seg_plug = manager.NeutronManager.get_service_plugins().get(
seg_plug = directory.get_plugin(
segment_ext.SegmentPluginBase.get_plugin_type())
seg_subnets = [subnet for subnet in subnets
if subnet.get('segment_id')]
@ -189,7 +189,7 @@ class DhcpRpcCallback(object):
LOG.debug('Network %(network_id)s requested from '
'%(host)s', {'network_id': network_id,
'host': host})
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
try:
network = plugin.get_network(context, network_id)
except exceptions.NetworkNotFound:
@ -198,7 +198,7 @@ class DhcpRpcCallback(object):
return
filters = dict(network_id=[network_id])
subnets = plugin.get_subnets(context, filters=filters)
seg_plug = manager.NeutronManager.get_service_plugins().get(
seg_plug = directory.get_plugin(
segment_ext.SegmentPluginBase.get_plugin_type())
if seg_plug and subnets:
seg_subnets = [subnet for subnet in subnets
@ -231,7 +231,7 @@ class DhcpRpcCallback(object):
LOG.debug('DHCP port deletion for %(network_id)s request from '
'%(host)s',
{'network_id': network_id, 'host': host})
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
plugin.delete_ports_by_device_id(context, device_id, network_id)
@db_api.retry_db_errors
@ -255,7 +255,7 @@ class DhcpRpcCallback(object):
port['port'][portbindings.HOST_ID] = host
if 'mac_address' not in port['port']:
port['port']['mac_address'] = constants.ATTR_NOT_SPECIFIED
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
return self._port_action(plugin, context, port, 'create_port')
@db_api.retry_db_errors
@ -265,7 +265,7 @@ class DhcpRpcCallback(object):
port = kwargs.get('port')
port['id'] = kwargs.get('port_id')
port['port'][portbindings.HOST_ID] = host
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
try:
old_port = plugin.get_port(context, port['id'])
if (old_port['device_id'] != n_const.DEVICE_ID_RESERVED_DHCP_PORT

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.plugins import directory
from oslo_log import helpers as log_helpers
from oslo_log import log as logging
import oslo_messaging
@ -20,7 +21,6 @@ import oslo_messaging
from neutron.common import constants
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron import manager
LOG = logging.getLogger(__name__)
@ -85,7 +85,7 @@ class DVRServerRpcCallback(object):
@property
def plugin(self):
if not getattr(self, '_plugin', None):
self._plugin = manager.NeutronManager.get_plugin()
self._plugin = directory.get_plugin()
return self._plugin
def get_dvr_mac_address_list(self, context):

View File

@ -15,6 +15,7 @@
from neutron_lib import constants
from neutron_lib import exceptions
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
@ -26,8 +27,6 @@ from neutron import context as neutron_context
from neutron.db import api as db_api
from neutron.extensions import l3
from neutron.extensions import portbindings
from neutron import manager
from neutron.plugins.common import constants as plugin_constants
LOG = logging.getLogger(__name__)
@ -52,14 +51,13 @@ class L3RpcCallback(object):
@property
def plugin(self):
if not hasattr(self, '_plugin'):
self._plugin = manager.NeutronManager.get_plugin()
self._plugin = directory.get_plugin()
return self._plugin
@property
def l3plugin(self):
if not hasattr(self, '_l3plugin'):
self._l3plugin = manager.NeutronManager.get_service_plugins()[
plugin_constants.L3_ROUTER_NAT]
self._l3plugin = directory.get_plugin(constants.L3)
return self._l3plugin
def get_router_ids(self, context, host):
@ -209,8 +207,7 @@ class L3RpcCallback(object):
return net_id
def get_service_plugin_list(self, context, **kwargs):
plugins = manager.NeutronManager.get_service_plugins()
return plugins.keys()
return directory.get_plugins().keys()
@db_api.retry_db_errors
def update_floatingip_statuses(self, context, router_id, fip_statuses):

View File

@ -13,10 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from neutron_lib.plugins import directory
import oslo_messaging
from neutron.common import constants
from neutron import manager
class MetadataRpcCallback(object):
@ -36,7 +36,7 @@ class MetadataRpcCallback(object):
@property
def plugin(self):
if not hasattr(self, '_plugin'):
self._plugin = manager.NeutronManager.get_plugin()
self._plugin = directory.get_plugin()
return self._plugin
def get_ports(self, context, filters):

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.plugins import directory
from oslo_log import log as logging
import oslo_messaging
@ -20,7 +21,6 @@ from neutron.common import constants
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.common import utils
from neutron import manager
LOG = logging.getLogger(__name__)
@ -74,7 +74,7 @@ class SecurityGroupServerRpcCallback(object):
@property
def plugin(self):
return manager.NeutronManager.get_plugin()
return directory.get_plugin()
def _get_devices_info(self, context, devices):
return dict(

View File

@ -13,13 +13,13 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import log as logging
from neutron.api import extensions
from neutron.api.v2 import base
from neutron import manager
from neutron.plugins.common import constants
from neutron.quota import resource_registry
LOG = logging.getLogger(__name__)
@ -73,10 +73,7 @@ def build_resource_info(plural_mappings, resource_map, which_service,
which_service = constants.CORE
if action_map is None:
action_map = {}
if which_service != constants.CORE:
plugin = manager.NeutronManager.get_service_plugins()[which_service]
else:
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin(which_service)
path_prefix = getattr(plugin, "path_prefix", "")
LOG.debug('Service %(service)s assigned prefix: %(prefix)s',
{'service': which_service, 'prefix': path_prefix})

View File

@ -14,6 +14,7 @@
# limitations under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_service import wsgi as base_wsgi
import routes as routes_mapper
@ -73,7 +74,8 @@ class APIRouter(base_wsgi.Router):
def __init__(self, **local_config):
mapper = routes_mapper.Mapper()
plugin = manager.NeutronManager.get_plugin()
manager.init()
plugin = directory.get_plugin()
ext_mgr = extensions.PluginAwareExtensionManager.get_instance()
ext_mgr.extend_resources("2.0", attributes.RESOURCE_ATTRIBUTE_MAP)

View File

@ -19,11 +19,13 @@ subnets.
import sys
from neutron_lib import constants
from neutron_lib.plugins import directory
from neutron.common import config
from neutron.common import rpc as n_rpc
from neutron import context
from neutron import manager
from neutron.plugins.common import constants
def main():
@ -31,9 +33,9 @@ def main():
config.setup_logging()
cxt = context.get_admin_context()
plugin = manager.NeutronManager.get_plugin()
l3_plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
manager.init()
plugin = directory.get_plugin()
l3_plugin = directory.get_plugin(constants.L3)
notifier = n_rpc.get_notifier('network')
for network in plugin.get_networks(cxt):
notifier.info(cxt, 'network.exists', {'network': network})

View File

@ -13,10 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.plugins import directory
from neutron.common import exceptions as n_exc
from neutron.core_extensions import base
from neutron.db import api as db_api
from neutron import manager
from neutron.objects.qos import policy as policy_object
from neutron.plugins.common import constants as plugin_constants
from neutron.services.qos import qos_consts
@ -27,8 +28,8 @@ class QosCoreResourceExtension(base.CoreResourceExtension):
@property
def plugin_loaded(self):
if not hasattr(self, '_plugin_loaded'):
service_plugins = manager.NeutronManager.get_service_plugins()
self._plugin_loaded = plugin_constants.QOS in service_plugins
self._plugin_loaded = (
plugin_constants.QOS in directory.get_plugins())
return self._plugin_loaded
def _get_policy_obj(self, context, policy_id):

View File

@ -19,6 +19,7 @@ import debtcollector
from eventlet import greenthread
from neutron_lib.api import converters
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
@ -43,7 +44,6 @@ from neutron.db import api as db_api
from neutron.db.models import agent as agent_model
from neutron.extensions import agent as ext_agent
from neutron.extensions import availability_zone as az_ext
from neutron import manager
LOG = logging.getLogger(__name__)
@ -455,7 +455,7 @@ class AgentExtRpcCallback(object):
"server start timestamp: %(server_time)s", log_dict)
return
if not self.plugin:
self.plugin = manager.NeutronManager.get_plugin()
self.plugin = directory.get_plugin()
agent_status, agent_state = self.plugin.create_or_update_agent(
context, agent_state)
self._update_local_agent_resource_versions(context, agent_state)

View File

@ -20,6 +20,7 @@ from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib.db import utils as db_utils
from neutron_lib import exceptions as exc
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_db import exception as os_db_exc
from oslo_db.sqlalchemy import utils as sa_utils
@ -55,11 +56,9 @@ from neutron.extensions import l3
from neutron import ipam
from neutron.ipam import exceptions as ipam_exc
from neutron.ipam import subnet_alloc
from neutron import manager
from neutron import neutron_plugin_base_v2
from neutron.objects import base as base_obj
from neutron.objects import subnetpool as subnetpool_obj
from neutron.plugins.common import constants as service_constants
LOG = logging.getLogger(__name__)
@ -584,8 +583,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
raise exc.BadRequest(resource='subnets', msg=reason)
def _update_router_gw_ports(self, context, network, subnet):
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
if l3plugin:
gw_ports = self._get_router_gw_ports_by_network(context,
network['id'])
@ -1343,9 +1341,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
except l3.RouterNotFound:
return
else:
l3plugin = (
manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT))
l3plugin = directory.get_plugin(constants.L3)
if l3plugin:
try:
ctx_admin = context.elevated()

View File

@ -15,6 +15,7 @@
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import helpers as log_helpers
@ -33,7 +34,6 @@ from neutron.db.models import dvr as dvr_models
from neutron.db import models_v2
from neutron.extensions import dvr as ext_dvr
from neutron.extensions import portbindings
from neutron import manager
_deprecate._moved_global('DistributedVirtualRouterMacAddress',
new_module=dvr_models)
@ -61,7 +61,7 @@ cfg.CONF.register_opts(dvr_mac_address_opts)
def _delete_mac_associated_with_agent(resource, event, trigger, context, agent,
**kwargs):
host = agent['host']
plugin = manager.NeutronManager.get_plugin()
plugin = directory.get_plugin()
if [a for a in plugin.get_agents(context, filters={'host': [host]})
if a['id'] != agent['id']]:
# there are still agents on this host, don't mess with the mac entry
@ -98,7 +98,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
return self._plugin
except AttributeError:
pass
self._plugin = manager.NeutronManager.get_plugin()
self._plugin = directory.get_plugin()
return self._plugin
def _get_dvr_mac_address_by_host(self, context, host):

View File

@ -14,8 +14,9 @@
# under the License.
from neutron_lib.api import validators
from neutron_lib import constants as lib_constants
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
from sqlalchemy.orm import exc
from sqlalchemy.sql import expression as expr
@ -34,11 +35,9 @@ from neutron.db import models_v2
from neutron.db import rbac_db_models as rbac_db
from neutron.extensions import external_net
from neutron.extensions import rbac as rbac_ext
from neutron import manager
from neutron.plugins.common import constants as service_constants
DEVICE_OWNER_ROUTER_GW = lib_constants.DEVICE_OWNER_ROUTER_GW
DEVICE_OWNER_ROUTER_GW = constants.DEVICE_OWNER_ROUTER_GW
_deprecate._moved_global('ExternalNetwork', new_module=ext_net_models)
@ -180,8 +179,7 @@ class External_net_db_mixin(object):
net_data[external_net.EXTERNAL] = False
def _process_l3_delete(self, context, network_id):
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
if l3plugin:
l3plugin.delete_disassociated_floatingips(context, network_id)

View File

@ -14,6 +14,7 @@
# under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import log as logging
@ -36,8 +37,6 @@ from neutron.db.models import l3_attrs
from neutron.db.models import l3agent as rb_model
from neutron.extensions import l3agentscheduler
from neutron.extensions import router_availability_zone as router_az
from neutron import manager
from neutron.plugins.common import constants as service_constants
_deprecate._moved_global('RouterL3AgentBinding',
@ -169,8 +168,7 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase,
router_id = router['id']
agent_id = agent['id']
if self.router_scheduler:
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
try:
if router.get('ha'):
self.router_scheduler.create_ha_port_and_bind(
@ -217,8 +215,7 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase,
self._unbind_router(context, router_id, agent_id)
router = self.get_router(context, router_id)
plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(constants.L3)
if router.get('ha'):
plugin.delete_ha_interfaces_on_host(context, router_id, agent.host)
# NOTE(Swami): Need to verify if there are DVR serviceable

View File

@ -18,8 +18,9 @@ import itertools
from debtcollector import removals
import netaddr
from neutron_lib.api import validators
from neutron_lib import constants as lib_constants
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import uuidutils
@ -46,8 +47,6 @@ from neutron.db import models_v2
from neutron.db import standardattrdescription_db as st_attr
from neutron.extensions import external_net
from neutron.extensions import l3
from neutron import manager
from neutron.plugins.common import constants
from neutron.plugins.common import utils as p_utils
LOG = logging.getLogger(__name__)
@ -58,10 +57,10 @@ _deprecate._moved_global('Router', new_module=l3_models)
_deprecate._moved_global('FloatingIP', new_module=l3_models)
DEVICE_OWNER_HA_REPLICATED_INT = lib_constants.DEVICE_OWNER_HA_REPLICATED_INT
DEVICE_OWNER_ROUTER_INTF = lib_constants.DEVICE_OWNER_ROUTER_INTF
DEVICE_OWNER_ROUTER_GW = lib_constants.DEVICE_OWNER_ROUTER_GW
DEVICE_OWNER_FLOATINGIP = lib_constants.DEVICE_OWNER_FLOATINGIP
DEVICE_OWNER_HA_REPLICATED_INT = constants.DEVICE_OWNER_HA_REPLICATED_INT
DEVICE_OWNER_ROUTER_INTF = constants.DEVICE_OWNER_ROUTER_INTF
DEVICE_OWNER_ROUTER_GW = constants.DEVICE_OWNER_ROUTER_GW
DEVICE_OWNER_FLOATINGIP = constants.DEVICE_OWNER_FLOATINGIP
EXTERNAL_GW_INFO = l3.EXTERNAL_GW_INFO
# Maps API field to DB column
@ -108,7 +107,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
@property
def _core_plugin(self):
return manager.NeutronManager.get_plugin()
return directory.get_plugin()
def _get_router(self, context, router_id):
try:
@ -224,13 +223,13 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
@db_api.retry_if_session_inactive()
def update_router(self, context, id, router):
r = router['router']
gw_info = r.pop(EXTERNAL_GW_INFO, lib_constants.ATTR_NOT_SPECIFIED)
gw_info = r.pop(EXTERNAL_GW_INFO, constants.ATTR_NOT_SPECIFIED)
original = self.get_router(context, id)
# check whether router needs and can be rescheduled to the proper
# l3 agent (associated with given external network);
# do check before update in DB as an exception will be raised
# in case no proper l3 agent found
if gw_info != lib_constants.ATTR_NOT_SPECIFIED:
if gw_info != constants.ATTR_NOT_SPECIFIED:
candidates = self._check_router_needs_rescheduling(
context, id, gw_info)
# Update the gateway outside of the DB update since it involves L2
@ -241,8 +240,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
candidates = None
router_db = self._update_router_db(context, id, r)
if candidates:
l3_plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3_plugin = directory.get_plugin(constants.L3)
l3_plugin.reschedule_router(context, id, candidates)
updated = self._make_router_dict(router_db)
registry.notify(resources.ROUTER, events.AFTER_UPDATE, self,
@ -278,11 +276,10 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
# first get plugin supporting l3 agent scheduling
# (either l3 service plugin or core_plugin)
l3_plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3_plugin = directory.get_plugin(constants.L3)
if (not utils.is_extension_supported(
l3_plugin,
lib_constants.L3_AGENT_SCHEDULER_EXT_ALIAS) or
constants.L3_AGENT_SCHEDULER_EXT_ALIAS) or
l3_plugin.router_scheduler is None):
# that might mean that we are dealing with non-agent-based
# implementation of l3 services
@ -321,7 +318,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
# Port has no 'tenant-id', as it is hidden from user
port_data = {'tenant_id': '', # intentionally not set
'network_id': network_id,
'fixed_ips': ext_ips or lib_constants.ATTR_NOT_SPECIFIED,
'fixed_ips': ext_ips or constants.ATTR_NOT_SPECIFIED,
'device_id': router['id'],
'device_owner': DEVICE_OWNER_ROUTER_GW,
'admin_state_up': True,
@ -1023,11 +1020,11 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
RouterPort.router_id, models_v2.IPAllocation.ip_address).join(
models_v2.Port, models_v2.IPAllocation).filter(
models_v2.Port.network_id == internal_port['network_id'],
RouterPort.port_type.in_(lib_constants.ROUTER_INTERFACE_OWNERS),
RouterPort.port_type.in_(constants.ROUTER_INTERFACE_OWNERS),
models_v2.IPAllocation.subnet_id == internal_subnet['id']
).join(gw_port, gw_port.device_id == RouterPort.router_id).filter(
gw_port.network_id == external_network_id,
gw_port.device_owner == lib_constants.DEVICE_OWNER_ROUTER_GW
gw_port.device_owner == constants.DEVICE_OWNER_ROUTER_GW
).distinct()
first_router_id = None
@ -1172,7 +1169,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
gw_port = router.gw_port
for fixed_ip in gw_port.fixed_ips:
addr = netaddr.IPAddress(fixed_ip.ip_address)
if addr.version == lib_constants.IP_VERSION_4:
if addr.version == constants.IP_VERSION_4:
next_hop = fixed_ip.ip_address
break
return {'fixed_ip_address': internal_ip_address,
@ -1190,7 +1187,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
return any(s.ip_version == 4 for s in net.subnets)
def _create_floatingip(self, context, floatingip,
initial_status=lib_constants.FLOATINGIP_STATUS_ACTIVE):
initial_status=constants.FLOATINGIP_STATUS_ACTIVE):
fip = floatingip['floatingip']
fip_id = uuidutils.generate_uuid()
@ -1212,7 +1209,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
'admin_state_up': True,
'device_id': 'PENDING',
'device_owner': DEVICE_OWNER_FLOATINGIP,
'status': lib_constants.PORT_STATUS_NOTAPPLICABLE,
'status': constants.PORT_STATUS_NOTAPPLICABLE,
'name': ''}
if fip.get('floating_ip_address'):
port['fixed_ips'] = [
@ -1278,7 +1275,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
@db_api.retry_if_session_inactive()
def create_floatingip(self, context, floatingip,
initial_status=lib_constants.FLOATINGIP_STATUS_ACTIVE):
initial_status=constants.FLOATINGIP_STATUS_ACTIVE):
return self._create_floatingip(context, floatingip, initial_status)
def _update_floatingip(self, context, id, floatingip):
@ -1629,8 +1626,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
port['subnets'] = []
port['extra_subnets'] = []
port['address_scopes'] = {lib_constants.IP_VERSION_4: None,
lib_constants.IP_VERSION_6: None}
port['address_scopes'] = {constants.IP_VERSION_4: None,
constants.IP_VERSION_6: None}
scopes = {}
for subnet in subnets_by_network[port['network_id']]:
@ -1665,18 +1662,18 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
for floating_ip in floating_ips:
router = routers_dict.get(floating_ip['router_id'])
if router:
router_floatingips = router.get(lib_constants.FLOATINGIP_KEY,
router_floatingips = router.get(constants.FLOATINGIP_KEY,
[])
router_floatingips.append(floating_ip)
router[lib_constants.FLOATINGIP_KEY] = router_floatingips
router[constants.FLOATINGIP_KEY] = router_floatingips
def _process_interfaces(self, routers_dict, interfaces):
for interface in interfaces:
router = routers_dict.get(interface['device_id'])
if router:
router_interfaces = router.get(lib_constants.INTERFACE_KEY, [])
router_interfaces = router.get(constants.INTERFACE_KEY, [])
router_interfaces.append(interface)
router[lib_constants.INTERFACE_KEY] = router_interfaces
router[constants.INTERFACE_KEY] = router_interfaces
def _get_router_info_list(self, context, router_ids=None, active=None,
device_owners=None):
@ -1803,7 +1800,7 @@ class L3_NAT_db_mixin(L3_NAT_dbonly_mixin, L3RpcNotifierMixin):
return router_interface_info
def create_floatingip(self, context, floatingip,
initial_status=lib_constants.FLOATINGIP_STATUS_ACTIVE):
initial_status=constants.FLOATINGIP_STATUS_ACTIVE):
floatingip_dict = super(L3_NAT_db_mixin, self).create_floatingip(
context, floatingip, initial_status)
router_id = floatingip_dict['router_id']
@ -1851,8 +1848,7 @@ def _prevent_l3_port_delete_callback(resource, event, trigger, **kwargs):
context = kwargs['context']
port_id = kwargs['port_id']
port_check = kwargs['port_check']
l3plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
if l3plugin and port_check:
l3plugin.prevent_l3_port_deletion(context, port_id)
@ -1860,14 +1856,12 @@ def _prevent_l3_port_delete_callback(resource, event, trigger, **kwargs):
def _notify_routers_callback(resource, event, trigger, **kwargs):
context = kwargs['context']
router_ids = kwargs['router_ids']
l3plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
l3plugin.notify_routers_updated(context, router_ids)
def _notify_subnet_gateway_ip_update(resource, event, trigger, **kwargs):
l3plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
if not l3plugin:
return
context = kwargs['context']
@ -1875,7 +1869,7 @@ def _notify_subnet_gateway_ip_update(resource, event, trigger, **kwargs):
subnet_id = kwargs['subnet_id']
query = context.session.query(models_v2.Port).filter_by(
network_id=network_id,
device_owner=lib_constants.DEVICE_OWNER_ROUTER_GW)
device_owner=constants.DEVICE_OWNER_ROUTER_GW)
query = query.join(models_v2.Port.fixed_ips).filter(
models_v2.IPAllocation.subnet_id == subnet_id)
router_ids = set(port['device_id'] for port in query)
@ -1899,8 +1893,7 @@ def _notify_subnetpool_address_scope_update(resource, event,
query = query.distinct()
router_ids = [r[0] for r in query]
l3plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
l3plugin.notify_routers_updated(context, router_ids)

View File

@ -16,6 +16,7 @@ import collections
from neutron_lib.api import validators
from neutron_lib import constants as const
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_log import helpers as log_helper
from oslo_log import log as logging
@ -39,8 +40,6 @@ from neutron.db import models_v2
from neutron.extensions import l3
from neutron.extensions import portbindings
from neutron.ipam import utils as ipam_utils
from neutron import manager
from neutron.plugins.common import constants
from neutron.plugins.common import utils as p_utils
@ -505,8 +504,7 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin,
L3_NAT_with_dvr_db_mixin, self).remove_router_interface(
context, router_id, interface_info)
plugin = manager.NeutronManager.get_service_plugins().get(
constants.L3_ROUTER_NAT)
plugin = directory.get_plugin(const.L3)
router_hosts_before = plugin._get_dvr_hosts_for_router(
context, router_id)

View File

@ -14,6 +14,7 @@
# under the License.
from neutron_lib import constants as n_const
from neutron_lib.plugins import directory
from oslo_log import log as logging
from sqlalchemy import or_
@ -27,8 +28,6 @@ from neutron.db import l3_agentschedulers_db as l3agent_sch_db
from neutron.db.models import l3agent as rb_model
from neutron.db import models_v2
from neutron.extensions import portbindings
from neutron import manager
from neutron.plugins.common import constants as service_constants
from neutron.plugins.ml2 import db as ml2_db
from neutron.plugins.ml2 import models as ml2_models
@ -95,8 +94,7 @@ class L3_DVRsch_db_mixin(l3agent_sch_db.L3AgentSchedulerDbMixin):
# Make sure we create the floatingip agent gateway port
# for the destination node if fip is associated with this
# fixed port
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(n_const.L3)
(
l3plugin.
check_for_fip_and_create_agent_gw_port_on_host_if_not_exists(
@ -372,8 +370,7 @@ def _notify_l3_agent_new_port(resource, event, trigger, **kwargs):
return
if n_utils.is_dvr_serviced(port['device_owner']):
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(n_const.L3)
context = kwargs['context']
l3plugin.dvr_handle_new_service_port(context, port)
l3plugin.update_arp_entry_for_dvr_service_port(context, port)
@ -382,8 +379,7 @@ def _notify_l3_agent_new_port(resource, event, trigger, **kwargs):
def _notify_port_delete(event, resource, trigger, **kwargs):
context = kwargs['context']
port = kwargs['port']
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(n_const.L3)
if port:
port_host = port.get(portbindings.HOST_ID)
allowed_address_pairs_list = port.get('allowed_address_pairs')
@ -406,8 +402,7 @@ def _notify_l3_agent_port_update(resource, event, trigger, **kwargs):
original_device_owner = original_port.get('device_owner', '')
new_device_owner = new_port.get('device_owner', '')
is_new_device_dvr_serviced = n_utils.is_dvr_serviced(new_device_owner)
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(n_const.L3)
context = kwargs['context']
is_port_no_longer_serviced = (
n_utils.is_dvr_serviced(original_device_owner) and

View File

@ -13,6 +13,7 @@
# under the License.
from neutron_lib import constants
from neutron_lib.plugins import directory
from sqlalchemy import func
from sqlalchemy import sql
@ -25,8 +26,6 @@ from neutron.db.models import l3 as l3_models
from neutron.db.models import l3_attrs
from neutron.db.models import l3agent as rb_model
from neutron.extensions import portbindings
from neutron import manager
from neutron.plugins.common import constants as service_constants
class L3_HA_scheduler_db_mixin(l3_sch_db.AZL3AgentSchedulerDbMixin):
@ -103,8 +102,7 @@ def _notify_l3_agent_ha_port_update(resource, event, trigger, **kwargs):
if (new_device_owner == constants.DEVICE_OWNER_ROUTER_HA_INTF and
new_port['status'] == constants.PORT_STATUS_ACTIVE and
original_port['status'] != new_port['status']):
l3plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3plugin = directory.get_plugin(constants.L3)
l3plugin.l3_rpc_notifier.routers_updated_on_host(
context, [new_port['device_id']], host)

View File

@ -13,13 +13,12 @@
# under the License.
from neutron_lib import constants as consts
from neutron_lib.plugins import directory
from oslo_log import log as logging
import oslo_messaging
from neutron._i18n import _LE
from neutron.common import utils
from neutron import manager
from neutron.plugins.common import constants as service_constants
LOG = logging.getLogger(__name__)
@ -32,8 +31,7 @@ class MeteringRpcCallbacks(object):
self.meter_plugin = meter_plugin
def get_sync_data_metering(self, context, **kwargs):
l3_plugin = manager.NeutronManager.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
l3_plugin = directory.get_plugin(consts.L3)
if not l3_plugin:
return

View File

@ -17,13 +17,13 @@ import abc
from neutron_lib.db import model_base
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
import sqlalchemy as sa
from sqlalchemy.ext import declarative
from sqlalchemy.orm import validates
from neutron._i18n import _
from neutron.api.v2 import attributes as attr
from neutron import manager
ACCESS_SHARED = 'access_as_shared'
@ -97,7 +97,7 @@ class NetworkRBAC(RBACColumns, model_base.BASEV2):
def get_valid_actions(self):
actions = (ACCESS_SHARED,)
pl = manager.NeutronManager.get_plugin()
pl = directory.get_plugin()
if 'external-net' in pl.supported_extension_aliases:
actions += (ACCESS_EXTERNAL,)
return actions

View File

@ -17,13 +17,13 @@ import abc
from neutron_lib.api import converters
from neutron_lib import constants
from neutron_lib import exceptions as nexception
from neutron_lib.plugins import directory
import six
from neutron._i18n import _
from neutron.api import extensions
from neutron.api.