| 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 | ## |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 24 | import asyncio |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 25 | import json |
| 26 | import logging |
| 27 | import random |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 28 | from json import JSONDecodeError |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 29 | |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 30 | import yaml |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 31 | from aiokafka import AIOKafkaProducer, AIOKafkaConsumer |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 32 | |
| 33 | from osm_policy_module.core.config import Config |
| 34 | |
| 35 | log = logging.getLogger(__name__) |
| 36 | |
| 37 | |
| 38 | class MonClient: |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 39 | def __init__(self, config: Config, loop=None): |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 40 | self.kafka_server = "{}:{}".format( |
| 41 | config.get("message", "host"), config.get("message", "port") |
| 42 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 43 | if not loop: |
| 44 | loop = asyncio.get_event_loop() |
| 45 | self.loop = loop |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 46 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 47 | async def create_alarm( |
| 48 | self, |
| 49 | metric_name: str, |
| 50 | ns_id: str, |
| 51 | vdu_name: str, |
| 52 | vnf_member_index: str, |
| 53 | threshold: int, |
| 54 | operation: str, |
| 55 | statistic: str = "AVERAGE", |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 56 | action: str = '', |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 57 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 58 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 59 | msg = self._build_create_alarm_payload( |
| 60 | cor_id, |
| 61 | metric_name, |
| 62 | ns_id, |
| 63 | vdu_name, |
| 64 | vnf_member_index, |
| 65 | threshold, |
| 66 | statistic, |
| 67 | operation, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 68 | action, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 69 | ) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 70 | log.debug("Sending create_alarm_request %s", msg) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 71 | producer = AIOKafkaProducer( |
| 72 | loop=self.loop, |
| 73 | bootstrap_servers=self.kafka_server, |
| 74 | key_serializer=str.encode, |
| 75 | value_serializer=str.encode, |
| 76 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 77 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 78 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 79 | await producer.send_and_wait( |
| 80 | "alarm_request", key="create_alarm_request", value=json.dumps(msg) |
| 81 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 82 | finally: |
| 83 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 84 | log.debug("Waiting for create_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 85 | consumer = AIOKafkaConsumer( |
| 86 | "alarm_response_" + str(cor_id), |
| 87 | loop=self.loop, |
| 88 | bootstrap_servers=self.kafka_server, |
| 89 | key_deserializer=bytes.decode, |
| 90 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 91 | auto_offset_reset="earliest", |
| 92 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 93 | await consumer.start() |
| 94 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 95 | try: |
| 96 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 97 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 98 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 99 | except JSONDecodeError: |
| 100 | content = yaml.safe_load(message.value) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 101 | log.debug("Received create_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 102 | if content["alarm_create_response"]["correlation_id"] == cor_id: |
| 103 | if not content["alarm_create_response"]["status"]: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 104 | raise ValueError("Error creating alarm in MON") |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 105 | alarm_uuid = content["alarm_create_response"]["alarm_uuid"] |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 106 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 107 | finally: |
| 108 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 109 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 110 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 111 | return alarm_uuid |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 112 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 113 | async def delete_alarm( |
| 114 | self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str |
| 115 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 116 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 117 | msg = self._build_delete_alarm_payload( |
| 118 | cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid |
| 119 | ) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 120 | log.debug("Sending delete_alarm_request %s", msg) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 121 | producer = AIOKafkaProducer( |
| 122 | loop=self.loop, |
| 123 | bootstrap_servers=self.kafka_server, |
| 124 | key_serializer=str.encode, |
| 125 | value_serializer=str.encode, |
| 126 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 127 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 128 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 129 | await producer.send_and_wait( |
| 130 | "alarm_request", key="delete_alarm_request", value=json.dumps(msg) |
| 131 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 132 | finally: |
| 133 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 134 | log.debug("Waiting for delete_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 135 | consumer = AIOKafkaConsumer( |
| 136 | "alarm_response_" + str(cor_id), |
| 137 | loop=self.loop, |
| 138 | bootstrap_servers=self.kafka_server, |
| 139 | key_deserializer=bytes.decode, |
| 140 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 141 | auto_offset_reset="earliest", |
| 142 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 143 | await consumer.start() |
| 144 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 145 | try: |
| 146 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 147 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 148 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 149 | except JSONDecodeError: |
| 150 | content = yaml.safe_load(message.value) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 151 | if content["alarm_delete_response"]["correlation_id"] == cor_id: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 152 | log.debug("Received delete_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 153 | if not content["alarm_delete_response"]["status"]: |
| 154 | raise ValueError( |
| 155 | "Error deleting alarm in MON. Response status is False." |
| 156 | ) |
| 157 | alarm_uuid = content["alarm_delete_response"]["alarm_uuid"] |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 158 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 159 | finally: |
| 160 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 161 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 162 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 163 | return alarm_uuid |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 164 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 165 | def _build_create_alarm_payload( |
| 166 | self, |
| 167 | cor_id: int, |
| 168 | metric_name: str, |
| 169 | ns_id: str, |
| 170 | vdu_name: str, |
| 171 | vnf_member_index: str, |
| 172 | threshold: int, |
| 173 | statistic: str, |
| 174 | operation: str, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 175 | action: str, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 176 | ): |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 177 | |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 178 | alarm_create_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 179 | "correlation_id": cor_id, |
| 180 | "alarm_name": "osm_alarm_{}_{}_{}_{}".format( |
| 181 | ns_id, vnf_member_index, vdu_name, metric_name |
| 182 | ), |
| 183 | "metric_name": metric_name, |
| 184 | "operation": operation, |
| 185 | "severity": "critical", |
| 186 | "threshold_value": threshold, |
| 187 | "statistic": statistic, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 188 | "action": action, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 189 | "tags": { |
| 190 | "ns_id": ns_id, |
| 191 | "vdu_name": vdu_name, |
| 192 | "vnf_member_index": vnf_member_index, |
| 193 | }, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 194 | } |
| 195 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 196 | "alarm_create_request": alarm_create_request, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 197 | } |
| 198 | return msg |
| 199 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 200 | def _build_delete_alarm_payload( |
| 201 | self, |
| 202 | cor_id: int, |
| 203 | ns_id: str, |
| 204 | vdu_name: str, |
| 205 | vnf_member_index: str, |
| 206 | alarm_uuid: str, |
| 207 | ): |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 208 | alarm_delete_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 209 | "correlation_id": cor_id, |
| 210 | "alarm_uuid": alarm_uuid, |
| 211 | "tags": { |
| 212 | "ns_id": ns_id, |
| 213 | "vdu_name": vdu_name, |
| 214 | "vnf_member_index": vnf_member_index, |
| 215 | }, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 216 | } |
| 217 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 218 | "alarm_delete_request": alarm_delete_request, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 219 | } |
| 220 | return msg |