| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 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 json |
| 25 | import logging |
| 26 | import random |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 27 | from json import JSONDecodeError |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 28 | |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 29 | import yaml |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 30 | from aiokafka import AIOKafkaProducer, AIOKafkaConsumer |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 31 | |
| 32 | from osm_policy_module.core.config import Config |
| 33 | |
| 34 | log = logging.getLogger(__name__) |
| 35 | |
| 36 | |
| 37 | class MonClient: |
| Mark Beierl | d37c54c | 2023-05-10 11:15:10 -0400 | [diff] [blame^] | 38 | def __init__(self, config: Config): |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 39 | self.kafka_server = "{}:{}".format( |
| 40 | config.get("message", "host"), config.get("message", "port") |
| 41 | ) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 42 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 43 | 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", |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 52 | action: str = "", |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 53 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 54 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 55 | 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 Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 64 | action, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 65 | ) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 66 | log.debug("Sending create_alarm_request %s", msg) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 67 | producer = AIOKafkaProducer( |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 68 | bootstrap_servers=self.kafka_server, |
| 69 | key_serializer=str.encode, |
| 70 | value_serializer=str.encode, |
| 71 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 72 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 73 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 74 | await producer.send_and_wait( |
| 75 | "alarm_request", key="create_alarm_request", value=json.dumps(msg) |
| 76 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 77 | finally: |
| 78 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 79 | log.debug("Waiting for create_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 80 | consumer = AIOKafkaConsumer( |
| 81 | "alarm_response_" + str(cor_id), |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 82 | bootstrap_servers=self.kafka_server, |
| 83 | key_deserializer=bytes.decode, |
| 84 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 85 | auto_offset_reset="earliest", |
| 86 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 87 | await consumer.start() |
| 88 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 89 | try: |
| 90 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 91 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 92 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 93 | except JSONDecodeError: |
| 94 | content = yaml.safe_load(message.value) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 95 | log.debug("Received create_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 96 | if content["alarm_create_response"]["correlation_id"] == cor_id: |
| 97 | if not content["alarm_create_response"]["status"]: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 98 | raise ValueError("Error creating alarm in MON") |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 99 | alarm_uuid = content["alarm_create_response"]["alarm_uuid"] |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 100 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 101 | finally: |
| 102 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 103 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 104 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 105 | return alarm_uuid |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 106 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 107 | async def delete_alarm( |
| 108 | self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str |
| 109 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 110 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 111 | msg = self._build_delete_alarm_payload( |
| 112 | cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid |
| 113 | ) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 114 | log.debug("Sending delete_alarm_request %s", msg) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 115 | producer = AIOKafkaProducer( |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 116 | bootstrap_servers=self.kafka_server, |
| 117 | key_serializer=str.encode, |
| 118 | value_serializer=str.encode, |
| 119 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 120 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 121 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 122 | await producer.send_and_wait( |
| 123 | "alarm_request", key="delete_alarm_request", value=json.dumps(msg) |
| 124 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 125 | finally: |
| 126 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 127 | log.debug("Waiting for delete_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 128 | consumer = AIOKafkaConsumer( |
| 129 | "alarm_response_" + str(cor_id), |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 130 | bootstrap_servers=self.kafka_server, |
| 131 | key_deserializer=bytes.decode, |
| 132 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 133 | auto_offset_reset="earliest", |
| 134 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 135 | await consumer.start() |
| 136 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 137 | try: |
| 138 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 139 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 140 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 141 | except JSONDecodeError: |
| 142 | content = yaml.safe_load(message.value) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 143 | if content["alarm_delete_response"]["correlation_id"] == cor_id: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 144 | log.debug("Received delete_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 145 | 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 Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 150 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 151 | finally: |
| 152 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 153 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 154 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 155 | return alarm_uuid |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 156 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 157 | 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 Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 167 | action: str, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 168 | ): |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 169 | alarm_create_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 170 | "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 Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 179 | "action": action, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 180 | "tags": { |
| 181 | "ns_id": ns_id, |
| 182 | "vdu_name": vdu_name, |
| 183 | "vnf_member_index": vnf_member_index, |
| 184 | }, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 185 | } |
| 186 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 187 | "alarm_create_request": alarm_create_request, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 188 | } |
| 189 | return msg |
| 190 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 191 | 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 Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 199 | alarm_delete_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 200 | "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 Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 207 | } |
| 208 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 209 | "alarm_delete_request": alarm_delete_request, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 210 | } |
| 211 | return msg |