Debian Codegraph @

neutron_lib/services/base.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import abc
14
15
16 class WorkerBase:
17
18 @property
19 def _workers(self):
20 try:
21 return self.__workers
22 except AttributeError:
23 self.__workers = []
24 return self.__workers
25
26 def get_workers(self):
27 """Returns a collection NeutronWorker instances needed by this service.
28
29 """
30 return list(self._workers)
31
32 def add_worker(self, worker):
33 """Adds NeutronWorker needed for this service
34
35 If a object needs to define workers thread/processes outside of API/RPC
36 workers then it will call this method to register worker. Should be
37 called on initialization stage before running services
38 """
39 self._workers.append(worker)
40
41 def add_workers(self, workers):
42 """Adds NeutronWorker list needed for this service
43
44 The same as add_worker but adds a list of workers
45 """
46 self._workers.extend(workers)
47
48
49 class ServicePluginBase(WorkerBase, metaclass=abc.ABCMeta):
50 """Define base interface for any Advanced Service plugin."""
51 supported_extension_aliases = []
52
53 @classmethod
54 def __subclasshook__(cls, klass):
55 """Checking plugin class.
56
57 The __subclasshook__ method is a class method
58 that will be called every time a class is tested
59 using issubclass(klass, ServicePluginBase).
60 In that case, it will check that every method
61 marked with the abstractmethod decorator is
62 provided by the plugin class.
63 """
64
65 if not cls.__abstractmethods__:
66 return NotImplemented
67
68 for method in cls.__abstractmethods__:
69 if any(method in base.__dict__ for base in klass.__mro__):
70 continue
71 return NotImplemented
72 return True
73
74 @abc.abstractmethod
75 def get_plugin_type(self):
76 """Return one of predefined service types.
77
78 """
79 pass
80
81 @abc.abstractmethod
82 def get_plugin_description(self):
83 """Return string description of the plugin."""
84 pass