Add TC Device

This commit is contained in:
Ofer Ben-Yacov 2017-01-15 15:26:38 +02:00
parent 4332f7eb76
commit ec91e081f9
8 changed files with 198 additions and 9 deletions

View File

@ -34,3 +34,4 @@ neutron.db.alembic_migrations =
wan-qos = wan_qos.db.migration:alembic_migrations
neutronclient.extension =
wan_qos = wan_qos.wanqos_client._wanqos
wan_tc_device = wan_qos.wanqos_client._wantcdevice

View File

@ -15,4 +15,7 @@
WANTC = 'WANTC'
WAN_TC = 'wan_tc'
WAN_TC_PATH = 'wan-tcs'
WAN_TC_PATH = 'wan-tcs'
WAN_TC_DEVICE = 'wan_tc_device'
WAN_TC_DEVICE_PATH = 'wan-tc-devices'

View File

@ -32,19 +32,24 @@ import sqlalchemy as sa
def upgrade():
op.create_table('wan_tc_class',
sa.Column('id', sa.String(length=36), nullable=False),
sa.Column('networks_id', sa.String(length=36),
sa.Column('parent_class', sa.String(length=36), nullable=False),
sa.Column('device_id', sa.String(length=36),
nullable=False),
sa.Column('project_id', sa.String(length=36),
nullable=False),
sa.Column('network_id', sa.String(length=36),
nullable=False),
sa.Column('class_ext_id', sa.Integer()),
sa.Column('min_rate',
sa.String(length=15), nullable=False),
sa.Column('min_rate', sa.String(length=15)),
sa.Column('max_rate', sa.String(length=15)),
sa.PrimaryKeyConstraint('id')
)
op.create_foreign_key(
'fk_wan_tc_class_networks',
'wan_tc_class', 'networks',
['networks_id'], ['id'],
['network_id'], ['id'],
)
op.create_table('wan_tc_selector',

View File

@ -32,12 +32,14 @@ class WanTcDb(object):
with context.session.begin(subtransactions=True):
if not device:
LOG.debug('New device connected: %s' % host_info)
now = timeutils.utcnow()
wan_tc_device = models.WanTcDevice(
id=uuidutils.generate_uuid(),
host=host_info['host'],
lan_port=host_info['lan_port'],
wan_port=host_info['wan_port'],
uptime=timeutils.utcnow()
uptime=now,
heartbeat_timestamp=now
)
context.session.add(wan_tc_device)
else:
@ -70,11 +72,29 @@ class WanTcDb(object):
def _device_to_dict(self, device):
device_dict = {
'id': device.id,
'host': device.host,
'lan_port': device.lan_port,
'wan_port': device.wan_port,
'uptime': device.uptime,
'heartbeat': device.heartbeat
'last_seen': device.heartbeat_timestamp
}
return device_dict
def delete_wan_tc_device(self, context, id):
device = context.session.query(models.WanTcDevice).filter_by(
id=id
).first()
if device:
with context.session.begin(subtransactions=True):
context.session.delete(device)
else:
LOG.error('Trying to delete none existing device. id=%s' % id)
def get_device(self, context, id):
device = context.session.query(models.WanTcDevice).filter_by(
id=id
).first()
if device:
return self._device_to_dict(device)

View File

@ -33,7 +33,7 @@ RESOURCE_ATTRIBUTE_MAP = {
'network_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True},
'tenant_id': {'allow_post': True, 'allow_put': False,
'project_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'required_by_policy': True,
'is_visible': True}

View File

@ -0,0 +1,99 @@
# Copyright 2016 Huawei corp.
# 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.api import extensions
from neutron.api.v2 import resource_helper
from wan_qos.common import constants
RESOURCE_ATTRIBUTE_MAP = {
constants.WAN_TC_DEVICE_PATH: {
'id': {'allow_post': False, 'allow_put': False,
'is_visible': True},
'host': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'lan_port': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True, 'default': ''},
'wan_port': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True},
'uptime': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True},
'last_seen': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'is_visible': True}
},
}
class Wantcdevice(extensions.ExtensionDescriptor):
@classmethod
def get_name(cls):
return "WAN Traffic Control device"
@classmethod
def get_alias(cls):
return "wan-tc-device"
@classmethod
def get_description(cls):
return "Device for limiting traffic on WAN links"
@classmethod
def get_updated(cls):
return "2017-01-15T00:00:00-00:00"
@classmethod
def get_resources(cls):
"""Returns Ext Resources."""
mem_actions = {}
plural_mappings = resource_helper.build_plural_mappings(
{}, RESOURCE_ATTRIBUTE_MAP)
resources = resource_helper.build_resource_info(plural_mappings,
RESOURCE_ATTRIBUTE_MAP,
constants.WANTC,
action_map=mem_actions,
register_quota=True,
translate_name=True)
return resources
def get_extended_resources(self, version):
if version == "2.0":
return RESOURCE_ATTRIBUTE_MAP
else:
return {}
class WanTcDevicePluginBase(object):
@abc.abstractmethod
def get_wan_tc_device(self, context, id, fields=None):
pass
@abc.abstractmethod
def get_wan_tc_devices(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
pass
@abc.abstractmethod
def delete_wan_tc_device(self, context, id):
pass

View File

@ -27,6 +27,7 @@ from wan_qos.common import api
from wan_qos.common import constants
from wan_qos.common import topics
from wan_qos.extensions import wanqos
from wan_qos.extensions import wantcdevice
from wan_qos.db import wan_qos_db
LOG = logging.getLogger(__name__)
@ -48,8 +49,9 @@ class PluginRpcCallback(object):
self.plugin.db.device_heartbeat(context, host)
class WanQosPlugin(wanqos.WanQosPluginBase):
supported_extension_aliases = ["wan-tc"]
class WanQosPlugin(wanqos.WanQosPluginBase,
wantcdevice.WanTcDevicePluginBase):
supported_extension_aliases = ['wan-tc', 'wan-tc-device']
def __init__(self):
self.db = wan_qos_db.WanTcDb()
@ -64,6 +66,17 @@ class WanQosPlugin(wanqos.WanQosPluginBase):
fanout=False)
self.conn.consume_in_threads()
def delete_wan_tc_device(self, context, id):
self.db.delete_wan_tc_device(context, id)
def get_wan_tc_device(self, context, id, fields=None):
return self.db.get_device(context, id)
def get_wan_tc_devices(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
return self.db.get_all_devices(context)
def get_plugin_type(self):
"""Get type of the plugin."""
return constants.WANTC

View File

@ -0,0 +1,48 @@
# Copyright 2016 Huawei corp.
# 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 neutronclient.common import extension
from wan_qos.common import constants
def args2body(self, parsed_args):
body = {constants.WAN_TC_DEVICE: {}, }
return body
class WanTcDevice(extension.NeutronClientExtension):
resource = constants.WAN_TC_DEVICE
resource_plural = '%ss' % constants.WAN_TC_DEVICE
path = constants.WAN_TC_DEVICE_PATH
object_path = '/%s' % path
resource_path = '/%s/%%s' % path
versions = ['2.0']
class WanTcDeviceShow(extension.ClientExtensionShow, WanTcDevice):
shell_command = 'wan-tc-device-show'
class WanTcDeviceList(extension.ClientExtensionList, WanTcDevice):
shell_command = 'wan-tc-device-list'
list_columns = ['id', 'host', 'lan_port', 'wan_port',
'uptime', 'last_seen']
pagination_support = True
sorting_support = True
class WanTcDeviceDelete(extension.ClientExtensionDelete, WanTcDevice):
shell_command = 'wan-tc-device-delete'