1 # Copyright (c) 2015 Red Hat Inc.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
21 from neutron.api import extensions
22 from neutron.api.v2 import attributes as attr
23 from neutron.api.v2 import base
24 from neutron.api.v2 import resource_helper
25 from neutron import manager
26 from neutron.plugins.common import constants
27 from neutron.services.qos import qos_consts
28 from neutron.services import service_base
33 QOS_RULE_COMMON_FIELDS = {
34 'id': {'allow_post': False, 'allow_put': False,
35 'validate': {'type:uuid': None},
38 'tenant_id': {'allow_post': True, 'allow_put': False,
39 'required_by_policy': True,
43 RESOURCE_ATTRIBUTE_MAP = {
45 'id': {'allow_post': False, 'allow_put': False,
46 'validate': {'type:uuid': None},
47 'is_visible': True, 'primary_key': True},
48 'name': {'allow_post': True, 'allow_put': True,
49 'is_visible': True, 'default': '',
50 'validate': {'type:string': None}},
51 'description': {'allow_post': True, 'allow_put': True,
52 'is_visible': True, 'default': '',
53 'validate': {'type:string': None}},
54 'shared': {'allow_post': True, 'allow_put': True,
55 'is_visible': True, 'default': False,
56 'convert_to': attr.convert_to_boolean},
57 'tenant_id': {'allow_post': True, 'allow_put': False,
58 'required_by_policy': True,
60 'rules': {'allow_post': False, 'allow_put': False, 'is_visible': True},
63 'type': {'allow_post': False, 'allow_put': False,
68 SUB_RESOURCE_ATTRIBUTE_MAP = {
69 'bandwidth_limit_rules': {
70 'parent': {'collection_name': 'policies',
71 'member_name': 'policy'},
72 'parameters': dict(QOS_RULE_COMMON_FIELDS,
74 'allow_post': True, 'allow_put': True,
75 'is_visible': True, 'default': None,
76 'validate': {'type:non_negative': None}},
78 'allow_post': True, 'allow_put': True,
79 'is_visible': True, 'default': 0,
80 'validate': {'type:non_negative': None}}})
84 EXTENDED_ATTRIBUTES_2_0 = {
85 'ports': {qos_consts.QOS_POLICY_ID: {
90 'validate': {'type:uuid_or_none': None}}},
91 'networks': {qos_consts.QOS_POLICY_ID: {
96 'validate': {'type:uuid_or_none': None}}}}
99 class Qos(extensions.ExtensionDescriptor):
100 """Quality of service API extension."""
111 def get_description(cls):
112 return "The Quality of Service extension."
115 def get_updated(cls):
116 return "2015-06-08T10:00:00-00:00"
119 def get_plugin_interface(cls):
123 def get_resources(cls):
124 """Returns Ext Resources."""
125 special_mappings = {'policies': 'policy'}
126 plural_mappings = resource_helper.build_plural_mappings(
127 special_mappings, itertools.chain(RESOURCE_ATTRIBUTE_MAP,
128 SUB_RESOURCE_ATTRIBUTE_MAP))
129 attr.PLURALS.update(plural_mappings)
131 resources = resource_helper.build_resource_info(
133 RESOURCE_ATTRIBUTE_MAP,
138 plugin = manager.NeutronManager.get_service_plugins()[constants.QOS]
139 for collection_name in SUB_RESOURCE_ATTRIBUTE_MAP:
140 resource_name = collection_name[:-1]
141 parent = SUB_RESOURCE_ATTRIBUTE_MAP[collection_name].get('parent')
142 params = SUB_RESOURCE_ATTRIBUTE_MAP[collection_name].get(
145 controller = base.create_resource(collection_name, resource_name,
149 allow_pagination=True,
152 resource = extensions.ResourceExtension(
155 path_prefix=QOS_PREFIX,
157 resources.append(resource)
161 def update_attributes_map(self, attributes, extension_attrs_map=None):
162 super(Qos, self).update_attributes_map(
163 attributes, extension_attrs_map=RESOURCE_ATTRIBUTE_MAP)
165 def get_extended_resources(self, version):
167 return dict(list(EXTENDED_ATTRIBUTES_2_0.items()) +
168 list(RESOURCE_ATTRIBUTE_MAP.items()))
173 @six.add_metaclass(abc.ABCMeta)
174 class QoSPluginBase(service_base.ServicePluginBase):
176 path_prefix = QOS_PREFIX
178 def get_plugin_description(self):
179 return "QoS Service Plugin for ports and networks"
181 def get_plugin_type(self):
185 def get_policy(self, context, policy_id, fields=None):
189 def get_policies(self, context, filters=None, fields=None,
190 sorts=None, limit=None, marker=None,
195 def create_policy(self, context, policy):
199 def update_policy(self, context, policy_id, policy):
203 def delete_policy(self, context, policy_id):
207 def get_policy_bandwidth_limit_rule(self, context, rule_id,
208 policy_id, fields=None):
212 def get_policy_bandwidth_limit_rules(self, context, policy_id,
213 filters=None, fields=None,
214 sorts=None, limit=None,
215 marker=None, page_reverse=False):
219 def create_policy_bandwidth_limit_rule(self, context, policy_id,
220 bandwidth_limit_rule):
224 def update_policy_bandwidth_limit_rule(self, context, rule_id, policy_id,
225 bandwidth_limit_rule):
229 def delete_policy_bandwidth_limit_rule(self, context, rule_id, policy_id):
233 def get_rule_types(self, context, filters=None, fields=None,
234 sorts=None, limit=None,
235 marker=None, page_reverse=False):