projects
/
osm
/
POL.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Coverity-CWE 330: Use of Insufficiently Random Values (137944 Cryptographically weak...
[osm/POL.git]
/
osm_policy_module
/
alarming
/
service.py
diff --git
a/osm_policy_module/alarming/service.py
b/osm_policy_module/alarming/service.py
index
4b393e5
..
f3a316d
100644
(file)
--- a/
osm_policy_module/alarming/service.py
+++ b/
osm_policy_module/alarming/service.py
@@
-22,7
+22,6
@@
# For those usages not covered by the Apache License, Version 2.0 please
# contact: bdiaz@whitestack.com or glavado@whitestack.com
##
# For those usages not covered by the Apache License, Version 2.0 please
# contact: bdiaz@whitestack.com or glavado@whitestack.com
##
-import asyncio
import json
import logging
import operator
import json
import logging
import operator
@@
-47,22
+46,25
@@
log = logging.getLogger(__name__)
class AlarmingService:
class AlarmingService:
- def __init__(self, config: Config
, loop=None
):
+ def __init__(self, config: Config):
self.conf = config
self.conf = config
- if not loop:
- loop = asyncio.get_event_loop()
- self.loop = loop
self.db_client = CommonDbClient(config)
self.db_client = CommonDbClient(config)
- self.mon_client = MonClient(config
, loop=self.loop
)
- self.lcm_client = LcmClient(config
, loop=self.loop
)
+ self.mon_client = MonClient(config)
+ self.lcm_client = LcmClient(config)
- async def configure_vnf_alarms(self, nsr_id: str):
+ async def configure_vnf_alarms(self, nsr_id: str
, vnf_member_index=None
):
log.info("Configuring vnf alarms for network service %s", nsr_id)
alarms_created = []
database.db.connect()
try:
with database.db.atomic():
log.info("Configuring vnf alarms for network service %s", nsr_id)
alarms_created = []
database.db.connect()
try:
with database.db.atomic():
- vnfrs = self.db_client.get_vnfrs(nsr_id)
+ if vnf_member_index is None:
+ vnfrs = self.db_client.get_vnfrs(nsr_id)
+ else:
+ vnfrs = []
+ vnfr = self.db_client.get_vnfr(nsr_id, vnf_member_index)
+ vnfrs.append(vnfr)
+ # vnfrs = self.db_client.get_vnfrs(nsr_id)
for vnfr in vnfrs:
log.debug("Processing vnfr: %s", vnfr)
vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"])
for vnfr in vnfrs:
log.debug("Processing vnfr: %s", vnfr)
vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"])
@@
-105,10
+107,17
@@
class AlarmingService:
)
alarm_action = dict()
for action_type in ["ok", "insufficient-data", "alarm"]:
)
alarm_action = dict()
for action_type in ["ok", "insufficient-data", "alarm"]:
- if "actions" in alarm_descriptor and action_type in alarm_descriptor["actions"]:
- for url in alarm_descriptor["actions"][action_type]:
+ if (
+ "actions" in alarm_descriptor
+ and action_type in alarm_descriptor["actions"]
+ ):
+ for url in alarm_descriptor["actions"][
+ action_type
+ ]:
if "webhook" in alarm_action:
if "webhook" in alarm_action:
- alarm_action["webhook"].append(url["url"])
+ alarm_action["webhook"].append(
+ url["url"]
+ )
else:
alarm_action["webhook"] = [url["url"]]
alarm_uuid = await self.mon_client.create_alarm(
else:
alarm_action["webhook"] = [url["url"]]
alarm_uuid = await self.mon_client.create_alarm(
@@
-119,6
+128,8
@@
class AlarmingService:
threshold=alarm_descriptor["value"],
operation=alarm_descriptor["operation"],
action=str(alarm_action),
threshold=alarm_descriptor["value"],
operation=alarm_descriptor["operation"],
action=str(alarm_action),
+ vnfr=vnfr,
+ vnfd=vnfd,
)
alarm = VnfAlarmRepository.create(
alarm_id=alarm_descriptor["alarm-id"],
)
alarm = VnfAlarmRepository.create(
alarm_id=alarm_descriptor["alarm-id"],
@@
-126,10
+137,10
@@
class AlarmingService:
nsr_id=nsr_id,
vnf_member_index=vnfr["member-vnf-index-ref"],
vdu_name=vdur["name"],
nsr_id=nsr_id,
vnf_member_index=vnfr["member-vnf-index-ref"],
vdu_name=vdur["name"],
- last_action=
'insufficient-data'
,
+ last_action=
"insufficient-data"
,
id_suffix=0,
ok_ack=False,
id_suffix=0,
ok_ack=False,
- alarm_ack=False
+ alarm_ack=False
,
)
for action_type in ["ok", "insufficient-data", "alarm"]:
if (
)
for action_type in ["ok", "insufficient-data", "alarm"]:
if (
@@
-205,8
+216,10
@@
class AlarmingService:
if vnf_member_index is None:
alarm_conditions = VnfAlarm.nsr_id == nsr_id
else:
if vnf_member_index is None:
alarm_conditions = VnfAlarm.nsr_id == nsr_id
else:
- query_list = [VnfAlarm.nsr_id == nsr_id,
- VnfAlarm.vnf_member_index == vnf_member_index]
+ query_list = [
+ VnfAlarm.nsr_id == nsr_id,
+ VnfAlarm.vnf_member_index == vnf_member_index,
+ ]
alarm_conditions = functools.reduce(operator.and_, query_list)
for alarm in VnfAlarmRepository.list(alarm_conditions):
log.debug("Deleting vnf alarm %s", alarm.alarm_uuid)
alarm_conditions = functools.reduce(operator.and_, query_list)
for alarm in VnfAlarmRepository.list(alarm_conditions):
log.debug("Deleting vnf alarm %s", alarm.alarm_uuid)
@@
-230,7
+243,7
@@
class AlarmingService:
database.db.close()
async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict):
database.db.close()
async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict):
- alert_timeout = int(self.conf.get(
'alert', 'timeout'
))
+ alert_timeout = int(self.conf.get(
"alert", "timeout"
))
database.db.connect()
try:
with database.db.atomic():
database.db.connect()
try:
with database.db.atomic():
@@
-245,17
+258,29
@@
class AlarmingService:
If both the status are 'ok', it avoid sending repetitive ok notification.
"""
if action.type == status:
If both the status are 'ok', it avoid sending repetitive ok notification.
"""
if action.type == status:
- if bool(self.conf.get('alert', 'enhanced_alarms')):
- if ((status != "ok" or (status == "ok" and alarm.ok_ack is False)) and
- (status != "alarm" or (status == "alarm" and alarm.alarm_ack is False))):
+ if bool(self.conf.get("alert", "enhanced_alarms")):
+ if (
+ status != "ok"
+ or (status == "ok" and alarm.ok_ack is False)
+ ) and (
+ status != "alarm"
+ or (status == "alarm" and alarm.alarm_ack is False)
+ ):
log.info(
"Executing request to url %s for vnf alarm %s with status %s",
action.url,
alarm.alarm_id,
log.info(
"Executing request to url %s for vnf alarm %s with status %s",
action.url,
alarm.alarm_id,
- status
+ status
,
)
try:
)
try:
- if status == "alarm" and alarm.last_action == "ok":
+ if (
+ status == "alarm"
+ and alarm.last_action == "ok"
+ or (
+ status == "alarm"
+ and alarm.last_action == "insufficient-data"
+ )
+ ):
alarm.id_suffix += 1
alarm.ok_ack = False
if status == "ok" and alarm.last_action == "alarm":
alarm.id_suffix += 1
alarm.ok_ack = False
if status == "ok" and alarm.last_action == "alarm":
@@
-265,11
+290,17
@@
class AlarmingService:
except Exception as e:
log.exception(e)
except Exception as e:
log.exception(e)
- payload["notify_details"]["alarm_number"] = alarm.id_suffix
+ payload["notify_details"][
+ "alarm_number"
+ ] = alarm.id_suffix
headers = {"content-type": "application/json"}
try:
headers = {"content-type": "application/json"}
try:
- resp = requests.post(url=action.url, data=json.dumps(payload),
- headers=headers, verify=False, timeout=alert_timeout)
+ resp = requests.post(
+ url=action.url,
+ data=json.dumps(payload),
+ headers=headers,
+ timeout=alert_timeout,
+ )
log.info("Response %s", resp)
if resp.status_code == 200:
if status == "ok":
log.info("Response %s", resp)
if resp.status_code == 200:
if status == "ok":
@@
-282,26
+313,38
@@
class AlarmingService:
alarm.alarm_ack = False
alarm.ok_ack = False
alarm.save()
alarm.alarm_ack = False
alarm.ok_ack = False
alarm.save()
+ except ConnectionError:
+ log.exception(
+ "Error connecting to url %s", action.url
+ )
except RequestException as e:
except RequestException as e:
- log.info("Error: RequestException while connecting to url %s", action.url)
+ log.info(
+ "Error: RequestException while connecting to url %s",
+ action.url,
+ )
log.debug("RequestException %s", e)
log.debug("RequestException %s", e)
- except ConnectionError:
- log.exception("Error connecting to url %s", action.url)
else:
log.info(
"Executing request to url %s for vnf alarm %s with status %s",
action.url,
alarm.alarm_id,
else:
log.info(
"Executing request to url %s for vnf alarm %s with status %s",
action.url,
alarm.alarm_id,
- status
+ status
,
)
try:
)
try:
- requests.post(url=action.url, json=json.dumps(payload), timeout=alert_timeout)
- except RequestException as e:
- log.info("Error: RequestException while connecting to url %s", action.url)
- log.debug("RequestException %s", e)
+ requests.post(
+ url=action.url,
+ json=json.dumps(payload),
+ timeout=alert_timeout,
+ )
except ConnectionError:
log.exception("Error connecting to url %s", action.url)
except ConnectionError:
log.exception("Error connecting to url %s", action.url)
+ except RequestException as e:
+ log.info(
+ "Error: RequestException while connecting to url %s",
+ action.url,
+ )
+ log.debug("RequestException %s", e)
except VnfAlarm.DoesNotExist:
log.debug(
except VnfAlarm.DoesNotExist:
log.debug(