| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| Atul Agarwal | db8c105 | 2020-04-28 15:42:28 +0530 | [diff] [blame] | 2 | # pylint: disable=no-member |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 3 | |
| 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 | ## |
| 25 | import asyncio |
| 26 | import json |
| 27 | import logging |
| 28 | |
| 29 | import requests |
| Atul Agarwal | db8c105 | 2020-04-28 15:42:28 +0530 | [diff] [blame] | 30 | from requests.exceptions import ConnectionError |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 31 | |
| 32 | from osm_policy_module.common.common_db_client import CommonDbClient |
| 33 | from osm_policy_module.common.lcm_client import LcmClient |
| 34 | from osm_policy_module.common.mon_client import MonClient |
| 35 | from osm_policy_module.core import database |
| 36 | from osm_policy_module.core.config import Config |
| 37 | from osm_policy_module.core.database import VnfAlarm, VnfAlarmRepository, AlarmActionRepository |
| 38 | from osm_policy_module.core.exceptions import VdurNotFound |
| 39 | |
| 40 | log = logging.getLogger(__name__) |
| 41 | |
| 42 | |
| 43 | class 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 Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 57 | database.db.connect() |
| 58 | try: |
| 59 | with database.db.atomic(): |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 60 | 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'], |
| 89 | vnfd['monitoring-param']) |
| 90 | ) |
| Gianpietro Lavado | 4161019 | 2019-12-06 15:46:23 +0000 | [diff] [blame] | 91 | metric_name = self._get_metric_name(vnf_monitoring_param, vdur, vnfd) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 92 | alarm_uuid = await self.mon_client.create_alarm( |
| Gianpietro Lavado | 4161019 | 2019-12-06 15:46:23 +0000 | [diff] [blame] | 93 | metric_name=metric_name, |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 94 | ns_id=nsr_id, |
| 95 | vdu_name=vdur['name'], |
| 96 | vnf_member_index=vnfr['member-vnf-index-ref'], |
| 97 | threshold=alarm_descriptor['value'], |
| 98 | operation=alarm_descriptor['operation'], |
| 99 | statistic=vnf_monitoring_param['aggregation-type'] |
| 100 | ) |
| 101 | alarm = VnfAlarmRepository.create( |
| 102 | alarm_id=alarm_descriptor['alarm-id'], |
| 103 | alarm_uuid=alarm_uuid, |
| 104 | nsr_id=nsr_id, |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 105 | vnf_member_index=vnfr['member-vnf-index-ref'], |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 106 | vdu_name=vdur['name'] |
| 107 | ) |
| 108 | for action_type in ['ok', 'insufficient-data', 'alarm']: |
| Benjamin Diaz | 889c53c | 2019-05-21 12:31:15 -0300 | [diff] [blame] | 109 | if 'actions' in alarm_descriptor and action_type in alarm_descriptor['actions']: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 110 | for url in alarm_descriptor['actions'][action_type]: |
| 111 | AlarmActionRepository.create( |
| 112 | type=action_type, |
| 113 | url=url['url'], |
| 114 | alarm=alarm |
| 115 | ) |
| 116 | alarms_created.append(alarm) |
| 117 | |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 118 | except Exception as e: |
| 119 | log.exception("Error configuring VNF alarms:") |
| 120 | if len(alarms_created) > 0: |
| 121 | log.debug("Cleaning alarm resources in MON") |
| 122 | for alarm in alarms_created: |
| 123 | try: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 124 | await self.mon_client.delete_alarm(alarm.nsr_id, |
| 125 | alarm.vnf_member_index, |
| 126 | alarm.vdu_name, |
| 127 | alarm.alarm_uuid) |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 128 | except ValueError: |
| 129 | log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
| 130 | raise e |
| 131 | finally: |
| 132 | database.db.close() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 133 | |
| 134 | async def delete_orphaned_alarms(self, nsr_id): |
| 135 | log.info("Deleting orphaned vnf alarms for network service %s", nsr_id) |
| 136 | database.db.connect() |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 137 | try: |
| 138 | with database.db.atomic(): |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 139 | for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
| 140 | try: |
| 141 | self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name) |
| 142 | except VdurNotFound: |
| 143 | log.debug("Deleting orphaned alarm %s", alarm.alarm_uuid) |
| 144 | try: |
| 145 | await self.mon_client.delete_alarm( |
| 146 | alarm.nsr_id, |
| 147 | alarm.vnf_member_index, |
| 148 | alarm.vdu_name, |
| 149 | alarm.alarm_uuid) |
| 150 | except ValueError: |
| 151 | log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
| 152 | alarm.delete_instance() |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 153 | except Exception as e: |
| 154 | log.exception("Error deleting orphaned alarms:") |
| 155 | raise e |
| 156 | finally: |
| 157 | database.db.close() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 158 | |
| 159 | async def delete_vnf_alarms(self, nsr_id): |
| 160 | log.info("Deleting vnf alarms for network service %s", nsr_id) |
| 161 | database.db.connect() |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 162 | try: |
| 163 | with database.db.atomic(): |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 164 | for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
| 165 | log.debug("Deleting vnf alarm %s", alarm.alarm_uuid) |
| 166 | try: |
| 167 | await self.mon_client.delete_alarm( |
| 168 | alarm.nsr_id, |
| 169 | alarm.vnf_member_index, |
| 170 | alarm.vdu_name, |
| 171 | alarm.alarm_uuid) |
| 172 | except ValueError: |
| 173 | log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
| 174 | alarm.delete_instance() |
| 175 | |
| Benjamin Diaz | acac755 | 2019-05-23 14:19:06 -0300 | [diff] [blame] | 176 | except Exception as e: |
| 177 | log.exception("Error deleting vnf alarms:") |
| 178 | raise e |
| 179 | finally: |
| 180 | database.db.close() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 181 | |
| 182 | async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict): |
| 183 | database.db.connect() |
| 184 | try: |
| 185 | with database.db.atomic(): |
| 186 | alarm = VnfAlarmRepository.get(VnfAlarm.alarm_uuid == alarm_uuid) |
| 187 | log.debug("Handling vnf alarm %s with status %s", alarm.alarm_id, status) |
| 188 | for action in alarm.actions: |
| 189 | if action.type == status: |
| 190 | log.info("Executing request to url %s for vnf alarm %s with status %s", action.url, |
| 191 | alarm.alarm_id, status) |
| Atul Agarwal | db8c105 | 2020-04-28 15:42:28 +0530 | [diff] [blame] | 192 | try: |
| 193 | requests.post(url=action.url, json=json.dumps(payload)) |
| 194 | except ConnectionError: |
| 195 | log.exception("Error connecting to url %s", action.url) |
| 196 | |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 197 | except VnfAlarm.DoesNotExist: |
| 198 | log.debug("There is no alarming action configured for alarm %s.", alarm_uuid) |
| 199 | finally: |
| 200 | database.db.close() |
| Gianpietro Lavado | 4161019 | 2019-12-06 15:46:23 +0000 | [diff] [blame] | 201 | |
| 202 | def _get_metric_name(self, vnf_monitoring_param: dict, vdur: dict, vnfd: dict): |
| 203 | vdu = next( |
| 204 | filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu']) |
| 205 | ) |
| 206 | if 'vdu-monitoring-param' in vnf_monitoring_param: |
| 207 | vdu_monitoring_param = next(filter( |
| 208 | lambda param: param['id'] == vnf_monitoring_param['vdu-monitoring-param'][ |
| 209 | 'vdu-monitoring-param-ref'], vdu['monitoring-param'])) |
| 210 | nfvi_metric = vdu_monitoring_param['nfvi-metric'] |
| 211 | return nfvi_metric |
| 212 | if 'vdu-metric' in vnf_monitoring_param: |
| 213 | vnf_metric_name = vnf_monitoring_param['vdu-metric']['vdu-metric-name-ref'] |
| 214 | return vnf_metric_name |
| 215 | if 'vnf-metric' in vnf_monitoring_param: |
| 216 | vnf_metric_name = vnf_monitoring_param['vnf-metric']['vnf-metric-name-ref'] |
| 217 | return vnf_metric_name |
| 218 | raise ValueError('No metric name found for vnf_monitoring_param %s' % vnf_monitoring_param['id']) |