]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
[qos] policy: add methods to interact with policy bindings
authorIhar Hrachyshka <ihrachys@redhat.com>
Thu, 2 Jul 2015 08:14:17 +0000 (11:14 +0300)
committerIhar Hrachyshka <ihrachys@redhat.com>
Thu, 2 Jul 2015 08:36:55 +0000 (11:36 +0300)
Detachment is not supported in this patch.

blueprint quantum-qos-api

Change-Id: I66f87b99241a25d39d08c124bae3779c872bc567

neutron/db/api.py
neutron/db/qos/api.py [new file with mode: 0644]
neutron/objects/qos/policy.py

index b74f56e7b6407dde8a3d37af90018de025d4c073..6de77700059999ec8849632b043b87f1a8226083 100644 (file)
@@ -91,6 +91,15 @@ class convert_db_exception_to_retry(object):
 
 
 # Common database operation implementations
+# TODO(QoS): consider handling multiple objects found, or no objects at all
+# TODO(QoS): consider changing the name and making it public, officially
+def _find_object(context, model, *kwargs):
+    with context.session.begin(subtransactions=True):
+        return (common_db_mixin.model_query(context, model)
+                .filter_by(**kwargs)
+                .first())
+
+
 def get_object(context, model, id):
     with context.session.begin(subtransactions=True):
         return (common_db_mixin.model_query(context, model)
diff --git a/neutron/db/qos/api.py b/neutron/db/qos/api.py
new file mode 100644 (file)
index 0000000..632c57e
--- /dev/null
@@ -0,0 +1,27 @@
+#    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.db.qos import models
+
+
+def create_policy_network_binding(context, policy_id, network_id):
+    with context.session.begin(subtransactions=True):
+        db_obj = models.QosNetworkPolicyBinding(policy_id=policy_id,
+                                                network_id=network_id)
+        context.session.add(db_obj)
+
+
+def create_policy_port_binding(context, policy_id, port_id):
+    with context.session.begin(subtransactions=True):
+        db_obj = models.QosPortPolicyBinding(policy_id=policy_id,
+                                             port_id=port_id)
+        context.session.add(db_obj)
index 2352673cc820ae3dec7ffe13665490cf968b0f3e..21605a555acff5d843848d3bbd9ad82ce9c5ee54 100644 (file)
@@ -16,6 +16,8 @@
 from oslo_versionedobjects import base as obj_base
 from oslo_versionedobjects import fields as obj_fields
 
+from neutron.db import api as db_api
+from neutron.db.qos import api as qos_db_api
 from neutron.db.qos import models as qos_db_model
 from neutron.objects import base
 
@@ -29,6 +31,9 @@ class QosPolicy(base.NeutronObject):
 
     db_model = qos_db_model.QosPolicy
 
+    port_binding_model = qos_db_model.QosPortPolicyBinding
+    network_binding_model = qos_db_model.QosNetworkPolicyBinding
+
     fields = {
         'id': obj_fields.UUIDField(),
         'tenant_id': obj_fields.UUIDField(),
@@ -36,3 +41,37 @@ class QosPolicy(base.NeutronObject):
         'description': obj_fields.StringField(),
         'shared': obj_fields.BooleanField()
     }
+
+    @classmethod
+    def _get_object_policy(cls, context, model, **kwargs):
+        # TODO(QoS): we should make sure we use public functions
+        binding_db_obj = db_api._find_object(context, model, **kwargs)
+        # TODO(QoS): rethink handling missing binding case
+        if binding_db_obj:
+            return cls.get_by_id(context, binding_db_obj['policy_id'])
+
+    @classmethod
+    def get_network_policy(cls, context, network_id):
+        return cls._get_object_policy(context, cls.network_binding_model,
+                                      network_id=network_id)
+
+    @classmethod
+    def get_port_policy(cls, context, port_id):
+        return cls._get_object_policy(context, cls.port_binding_model,
+                                      port_id=port_id)
+
+    def attach_network(self, network_id):
+        qos_db_api.create_policy_network_binding(policy_id=self.id,
+                                                 network_id=network_id)
+
+    def attach_port(self, port_id):
+        qos_db_api.create_policy_port_binding(policy_id=self.id,
+                                              port_id=port_id)
+
+    def detach_network(self, network_id):
+        # TODO(QoS): implement it, in the next life maybe
+        pass
+
+    def detach_port(self, port_id):
+        # TODO(QoS): implement it, in the next life maybe
+        pass