From b167ec6d4f5577c54a1a02e6a835b875a6e2150a Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Thu, 2 Jul 2015 11:14:17 +0300 Subject: [PATCH] [qos] policy: add methods to interact with policy bindings Detachment is not supported in this patch. blueprint quantum-qos-api Change-Id: I66f87b99241a25d39d08c124bae3779c872bc567 --- neutron/db/api.py | 9 ++++++++ neutron/db/qos/api.py | 27 ++++++++++++++++++++++++ neutron/objects/qos/policy.py | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 neutron/db/qos/api.py diff --git a/neutron/db/api.py b/neutron/db/api.py index b74f56e7b..6de777000 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -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 index 000000000..632c57e9e --- /dev/null +++ b/neutron/db/qos/api.py @@ -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) diff --git a/neutron/objects/qos/policy.py b/neutron/objects/qos/policy.py index 2352673cc..21605a555 100644 --- a/neutron/objects/qos/policy.py +++ b/neutron/objects/qos/policy.py @@ -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 -- 2.45.2