3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from Queue import Queue
16 from threading import Thread
18 from cmframework.server.cmsingleton import CMSingleton
19 from cmframework.utils.cmalarmwork import CMAlarmWork
20 from cmframework.apis import cmerror
23 class AlarmHandler(Thread, CMSingleton):
25 super(AlarmHandler, self).__init__()
27 self.handler_lib = None
32 def set_library_impl(self, handler_lib_impl_module, **kw):
34 logging.debug('Loading alarmhandler lib from %s', handler_lib_impl_module)
35 # Separate class path and module name
36 parts = handler_lib_impl_module.rsplit('.', 1)
37 module_path = parts[0]
39 logging.debug('Importing %s from %s', class_name, module_path)
40 module = __import__(module_path, fromlist=[module_path])
41 classobj = getattr(module, class_name)
42 logging.debug('Constructing alarm handler lib with args %r', kw)
43 self.handler_lib = classobj(**kw)
44 except ImportError as exp1:
45 raise cmerror.CMError(str(exp1))
46 except Exception as exp2: # pylint: disable=broad-except
47 raise cmerror.CMError(str(exp2))
49 def cancel_alarm_with_dn(self,
53 logging.debug('AlarmHandler.cancel_alarm_with_dn called')
55 alarmwork = CMAlarmWork(CMAlarmWork.OPER_CANCEL, alarm_id, dn, supplementary_info)
57 self._add_work(alarmwork)
59 def raise_alarm_with_dn(self,
63 logging.debug('AlarmHandler.raise_alarm_with_dn called')
65 alarmwork = CMAlarmWork(CMAlarmWork.OPER_RAISE, alarm_id, dn, supplementary_info)
67 self._add_work(alarmwork)
70 return self.works.get()
72 def _add_work(self, work):
73 logging.debug('AlarmHandler._add_work called with %s', work)
79 work = self._get_work()
81 logging.debug('Asking handler lib to handle work: %s', work)
82 self.handler_lib.handle_alarm_work(work)
83 logging.debug('Handler lib handled work: %s', work)
85 logging.warning('No handler lib set to handle work')