Modify taas extensions to use neutron-lib API def

Change-Id: I2920a6e375c3dbd0521ae2c48a4c91e5ab613629
Depends-On: https://review.opendev.org/c/openstack/neutron-lib/+/824926
This commit is contained in:
zitptan 2022-01-05 14:58:03 +05:30 committed by Lajos Katona
parent 53d353bfbc
commit fedca42dbd
11 changed files with 148 additions and 240 deletions

View File

@ -0,0 +1,32 @@
# Create a tap flow
# POST /taas/tap_flows
#"create_tap_flow": "rule:admin_or_owner"
# Update a tap flow
# PUT /taas/tap_flows/{id}
#"update_tap_flow": "rule:admin_or_owner"
# Show a tap flow
# GET /taas/tap_flows/{id}
#"get_tap_flow": "rule:admin_or_owner"
# Delete a tap flow
# DELETE /taas/tap_flows/{id}
#"delete_tap_flow": "rule:admin_or_owner"
# Create a tap service
# POST /taas/tap_services
#"create_tap_service": "rule:admin_or_owner"
# Updates a tap service
# PUT /taas/tap_services/{id}
#"update_tap_service": "rule:admin_or_owner"
# Show a tap service
# GET /taas/tap_services/{id}
#"get_tap_service": "rule:admin_or_owner"
# Delete a tap service
# DELETE /taas/tap_services/{id}
#"delete_tap_service": "rule:admin_or_owner"

View File

@ -0,0 +1,45 @@
# 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 oslo_config import cfg
from neutron_taas._i18n import _
taas_quota_opts = [
cfg.IntOpt('quota_tap_service',
default=1,
help=_('Number of Tap Service instances allowed per tenant')),
cfg.IntOpt('quota_tap_flow',
default=10,
help=_('Number of Tap flows allowed per tenant'))
]
TaasOpts = [
cfg.IntOpt(
'vlan_range_start',
default=3900,
help=_("Starting range of TAAS VLAN IDs")),
cfg.IntOpt(
'vlan_range_end',
default=4000,
help=_("End range of TAAS VLAN IDs")),
]
def register():
cfg.CONF.register_opts(taas_quota_opts, 'QUOTAS')
cfg.CONF.register_opts(TaasOpts, 'taas')

View File

@ -24,12 +24,14 @@ from neutron_lib.db import api as db_api
from neutron_lib.db import model_base
from neutron_lib.db import model_query
from neutron_lib.db import utils as db_utils
from neutron_lib.exceptions import taas as taas_exc
from neutron_lib.plugins import directory
from neutron_taas.extensions import taas
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import uuidutils
from neutron_taas.extensions import taas as taas_extension
LOG = logging.getLogger(__name__)
@ -82,7 +84,7 @@ class TapIdAssociation(model_base.BASEV2):
primaryjoin='TapService.id==TapIdAssociation.tap_service_id')
class Taas_db_Mixin(taas.TaasPluginBase):
class Taas_db_Mixin(taas_extension.TaasPluginBase):
def _core_plugin(self):
return directory.get_plugin()
@ -93,7 +95,7 @@ class Taas_db_Mixin(taas.TaasPluginBase):
try:
return model_query.get_by_id(context, TapService, id)
except exc.NoResultFound:
raise taas.TapServiceNotFound(tap_id=id)
raise taas_exc.TapServiceNotFound(tap_id=id)
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_READER
@ -103,14 +105,14 @@ class Taas_db_Mixin(taas.TaasPluginBase):
return query.filter(TapIdAssociation.tap_service_id ==
tap_service_id).one()
except exc.NoResultFound:
raise taas.TapServiceNotFound(tap_id=tap_service_id)
raise taas_exc.TapServiceNotFound(tap_id=tap_service_id)
@db_api.CONTEXT_READER
def _get_tap_flow(self, context, id):
try:
return model_query.get_by_id(context, TapFlow, id)
except exc.NoResultFound:
raise taas.TapFlowNotFound(flow_id=id)
raise taas_exc.TapFlowNotFound(flow_id=id)
def _make_tap_service_dict(self, tap_service, fields=None):
res = {'id': tap_service['id'],
@ -188,7 +190,7 @@ class Taas_db_Mixin(taas.TaasPluginBase):
query.update({"tap_service_id": tap_service_id})
return query
# not found
raise taas.TapServiceLimitReached()
raise taas_exc.TapServiceLimitReached()
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
@ -235,7 +237,7 @@ class Taas_db_Mixin(taas.TaasPluginBase):
count = context.session.query(TapService).filter_by(id=id).delete()
if not count:
raise taas.TapServiceNotFound(tap_id=id)
raise taas_exc.TapServiceNotFound(tap_id=id)
@db_api.CONTEXT_WRITER
def delete_tap_flow(self, context, id):
@ -243,7 +245,7 @@ class Taas_db_Mixin(taas.TaasPluginBase):
count = context.session.query(TapFlow).filter_by(id=id).delete()
if not count:
raise taas.TapFlowNotFound(flow_id=id)
raise taas_exc.TapFlowNotFound(flow_id=id)
def get_tap_service(self, context, id, fields=None):
LOG.debug("get_tap_service() called")

View File

@ -15,187 +15,53 @@
import abc
from neutron_lib.api.definitions import taas as taas_api_def
from neutron_lib.api import extensions
from neutron_lib import exceptions as qexception
from neutron_lib.services import base as service_base
from neutron.api.v2 import resource_helper
from neutron_taas._i18n import _
from neutron_taas.common import constants
from oslo_config import cfg
from neutron_taas.common import config
import six
# TaaS exception handling classes
config.register()
class TapServiceNotFound(qexception.NotFound):
message = _("Tap Service %(tap_id)s does not exist")
class Taas(extensions.APIExtensionDescriptor):
class TapFlowNotFound(qexception.NotFound):
message = _("Tap Flow %(flow_id)s does not exist")
class InvalidDestinationPort(qexception.NotFound):
message = _("Destination Port %(port)s does not exist")
class InvalidSourcePort(qexception.NotFound):
message = _("Source Port %(port)s does not exist")
class PortDoesNotBelongToTenant(qexception.NotAuthorized):
message = _("The specified port does not belong to the tenant")
class TapServiceNotBelongToTenant(qexception.NotAuthorized):
message = _("Specified Tap Service does not belong to the tenant")
class TapServiceLimitReached(qexception.OverQuota):
message = _("Reached the maximum quota for Tap Services")
direction_enum = ['IN', 'OUT', 'BOTH']
'''
Resource Attribute Map:
Note:
'tap_services' data model refers to the Tap Service created.
port_id specifies destination port to which the mirrored data is sent.
'''
RESOURCE_ATTRIBUTE_MAP = {
'tap_services': {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:uuid': None}, 'is_visible': True,
'primary_key': True},
'tenant_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'required_by_policy': True, 'is_visible': True},
'name': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'description': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'port_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:uuid': None},
'is_visible': True},
'status': {'allow_post': False, 'allow_put': False,
'is_visible': True}
},
'tap_flows': {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:uuid': None}, 'is_visible': True,
'primary_key': True},
'tenant_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'required_by_policy': True, 'is_visible': True},
'name': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'description': {'allow_post': True, 'allow_put': True,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'tap_service_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:uuid': None},
'required_by_policy': True, 'is_visible': True},
'source_port': {'allow_post': True, 'allow_put': False,
'validate': {'type:uuid': None},
'required_by_policy': True, 'is_visible': True},
'direction': {'allow_post': True, 'allow_put': False,
'validate': {'type:values': direction_enum},
'is_visible': True},
'status': {'allow_post': False, 'allow_put': False,
'is_visible': True}
}
}
taas_quota_opts = [
cfg.IntOpt('quota_tap_service',
default=1,
help=_('Number of Tap Service instances allowed per tenant')),
cfg.IntOpt('quota_tap_flow',
default=10,
help=_('Number of Tap flows allowed per tenant'))
]
cfg.CONF.register_opts(taas_quota_opts, 'QUOTAS')
TaasOpts = [
cfg.IntOpt(
'vlan_range_start',
default=3900,
help=_("Starting range of TAAS VLAN IDs")),
cfg.IntOpt(
'vlan_range_end',
default=4000,
help=_("End range of TAAS VLAN IDs")),
]
cfg.CONF.register_opts(TaasOpts, 'taas')
class Taas(extensions.ExtensionDescriptor):
@classmethod
def get_name(cls):
return "Neutron Tap as a Service"
@classmethod
def get_alias(cls):
return "taas"
@classmethod
def get_description(cls):
return "Neutron Tap as a Service Extension."
@classmethod
def get_updated(cls):
return "2015-01-14T10:00:00-00:00"
@classmethod
def get_plugin_interface(cls):
return TaasPluginBase
api_definition = taas_api_def
@classmethod
def get_resources(cls):
"""Returns Ext Resources."""
plural_mappings = resource_helper.build_plural_mappings(
{}, RESOURCE_ATTRIBUTE_MAP)
{}, taas_api_def.RESOURCE_ATTRIBUTE_MAP)
return resource_helper.build_resource_info(plural_mappings,
RESOURCE_ATTRIBUTE_MAP,
constants.TAAS,
translate_name=False,
allow_bulk=True)
resources = resource_helper.build_resource_info(
plural_mappings,
taas_api_def.RESOURCE_ATTRIBUTE_MAP,
taas_api_def.ALIAS,
translate_name=False,
allow_bulk=True)
def update_attributes_map(self, attributes):
super(Taas, self).update_attributes_map(
attributes, extension_attrs_map=RESOURCE_ATTRIBUTE_MAP)
return resources
def get_extended_resources(self, version):
if version == "2.0":
return RESOURCE_ATTRIBUTE_MAP
else:
return {}
@classmethod
def get_plugin_interface(cls):
return TaasPluginBase
@six.add_metaclass(abc.ABCMeta)
class TaasPluginBase(service_base.ServicePluginBase):
def get_plugin_description(self):
return "Tap Service Plugin"
return taas_api_def.DESCRIPTION
@classmethod
def get_plugin_type(cls):
return constants.TAAS
return taas_api_def.ALIAS
@abc.abstractmethod
def create_tap_service(self, context, tap_service):

View File

@ -13,47 +13,10 @@
# under the License.
#
from neutron_lib.api import extensions
# Regex for a comma-seperate list of integer values (VLANs)
# For ex. "9,18,27-36,45-54" or "0-4095" or "9,18,27,36"
RANGE_REGEX = r"^([0-9]+(-[0-9]+)?)(,([0-9]+(-[0-9]+)?))*$"
EXTENDED_ATTRIBUTES_2_0 = {
'tap_flows': {
'vlan_filter': {'allow_post': True, 'allow_put': False,
'validate': {'type:regex_or_none': RANGE_REGEX},
'is_visible': True, 'default': None}
}
}
from neutron_lib.api.definitions import vlan_filter
from neutron_lib.api import extensions as api_extensions
class Vlan_filter(extensions.ExtensionDescriptor):
"""Extension class supporting vlan_filter for tap_flows."""
@classmethod
def get_name(cls):
return "TaaS Vlan Filter Extension"
@classmethod
def get_alias(cls):
return 'taas-vlan-filter'
@classmethod
def get_description(cls):
return "Vlan Filter support for Tap Flows."
@classmethod
def get_updated(cls):
return "2019-01-23T00:00:00-00:00"
def get_extended_resources(self, version):
if version == "2.0":
return EXTENDED_ATTRIBUTES_2_0
return {}
def get_required_extensions(self):
return ["taas"]
def get_optional_extensions(self):
return []
class Vlan_filter(api_extensions.APIExtensionDescriptor):
"""Extension class supporting taas VLAN filtering."""
api_definition = vlan_filter

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_taas.extensions import taas
from neutron_lib.exceptions import taas
from oslo_log import log

View File

@ -23,10 +23,10 @@ from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from neutron_lib.exceptions import taas as taas_exc
from neutron_taas.common import constants as taas_consts
from neutron_taas.db import taas_db
from neutron_taas.extensions import taas as taas_ex
from neutron_taas.services.taas.service_drivers import (service_driver_context
as sd_context)
@ -84,7 +84,7 @@ class TaasPlugin(taas_db.Taas_db_Mixin):
# Check if the port is owned by the tenant.
if port['tenant_id'] != tenant_id:
raise taas_ex.PortDoesNotBelongToTenant()
raise taas_exc.PortDoesNotBelongToTenant()
# Extract the host where the port is located
host = port['binding:host_id']
@ -155,7 +155,7 @@ class TaasPlugin(taas_db.Taas_db_Mixin):
ts_tenant_id = ts['tenant_id']
if tenant_id != ts_tenant_id:
raise taas_ex.TapServiceNotBelongToTenant()
raise taas_exc.TapServiceNotBelongToTenant()
# create tap flow in the db model
tf = super(TaasPlugin, self).create_tap_flow(context, tap_flow)
@ -213,7 +213,7 @@ class TaasPlugin(taas_db.Taas_db_Mixin):
for t_s in t_s_collection:
try:
self.delete_tap_service(context, t_s['id'])
except taas_ex.TapServiceNotFound:
except taas_exc.TapServiceNotFound:
LOG.debug("Not found tap_service: %s", t_s['id'])
t_f_collection = self.get_tap_flows(
@ -223,5 +223,5 @@ class TaasPlugin(taas_db.Taas_db_Mixin):
for t_f in t_f_collection:
try:
self.delete_tap_flow(context, t_f['id'])
except taas_ex.TapFlowNotFound:
except taas_exc.TapFlowNotFound:
LOG.debug("Not found tap_flow: %s", t_f['id'])

View File

@ -16,11 +16,12 @@
from neutron.tests.unit import testlib_api
from neutron_lib import context
from neutron_lib.exceptions import taas as taas_exc
from oslo_utils import importutils
from oslo_utils import uuidutils
from neutron_taas.db import taas_db
from neutron_taas.extensions import taas
DB_PLUGIN_KLAAS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
@ -152,7 +153,7 @@ class TaaSDbTestCase(testlib_api.SqlTestCase):
data = self._get_tap_service_data()
result = self._create_tap_service(data)
self._delete_tap_service(result['id'])
self.assertRaises(taas.TapServiceNotFound,
self.assertRaises(taas_exc.TapServiceNotFound,
self._get_tap_service, result['id'])
def test_tap_flow_get(self):
@ -225,5 +226,5 @@ class TaaSDbTestCase(testlib_api.SqlTestCase):
name=tf_name)
tf = self._create_tap_flow(tf_data)
self._delete_tap_flow(tf['id'])
self.assertRaises(taas.TapFlowNotFound,
self.assertRaises(taas_exc.TapFlowNotFound,
self._get_tap_flow, tf['id'])

View File

@ -18,10 +18,12 @@ from webob import exc
from oslo_utils import uuidutils
from neutron.api import extensions
from neutron.tests.unit.api.v2 import test_base as test_api_v2
from neutron.tests.unit.extensions import base as test_api_v2_extension
from neutron.tests.unit.extensions import base as test_extensions_base
from neutron_lib.api.definitions import taas as taas_api
from neutron_taas.extensions import taas as taas_ext
from neutron_taas import extensions as taas_extensions
_uuid = uuidutils.generate_uuid
_get_path = test_api_v2._get_path
@ -30,18 +32,22 @@ TAP_SERVICE_PATH = 'taas/tap_services'
TAP_FLOW_PATH = 'taas/tap_flows'
class TaasExtensionTestCase(test_api_v2_extension.ExtensionTestCase):
fmt = 'json'
class TaasExtensionTestCase(test_extensions_base.ExtensionTestCase):
def setUp(self):
extensions.append_api_extensions_path(taas_extensions.__path__)
super(TaasExtensionTestCase, self).setUp()
plural_mappings = {'tap_service': 'tap_services',
'tap_flow': 'tap_flows'}
self.setup_extension(
'neutron_taas.extensions.taas.TaasPluginBase',
'TAAS',
taas_ext.Taas,
'%s.%s' % (taas_extensions.taas.TaasPluginBase.__module__,
taas_extensions.taas.TaasPluginBase.__name__),
taas_api.ALIAS,
taas_extensions.taas.Taas,
'taas',
plural_mappings={}
)
plural_mappings=plural_mappings,
translate_resource_name=False)
self.instance = self.plugin.return_value
def test_create_tap_service(self):
tenant_id = _uuid()
@ -56,7 +62,7 @@ class TaasExtensionTestCase(test_api_v2_extension.ExtensionTestCase):
expected_ret_val = copy.copy(data['tap_service'])
expected_ret_val.update({'id': _uuid()})
instance = self.plugin.return_value
instance.create_tap_service.return_value = expected_ret_val
self.instance.create_tap_service.return_value = expected_ret_val
res = self.api.post(_get_path(TAP_SERVICE_PATH, fmt=self.fmt),
self.serialize(data),

View File

@ -17,13 +17,13 @@ import copy
from unittest import mock
from neutron_taas.common import constants as taas_consts
from neutron_taas.extensions import taas as taas_ext
from neutron_taas.extensions import vlan_filter as vlan_filter_ext
from neutron_taas.tests.unit.extensions import test_taas as test_taas_ext
from oslo_utils import uuidutils
from webob import exc
from neutron.tests.unit.api.v2 import test_base as test_api_v2
from neutron_lib.api.definitions import taas as taas_api_def
from neutron_lib.api.definitions import vlan_filter as vlan_filter_ext
import webtest
@ -32,19 +32,12 @@ _get_path = test_api_v2._get_path
class VlanFilterExtensionTestCase(test_taas_ext.TaasExtensionTestCase):
def setUp(self):
super(test_taas_ext.TaasExtensionTestCase, self).setUp()
attr_map = taas_ext.RESOURCE_ATTRIBUTE_MAP
def setUp(self):
super(VlanFilterExtensionTestCase, self).setUp()
attr_map = taas_api_def.RESOURCE_ATTRIBUTE_MAP
attr_map['tap_flows'].update(
vlan_filter_ext.EXTENDED_ATTRIBUTES_2_0['tap_flows'])
self.setup_extension(
'neutron_taas.extensions.taas.TaasPluginBase',
'TAAS',
taas_ext.Taas,
'taas',
plural_mappings={}
)
vlan_filter_ext.RESOURCE_ATTRIBUTE_MAP['tap_flows'])
def _get_expected_tap_flow(self, data):
ret = super(VlanFilterExtensionTestCase,

View File

@ -20,6 +20,7 @@ from unittest import mock
from neutron_lib import constants
from neutron_lib import context
from neutron_lib.exceptions import taas as taas_exc
from neutron_lib import rpc as n_rpc
from neutron_lib.utils import net as n_utils
from oslo_config import cfg
@ -28,7 +29,6 @@ from oslo_utils import uuidutils
from neutron.tests.unit import testlib_api
import neutron_taas.db.taas_db # noqa
import neutron_taas.extensions.taas as taas_ext
from neutron_taas.services.taas.service_drivers import taas_agent_api
from neutron_taas.services.taas.service_drivers import taas_rpc
from neutron_taas.services.taas import taas_plugin
@ -164,7 +164,7 @@ class TestTaasPlugin(testlib_api.SqlTestCase):
self._context, ts_id_2)
self.assertEqual(set([1, 2]), set([tap_id_assoc_1['taas_id'],
tap_id_assoc_2['taas_id']]))
with testtools.ExpectedException(taas_ext.TapServiceLimitReached):
with testtools.ExpectedException(taas_exc.TapServiceLimitReached):
self._plugin.create_tap_id_association(
self._context,
ts_4['id']
@ -182,7 +182,7 @@ class TestTaasPlugin(testlib_api.SqlTestCase):
def test_create_tap_service_wrong_tenant_id(self):
self._port_details['tenant_id'] = 'other-tenant'
with testtools.ExpectedException(taas_ext.PortDoesNotBelongToTenant), \
with testtools.ExpectedException(taas_exc.PortDoesNotBelongToTenant), \
self.tap_service():
pass
self.assertEqual([], self.driver.mock_calls)
@ -246,7 +246,7 @@ class TestTaasPlugin(testlib_api.SqlTestCase):
"dummyHost")
def test_delete_tap_service_non_existent(self):
with testtools.ExpectedException(taas_ext.TapServiceNotFound):
with testtools.ExpectedException(taas_exc.TapServiceNotFound):
self._plugin.delete_tap_service(self._context, 'non-existent')
def test_create_tap_flow(self):
@ -256,7 +256,7 @@ class TestTaasPlugin(testlib_api.SqlTestCase):
def test_create_tap_flow_wrong_tenant_id(self):
with self.tap_service() as ts, \
testtools.ExpectedException(
taas_ext.TapServiceNotBelongToTenant), \
taas_exc.TapServiceNotBelongToTenant), \
self.tap_flow(tap_service=ts['id'], tenant_id='other-tenant'):
pass