summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/neutron/qos.py
blob: d767050aee34a9ccba7c408f0ffe9295d1728dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#
#    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 heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine.resources.openstack.neutron import neutron
from heat.engine import support


class QoSPolicy(neutron.NeutronResource):
    """A resource for Neutron QoS Policy.

    This QoS policy can be associated with neutron resources,
    such as port and network, to provide QoS capabilities.

    The default policy usage of this resource is limited to
    administrators only.
    """

    required_service_extension = 'qos'

    entity = 'qos_policy'

    res_info_key = 'policy'

    support_status = support.SupportStatus(version='6.0.0')

    PROPERTIES = (
        NAME, DESCRIPTION, SHARED, TENANT_ID,
    ) = (
        'name', 'description', 'shared', 'tenant_id',
    )

    ATTRIBUTES = (
        RULES_ATTR,
    ) = (
        'rules',
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('The name for the QoS policy.'),
            update_allowed=True
        ),
        DESCRIPTION: properties.Schema(
            properties.Schema.STRING,
            _('The description for the QoS policy.'),
            update_allowed=True
        ),
        SHARED: properties.Schema(
            properties.Schema.BOOLEAN,
            _('Whether this QoS policy should be shared to other tenants.'),
            default=False,
            update_allowed=True
        ),
        TENANT_ID: properties.Schema(
            properties.Schema.STRING,
            _('The owner tenant ID of this QoS policy.')
        ),
    }

    attributes_schema = {
        RULES_ATTR: attributes.Schema(
            _("A list of all rules for the QoS policy."),
            type=attributes.Schema.LIST
        )
    }

    def handle_create(self):
        props = self.prepare_properties(
            self.properties,
            self.physical_resource_name())

        policy = self.client().create_qos_policy({'policy': props})['policy']
        self.resource_id_set(policy['id'])

    def handle_delete(self):
        if self.resource_id is None:
            return

        with self.client_plugin().ignore_not_found:
            self.client().delete_qos_policy(self.resource_id)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if prop_diff:
            self.prepare_update_properties(prop_diff)
            self.client().update_qos_policy(
                self.resource_id,
                {'policy': prop_diff})


class QoSRule(neutron.NeutronResource):
    """A resource for Neutron QoS base rule."""

    required_service_extension = 'qos'

    support_status = support.SupportStatus(version='6.0.0')

    PROPERTIES = (
        POLICY, TENANT_ID,
    ) = (
        'policy', 'tenant_id',
    )

    properties_schema = {
        POLICY: properties.Schema(
            properties.Schema.STRING,
            _('ID or name of the QoS policy.'),
            required=True,
            constraints=[constraints.CustomConstraint('neutron.qos_policy')]
        ),
        TENANT_ID: properties.Schema(
            properties.Schema.STRING,
            _('The owner tenant ID of this rule.')
        ),
    }

    def __init__(self, name, json_snippet, stack):
        super(QoSRule, self).__init__(name, json_snippet, stack)
        self._policy_id = None

    @property
    def policy_id(self):
        if not self._policy_id:
            self._policy_id = self.client_plugin().get_qos_policy_id(
                self.properties[self.POLICY])

        return self._policy_id


class QoSBandwidthLimitRule(QoSRule):
    """A resource for Neutron QoS bandwidth limit rule.

    This rule can be associated with QoS policy, and then the policy
    can be used by neutron port and network, to provide bandwidth limit
    QoS capabilities.

    The default policy usage of this resource is limited to
    administrators only.
    """

    entity = 'bandwidth_limit_rule'

    PROPERTIES = (
        MAX_BANDWIDTH, MAX_BURST_BANDWIDTH, DIRECTION
    ) = (
        'max_kbps', 'max_burst_kbps', 'direction'
    )

    properties_schema = {
        MAX_BANDWIDTH: properties.Schema(
            properties.Schema.INTEGER,
            _('Max bandwidth in kbps.'),
            required=True,
            update_allowed=True,
            constraints=[
                constraints.Range(min=0)
            ]
        ),
        MAX_BURST_BANDWIDTH: properties.Schema(
            properties.Schema.INTEGER,
            _('Max burst bandwidth in kbps.'),
            update_allowed=True,
            constraints=[
                constraints.Range(min=0)
            ],
            default=0
        ),
        DIRECTION: properties.Schema(
            properties.Schema.STRING,
            _('Traffic direction from the point of view of the port.'),
            update_allowed=True,
            constraints=[
                constraints.AllowedValues(['egress', 'ingress']),
            ],
            default='egress',
            support_status=support.SupportStatus(version='13.0.0')
        )
    }

    properties_schema.update(QoSRule.properties_schema)

    def handle_create(self):
        props = self.prepare_properties(self.properties,
                                        self.physical_resource_name())
        props.pop(self.POLICY)

        rule = self.client().create_bandwidth_limit_rule(
            self.policy_id,
            {'bandwidth_limit_rule': props})['bandwidth_limit_rule']

        self.resource_id_set(rule['id'])

    def handle_delete(self):
        if self.resource_id is None:
            return

        with self.client_plugin().ignore_not_found:
            self.client().delete_bandwidth_limit_rule(
                self.resource_id, self.policy_id)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if prop_diff:
            self.client().update_bandwidth_limit_rule(
                self.resource_id,
                self.policy_id,
                {'bandwidth_limit_rule': prop_diff})

    def _res_get_args(self):
        return [self.resource_id, self.policy_id]


class QoSDscpMarkingRule(QoSRule):
    """A resource for Neutron QoS DSCP marking rule.

    This rule can be associated with QoS policy, and then the policy
    can be used by neutron port and network, to provide DSCP marking
    QoS capabilities.

    The default policy usage of this resource is limited to
    administrators only.
    """

    support_status = support.SupportStatus(version='7.0.0')

    entity = 'dscp_marking_rule'

    PROPERTIES = (
        DSCP_MARK,
    ) = (
        'dscp_mark',
    )

    properties_schema = {
        DSCP_MARK: properties.Schema(
            properties.Schema.INTEGER,
            _('DSCP mark between 0 and 56, except 2-6, 42, 44, and 50-54.'),
            required=True,
            update_allowed=True,
            constraints=[
                constraints.AllowedValues([
                    0, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
                    36, 38, 40, 46, 48, 56]
                )
            ]
        )
    }

    properties_schema.update(QoSRule.properties_schema)

    def handle_create(self):
        props = self.prepare_properties(self.properties,
                                        self.physical_resource_name())
        props.pop(self.POLICY)

        rule = self.client().create_dscp_marking_rule(
            self.policy_id,
            {'dscp_marking_rule': props})['dscp_marking_rule']

        self.resource_id_set(rule['id'])

    def handle_delete(self):
        if self.resource_id is None:
            return

        with self.client_plugin().ignore_not_found:
            self.client().delete_dscp_marking_rule(
                self.resource_id, self.policy_id)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if prop_diff:
            self.client().update_dscp_marking_rule(
                self.resource_id,
                self.policy_id,
                {'dscp_marking_rule': prop_diff})

    def _res_get_args(self):
        return [self.resource_id, self.policy_id]


class QoSMinimumBandwidthRule(QoSRule):
    """A resource for guaranteeing bandwidth.

    This rule can be associated with a QoS policy, and then the policy
    can be used by a neutron port to provide guaranteed bandwidth QoS
    capabilities.

    Depending on drivers the guarantee may be enforced on two levels.
    First when a server is placed (scheduled) on physical infrastructure
    and/or second in the data plane of the physical hypervisor. For details
    please see Neutron documentation:

    https://docs.openstack.org/neutron/latest/admin/config-qos-min-bw.html

    The default policy usage of this resource is limited to
    administrators only.
    """

    entity = 'minimum_bandwidth_rule'

    required_service_extension = 'qos-bw-minimum-ingress'

    support_status = support.SupportStatus(
        status=support.SUPPORTED,
        version='14.0.0',
    )

    PROPERTIES = (
        MIN_BANDWIDTH, DIRECTION
    ) = (
        'min_kbps', 'direction'
    )

    properties_schema = {
        MIN_BANDWIDTH: properties.Schema(
            properties.Schema.INTEGER,
            _('Min bandwidth in kbps.'),
            required=True,
            update_allowed=True,
            constraints=[
                constraints.Range(min=0),
            ],
        ),
        DIRECTION: properties.Schema(
            properties.Schema.STRING,
            _('Traffic direction from the point of view of the port.'),
            update_allowed=True,
            constraints=[
                constraints.AllowedValues(['egress', 'ingress']),
            ],
            default='egress',
        ),
    }

    properties_schema.update(QoSRule.properties_schema)

    def handle_create(self):
        props = self.prepare_properties(self.properties,
                                        self.physical_resource_name())
        props.pop(self.POLICY)

        rule = self.client().create_minimum_bandwidth_rule(
            self.policy_id,
            {'minimum_bandwidth_rule': props})['minimum_bandwidth_rule']

        self.resource_id_set(rule['id'])

    def handle_delete(self):
        if self.resource_id is None:
            return

        with self.client_plugin().ignore_not_found:
            self.client().delete_minimum_bandwidth_rule(
                self.resource_id, self.policy_id)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if prop_diff:
            self.client().update_minimum_bandwidth_rule(
                self.resource_id,
                self.policy_id,
                {'minimum_bandwidth_rule': prop_diff})

    def _res_get_args(self):
        return [self.resource_id, self.policy_id]


def resource_mapping():
    return {
        'OS::Neutron::QoSPolicy': QoSPolicy,
        'OS::Neutron::QoSBandwidthLimitRule': QoSBandwidthLimitRule,
        'OS::Neutron::QoSDscpMarkingRule': QoSDscpMarkingRule,
        'OS::Neutron::QoSMinimumBandwidthRule': QoSMinimumBandwidthRule,
    }