From 0a779b4389b160cc763f2d3e115c1eb982baa463 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Wed, 20 Dec 2017 14:26:55 +0200 Subject: [PATCH] TVD: l2gw support Creating a wrapper driver for l2gw for hte TVD plugin This driver choose the actual driver based on the project id. Change-Id: I2edda8c780b7e14b36e3033108186ad63dd0449e --- .../services/l2gateway/nsx_tvd/__init__.py | 0 .../services/l2gateway/nsx_tvd/driver.py | 157 ++++++++++++++++++ vmware_nsx/services/l2gateway/nsx_v/driver.py | 9 +- .../services/l2gateway/nsx_v3/driver.py | 15 +- 4 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 vmware_nsx/services/l2gateway/nsx_tvd/__init__.py create mode 100644 vmware_nsx/services/l2gateway/nsx_tvd/driver.py diff --git a/vmware_nsx/services/l2gateway/nsx_tvd/__init__.py b/vmware_nsx/services/l2gateway/nsx_tvd/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vmware_nsx/services/l2gateway/nsx_tvd/driver.py b/vmware_nsx/services/l2gateway/nsx_tvd/driver.py new file mode 100644 index 0000000000..89da9d0180 --- /dev/null +++ b/vmware_nsx/services/l2gateway/nsx_tvd/driver.py @@ -0,0 +1,157 @@ +# Copyright 2015 VMware, Inc. +# +# 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. + +from networking_l2gw.db.l2gateway import l2gateway_db +from neutron_lib import exceptions as n_exc +from neutron_lib.plugins import directory +from oslo_log import log as logging + +from vmware_nsx.db import db as nsx_db +from vmware_nsx.extensions import projectpluginmap +from vmware_nsx.services.l2gateway.nsx_v import driver as v_driver +from vmware_nsx.services.l2gateway.nsx_v3 import driver as t_driver + +LOG = logging.getLogger(__name__) + + +class NsxTvdL2GatewayDriver(l2gateway_db.L2GatewayMixin): + """Class to handle API calls for L2 gateway and NSX-TVD plugin wrapper.""" + + def __init__(self, plugin): + super(NsxTvdL2GatewayDriver, self).__init__() + self._plugin = plugin + + # supported drivers: + self.drivers = {} + try: + self.drivers[projectpluginmap.NsxPlugins.NSX_T] = ( + t_driver.NsxV3Driver(plugin)) + except Exception: + LOG.warning("NsxTvdL2GatewayDriver failed to initialize the NSX-T " + "driver") + self.drivers[projectpluginmap.NsxPlugins.NSX_T] = None + try: + self.drivers[projectpluginmap.NsxPlugins.NSX_V] = ( + v_driver.NsxvL2GatewayDriver(plugin)) + except Exception: + LOG.warning("NsxTvdL2GatewayDriver failed to initialize the NSX-V " + "driver") + self.drivers[projectpluginmap.NsxPlugins.NSX_V] = None + + def _get_driver_for_project(self, context, project): + """Get the l2gw driver by the plugin of the project""" + mapping = nsx_db.get_project_plugin_mapping( + context.session, project) + if mapping: + plugin_type = mapping['plugin'] + else: + msg = _("Couldn't find the plugin project %s is using") % project + raise n_exc.InvalidInput(error_message=msg) + + if plugin_type not in self.drivers: + msg = (_("Project %(project)s with plugin %(plugin)s has no " + "support for L2GW") % {'project': project, + 'plugin': plugin_type}) + raise n_exc.InvalidInput(error_message=msg) + + # make sure the core plugin is supported + core_plugin = directory.get_plugin() + if not core_plugin.get_plugin_by_type(plugin_type): + msg = (_("Plugin %(plugin)s for project %(project)s is not " + "supported by the core plugin") % {'project': project, + 'plugin': plugin_type}) + raise n_exc.InvalidInput(error_message=msg) + + return self.drivers[plugin_type] + + def create_l2_gateway(self, context, l2_gateway): + d = self._get_driver_for_project( + context, l2_gateway['l2_gateway']['tenant_id']) + return d.create_l2_gateway(context, l2_gateway) + + def create_l2_gateway_precommit(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def create_l2_gateway_postcommit(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def update_l2_gateway(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def update_l2_gateway_precommit(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def update_l2_gateway_postcommit(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def create_l2_gateway_connection(self, context, l2_gateway_connection): + d = self._get_driver_for_project( + context, + l2_gateway_connection['l2_gateway_connection']['tenant_id']) + return d.create_l2_gateway_connection(context, l2_gateway_connection) + + def create_l2_gateway_connection_precommit(self, contex, gw_connection): + # Not implemented by any of the plugins + pass + + def create_l2_gateway_connection_postcommit(self, context, gw_connection): + d = self._get_driver_for_project(context, gw_connection['tenant_id']) + return d.create_l2_gateway_connection_postcommit( + context, gw_connection) + + def _get_gw_connection_driver(self, context, l2gw_connection_id): + l2gw_conn = self._plugin._get_l2_gateway_connection( + context, l2gw_connection_id) + return self._get_driver_for_project(context, l2gw_conn.tenant_id) + + def delete_l2_gateway_connection(self, context, l2_gateway_connection_id): + d = self._get_gw_connection_driver(context, l2_gateway_connection_id) + return d.delete_l2_gateway_connection( + context, l2_gateway_connection_id) + + def delete_l2_gateway_connection_precommit(self, context, + l2_gateway_connection): + # Not implemented by any of the plugins + pass + + def delete_l2_gateway_connection_postcommit(self, context, + l2_gateway_connection_id): + # Not implemented by any of the plugins + #Note(asarfaty): in postcommit the l2_gateway_connection was already + # deleted so we cannot decide on the plugin by the project of the + # connection. + pass + + def delete_l2_gateway(self, context, l2_gateway_id): + l2gw = self._plugin._get_l2_gateway(context, l2_gateway_id) + d = self._get_driver_for_project( + context, l2gw['tenant_id']) + return d.delete_l2_gateway(context, l2_gateway_id) + + def delete_l2_gateway_precommit(self, context, l2_gateway): + # Not implemented by any of the plugins + pass + + def delete_l2_gateway_postcommit(self, context, l2_gateway): + # Not implemented by any of the plugins + #Note(asarfaty): in postcommit the l2_gateway was already deleted + # so we cannot decide on the plugin by the project of the gw. + pass diff --git a/vmware_nsx/services/l2gateway/nsx_v/driver.py b/vmware_nsx/services/l2gateway/nsx_v/driver.py index b9badac850..806a5a7919 100644 --- a/vmware_nsx/services/l2gateway/nsx_v/driver.py +++ b/vmware_nsx/services/l2gateway/nsx_v/driver.py @@ -28,6 +28,7 @@ from vmware_nsx.common import exceptions as nsx_exc from vmware_nsx.common import nsxv_constants from vmware_nsx.db import db as nsx_db from vmware_nsx.db import nsxv_db +from vmware_nsx.extensions import projectpluginmap from vmware_nsx.plugins.nsx_v import availability_zones as nsx_az from vmware_nsx.plugins.nsx_v.vshield.common import exceptions @@ -41,10 +42,16 @@ class NsxvL2GatewayDriver(l2gateway_db.L2GatewayMixin): def __init__(self, plugin): super(NsxvL2GatewayDriver, self).__init__() self._plugin = plugin + self.__core_plugin = None @property def _core_plugin(self): - return directory.get_plugin() + if not self.__core_plugin: + self.__core_plugin = directory.get_plugin() + if self.__core_plugin.is_tvd_plugin(): + self.__core_plugin = self.__core_plugin.get_plugin_by_type( + projectpluginmap.NsxPlugins.NSX_V) + return self.__core_plugin @property def _nsxv(self): diff --git a/vmware_nsx/services/l2gateway/nsx_v3/driver.py b/vmware_nsx/services/l2gateway/nsx_v3/driver.py index 8b24dff81c..ffb0eb126b 100644 --- a/vmware_nsx/services/l2gateway/nsx_v3/driver.py +++ b/vmware_nsx/services/l2gateway/nsx_v3/driver.py @@ -36,6 +36,7 @@ from neutron_lib.plugins import directory from vmware_nsx._i18n import _ from vmware_nsx.common import utils as nsx_utils from vmware_nsx.db import db as nsx_db +from vmware_nsx.extensions import projectpluginmap from vmware_nsxlib.v3 import exceptions as nsxlib_exc from vmware_nsxlib.v3 import nsx_constants @@ -56,10 +57,16 @@ class NsxV3Driver(l2gateway_db.L2GatewayMixin): self.subscribe_callback_notifications() LOG.debug("Initialization complete for NSXv3 driver for " "L2 gateway service plugin.") + self.__core_plugin = None @property def _core_plugin(self): - return directory.get_plugin() + if not self.__core_plugin: + self.__core_plugin = directory.get_plugin() + if self.__core_plugin.is_tvd_plugin(): + self.__core_plugin = self.__core_plugin.get_plugin_by_type( + projectpluginmap.NsxPlugins.NSX_T) + return self.__core_plugin def subscribe_callback_notifications(self): registry.subscribe(self._prevent_l2gw_port_delete, resources.PORT, @@ -289,13 +296,13 @@ class NsxV3Driver(l2gateway_db.L2GatewayMixin): gw_connection['id']) return gw_connection - def delete_l2_gateway_connection(self, context, gw_connection): + def delete_l2_gateway_connection_postcommit(self, context, gw_connection): pass def delete_l2_gateway_connection_precommit(self, context, gw_connection): pass - def delete_l2_gateway_connection_postcommit(self, context, gw_connection): + def delete_l2_gateway_connection(self, context, gw_connection): """Delete a L2 gateway connection.""" conn_mapping = nsx_db.get_l2gw_connection_mapping( session=context.session, @@ -312,7 +319,7 @@ class NsxV3Driver(l2gateway_db.L2GatewayMixin): "backend due to exc: %(exc)s", {'id': bridge_endpoint_id, 'exc': e}) raise l2gw_exc.L2GatewayServiceDriverError( - method='delete_l2_gateway_connection_postcommit') + method='delete_l2_gateway_connection') def prevent_l2gw_port_deletion(self, context, port_id): """Prevent core plugin from deleting L2 gateway port."""