blob: 7c8340bf79281eb503499da36bf03b432757b1d4 [file] [log] [blame]
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -08001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -08003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Loader to load each adapter.
19In this initial simple implementation we scan all subdirs in this directory,
20look for a python module with the same name as the subdir, and if module
21has a class that implements the IAdapterInterface, instantiate class and
22add it to plugins.
23"""
24import os
25
26import structlog
27from twisted.internet.defer import inlineCallbacks, returnValue
Zsolt Harasztidafefe12016-11-14 21:29:58 -080028from zope.interface import implementer
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080029from zope.interface.verify import verifyClass
30
Zsolt Haraszti66862032016-11-28 14:28:39 -080031from voltha.adapters.interface import IAdapterInterface
32from voltha.core.adapter_agent import AdapterAgent
Zsolt Haraszti66862032016-11-28 14:28:39 -080033from voltha.registry import IComponent
Shad Ansarid1aa9e72017-06-23 21:34:25 -070034from voltha.adapters.iadapter import IAdapter, OltAdapter, OnuAdapter
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080035
36log = structlog.get_logger()
37
38
39mydir = os.path.abspath(os.path.dirname(__file__))
40
41
Zsolt Harasztidafefe12016-11-14 21:29:58 -080042@implementer(IComponent)
Zsolt Haraszti66862032016-11-28 14:28:39 -080043class AdapterLoader(object):
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080044
45 def __init__(self, config):
46 self.config = config
Zsolt Haraszti66862032016-11-28 14:28:39 -080047 self.adapter_agents = {} # adapter-name -> adapter instance
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080048
49 @inlineCallbacks
50 def start(self):
51 log.debug('starting')
Zsolt Harasztidafefe12016-11-14 21:29:58 -080052 for adapter_name, adapter_class in self._find_adapters():
Zsolt Haraszti66862032016-11-28 14:28:39 -080053 agent = AdapterAgent(adapter_name, adapter_class)
54 yield agent.start()
55 self.adapter_agents[adapter_name] = agent
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080056 log.info('started')
57 returnValue(self)
58
59 @inlineCallbacks
60 def stop(self):
61 log.debug('stopping')
Zsolt Haraszti66862032016-11-28 14:28:39 -080062 for proxy in self.adapter_agents.values():
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080063 yield proxy.stop()
Zsolt Haraszti66862032016-11-28 14:28:39 -080064 self.adapter_agents = {}
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080065 log.info('stopped')
66
Zsolt Haraszti66862032016-11-28 14:28:39 -080067 def get_agent(self, adapter_name):
68 return self.adapter_agents[adapter_name]
69
Zsolt Harasztidafefe12016-11-14 21:29:58 -080070 def _find_adapters(self):
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080071 subdirs = os.walk(mydir).next()[1]
Zsolt Harasztief05ad22017-01-07 22:08:06 -080072 for subdir in subdirs:
73 try:
Zsolt Haraszti89a27302016-12-08 16:53:06 -080074 adapter_name = subdir
75 py_file = os.path.join(mydir, subdir, subdir + '.py')
76 if os.path.isfile(py_file):
77 try:
78 package_name = __package__ + '.' + subdir
79 pkg = __import__(package_name, None, None, [adapter_name])
80 module = getattr(pkg, adapter_name)
81 except ImportError, e:
82 log.exception('cannot-load', file=py_file, e=e)
83 continue
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080084
Zsolt Haraszti89a27302016-12-08 16:53:06 -080085 for attr_name in dir(module):
86 cls = getattr(module, attr_name)
87 if isinstance(cls, type) and \
Shad Ansari7bca1722017-06-16 13:24:22 -070088 cls is not IAdapter and \
Shad Ansarid1aa9e72017-06-23 21:34:25 -070089 cls is not OltAdapter and \
90 cls is not OnuAdapter and \
Zsolt Haraszti89a27302016-12-08 16:53:06 -080091 IAdapterInterface.implementedBy(cls):
92 verifyClass(IAdapterInterface, cls)
93 yield adapter_name, cls
Zsolt Harasztief05ad22017-01-07 22:08:06 -080094 except Exception, e:
95 log.exception('failed', e=e)