From 41f73e232738a0b4a596abe2fc5d90db5db74263 Mon Sep 17 00:00:00 2001
From: Ofer Ben-Yacov <ofer.benyacov@gmail.com>
Date: Wed, 4 Jan 2017 11:08:30 +0200
Subject: [PATCH] DB models

---
 etc/wan_qos_agent.ini            |  2 +-
 wan_qos/common/constants.py      |  6 ++--
 wan_qos/db/models/__init__.py    |  0
 wan_qos/db/models/wan_tc.py      | 48 ++++++++++++++++++++++++++++++++
 wan_qos/db/wan_qos_db.py         | 24 ++++++++++++++++
 wan_qos/extensions/wanqos.py     |  6 ++--
 wan_qos/services/plugin.py       | 26 +++++++++++++++--
 wan_qos/wanqos_client/_wanqos.py | 30 ++++++++++----------
 8 files changed, 118 insertions(+), 24 deletions(-)
 create mode 100644 wan_qos/db/models/__init__.py
 create mode 100644 wan_qos/db/models/wan_tc.py
 create mode 100644 wan_qos/db/wan_qos_db.py

diff --git a/etc/wan_qos_agent.ini b/etc/wan_qos_agent.ini
index 5288e45..66cdbf0 100644
--- a/etc/wan_qos_agent.ini
+++ b/etc/wan_qos_agent.ini
@@ -2,7 +2,7 @@
 # Show debugging output in log (sets DEBUG log level output)
 # debug = False
 
-[WANQOS]
+[WANTC]
 lan_port_name = 'enp1s0f0'
 lan_max_rate = '100mbit'
 wan_port_name = 'enp1s0f1'
diff --git a/wan_qos/common/constants.py b/wan_qos/common/constants.py
index c8ba3b5..2b27f26 100644
--- a/wan_qos/common/constants.py
+++ b/wan_qos/common/constants.py
@@ -13,6 +13,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-WANQOS = 'WANQOS'
-WAN_QOS = 'wan_tc'
-WAN_QOS_PATH = 'wan-tcs'
\ No newline at end of file
+WANTC = 'WANTC'
+WAN_TC = 'wan_tc'
+WAN_TC_PATH = 'wan-tcs'
\ No newline at end of file
diff --git a/wan_qos/db/models/__init__.py b/wan_qos/db/models/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/wan_qos/db/models/wan_tc.py b/wan_qos/db/models/wan_tc.py
new file mode 100644
index 0000000..8393707
--- /dev/null
+++ b/wan_qos/db/models/wan_tc.py
@@ -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 neutron_lib.db import model_base
+import sqlalchemy as sa
+
+
+class WanTcClass(model_base.BASEV2,
+                  model_base.HasId, model_base.HasProject):
+    __tablename__ = 'wan_tc_class'
+    class_ext_id = sa.Column(sa.Integer)
+    parent_class = sa.Column(sa.String(36),
+                             sa.ForeignKey('wan_tc_class.id',
+                                           ondelete='CASCADE'),
+                             nullable=True)
+    network_id = sa.Column(sa.String(36),
+                           sa.ForeignKey('networks.id',
+                                         ondelete='CASCADE'),
+                           nullable=False,
+                           unique=True,
+                           primary_key=True)
+    min_rate = sa.Column(sa.String(15), nullable=False)
+    max_rate = sa.Column(sa.String(15))
+
+
+class WanTcSelector(model_base.BASEV2,
+                     model_base.HasId, model_base.HasProject):
+    __tablename__ = 'wan_tc_selector'
+    class_id = sa.Column(sa.String(36),
+                         sa.ForeignKey('wan_tc_class.id',
+                                       ondelete='CASCADE'),
+                         nullable=False,
+                         primary_key=True)
+    protocol = sa.Column(sa.String(15))
+    match = sa.Column(sa.String(15))
+
diff --git a/wan_qos/db/wan_qos_db.py b/wan_qos/db/wan_qos_db.py
new file mode 100644
index 0000000..4770ad6
--- /dev/null
+++ b/wan_qos/db/wan_qos_db.py
@@ -0,0 +1,24 @@
+# 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 oslo_utils import uuidutils
+
+from wan_qos.db.models import wan_tc as model
+from wan_qos.common import constants
+
+
+class WanTcDb():
+    def create_wan_tc_class(self, context, wan_qos_class):
+        pass
diff --git a/wan_qos/extensions/wanqos.py b/wan_qos/extensions/wanqos.py
index 228bd62..7f49fb8 100644
--- a/wan_qos/extensions/wanqos.py
+++ b/wan_qos/extensions/wanqos.py
@@ -21,7 +21,7 @@ from neutron.api.v2 import resource_helper
 from wan_qos.common import constants
 
 RESOURCE_ATTRIBUTE_MAP = {
-    constants.WAN_QOS_PATH: {
+    constants.WAN_TC_PATH: {
         'id': {'allow_post': False, 'allow_put': False,
                'is_visible': True},
         'max_rate': {'allow_post': True, 'allow_put': False,
@@ -45,7 +45,7 @@ class Wanqos(extensions.ExtensionDescriptor):
 
     @classmethod
     def get_name(cls):
-        return "WAN QoS"
+        return "WAN Traffic Control"
 
     @classmethod
     def get_alias(cls):
@@ -68,7 +68,7 @@ class Wanqos(extensions.ExtensionDescriptor):
             {}, RESOURCE_ATTRIBUTE_MAP)
         resources = resource_helper.build_resource_info(plural_mappings,
                                                         RESOURCE_ATTRIBUTE_MAP,
-                                                        constants.WANQOS,
+                                                        constants.WANTC,
                                                         action_map=mem_actions,
                                                         register_quota=True,
                                                         translate_name=True)
diff --git a/wan_qos/services/plugin.py b/wan_qos/services/plugin.py
index 4178617..a5919ac 100644
--- a/wan_qos/services/plugin.py
+++ b/wan_qos/services/plugin.py
@@ -15,10 +15,12 @@
 
 from neutron.common import rpc as n_rpc
 from neutron.db import agents_db
+from neutron_lib import exceptions
 
 from oslo_config import cfg
 from oslo_log import log as logging
 from oslo_utils import importutils
+
 import oslo_messaging as messaging
 
 from wan_qos.common import api
@@ -60,7 +62,7 @@ class WanQosPlugin(wanqos.WanQosPluginBase):
 
     def get_plugin_type(self):
         """Get type of the plugin."""
-        return constants.WANQOS
+        return constants.WANTC
 
     def get_plugin_description(self):
         """Get description of the plugin."""
@@ -84,6 +86,26 @@ class WanQosPlugin(wanqos.WanQosPluginBase):
         pass
         # self.agent_rpc.create_wan_qos(context, wan_qos)
 
-    def agent_up_notification(self, host):
+        tenant_id = self._get_tenant_id_for_create(context, wan_qos_class)
+
+
+
+    @staticmethod
+    def _get_tenant_id_for_create(self, context, resource):
+        """Get tenant id for creation of resources."""
+        if context.is_admin and 'tenant_id' in resource:
+            tenant_id = resource['tenant_id']
+        elif ('tenant_id' in resource and
+                      resource['tenant_id'] != context.tenant_id):
+            reason = 'Cannot create resource for another tenant'
+            raise exceptions.AdminRequired(reason=reason)
+        else:
+            tenant_id = context.tenant_id
+        return tenant_id
+
+
+
+
+def agent_up_notification(self, host):
         LOG.debug('agent %s is up' % host)
         return 'OK'
diff --git a/wan_qos/wanqos_client/_wanqos.py b/wan_qos/wanqos_client/_wanqos.py
index 6b159c8..eeb212a 100644
--- a/wan_qos/wanqos_client/_wanqos.py
+++ b/wan_qos/wanqos_client/_wanqos.py
@@ -19,35 +19,35 @@ from wan_qos.common import constants
 
 
 def args2body(self, parsed_args):
-    body = {'wan_tc': {}, }
+    body = {constants.WAN_TC: {}, }
     return body
 
 
-class WanQos(extension.NeutronClientExtension):
-    resource = constants.WAN_QOS
-    resource_plural = '%ss' % constants.WAN_QOS
-    path = constants.WAN_QOS_PATH
+class WanTc(extension.NeutronClientExtension):
+    resource = constants.WAN_TC
+    resource_plural = '%ss' % constants.WAN_TC
+    path = constants.WAN_TC_PATH
     object_path = '/%s' % path
     resource_path = '/%s/%%s' % path
     versions = ['2.0']
 
 
-class WanQosShow(extension.ClientExtensionShow, WanQos):
+class WanTcShow(extension.ClientExtensionShow, WanTc):
 
-    shell_command = 'wan-qos-show'
+    shell_command = 'wan-tc-show'
 
 
-class WanQosList(extension.ClientExtensionList, WanQos):
+class WanTcList(extension.ClientExtensionList, WanTc):
 
-    shell_command = 'wan-qos-list'
+    shell_command = 'wan-tc-list'
     list_columns = ['id', 'name', 'network']
     pagination_support = True
     sorting_support = True
 
 
-class WanQosCreate(extension.ClientExtensionCreate, WanQos):
+class WanTcCreate(extension.ClientExtensionCreate, WanTc):
 
-    shell_command = 'wan-qos-create'
+    shell_command = 'wan-tc-create'
 
     def add_known_arguments(self, parser):
         pass
@@ -59,14 +59,14 @@ class WanQosCreate(extension.ClientExtensionCreate, WanQos):
         return body
 
 
-class WanQosDelete(extension.ClientExtensionDelete, WanQos):
+class WanTcDelete(extension.ClientExtensionDelete, WanTc):
 
-    shell_command = 'wan-qos-delete'
+    shell_command = 'wan-tc-delete'
 
 
-class WanQosUpdate(extension.ClientExtensionUpdate, WanQos):
+class WanTcUpdate(extension.ClientExtensionUpdate, WanTc):
 
-    shell_command = 'wan-qos-update'
+    shell_command = 'wan-tc-update'
 
     def add_known_arguments(self, parser):
         pass