Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / extensions / qos.py
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
16 import abc
17 import itertools
18
19 import six
20
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
29
30 QOS_PREFIX = "/qos"
31
32 # Attribute Map
33 QOS_RULE_COMMON_FIELDS = {
34     'id': {'allow_post': False, 'allow_put': False,
35            'validate': {'type:uuid': None},
36            'is_visible': True,
37            'primary_key': True},
38     'tenant_id': {'allow_post': True, 'allow_put': False,
39                   'required_by_policy': True,
40                   'is_visible': True},
41 }
42
43 RESOURCE_ATTRIBUTE_MAP = {
44     'policies': {
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,
59                       'is_visible': True},
60         'rules': {'allow_post': False, 'allow_put': False, 'is_visible': True},
61     },
62     'rule_types': {
63         'type': {'allow_post': False, 'allow_put': False,
64                  'is_visible': True}
65     }
66 }
67
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,
73                            **{'max_kbps': {
74                                   'allow_post': True, 'allow_put': True,
75                                   'is_visible': True, 'default': None,
76                                   'validate': {'type:non_negative': None}},
77                               'max_burst_kbps': {
78                                   'allow_post': True, 'allow_put': True,
79                                   'is_visible': True, 'default': 0,
80                                   'validate': {'type:non_negative': None}}})
81     }
82 }
83
84 EXTENDED_ATTRIBUTES_2_0 = {
85     'ports': {qos_consts.QOS_POLICY_ID: {
86                                     'allow_post': True,
87                                     'allow_put': True,
88                                     'is_visible': True,
89                                     'default': None,
90                                     'validate': {'type:uuid_or_none': None}}},
91     'networks': {qos_consts.QOS_POLICY_ID: {
92                                     'allow_post': True,
93                                     'allow_put': True,
94                                     'is_visible': True,
95                                     'default': None,
96                                     'validate': {'type:uuid_or_none': None}}}}
97
98
99 class Qos(extensions.ExtensionDescriptor):
100     """Quality of service API extension."""
101
102     @classmethod
103     def get_name(cls):
104         return "qos"
105
106     @classmethod
107     def get_alias(cls):
108         return "qos"
109
110     @classmethod
111     def get_description(cls):
112         return "The Quality of Service extension."
113
114     @classmethod
115     def get_updated(cls):
116         return "2015-06-08T10:00:00-00:00"
117
118     @classmethod
119     def get_plugin_interface(cls):
120         return QoSPluginBase
121
122     @classmethod
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)
130
131         resources = resource_helper.build_resource_info(
132                 plural_mappings,
133                 RESOURCE_ATTRIBUTE_MAP,
134                 constants.QOS,
135                 translate_name=True,
136                 allow_bulk=True)
137
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(
143                 'parameters')
144
145             controller = base.create_resource(collection_name, resource_name,
146                                               plugin, params,
147                                               allow_bulk=True,
148                                               parent=parent,
149                                               allow_pagination=True,
150                                               allow_sorting=True)
151
152             resource = extensions.ResourceExtension(
153                 collection_name,
154                 controller, parent,
155                 path_prefix=QOS_PREFIX,
156                 attr_map=params)
157             resources.append(resource)
158
159         return resources
160
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)
164
165     def get_extended_resources(self, version):
166         if version == "2.0":
167             return dict(list(EXTENDED_ATTRIBUTES_2_0.items()) +
168                         list(RESOURCE_ATTRIBUTE_MAP.items()))
169         else:
170             return {}
171
172
173 @six.add_metaclass(abc.ABCMeta)
174 class QoSPluginBase(service_base.ServicePluginBase):
175
176     path_prefix = QOS_PREFIX
177
178     def get_plugin_description(self):
179         return "QoS Service Plugin for ports and networks"
180
181     def get_plugin_type(self):
182         return constants.QOS
183
184     @abc.abstractmethod
185     def get_policy(self, context, policy_id, fields=None):
186         pass
187
188     @abc.abstractmethod
189     def get_policies(self, context, filters=None, fields=None,
190                      sorts=None, limit=None, marker=None,
191                      page_reverse=False):
192         pass
193
194     @abc.abstractmethod
195     def create_policy(self, context, policy):
196         pass
197
198     @abc.abstractmethod
199     def update_policy(self, context, policy_id, policy):
200         pass
201
202     @abc.abstractmethod
203     def delete_policy(self, context, policy_id):
204         pass
205
206     @abc.abstractmethod
207     def get_policy_bandwidth_limit_rule(self, context, rule_id,
208                                         policy_id, fields=None):
209         pass
210
211     @abc.abstractmethod
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):
216         pass
217
218     @abc.abstractmethod
219     def create_policy_bandwidth_limit_rule(self, context, policy_id,
220                                            bandwidth_limit_rule):
221         pass
222
223     @abc.abstractmethod
224     def update_policy_bandwidth_limit_rule(self, context, rule_id, policy_id,
225                                            bandwidth_limit_rule):
226         pass
227
228     @abc.abstractmethod
229     def delete_policy_bandwidth_limit_rule(self, context, rule_id, policy_id):
230         pass
231
232     @abc.abstractmethod
233     def get_rule_types(self, context, filters=None, fields=None,
234                        sorts=None, limit=None,
235                        marker=None, page_reverse=False):
236         pass