b93dba7ac2802180a0a5c214cd69796be66717f3
1 # -*- coding: utf-8 -*-
2 # pylint: disable=no-member
4 # Copyright 2018 Whitestack, LLC
5 # *************************************************************
7 # This file is part of OSM Monitoring module
8 # All Rights Reserved to Whitestack, LLC
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
14 # http://www.apache.org/licenses/LICENSE-2.0
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
22 # For those usages not covered by the Apache License, Version 2.0 please
23 # contact: bdiaz@whitestack.com or glavado@whitestack.com
30 from requests
.exceptions
import ConnectionError
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 (
40 AlarmActionRepository
,
42 from osm_policy_module
.core
.exceptions
import VdurNotFound
44 log
= logging
.getLogger(__name__
)
47 class AlarmingService
:
48 def __init__(self
, config
: Config
, loop
=None):
51 loop
= asyncio
.get_event_loop()
53 self
.db_client
= CommonDbClient(config
)
54 self
.mon_client
= MonClient(config
, loop
=self
.loop
)
55 self
.lcm_client
= LcmClient(config
, loop
=self
.loop
)
57 async def configure_vnf_alarms(self
, nsr_id
: str):
58 log
.info("Configuring vnf alarms for network service %s", nsr_id
)
62 with database
.db
.atomic():
63 vnfrs
= self
.db_client
.get_vnfrs(nsr_id
)
65 log
.debug("Processing vnfr: %s", vnfr
)
66 vnfd
= self
.db_client
.get_vnfd(vnfr
["vnfd-id"])
67 for vdur
in vnfr
["vdur"]:
70 lambda vdu
: vdu
["id"] == vdur
["vdu-id-ref"], vnfd
["vdu"]
74 alarm_descriptors
= vdu
["alarm"]
75 for alarm_descriptor
in alarm_descriptors
:
77 VnfAlarmRepository
.get(
79 == alarm_descriptor
["alarm-id"],
80 VnfAlarm
.vnf_member_index
81 == vnfr
["member-vnf-index-ref"],
82 VnfAlarm
.vdu_name
== vdur
["name"],
83 VnfAlarm
.nsr_id
== nsr_id
,
86 "vdu %s already has an alarm configured with same id %s",
88 alarm_descriptor
["alarm-id"],
91 except VnfAlarm
.DoesNotExist
:
93 vnf_monitoring_param
= next(
95 lambda param
: param
["id"]
96 == alarm_descriptor
["vnf-monitoring-param-ref"],
97 vdu
.get("monitoring-parameter", []),
101 metric_name
= self
._get
_metric
_name
(
104 alarm_action
= dict()
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 if "webhook" in alarm_action
:
109 alarm_action
["webhook"].append(url
["url"])
111 alarm_action
["webhook"] = [url
["url"]]
112 alarm_uuid
= await self
.mon_client
.create_alarm(
113 metric_name
=metric_name
,
115 vdu_name
=vdur
["name"],
116 vnf_member_index
=vnfr
["member-vnf-index-ref"],
117 threshold
=alarm_descriptor
["value"],
118 operation
=alarm_descriptor
["operation"],
119 action
=str(alarm_action
),
121 alarm
= VnfAlarmRepository
.create(
122 alarm_id
=alarm_descriptor
["alarm-id"],
123 alarm_uuid
=alarm_uuid
,
125 vnf_member_index
=vnfr
["member-vnf-index-ref"],
126 vdu_name
=vdur
["name"],
128 for action_type
in ["ok", "insufficient-data", "alarm"]:
130 "actions" in alarm_descriptor
131 and action_type
in alarm_descriptor
["actions"]
133 for url
in alarm_descriptor
["actions"][
136 AlarmActionRepository
.create(
141 alarms_created
.append(alarm
)
143 except Exception as e
:
144 log
.exception("Error configuring VNF alarms:")
145 if len(alarms_created
) > 0:
146 log
.debug("Cleaning alarm resources in MON")
147 for alarm
in alarms_created
:
149 await self
.mon_client
.delete_alarm(
151 alarm
.vnf_member_index
,
157 "Error deleting alarm in MON %s", alarm
.alarm_uuid
163 async def delete_orphaned_alarms(self
, nsr_id
):
164 # TODO: Review as it seems this code is never called
165 log
.info("Deleting orphaned vnf alarms for network service %s", nsr_id
)
166 database
.db
.connect()
168 with database
.db
.atomic():
169 for alarm
in VnfAlarmRepository
.list(VnfAlarm
.nsr_id
== nsr_id
):
171 self
.db_client
.get_vdur(
172 nsr_id
, alarm
.vnf_member_index
, alarm
.vdu_name
175 log
.debug("Deleting orphaned alarm %s", alarm
.alarm_uuid
)
177 await self
.mon_client
.delete_alarm(
179 alarm
.vnf_member_index
,
185 "Error deleting alarm in MON %s", alarm
.alarm_uuid
187 alarm
.delete_instance()
188 except Exception as e
:
189 log
.exception("Error deleting orphaned alarms:")
194 async def delete_vnf_alarms(self
, nsr_id
):
195 log
.info("Deleting vnf alarms for network service %s", nsr_id
)
196 database
.db
.connect()
198 with database
.db
.atomic():
199 for alarm
in VnfAlarmRepository
.list(VnfAlarm
.nsr_id
== nsr_id
):
200 log
.debug("Deleting vnf alarm %s", alarm
.alarm_uuid
)
202 await self
.mon_client
.delete_alarm(
204 alarm
.vnf_member_index
,
210 "Error deleting alarm in MON %s", alarm
.alarm_uuid
212 alarm
.delete_instance()
214 except Exception as e
:
215 log
.exception("Error deleting vnf alarms:")
220 async def handle_alarm(self
, alarm_uuid
: str, status
: str, payload
: dict):
221 database
.db
.connect()
223 with database
.db
.atomic():
224 alarm
= VnfAlarmRepository
.get(VnfAlarm
.alarm_uuid
== alarm_uuid
)
226 "Handling vnf alarm %s with status %s", alarm
.alarm_id
, status
228 for action
in alarm
.actions
:
229 if action
.type == status
:
231 "Executing request to url %s for vnf alarm %s with status %s",
237 requests
.post(url
=action
.url
, json
=json
.dumps(payload
))
238 except ConnectionError
:
239 log
.exception("Error connecting to url %s", action
.url
)
241 except VnfAlarm
.DoesNotExist
:
243 "There is no alarming action configured for alarm %s.", alarm_uuid
248 def _get_metric_name(self
, vnf_monitoring_param
: dict):
249 if "performance-metric" in vnf_monitoring_param
:
250 return vnf_monitoring_param
["performance-metric"]
252 "No metric name found for vnf_monitoring_param %s"
253 % vnf_monitoring_param
["id"]