Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / utils / cmalarmhandler.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import logging
15 from Queue import Queue
16 from threading import Thread
17
18 from cmframework.server.cmsingleton import CMSingleton
19 from cmframework.utils.cmalarmwork import CMAlarmWork
20 from cmframework.apis import cmerror
21
22
23 class AlarmHandler(Thread, CMSingleton):
24     def __init__(self):
25         super(AlarmHandler, self).__init__()
26
27         self.handler_lib = None
28         self.works = Queue()
29
30         self.daemon = True
31
32     def set_library_impl(self, handler_lib_impl_module, **kw):
33         try:
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]
38             class_name = parts[1]
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))
48
49     def cancel_alarm_with_dn(self,
50                              alarm_id,
51                              dn,
52                              supplementary_info):
53         logging.debug('AlarmHandler.cancel_alarm_with_dn called')
54
55         alarmwork = CMAlarmWork(CMAlarmWork.OPER_CANCEL, alarm_id, dn, supplementary_info)
56
57         self._add_work(alarmwork)
58
59     def raise_alarm_with_dn(self,
60                             alarm_id,
61                             dn,
62                             supplementary_info):
63         logging.debug('AlarmHandler.raise_alarm_with_dn called')
64
65         alarmwork = CMAlarmWork(CMAlarmWork.OPER_RAISE, alarm_id, dn, supplementary_info)
66
67         self._add_work(alarmwork)
68
69     def _get_work(self):
70         return self.works.get()
71
72     def _add_work(self, work):
73         logging.debug('AlarmHandler._add_work called with %s', work)
74
75         self.works.put(work)
76
77     def run(self):
78         while True:
79             work = self._get_work()
80             if self.handler_lib:
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)
84             else:
85                 logging.warning('No handler lib set to handle work')