blob: d5e8dfc3da089f8d1f64ef1a9a06871ef6523293 [file] [log] [blame]
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -03001# -*- 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##
24import json
25import logging
26import random
Benjamin Diaz127bbba2018-11-26 18:05:53 -030027from json import JSONDecodeError
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030028
Benjamin Diaz127bbba2018-11-26 18:05:53 -030029import yaml
Benjamin Diaz16256cb2018-10-11 12:34:20 -030030from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030031
32from osm_policy_module.core.config import Config
33
34log = logging.getLogger(__name__)
35
36
37class MonClient:
Mark Beierld37c54c2023-05-10 11:15:10 -040038 def __init__(self, config: Config):
garciadeblas4584f8e2021-05-14 16:50:06 +020039 self.kafka_server = "{}:{}".format(
40 config.get("message", "host"), config.get("message", "port")
41 )
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030042
garciadeblas4584f8e2021-05-14 16:50:06 +020043 async def create_alarm(
44 self,
45 metric_name: str,
46 ns_id: str,
47 vdu_name: str,
48 vnf_member_index: str,
49 threshold: int,
50 operation: str,
51 statistic: str = "AVERAGE",
garciadeblasbe42d542022-11-14 00:29:47 +010052 action: str = "",
garciadeblas4584f8e2021-05-14 16:50:06 +020053 ):
Benjamin Diaz16256cb2018-10-11 12:34:20 -030054 cor_id = random.randint(1, 10e7)
garciadeblas4584f8e2021-05-14 16:50:06 +020055 msg = self._build_create_alarm_payload(
56 cor_id,
57 metric_name,
58 ns_id,
59 vdu_name,
60 vnf_member_index,
61 threshold,
62 statistic,
63 operation,
Atul Agarwale9228cf2021-03-19 10:11:38 +000064 action,
garciadeblas4584f8e2021-05-14 16:50:06 +020065 )
Benjamin Diazf7451f82019-04-01 14:56:26 -030066 log.debug("Sending create_alarm_request %s", msg)
garciadeblas4584f8e2021-05-14 16:50:06 +020067 producer = AIOKafkaProducer(
garciadeblas4584f8e2021-05-14 16:50:06 +020068 bootstrap_servers=self.kafka_server,
69 key_serializer=str.encode,
70 value_serializer=str.encode,
71 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -030072 await producer.start()
Benjamin Diaz16256cb2018-10-11 12:34:20 -030073 try:
garciadeblas4584f8e2021-05-14 16:50:06 +020074 await producer.send_and_wait(
75 "alarm_request", key="create_alarm_request", value=json.dumps(msg)
76 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -030077 finally:
78 await producer.stop()
Benjamin Diazf7451f82019-04-01 14:56:26 -030079 log.debug("Waiting for create_alarm_response...")
Benjamin Diaz127bbba2018-11-26 18:05:53 -030080 consumer = AIOKafkaConsumer(
81 "alarm_response_" + str(cor_id),
Benjamin Diaz127bbba2018-11-26 18:05:53 -030082 bootstrap_servers=self.kafka_server,
83 key_deserializer=bytes.decode,
84 value_deserializer=bytes.decode,
garciadeblas4584f8e2021-05-14 16:50:06 +020085 auto_offset_reset="earliest",
86 )
Benjamin Diaz127bbba2018-11-26 18:05:53 -030087 await consumer.start()
88 alarm_uuid = None
Benjamin Diaz16256cb2018-10-11 12:34:20 -030089 try:
90 async for message in consumer:
Benjamin Diaz127bbba2018-11-26 18:05:53 -030091 try:
Benjamin Diaz16256cb2018-10-11 12:34:20 -030092 content = json.loads(message.value)
Benjamin Diaz127bbba2018-11-26 18:05:53 -030093 except JSONDecodeError:
94 content = yaml.safe_load(message.value)
Benjamin Diazf7451f82019-04-01 14:56:26 -030095 log.debug("Received create_alarm_response %s", content)
garciadeblas4584f8e2021-05-14 16:50:06 +020096 if content["alarm_create_response"]["correlation_id"] == cor_id:
97 if not content["alarm_create_response"]["status"]:
Benjamin Diaz127bbba2018-11-26 18:05:53 -030098 raise ValueError("Error creating alarm in MON")
garciadeblas4584f8e2021-05-14 16:50:06 +020099 alarm_uuid = content["alarm_create_response"]["alarm_uuid"]
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300100 break
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300101 finally:
102 await consumer.stop()
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300103 if not alarm_uuid:
garciadeblas4584f8e2021-05-14 16:50:06 +0200104 raise ValueError("No alarm deletion response from MON. Is MON up?")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300105 return alarm_uuid
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300106
garciadeblas4584f8e2021-05-14 16:50:06 +0200107 async def delete_alarm(
108 self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str
109 ):
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300110 cor_id = random.randint(1, 10e7)
garciadeblas4584f8e2021-05-14 16:50:06 +0200111 msg = self._build_delete_alarm_payload(
112 cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid
113 )
Benjamin Diazf7451f82019-04-01 14:56:26 -0300114 log.debug("Sending delete_alarm_request %s", msg)
garciadeblas4584f8e2021-05-14 16:50:06 +0200115 producer = AIOKafkaProducer(
garciadeblas4584f8e2021-05-14 16:50:06 +0200116 bootstrap_servers=self.kafka_server,
117 key_serializer=str.encode,
118 value_serializer=str.encode,
119 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300120 await producer.start()
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300121 try:
garciadeblas4584f8e2021-05-14 16:50:06 +0200122 await producer.send_and_wait(
123 "alarm_request", key="delete_alarm_request", value=json.dumps(msg)
124 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300125 finally:
126 await producer.stop()
Benjamin Diazf7451f82019-04-01 14:56:26 -0300127 log.debug("Waiting for delete_alarm_response...")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300128 consumer = AIOKafkaConsumer(
129 "alarm_response_" + str(cor_id),
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300130 bootstrap_servers=self.kafka_server,
131 key_deserializer=bytes.decode,
132 value_deserializer=bytes.decode,
garciadeblas4584f8e2021-05-14 16:50:06 +0200133 auto_offset_reset="earliest",
134 )
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300135 await consumer.start()
136 alarm_uuid = None
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300137 try:
138 async for message in consumer:
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300139 try:
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300140 content = json.loads(message.value)
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300141 except JSONDecodeError:
142 content = yaml.safe_load(message.value)
garciadeblas4584f8e2021-05-14 16:50:06 +0200143 if content["alarm_delete_response"]["correlation_id"] == cor_id:
Benjamin Diazf7451f82019-04-01 14:56:26 -0300144 log.debug("Received delete_alarm_response %s", content)
garciadeblas4584f8e2021-05-14 16:50:06 +0200145 if not content["alarm_delete_response"]["status"]:
146 raise ValueError(
147 "Error deleting alarm in MON. Response status is False."
148 )
149 alarm_uuid = content["alarm_delete_response"]["alarm_uuid"]
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300150 break
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300151 finally:
152 await consumer.stop()
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300153 if not alarm_uuid:
garciadeblas4584f8e2021-05-14 16:50:06 +0200154 raise ValueError("No alarm deletion response from MON. Is MON up?")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300155 return alarm_uuid
Benjamin Diazf35f9142018-10-08 16:25:36 -0300156
garciadeblas4584f8e2021-05-14 16:50:06 +0200157 def _build_create_alarm_payload(
158 self,
159 cor_id: int,
160 metric_name: str,
161 ns_id: str,
162 vdu_name: str,
163 vnf_member_index: str,
164 threshold: int,
165 statistic: str,
166 operation: str,
Atul Agarwale9228cf2021-03-19 10:11:38 +0000167 action: str,
garciadeblas4584f8e2021-05-14 16:50:06 +0200168 ):
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300169 alarm_create_request = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200170 "correlation_id": cor_id,
171 "alarm_name": "osm_alarm_{}_{}_{}_{}".format(
172 ns_id, vnf_member_index, vdu_name, metric_name
173 ),
174 "metric_name": metric_name,
175 "operation": operation,
176 "severity": "critical",
177 "threshold_value": threshold,
178 "statistic": statistic,
Atul Agarwale9228cf2021-03-19 10:11:38 +0000179 "action": action,
garciadeblas4584f8e2021-05-14 16:50:06 +0200180 "tags": {
181 "ns_id": ns_id,
182 "vdu_name": vdu_name,
183 "vnf_member_index": vnf_member_index,
184 },
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300185 }
186 msg = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200187 "alarm_create_request": alarm_create_request,
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300188 }
189 return msg
190
garciadeblas4584f8e2021-05-14 16:50:06 +0200191 def _build_delete_alarm_payload(
192 self,
193 cor_id: int,
194 ns_id: str,
195 vdu_name: str,
196 vnf_member_index: str,
197 alarm_uuid: str,
198 ):
Benjamin Diazf35f9142018-10-08 16:25:36 -0300199 alarm_delete_request = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200200 "correlation_id": cor_id,
201 "alarm_uuid": alarm_uuid,
202 "tags": {
203 "ns_id": ns_id,
204 "vdu_name": vdu_name,
205 "vnf_member_index": vnf_member_index,
206 },
Benjamin Diazf35f9142018-10-08 16:25:36 -0300207 }
208 msg = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200209 "alarm_delete_request": alarm_delete_request,
Benjamin Diazf35f9142018-10-08 16:25:36 -0300210 }
211 return msg