summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-19 18:05:34 +0000
committerGerrit Code Review <review@openstack.org>2017-06-19 18:05:34 +0000
commit38e1a0c8cb3cd580eec961a6ac8b443f04a6fc57 (patch)
tree46914ea304456cf270b53173406dda9f3814acf8
parent7b3539c421166ac5150d534d60ccbcb842daa634 (diff)
parent25f67e66ae92c989fda5d4e6c5db44eef5b178d2 (diff)
Merge "NSX|V3: Use QoS precommit callback to validate rules"
-rw-r--r--vmware_nsx/services/qos/nsx_v3/driver.py6
-rw-r--r--vmware_nsx/services/qos/nsx_v3/utils.py24
-rw-r--r--vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py69
3 files changed, 38 insertions, 61 deletions
diff --git a/vmware_nsx/services/qos/nsx_v3/driver.py b/vmware_nsx/services/qos/nsx_v3/driver.py
index 1c3478d..2af7b33 100644
--- a/vmware_nsx/services/qos/nsx_v3/driver.py
+++ b/vmware_nsx/services/qos/nsx_v3/driver.py
@@ -85,6 +85,12 @@ class NSXv3QosDriver(base.DriverBase):
85 def delete_policy(self, context, policy): 85 def delete_policy(self, context, policy):
86 self.handler.delete_policy(context, policy.id) 86 self.handler.delete_policy(context, policy.id)
87 87
88 def update_policy_precommit(self, context, policy):
89 """Validate rules values, before creation"""
90 if (hasattr(policy, "rules")):
91 for rule in policy["rules"]:
92 self.handler.validate_policy_rule(context, policy.id, rule)
93
88 94
89def register(): 95def register():
90 """Register the NSX-V3 QoS driver.""" 96 """Register the NSX-V3 QoS driver."""
diff --git a/vmware_nsx/services/qos/nsx_v3/utils.py b/vmware_nsx/services/qos/nsx_v3/utils.py
index f54b25f..4959de8 100644
--- a/vmware_nsx/services/qos/nsx_v3/utils.py
+++ b/vmware_nsx/services/qos/nsx_v3/utils.py
@@ -17,7 +17,9 @@
17from oslo_config import cfg 17from oslo_config import cfg
18from oslo_log import log as logging 18from oslo_log import log as logging
19 19
20from neutron.services.qos import qos_consts
20from neutron_lib.api import validators 21from neutron_lib.api import validators
22from neutron_lib import exceptions as n_exc
21from neutron_lib.plugins import directory 23from neutron_lib.plugins import directory
22 24
23from vmware_nsx._i18n import _ 25from vmware_nsx._i18n import _
@@ -95,18 +97,18 @@ class QosNotificationsHandler(object):
95 # Validate the max bandwidth value minimum value 97 # Validate the max bandwidth value minimum value
96 # (max value is above what neutron allows so no need to check it) 98 # (max value is above what neutron allows so no need to check it)
97 if (bw_rule.max_kbps < MAX_KBPS_MIN_VALUE): 99 if (bw_rule.max_kbps < MAX_KBPS_MIN_VALUE):
98 LOG.warning("Invalid input for max_kbps. " 100 msg = (_("Invalid input for max_kbps. "
99 "The minimal legal value is %s", 101 "The minimal legal value is %s") % MAX_KBPS_MIN_VALUE)
100 MAX_KBPS_MIN_VALUE) 102 LOG.error(msg)
101 bw_rule.max_kbps = MAX_KBPS_MIN_VALUE 103 raise n_exc.InvalidInput(error_message=msg)
102 104
103 # validate the burst size value max value 105 # validate the burst size value max value
104 # (max value is 0, and neutron already validates this) 106 # (max value is 0, and neutron already validates this)
105 if (bw_rule.max_burst_kbps > MAX_BURST_MAX_VALUE): 107 if (bw_rule.max_burst_kbps > MAX_BURST_MAX_VALUE):
106 LOG.warning("Invalid input for burst_size. " 108 msg = (_("Invalid input for burst_size. "
107 "The maximal legal value is %s", 109 "The maximal legal value is %s") % MAX_BURST_MAX_VALUE)
108 MAX_BURST_MAX_VALUE) 110 LOG.error(msg)
109 bw_rule.max_burst_kbps = MAX_BURST_MAX_VALUE 111 raise n_exc.InvalidInput(error_message=msg)
110 112
111 def _get_bw_values_from_rule(self, bw_rule): 113 def _get_bw_values_from_rule(self, bw_rule):
112 """Translate the neutron bandwidth_limit_rule values, into the 114 """Translate the neutron bandwidth_limit_rule values, into the
@@ -115,7 +117,6 @@ class QosNotificationsHandler(object):
115 """ 117 """
116 if bw_rule: 118 if bw_rule:
117 shaping_enabled = True 119 shaping_enabled = True
118 self._validate_bw_values(bw_rule)
119 120
120 # translate kbps -> bytes 121 # translate kbps -> bytes
121 burst_size = int(bw_rule.max_burst_kbps) * 128 122 burst_size = int(bw_rule.max_burst_kbps) * 128
@@ -168,3 +169,8 @@ class QosNotificationsHandler(object):
168 average_bandwidth=average_bw, 169 average_bandwidth=average_bw,
169 qos_marking=qos_marking, 170 qos_marking=qos_marking,
170 dscp=dscp) 171 dscp=dscp)
172
173 def validate_policy_rule(self, context, policy_id, rule):
174 """Raise an exception if the rule values are not supported"""
175 if rule.rule_type == qos_consts.RULE_TYPE_BANDWIDTH_LIMIT:
176 self._validate_bw_values(rule)
diff --git a/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py b/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py
index 2a7813f..88ee7bd 100644
--- a/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py
+++ b/vmware_nsx/tests/unit/services/qos/test_nsxv3_notification.py
@@ -18,6 +18,7 @@ from neutron_lib import context
18from oslo_config import cfg 18from oslo_config import cfg
19from oslo_utils import uuidutils 19from oslo_utils import uuidutils
20 20
21from neutron.common import exceptions
21from neutron.objects import base as base_object 22from neutron.objects import base as base_object
22from neutron.objects.qos import policy as policy_object 23from neutron.objects.qos import policy as policy_object
23from neutron.objects.qos import rule as rule_object 24from neutron.objects.qos import rule as rule_object
@@ -173,8 +174,7 @@ class TestQosNsxV3Notification(base.BaseQosTestCase,
173 174
174 @mock.patch.object(policy_object.QosPolicy, '_reload_rules') 175 @mock.patch.object(policy_object.QosPolicy, '_reload_rules')
175 def test_bw_rule_create_profile_minimal_val(self, *mocks): 176 def test_bw_rule_create_profile_minimal_val(self, *mocks):
176 # test the switch profile update when a QoS rule is created 177 # test driver precommit with an invalid limit value
177 # with an invalid limit value
178 bad_limit = qos_utils.MAX_KBPS_MIN_VALUE - 1 178 bad_limit = qos_utils.MAX_KBPS_MIN_VALUE - 1
179 rule_data = { 179 rule_data = {
180 'bandwidth_limit_rule': {'id': uuidutils.generate_uuid(), 180 'bandwidth_limit_rule': {'id': uuidutils.generate_uuid(),
@@ -189,35 +189,17 @@ class TestQosNsxV3Notification(base.BaseQosTestCase,
189 # add a rule to the policy 189 # add a rule to the policy
190 setattr(_policy, "rules", [rule]) 190 setattr(_policy, "rules", [rule])
191 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_object', 191 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_object',
192 return_value=_policy): 192 return_value=_policy),\
193 with mock.patch( 193 mock.patch('neutron.objects.db.api.update_object',
194 'vmware_nsxlib.v3.core_resources.NsxLibQosSwitchingProfile.' 194 return_value=rule_data):
195 'update_shaping' 195 self.assertRaises(
196 ) as update_profile: 196 exceptions.DriverCallError,
197 with mock.patch('neutron.objects.db.api.update_object', 197 self.qos_plugin.update_policy_bandwidth_limit_rule,
198 return_value=rule_data): 198 self.ctxt, rule.id, _policy.id, rule_data)
199 self.qos_plugin.update_policy_bandwidth_limit_rule(
200 self.ctxt, rule.id, _policy.id, rule_data)
201
202 # validate the data on the profile
203 rule_dict = rule_data['bandwidth_limit_rule']
204 expected_bw = qos_utils.MAX_KBPS_MIN_VALUE / 1024
205 expected_burst = rule_dict['max_burst_kbps'] * 128
206 expected_peak = int(expected_bw * self.peak_bw_multiplier)
207 update_profile.assert_called_once_with(
208 self.fake_profile_id,
209 average_bandwidth=expected_bw,
210 burst_size=expected_burst,
211 peak_bandwidth=expected_peak,
212 shaping_enabled=True,
213 dscp=0,
214 qos_marking='trusted'
215 )
216 199
217 @mock.patch.object(policy_object.QosPolicy, '_reload_rules') 200 @mock.patch.object(policy_object.QosPolicy, '_reload_rules')
218 def test_bw_rule_create_profile_maximal_val(self, *mocks): 201 def test_bw_rule_create_profile_maximal_val(self, *mocks):
219 # test the switch profile update when a QoS rule is created 202 # test driver precommit with an invalid burst value
220 # with an invalid burst value
221 bad_burst = qos_utils.MAX_BURST_MAX_VALUE + 1 203 bad_burst = qos_utils.MAX_BURST_MAX_VALUE + 1
222 rule_data = { 204 rule_data = {
223 'bandwidth_limit_rule': {'id': uuidutils.generate_uuid(), 205 'bandwidth_limit_rule': {'id': uuidutils.generate_uuid(),
@@ -232,30 +214,13 @@ class TestQosNsxV3Notification(base.BaseQosTestCase,
232 # add a rule to the policy 214 # add a rule to the policy
233 setattr(_policy, "rules", [rule]) 215 setattr(_policy, "rules", [rule])
234 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_object', 216 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_object',
235 return_value=_policy): 217 return_value=_policy),\
236 with mock.patch( 218 mock.patch('neutron.objects.db.api.update_object',
237 'vmware_nsxlib.v3.core_resources.NsxLibQosSwitchingProfile.' 219 return_value=rule_data):
238 'update_shaping' 220 self.assertRaises(
239 ) as update_profile: 221 exceptions.DriverCallError,
240 with mock.patch('neutron.objects.db.api.update_object', 222 self.qos_plugin.update_policy_bandwidth_limit_rule,
241 return_value=rule_data): 223 self.ctxt, rule.id, _policy.id, rule_data)
242 self.qos_plugin.update_policy_bandwidth_limit_rule(
243 self.ctxt, rule.id, _policy.id, rule_data)
244
245 # validate the data on the profile
246 rule_dict = rule_data['bandwidth_limit_rule']
247 expected_burst = qos_utils.MAX_BURST_MAX_VALUE * 128
248 expected_bw = int(rule_dict['max_kbps'] / 1024)
249 expected_peak = int(expected_bw * self.peak_bw_multiplier)
250 update_profile.assert_called_once_with(
251 self.fake_profile_id,
252 average_bandwidth=expected_bw,
253 burst_size=expected_burst,
254 peak_bandwidth=expected_peak,
255 shaping_enabled=True,
256 dscp=0,
257 qos_marking='trusted'
258 )
259 224
260 @mock.patch.object(policy_object.QosPolicy, '_reload_rules') 225 @mock.patch.object(policy_object.QosPolicy, '_reload_rules')
261 def test_dscp_rule_create_profile(self, *mocks): 226 def test_dscp_rule_create_profile(self, *mocks):