Debian Codegraph @

neutron_lib/api/extensions.py

raw
1 # Copyright 2011 OpenStack Foundation.
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
18 from neutron_lib._i18n import _
19 from neutron_lib import constants
20
21
22 _UNSET = constants.Sentinel()
23
24
25 def is_extension_supported(plugin, alias):
26 """Validate that the extension is supported.
27
28 :param plugin: The plugin class.
29 :param alias: The alias to check.
30 :returns: True if the alias is supported else False.
31 """
32 return alias in getattr(plugin, "supported_extension_aliases", [])
33
34
35 class ExtensionDescriptor(metaclass=abc.ABCMeta):
36 """Base class that defines the contract for extensions."""
37
38 @abc.abstractmethod
39 def get_name(self):
40 """The name of the extension.
41
42 e.g. 'Fox In Socks'
43 """
44
45 @abc.abstractmethod
46 def get_alias(self):
47 """The alias for the extension.
48
49 e.g. 'FOXNSOX'
50 """
51
52 @abc.abstractmethod
53 def get_description(self):
54 """Friendly description for the extension.
55
56 e.g. 'The Fox In Socks Extension'
57 """
58
59 @abc.abstractmethod
60 def get_updated(self):
61 """The timestamp when the extension was last updated.
62
63 e.g. '2011-01-22T13:25:27-06:00'
64 """
65 # NOTE(justinsb): Not sure of the purpose of this is, vs the XML NS
66
67 def get_resources(self):
68 """List of extensions.ResourceExtension extension objects.
69
70 Resources define new nouns, and are accessible through URLs.
71 """
72 return []
73
74 def get_actions(self):
75 """List of extensions.ActionExtension extension objects.
76
77 Actions are verbs callable from the API.
78 """
79 return []
80
81 def get_request_extensions(self):
82 """List of extensions.RequestExtension extension objects.
83
84 Request extensions are used to handle custom request data.
85 """
86 return []
87
88 def get_extended_resources(self, version):
89 """Retrieve extended resources or attributes for core resources.
90
91 Extended attributes are implemented by a core plugin similarly
92 to the attributes defined in the core, and can appear in
93 request and response messages. Their names are scoped with the
94 extension's prefix. The core API version is passed to this
95 function, which must return a
96 map[<resource_name>][<attribute_name>][<attribute_property>]
97 specifying the extended resource attribute properties required
98 by that API version.
99 Extension can add resources and their attr definitions too.
100 The returned map can be integrated into RESOURCE_ATTRIBUTE_MAP.
101 """
102 return {}
103
104 def get_plugin_interface(self):
105 """Returns an abstract class which defines contract for the plugin.
106
107 The abstract class should inherit from
108 neutron_lib.services.base.ServicePluginBase.
109 Methods in this abstract class should be decorated as abstractmethod
110 """
111
112 def get_required_extensions(self):
113 """Return list of extensions required for processing this descriptor.
114
115 Without these extensions present in a neutron deployment, the
116 introduced extension cannot load or function properly.
117 """
118 return []
119
120 def get_optional_extensions(self):
121 """Returns a list of optionally required extensions.
122
123 Unlike get_required_extensions. This will not fail the loading of
124 the extension if one of these extensions is not present. This is
125 useful for an extension that extends multiple resources across
126 other extensions that should still work for the remaining extensions
127 when one is missing.
128 """
129 return []
130
131 @classmethod
132 def update_attributes_map(cls, extended_attributes,
133 extension_attrs_map=None):
134 """Update attributes map for this extension.
135
136 This is default method for extending an extension's attributes map.
137 An extension can use this method and supplying its own resource
138 attribute map in extension_attrs_map argument to extend all its
139 attributes that needs to be extended.
140 If an extension does not implement update_attributes_map, the method
141 does nothing and just return.
142 """
143 if not extension_attrs_map:
144 return
145
146 for resource, attrs in extension_attrs_map.items():
147 extended_attrs = extended_attributes.get(resource)
148 if extended_attrs:
149 attrs.update(extended_attrs)
150
151 def get_pecan_resources(self):
152 """List of PecanResourceExtension extension objects.
153
154 Resources define new nouns, and are accessible through URLs.
155 The controllers associated with each instance of
156 extensions.ResourceExtension should be a subclass of
157 neutron.pecan_wsgi.controllers.utils.NeutronPecanController.
158 If a resource is defined in both get_resources and get_pecan_resources,
159 the resource defined in get_pecan_resources will take precedence.
160 """
161 return []
162
163
164 class APIExtensionDescriptor(ExtensionDescriptor):
165 """Base class that defines the contract for extensions.
166
167 Concrete implementations of API extensions should first provide
168 an API definition in neutron_lib.api.definitions. The API
169 definition module (object reference) can then be specified as a
170 class level attribute on the concrete extension.
171
172 For example::
173
174 from neutron_lib.api.definitions import provider_net
175 from neutron_lib.api import extensions
176
177
178 class Providernet(extensions.APIExtensionDescriptor):
179 api_definition = provider_net
180 # nothing else needed if default behavior is acceptable
181
182
183 If extension implementations need to override the default behavior of
184 this class they can override the respective method directly.
185 """
186 api_definition: constants.Sentinel | object = _UNSET
187
188 @classmethod
189 def _assert_api_definition(cls, attr=None):
190 if cls.api_definition == _UNSET:
191 raise NotImplementedError(
192 _("Extension module API definition not set."))
193 if attr and getattr(cls.api_definition, attr, _UNSET) == _UNSET:
194 raise NotImplementedError(_("Extension module API definition "
195 "does not define '%s'") % attr)
196
197 @classmethod
198 def get_name(cls):
199 """The name of the API definition."""
200 cls._assert_api_definition('NAME')
201 return cls.api_definition.NAME
202
203 @classmethod
204 def get_alias(cls):
205 """The alias for the API definition."""
206 cls._assert_api_definition('ALIAS')
207 return cls.api_definition.ALIAS
208
209 @classmethod
210 def get_description(cls):
211 """Friendly description for the API definition."""
212 cls._assert_api_definition('DESCRIPTION')
213 return cls.api_definition.DESCRIPTION
214
215 @classmethod
216 def get_updated(cls):
217 """The timestamp when the API definition was last updated."""
218 cls._assert_api_definition('UPDATED_TIMESTAMP')
219 return cls.api_definition.UPDATED_TIMESTAMP
220
221 @classmethod
222 def get_extended_resources(cls, version):
223 """Retrieve the extended resource map for the API definition.
224
225 :param version: The API version to retrieve the resource attribute
226 map for.
227 :returns: The extended resource map for the underlying API definition
228 if the version is 2.0. The extended resource map returned includes
229 both the API definition's RESOURCE_ATTRIBUTE_MAP and
230 SUB_RESOURCE_ATTRIBUTE_MAP where applicable. If the version is
231 not 2.0, an empty dict is returned.
232 """
233 if version == "2.0":
234 cls._assert_api_definition('RESOURCE_ATTRIBUTE_MAP')
235 cls._assert_api_definition('SUB_RESOURCE_ATTRIBUTE_MAP')
236 # support api defs that use None for sub attr map
237 sub_attrs = cls.api_definition.SUB_RESOURCE_ATTRIBUTE_MAP or {}
238 return dict(
239 list(cls.api_definition.RESOURCE_ATTRIBUTE_MAP.items()) +
240 list(sub_attrs.items()))
241 else:
242 return {}
243
244 @classmethod
245 def get_required_extensions(cls):
246 """Returns the API definition's required extensions."""
247 cls._assert_api_definition('REQUIRED_EXTENSIONS')
248 return cls.api_definition.REQUIRED_EXTENSIONS
249
250 @classmethod
251 def get_optional_extensions(cls):
252 """Returns the API definition's optional extensions."""
253 cls._assert_api_definition('OPTIONAL_EXTENSIONS')
254 return cls.api_definition.OPTIONAL_EXTENSIONS
255
256 @classmethod
257 def update_attributes_map(cls, extended_attributes,
258 extension_attrs_map=None):
259 """Update attributes map for this extension.
260
261 Behaves like ExtensionDescriptor.update_attributes_map(), but
262 if extension_attrs_map is not given the dict returned from
263 self.get_extended_resources('2.0') is used.
264 """
265 if extension_attrs_map is None:
266 extension_attrs_map = cls.get_extended_resources('2.0')
267 super().update_attributes_map(
268 extended_attributes, extension_attrs_map=extension_attrs_map)