blob: 55fe864e238e1feb29f4db7a78c8b305c010d72e [file] [log] [blame]
Benjamin Diazf7451f82019-04-01 14:56:26 -03001# -*- coding: utf-8 -*-
Atul Agarwaldb8c1052020-04-28 15:42:28 +05302# pylint: disable=no-member
Benjamin Diazf7451f82019-04-01 14:56:26 -03003
4# Copyright 2018 Whitestack, LLC
5# *************************************************************
6
7# This file is part of OSM Monitoring module
8# All Rights Reserved to Whitestack, LLC
9
10# Licensed under the Apache License, Version 2.0 (the "License"); you may
11# not use this file except in compliance with the License. You may obtain
12# a copy of the License at
13
14# http://www.apache.org/licenses/LICENSE-2.0
15
16# Unless required by applicable law or agreed to in writing, software
17# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19# License for the specific language governing permissions and limitations
20# under the License.
21
22# For those usages not covered by the Apache License, Version 2.0 please
23# contact: bdiaz@whitestack.com or glavado@whitestack.com
24##
25import asyncio
26import json
27import logging
28
29import requests
Atul Agarwaldb8c1052020-04-28 15:42:28 +053030from requests.exceptions import ConnectionError
Benjamin Diazf7451f82019-04-01 14:56:26 -030031
32from osm_policy_module.common.common_db_client import CommonDbClient
33from osm_policy_module.common.lcm_client import LcmClient
34from osm_policy_module.common.mon_client import MonClient
35from osm_policy_module.core import database
36from osm_policy_module.core.config import Config
37from osm_policy_module.core.database import VnfAlarm, VnfAlarmRepository, AlarmActionRepository
38from osm_policy_module.core.exceptions import VdurNotFound
39
40log = logging.getLogger(__name__)
41
42
43class AlarmingService:
44
45 def __init__(self, config: Config, loop=None):
46 self.conf = config
47 if not loop:
48 loop = asyncio.get_event_loop()
49 self.loop = loop
50 self.db_client = CommonDbClient(config)
51 self.mon_client = MonClient(config, loop=self.loop)
52 self.lcm_client = LcmClient(config, loop=self.loop)
53
54 async def configure_vnf_alarms(self, nsr_id: str):
55 log.info("Configuring vnf alarms for network service %s", nsr_id)
56 alarms_created = []
Benjamin Diazacac7552019-05-23 14:19:06 -030057 database.db.connect()
58 try:
59 with database.db.atomic():
Benjamin Diazf7451f82019-04-01 14:56:26 -030060 vnfrs = self.db_client.get_vnfrs(nsr_id)
61 for vnfr in vnfrs:
62 log.debug("Processing vnfr: %s", vnfr)
63 vnfd = self.db_client.get_vnfd(vnfr['vnfd-id'])
64 for vdur in vnfr['vdur']:
65 vdu = next(
66 filter(
67 lambda vdu: vdu['id'] == vdur['vdu-id-ref'],
68 vnfd['vdu']
69 )
70 )
71 if 'alarm' in vdu:
72 alarm_descriptors = vdu['alarm']
73 for alarm_descriptor in alarm_descriptors:
74 try:
75 VnfAlarmRepository.get(
76 VnfAlarm.alarm_id == alarm_descriptor['alarm-id'],
77 VnfAlarm.vnf_member_index == vnfr['member-vnf-index-ref'],
78 VnfAlarm.vdu_name == vdur['name'],
79 VnfAlarm.nsr_id == nsr_id
80 )
81 log.debug("vdu %s already has an alarm configured with same id %s", vdur['name'],
82 alarm_descriptor['alarm-id'])
83 continue
84 except VnfAlarm.DoesNotExist:
85 pass
86 vnf_monitoring_param = next(
87 filter(
88 lambda param: param['id'] == alarm_descriptor['vnf-monitoring-param-ref'],
garciaale7ce998f2020-12-03 18:05:24 -030089 vdu.get('monitoring-parameter', [])
90 ),
91 {}
Benjamin Diazf7451f82019-04-01 14:56:26 -030092 )
garciaale7ce998f2020-12-03 18:05:24 -030093 metric_name = self._get_metric_name(vnf_monitoring_param)
Benjamin Diazf7451f82019-04-01 14:56:26 -030094 alarm_uuid = await self.mon_client.create_alarm(
Gianpietro Lavado41610192019-12-06 15:46:23 +000095 metric_name=metric_name,
Benjamin Diazf7451f82019-04-01 14:56:26 -030096 ns_id=nsr_id,
97 vdu_name=vdur['name'],
98 vnf_member_index=vnfr['member-vnf-index-ref'],
99 threshold=alarm_descriptor['value'],
garciaale7ce998f2020-12-03 18:05:24 -0300100 operation=alarm_descriptor['operation']
Benjamin Diazf7451f82019-04-01 14:56:26 -0300101 )
102 alarm = VnfAlarmRepository.create(
103 alarm_id=alarm_descriptor['alarm-id'],
104 alarm_uuid=alarm_uuid,
105 nsr_id=nsr_id,
Benjamin Diaz3736ad82019-06-05 16:24:34 -0300106 vnf_member_index=vnfr['member-vnf-index-ref'],
Benjamin Diazf7451f82019-04-01 14:56:26 -0300107 vdu_name=vdur['name']
108 )
109 for action_type in ['ok', 'insufficient-data', 'alarm']:
Benjamin Diaz889c53c2019-05-21 12:31:15 -0300110 if 'actions' in alarm_descriptor and action_type in alarm_descriptor['actions']:
Benjamin Diazf7451f82019-04-01 14:56:26 -0300111 for url in alarm_descriptor['actions'][action_type]:
112 AlarmActionRepository.create(
113 type=action_type,
114 url=url['url'],
115 alarm=alarm
116 )
117 alarms_created.append(alarm)
118
Benjamin Diazacac7552019-05-23 14:19:06 -0300119 except Exception as e:
120 log.exception("Error configuring VNF alarms:")
121 if len(alarms_created) > 0:
122 log.debug("Cleaning alarm resources in MON")
123 for alarm in alarms_created:
124 try:
Benjamin Diazf7451f82019-04-01 14:56:26 -0300125 await self.mon_client.delete_alarm(alarm.nsr_id,
126 alarm.vnf_member_index,
127 alarm.vdu_name,
128 alarm.alarm_uuid)
Benjamin Diazacac7552019-05-23 14:19:06 -0300129 except ValueError:
130 log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
131 raise e
132 finally:
133 database.db.close()
Benjamin Diazf7451f82019-04-01 14:56:26 -0300134
135 async def delete_orphaned_alarms(self, nsr_id):
garciaale7ce998f2020-12-03 18:05:24 -0300136 # TODO: Review as it seems this code is never called
Benjamin Diazf7451f82019-04-01 14:56:26 -0300137 log.info("Deleting orphaned vnf alarms for network service %s", nsr_id)
138 database.db.connect()
Benjamin Diazacac7552019-05-23 14:19:06 -0300139 try:
140 with database.db.atomic():
Benjamin Diazf7451f82019-04-01 14:56:26 -0300141 for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id):
142 try:
143 self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name)
144 except VdurNotFound:
145 log.debug("Deleting orphaned alarm %s", alarm.alarm_uuid)
146 try:
147 await self.mon_client.delete_alarm(
148 alarm.nsr_id,
149 alarm.vnf_member_index,
150 alarm.vdu_name,
151 alarm.alarm_uuid)
152 except ValueError:
153 log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
154 alarm.delete_instance()
Benjamin Diazacac7552019-05-23 14:19:06 -0300155 except Exception as e:
156 log.exception("Error deleting orphaned alarms:")
157 raise e
158 finally:
159 database.db.close()
Benjamin Diazf7451f82019-04-01 14:56:26 -0300160
161 async def delete_vnf_alarms(self, nsr_id):
162 log.info("Deleting vnf alarms for network service %s", nsr_id)
163 database.db.connect()
Benjamin Diazacac7552019-05-23 14:19:06 -0300164 try:
165 with database.db.atomic():
Benjamin Diazf7451f82019-04-01 14:56:26 -0300166 for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id):
167 log.debug("Deleting vnf alarm %s", alarm.alarm_uuid)
168 try:
169 await self.mon_client.delete_alarm(
170 alarm.nsr_id,
171 alarm.vnf_member_index,
172 alarm.vdu_name,
173 alarm.alarm_uuid)
174 except ValueError:
175 log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
176 alarm.delete_instance()
177
Benjamin Diazacac7552019-05-23 14:19:06 -0300178 except Exception as e:
179 log.exception("Error deleting vnf alarms:")
180 raise e
181 finally:
182 database.db.close()
Benjamin Diazf7451f82019-04-01 14:56:26 -0300183
184 async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict):
185 database.db.connect()
186 try:
187 with database.db.atomic():
188 alarm = VnfAlarmRepository.get(VnfAlarm.alarm_uuid == alarm_uuid)
189 log.debug("Handling vnf alarm %s with status %s", alarm.alarm_id, status)
190 for action in alarm.actions:
191 if action.type == status:
192 log.info("Executing request to url %s for vnf alarm %s with status %s", action.url,
193 alarm.alarm_id, status)
Atul Agarwaldb8c1052020-04-28 15:42:28 +0530194 try:
195 requests.post(url=action.url, json=json.dumps(payload))
196 except ConnectionError:
197 log.exception("Error connecting to url %s", action.url)
198
Benjamin Diazf7451f82019-04-01 14:56:26 -0300199 except VnfAlarm.DoesNotExist:
200 log.debug("There is no alarming action configured for alarm %s.", alarm_uuid)
201 finally:
202 database.db.close()
Gianpietro Lavado41610192019-12-06 15:46:23 +0000203
garciaale7ce998f2020-12-03 18:05:24 -0300204 def _get_metric_name(self, vnf_monitoring_param: dict):
205 if 'performance-metric' in vnf_monitoring_param:
206 return vnf_monitoring_param['performance-metric']
Gianpietro Lavado41610192019-12-06 15:46:23 +0000207 raise ValueError('No metric name found for vnf_monitoring_param %s' % vnf_monitoring_param['id'])