590b06a4211b70380de42aec2a67438ac7466d77
[osm/POL.git] / osm_policy_module / alarming / service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import asyncio
25 import json
26 import logging
27
28 import requests
29
30 from osm_policy_module.common.common_db_client import CommonDbClient
31 from osm_policy_module.common.lcm_client import LcmClient
32 from osm_policy_module.common.mon_client import MonClient
33 from osm_policy_module.core import database
34 from osm_policy_module.core.config import Config
35 from osm_policy_module.core.database import VnfAlarm, VnfAlarmRepository, AlarmActionRepository
36 from osm_policy_module.core.exceptions import VdurNotFound
37
38 log = logging.getLogger(__name__)
39
40
41 class AlarmingService:
42
43 def __init__(self, config: Config, loop=None):
44 self.conf = config
45 if not loop:
46 loop = asyncio.get_event_loop()
47 self.loop = loop
48 self.db_client = CommonDbClient(config)
49 self.mon_client = MonClient(config, loop=self.loop)
50 self.lcm_client = LcmClient(config, loop=self.loop)
51
52 async def configure_vnf_alarms(self, nsr_id: str):
53 log.info("Configuring vnf alarms for network service %s", nsr_id)
54 alarms_created = []
55 database.db.connect(reuse_if_open=True)
56 with database.db.atomic() as tx:
57 try:
58 vnfrs = self.db_client.get_vnfrs(nsr_id)
59 for vnfr in vnfrs:
60 log.debug("Processing vnfr: %s", vnfr)
61 vnfd = self.db_client.get_vnfd(vnfr['vnfd-id'])
62 for vdur in vnfr['vdur']:
63 vdu = next(
64 filter(
65 lambda vdu: vdu['id'] == vdur['vdu-id-ref'],
66 vnfd['vdu']
67 )
68 )
69 if 'alarm' in vdu:
70 alarm_descriptors = vdu['alarm']
71 for alarm_descriptor in alarm_descriptors:
72 try:
73 VnfAlarmRepository.get(
74 VnfAlarm.alarm_id == alarm_descriptor['alarm-id'],
75 VnfAlarm.vnf_member_index == vnfr['member-vnf-index-ref'],
76 VnfAlarm.vdu_name == vdur['name'],
77 VnfAlarm.nsr_id == nsr_id
78 )
79 log.debug("vdu %s already has an alarm configured with same id %s", vdur['name'],
80 alarm_descriptor['alarm-id'])
81 continue
82 except VnfAlarm.DoesNotExist:
83 pass
84 vnf_monitoring_param = next(
85 filter(
86 lambda param: param['id'] == alarm_descriptor['vnf-monitoring-param-ref'],
87 vnfd['monitoring-param'])
88 )
89 alarm_uuid = await self.mon_client.create_alarm(
90 metric_name=alarm_descriptor['vnf-monitoring-param-ref'],
91 ns_id=nsr_id,
92 vdu_name=vdur['name'],
93 vnf_member_index=vnfr['member-vnf-index-ref'],
94 threshold=alarm_descriptor['value'],
95 operation=alarm_descriptor['operation'],
96 statistic=vnf_monitoring_param['aggregation-type']
97 )
98 alarm = VnfAlarmRepository.create(
99 alarm_id=alarm_descriptor['alarm-id'],
100 alarm_uuid=alarm_uuid,
101 nsr_id=nsr_id,
102 vnf_member_index=int(vnfr['member-vnf-index-ref']),
103 vdu_name=vdur['name']
104 )
105 for action_type in ['ok', 'insufficient-data', 'alarm']:
106 if 'actions' in alarm_descriptor and action_type in alarm_descriptor['actions']:
107 for url in alarm_descriptor['actions'][action_type]:
108 AlarmActionRepository.create(
109 type=action_type,
110 url=url['url'],
111 alarm=alarm
112 )
113 alarms_created.append(alarm)
114
115 except Exception as e:
116 log.exception("Error configuring VNF alarms:")
117 tx.rollback()
118 if len(alarms_created) > 0:
119 log.debug("Cleaning alarm resources in MON")
120 for alarm in alarms_created:
121 await self.mon_client.delete_alarm(alarm.nsr_id,
122 alarm.vnf_member_index,
123 alarm.vdu_name,
124 alarm.alarm_uuid)
125 raise e
126 database.db.close()
127
128 async def delete_orphaned_alarms(self, nsr_id):
129 log.info("Deleting orphaned vnf alarms for network service %s", nsr_id)
130 database.db.connect()
131 with database.db.atomic() as tx:
132 try:
133 for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id):
134 try:
135 self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name)
136 except VdurNotFound:
137 log.debug("Deleting orphaned alarm %s", alarm.alarm_uuid)
138 try:
139 await self.mon_client.delete_alarm(
140 alarm.nsr_id,
141 alarm.vnf_member_index,
142 alarm.vdu_name,
143 alarm.alarm_uuid)
144 except ValueError:
145 log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
146 alarm.delete_instance()
147
148 except Exception as e:
149 log.exception("Error deleting orphaned alarms:")
150 tx.rollback()
151 raise e
152 database.db.close()
153
154 async def delete_vnf_alarms(self, nsr_id):
155 log.info("Deleting vnf alarms for network service %s", nsr_id)
156 database.db.connect()
157 with database.db.atomic() as tx:
158 try:
159 for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id):
160 log.debug("Deleting vnf alarm %s", alarm.alarm_uuid)
161 try:
162 await self.mon_client.delete_alarm(
163 alarm.nsr_id,
164 alarm.vnf_member_index,
165 alarm.vdu_name,
166 alarm.alarm_uuid)
167 except ValueError:
168 log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
169 alarm.delete_instance()
170
171 except Exception as e:
172 log.exception("Error deleting orphaned alarms:")
173 tx.rollback()
174 raise e
175 database.db.close()
176
177 async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict):
178 database.db.connect()
179 try:
180 with database.db.atomic():
181 alarm = VnfAlarmRepository.get(VnfAlarm.alarm_uuid == alarm_uuid)
182 log.debug("Handling vnf alarm %s with status %s", alarm.alarm_id, status)
183 for action in alarm.actions:
184 if action.type == status:
185 log.info("Executing request to url %s for vnf alarm %s with status %s", action.url,
186 alarm.alarm_id, status)
187 requests.post(url=action.url, json=json.dumps(payload))
188 except VnfAlarm.DoesNotExist:
189 log.debug("There is no alarming action configured for alarm %s.", alarm_uuid)
190 finally:
191 database.db.close()