blob: 767cb7e1cfd30cad86eea8f91224518bbbbaa1a8 [file] [log] [blame]
Zsolt Harasztidafefe12016-11-14 21:29:58 -08001#!/usr/bin/env python
2#
3# Copyright 2016 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
56 def unregister(self, name):
57 if name in self.components:
58 del self.components[name]
59
60 def __call__(self, name):
61 return self.components[name]
62
63 def iterate(self):
64 return self.components.values()
65
66
67# public shared registry
68registry = Registry()