summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-16 21:04:38 +0000
committerGerrit Code Review <review@openstack.org>2017-06-16 21:04:38 +0000
commit4ce39e5b9fbab3f7f4ac91821fafcfc6b0b21a1c (patch)
tree46ce5fde482daf130e5350c062539fefb52bd999
parent1401560a67b80bf73c5dfa6e686068bdecce153c (diff)
parentd35f7e17168c1cf95e7559efb1065322c8541ec8 (diff)
Merge "rehome qos service DriverBase class"
-rw-r--r--neutron_lib/services/qos/__init__.py0
-rw-r--r--neutron_lib/services/qos/base.py162
-rw-r--r--neutron_lib/services/qos/constants.py56
-rw-r--r--neutron_lib/tests/unit/services/qos/__init__.py0
-rw-r--r--neutron_lib/tests/unit/services/qos/test_base.py81
-rw-r--r--releasenotes/notes/rehome-qos-driverbase-f729875b2ad74ce0.yaml6
6 files changed, 305 insertions, 0 deletions
diff --git a/neutron_lib/services/qos/__init__.py b/neutron_lib/services/qos/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/neutron_lib/services/qos/__init__.py
diff --git a/neutron_lib/services/qos/base.py b/neutron_lib/services/qos/base.py
new file mode 100644
index 0000000..4534a82
--- /dev/null
+++ b/neutron_lib/services/qos/base.py
@@ -0,0 +1,162 @@
1# Copyright 2016 Hewlett Packard Enterprise Development Company, LP
2# Copyright 2016 Red Hat Inc.
3#
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17from oslo_log import log as logging
18
19from neutron_lib.api import validators as lib_validators
20from neutron_lib.callbacks import events
21from neutron_lib.callbacks import registry
22from neutron_lib.services.qos import constants
23
24LOG = logging.getLogger(__name__)
25
26
27@registry.has_registry_receivers
28class DriverBase(object):
29
30 def __init__(self, name, vif_types, vnic_types,
31 supported_rules,
32 requires_rpc_notifications=False):
33 """Instantiate a qos driver.
34
35 :param name: driver name.
36 :param vif_types: list of interfaces (VIFs) supported.
37 :param vnic_types: list of vnic types supported.
38 :param supported_rules: dict of supported rules.
39 :param requires_rpc_notifications: indicates if this driver
40 expects rpc push notifications to be sent from the driver.
41 """
42
43 self.name = name
44 self.vif_types = vif_types
45 self.vnic_types = vnic_types
46 self.supported_rules = supported_rules
47 self.requires_rpc_notifications = requires_rpc_notifications
48
49 @registry.receives(constants.QOS_PLUGIN, [events.AFTER_INIT])
50 def _register(self, resource, event, trigger, **kwargs):
51 if self.is_loaded:
52 # trigger is the QosServiceDriverManager
53 trigger.register_driver(self)
54
55 def is_loaded(self):
56 """True if the driver is active for the Neutron Server.
57
58 Implement this property to determine if your driver is actively
59 configured for this Neutron Server deployment.
60 """
61 return True
62
63 def is_vif_type_compatible(self, vif_type):
64 """True if the driver is compatible with the VIF type."""
65 return vif_type in self.vif_types
66
67 def is_vnic_compatible(self, vnic_type):
68 """True if the driver is compatible with the specific VNIC type."""
69 return vnic_type in self.vnic_types
70
71 def is_rule_supported(self, rule):
72 supported_parameters = self.supported_rules.get(rule.rule_type)
73 if not supported_parameters:
74 LOG.debug("Rule type %(rule_type)s is not supported by "
75 "%(driver_name)s",
76 {'rule_type': rule.rule_type,
77 'driver_name': self.name})
78 return False
79 for parameter, validators in supported_parameters.items():
80 parameter_value = rule.get(parameter)
81 for validator_type, validator_data in validators.items():
82 validator_function = lib_validators.get_validator(
83 validator_type)
84 validate_result = validator_function(parameter_value,
85 validator_data)
86 # NOTE(slaweq): validator functions returns None if data is
87 # valid or string with reason why data is not valid
88 if validate_result:
89 LOG.debug("Parameter %(parameter)s=%(value)s in "
90 "rule type %(rule_type)s is not "
91 "supported by %(driver_name)s. "
92 "Validate result: %(validate_result)s",
93 {'parameter': parameter,
94 'value': parameter_value,
95 'rule_type': rule.rule_type,
96 'driver_name': self.name,
97 'validate_result': validate_result})
98 return False
99 return True
100
101 def create_policy(self, context, policy):
102 """Create policy invocation.
103
104 This method can be implemented by the specific driver subclass
105 to update the backend where necessary with the specific policy
106 information.
107
108 :param context: current running context information
109 :param policy: a QoSPolicy object being created, which will have no
110 rules.
111 """
112
113 def create_policy_precommit(self, context, policy):
114 """Create policy precommit.
115
116 This method can be implemented by the specific driver subclass
117 to handle the precommit event of a policy that is being created.
118
119 :param context: current running context information
120 :param policy: a QoSPolicy object being created, which will have no
121 rules.
122 """
123
124 def update_policy(self, context, policy):
125 """Update policy invocation.
126
127 This method can be implemented by the specific driver subclass
128 to update the backend where necessary.
129
130 :param context: current running context information
131 :param policy: a QoSPolicy object being updated.
132 """
133
134 def update_policy_precommit(self, context, policy):
135 """Update policy precommit.
136
137 This method can be implemented by the specific driver subclass
138 to handle update precommit event of a policy that is being updated.
139
140 :param context: current running context information
141 :param policy: a QoSPolicy object being updated.
142 """
143
144 def delete_policy(self, context, policy):
145 """Delete policy invocation.
146
147 This method can be implemented by the specific driver subclass
148 to delete the backend policy where necessary.
149
150 :param context: current running context information
151 :param policy: a QoSPolicy object being deleted
152 """
153
154 def delete_policy_precommit(self, context, policy):
155 """Delete policy precommit.
156
157 This method can be implemented by the specific driver subclass
158 to handle delete precommit event of a policy that is being deleted.
159
160 :param context: current running context information
161 :param policy: a QoSPolicy object being deleted
162 """
diff --git a/neutron_lib/services/qos/constants.py b/neutron_lib/services/qos/constants.py
new file mode 100644
index 0000000..e4a14cf
--- /dev/null
+++ b/neutron_lib/services/qos/constants.py
@@ -0,0 +1,56 @@
1# Copyright (c) 2015 Red Hat Inc.
2# All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16RULE_TYPE_BANDWIDTH_LIMIT = 'bandwidth_limit'
17RULE_TYPE_DSCP_MARKING = 'dscp_marking'
18RULE_TYPE_MINIMUM_BANDWIDTH = 'minimum_bandwidth'
19VALID_RULE_TYPES = [RULE_TYPE_BANDWIDTH_LIMIT,
20 RULE_TYPE_DSCP_MARKING,
21 RULE_TYPE_MINIMUM_BANDWIDTH,
22 ]
23
24# Names of rules' attributes
25MAX_KBPS = "max_kbps"
26MAX_BURST = "max_burst_kbps"
27MIN_KBPS = "min_kbps"
28DIRECTION = "direction"
29DSCP_MARK = "dscp_mark"
30
31QOS_POLICY_ID = 'qos_policy_id'
32
33QOS_PLUGIN = 'qos_plugin'
34
35# NOTE(slaweq): Value used to calculate burst value for egress bandwidth limit
36# if burst is not given by user. In such case burst value will be calculated
37# as 80% of bw_limit to ensure that at least limits for TCP traffic will work
38# fine.
39DEFAULT_BURST_RATE = 0.8
40
41# Method names for QoSDriver
42PRECOMMIT_POSTFIX = '_precommit'
43CREATE_POLICY = 'create_policy'
44CREATE_POLICY_PRECOMMIT = CREATE_POLICY + PRECOMMIT_POSTFIX
45UPDATE_POLICY = 'update_policy'
46UPDATE_POLICY_PRECOMMIT = UPDATE_POLICY + PRECOMMIT_POSTFIX
47DELETE_POLICY = 'delete_policy'
48DELETE_POLICY_PRECOMMIT = DELETE_POLICY + PRECOMMIT_POSTFIX
49
50QOS_CALL_METHODS = (
51 CREATE_POLICY,
52 CREATE_POLICY_PRECOMMIT,
53 UPDATE_POLICY,
54 UPDATE_POLICY_PRECOMMIT,
55 DELETE_POLICY,
56 DELETE_POLICY_PRECOMMIT, )
diff --git a/neutron_lib/tests/unit/services/qos/__init__.py b/neutron_lib/tests/unit/services/qos/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/neutron_lib/tests/unit/services/qos/__init__.py
diff --git a/neutron_lib/tests/unit/services/qos/test_base.py b/neutron_lib/tests/unit/services/qos/test_base.py
new file mode 100644
index 0000000..82e9e5c
--- /dev/null
+++ b/neutron_lib/tests/unit/services/qos/test_base.py
@@ -0,0 +1,81 @@
1# All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import mock
16
17from neutron_lib.api.definitions import portbindings
18from neutron_lib.services.qos import base as qos_base
19from neutron_lib.services.qos import constants as qos_consts
20from neutron_lib.tests import _base
21
22
23SUPPORTED_RULES = {
24 qos_consts.RULE_TYPE_MINIMUM_BANDWIDTH: {
25 "min_kbps": {'type:values': None},
26 'direction': {'type:values': ['egress']}
27 }
28}
29
30
31def _make_rule(rule_type='fake-rule-type', params=None):
32 mock_rule = mock.MagicMock()
33 mock_rule.rule_type = rule_type
34 params = params or {}
35 mock_rule.get = params.get
36 return mock_rule
37
38
39def _make_driver(name='fake-driver',
40 vif_types=[portbindings.VIF_TYPE_OVS],
41 vnic_types=[portbindings.VNIC_NORMAL],
42 supported_rules=SUPPORTED_RULES,
43 requires_rpc_notifications=False):
44 return qos_base.DriverBase(
45 name, vif_types, vnic_types, supported_rules,
46 requires_rpc_notifications=requires_rpc_notifications)
47
48
49class TestDriverBase(_base.BaseTestCase):
50
51 def test_is_loaded(self):
52 self.assertTrue(_make_driver().is_loaded())
53
54 def test_is_vif_type_compatible(self):
55 self.assertTrue(
56 _make_driver().is_vif_type_compatible(
57 portbindings.VIF_TYPE_OVS))
58 self.assertFalse(
59 _make_driver().is_vif_type_compatible(
60 portbindings.VIF_TYPE_BRIDGE))
61
62 def test_is_vnic_compatible(self):
63 self.assertTrue(
64 _make_driver().is_vnic_compatible(portbindings.VNIC_NORMAL))
65 self.assertFalse(
66 _make_driver().is_vnic_compatible(portbindings.VNIC_BAREMETAL))
67
68 def test_is_rule_supported_with_unsupported_rule(self):
69 self.assertFalse(_make_driver().is_rule_supported(_make_rule()))
70
71 def test_is_rule_supported(self):
72 self.assertTrue(
73 _make_driver().is_rule_supported(
74 _make_rule(
75 rule_type=qos_consts.RULE_TYPE_MINIMUM_BANDWIDTH,
76 params={'min_kbps': None, 'direction': 'egress'})))
77 self.assertFalse(
78 _make_driver().is_rule_supported(
79 _make_rule(
80 rule_type=qos_consts.RULE_TYPE_MINIMUM_BANDWIDTH,
81 params={'min_kbps': None, 'direction': 'ingress'})))
diff --git a/releasenotes/notes/rehome-qos-driverbase-f729875b2ad74ce0.yaml b/releasenotes/notes/rehome-qos-driverbase-f729875b2ad74ce0.yaml
new file mode 100644
index 0000000..1bbf9b0
--- /dev/null
+++ b/releasenotes/notes/rehome-qos-driverbase-f729875b2ad74ce0.yaml
@@ -0,0 +1,6 @@
1---
2features:
3 - The ``DriverBase`` class from ``neutron.services.qos.drivers.base`` is now
4 available in the ``neutron_lib.services.qos.base`` module.
5 - The constants defined in ``neutron.services.qos.qos_consts`` are now
6 available in ``neutron_lib.services.qos.constants``.