8cd33dc340efaa0924552f32c4132c6de6608c18
[ta/config-manager.git] / cmalarm.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
15 import logging
16
17 from cmframework.utils.cmalarmhandler import AlarmHandler
18
19
20 class CMAlarm(object):
21     NODE_REBOOT_REQUEST_ALARM = '45001'
22     ACTIVATION_FAILED_ALARM = '45002'
23
24     def _get_alarm_id(self):
25         raise NotImplementedError('Not implemented alarm')
26
27     def raise_alarm_for_node(self, node_name, supplementary_info=None):
28         logging.debug('raise_alarm_for_node called with %s %s', node_name, supplementary_info)
29
30         self._raise_alarm_with_dn('NODE-{}'.format(node_name), supplementary_info)
31
32     def raise_alarm_for_sg(self, sg_name, supplementary_info=None):
33         logging.debug('raise_alarm_for_sg called with %s %s', sg_name, supplementary_info)
34
35         self._raise_alarm_with_dn('SG-{}'.format(sg_name), supplementary_info)
36
37     def _raise_alarm_with_dn(self, dn, supplementary_info=None):
38         logging.debug('raise_alarm called for %s with %s %s',
39                       self._get_alarm_id(),
40                       dn,
41                       supplementary_info)
42
43         if not supplementary_info:
44             supplementary_info = {}
45
46         try:
47             alarm_handler = AlarmHandler()
48
49             alarm_handler.raise_alarm_with_dn(self._get_alarm_id(),
50                                               dn,
51                                               supplementary_info)
52         except Exception as ex:  # pylint: disable=broad-except
53             logging.warning('Alarm raising failed: %s', str(ex))
54
55     def cancel_alarm_for_node(self, node_name, supplementary_info=None):
56         logging.debug('cancel_alarm called with %s %s', node_name, supplementary_info)
57
58         self._cancel_alarm_with_dn('NODE-{}'.format(node_name), supplementary_info)
59
60     def cancel_alarm_for_sg(self, sg_name, supplementary_info=None):
61         logging.debug('cancel_alarm called with %s %s', sg_name, supplementary_info)
62
63         self._cancel_alarm_with_dn('SG-{}'.format(sg_name), supplementary_info)
64
65     def _cancel_alarm_with_dn(self, dn, supplementary_info=None):
66         logging.debug('cancel_alarm called for %s with %s %s',
67                       self._get_alarm_id(),
68                       dn,
69                       supplementary_info)
70
71         if not supplementary_info:
72             supplementary_info = {}
73
74         try:
75             alarm_handler = AlarmHandler()
76
77             alarm_handler.cancel_alarm_with_dn(self._get_alarm_id(),
78                                                dn,
79                                                supplementary_info)
80         except Exception as ex:  # pylint: disable=broad-except
81             logging.warning('Alarm canceling failed: %s', str(ex))
82
83
84 class CMRebootRequestAlarm(CMAlarm):
85     def _get_alarm_id(self):
86         return CMAlarm.NODE_REBOOT_REQUEST_ALARM
87
88
89 class CMActivationFailedAlarm(CMAlarm):
90     def _get_alarm_id(self):
91         return CMAlarm.ACTIVATION_FAILED_ALARM