390cfe2cca475260370bf0d31951841b9e0b9480
[pti/o2.git] / o2ims / service / command / purge_alarm_handler.py
1 # Copyright (C) 2024 Wind River Systems, Inc.
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 json
16
17 from o2common.service.unit_of_work import AbstractUnitOfWork
18 from o2common.adapter.notifications import AbstractNotifications
19
20 from o2ims.adapter.clients.fault_client import StxEventClient
21 from o2ims.domain import commands, alarm_obj
22
23 from o2common.helper import o2logging
24 logger = o2logging.get_logger(__name__)
25
26
27 def purge_alarm_event(
28     cmd: commands.PubAlarm2SMO,
29     uow: AbstractUnitOfWork,
30     notifications: AbstractNotifications,
31 ):
32     logger.debug('In purge_alarm_event')
33     fault_client = StxEventClient(uow)
34     data = cmd.data
35     with uow:
36         alarm_event_record = uow.alarm_event_records.get(data.id)
37         alarm_id = json.loads(alarm_event_record.extensions).get('alarm_id')
38
39         events = fault_client.suppression_list(alarm_id)
40         for event_id in events:
41             event = fault_client.suppress(event_id.id)
42             alarm_event_record.hash = event.hash
43             alarm_event_record.extensions = json.dumps(event.filtered)
44             alarm_event_record.alarmChangedTime = event.updatetime.\
45                 strftime("%Y-%m-%dT%H:%M:%S")
46             alarm_event_record.perceivedSeverity = \
47                 alarm_obj.PerceivedSeverityEnum.CLEARED
48
49             uow.alarm_event_records.update(alarm_event_record)
50             break
51         uow.commit()