blob: 270bd71e2515d11a2201ea799acea7e0150ba3a1 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Simple component registry to provide centralized access to any registered
20components.
21"""
22from collections import OrderedDict
23from zope.interface import Interface
24
25
26class IComponent(Interface):
27 """
28 A Voltha Component
29 """
30
31 def start():
32 """
33 Called once the componet is instantiated. Can be used for async
34 initialization.
35 :return: (None or Deferred)
36 """
37
38 def stop():
39 """
40 Called once before the component is unloaded. Can be used for async
41 cleanup operations.
42 :return: (None or Deferred)
43 """
44
45
46class Registry(object):
47
48 def __init__(self):
49 self.components = OrderedDict()
50
51 def register(self, name, component):
52 assert IComponent.providedBy(component)
53 assert name not in self.components
54 self.components[name] = component
55 return component
56
57 def unregister(self, name):
58 if name in self.components:
59 del self.components[name]
60
61 def __call__(self, name):
62 return self.components[name]
63
64 def iterate(self):
65 return self.components.values()
66
67
68# public shared registry
69registry = Registry()