| 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 = "", |
| Rahul Kumar | bb6bdfa | 2023-11-09 08:45:38 +0000 | [diff] [blame^] | 53 | vnfr: object = None, |
| 54 | vnfd: object = None, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 55 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 56 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 57 | msg = self._build_create_alarm_payload( |
| 58 | cor_id, |
| 59 | metric_name, |
| 60 | ns_id, |
| 61 | vdu_name, |
| 62 | vnf_member_index, |
| 63 | threshold, |
| 64 | statistic, |
| 65 | operation, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 66 | action, |
| Rahul Kumar | bb6bdfa | 2023-11-09 08:45:38 +0000 | [diff] [blame^] | 67 | vnfr, |
| 68 | vnfd, |
| 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( |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 72 | bootstrap_servers=self.kafka_server, |
| 73 | key_serializer=str.encode, |
| 74 | value_serializer=str.encode, |
| 75 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 76 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 77 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 78 | await producer.send_and_wait( |
| 79 | "alarm_request", key="create_alarm_request", value=json.dumps(msg) |
| 80 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 81 | finally: |
| 82 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 83 | log.debug("Waiting for create_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 84 | consumer = AIOKafkaConsumer( |
| 85 | "alarm_response_" + str(cor_id), |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 86 | bootstrap_servers=self.kafka_server, |
| 87 | key_deserializer=bytes.decode, |
| 88 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 89 | auto_offset_reset="earliest", |
| 90 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 91 | await consumer.start() |
| 92 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 93 | try: |
| 94 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 95 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 96 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 97 | except JSONDecodeError: |
| 98 | content = yaml.safe_load(message.value) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 99 | log.debug("Received create_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 100 | if content["alarm_create_response"]["correlation_id"] == cor_id: |
| 101 | if not content["alarm_create_response"]["status"]: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 102 | raise ValueError("Error creating alarm in MON") |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 103 | alarm_uuid = content["alarm_create_response"]["alarm_uuid"] |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 104 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 105 | finally: |
| 106 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 107 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 108 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 109 | return alarm_uuid |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 110 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 111 | async def delete_alarm( |
| 112 | self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str |
| 113 | ): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 114 | cor_id = random.randint(1, 10e7) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 115 | msg = self._build_delete_alarm_payload( |
| 116 | cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid |
| 117 | ) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 118 | log.debug("Sending delete_alarm_request %s", msg) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 119 | producer = AIOKafkaProducer( |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 120 | bootstrap_servers=self.kafka_server, |
| 121 | key_serializer=str.encode, |
| 122 | value_serializer=str.encode, |
| 123 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 124 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 125 | try: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 126 | await producer.send_and_wait( |
| 127 | "alarm_request", key="delete_alarm_request", value=json.dumps(msg) |
| 128 | ) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 129 | finally: |
| 130 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 131 | log.debug("Waiting for delete_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 132 | consumer = AIOKafkaConsumer( |
| 133 | "alarm_response_" + str(cor_id), |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 134 | bootstrap_servers=self.kafka_server, |
| 135 | key_deserializer=bytes.decode, |
| 136 | value_deserializer=bytes.decode, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 137 | auto_offset_reset="earliest", |
| 138 | ) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 139 | await consumer.start() |
| 140 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 141 | try: |
| 142 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 143 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 144 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 145 | except JSONDecodeError: |
| 146 | content = yaml.safe_load(message.value) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 147 | if content["alarm_delete_response"]["correlation_id"] == cor_id: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 148 | log.debug("Received delete_alarm_response %s", content) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 149 | if not content["alarm_delete_response"]["status"]: |
| 150 | raise ValueError( |
| 151 | "Error deleting alarm in MON. Response status is False." |
| 152 | ) |
| 153 | alarm_uuid = content["alarm_delete_response"]["alarm_uuid"] |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 154 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 155 | finally: |
| 156 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 157 | if not alarm_uuid: |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 158 | raise ValueError("No alarm deletion response from MON. Is MON up?") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 159 | return alarm_uuid |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 160 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 161 | def _build_create_alarm_payload( |
| 162 | self, |
| 163 | cor_id: int, |
| 164 | metric_name: str, |
| 165 | ns_id: str, |
| 166 | vdu_name: str, |
| 167 | vnf_member_index: str, |
| 168 | threshold: int, |
| 169 | statistic: str, |
| 170 | operation: str, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 171 | action: str, |
| Rahul Kumar | bb6bdfa | 2023-11-09 08:45:38 +0000 | [diff] [blame^] | 172 | vnfr=None, |
| 173 | vnfd=None, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 174 | ): |
| Rahul Kumar | bb6bdfa | 2023-11-09 08:45:38 +0000 | [diff] [blame^] | 175 | tags = { |
| 176 | "ns_id": ns_id, |
| 177 | "vdu_name": vdu_name, |
| 178 | "vnf_member_index": vnf_member_index, |
| 179 | } |
| 180 | if vnfr and vnfd: |
| 181 | # TODO: Change for multiple DF support |
| 182 | df = vnfd.get("df", [{}])[0] |
| 183 | if "exporters-endpoints" in df: |
| 184 | metric_port = df["exporters-endpoints"].get("metric-port", 9100) |
| 185 | if metric_name.startswith("kpi_"): |
| 186 | metric_name = metric_name.replace("kpi_", "") |
| 187 | metric_name.strip() |
| 188 | for vdu in vnfr["vdur"]: |
| 189 | if vdu["name"] == vdu_name: |
| 190 | vdu_ip = vdu["ip-address"] |
| 191 | tags = {"instance": vdu_ip + ":" + str(metric_port)} |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 192 | alarm_create_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 193 | "correlation_id": cor_id, |
| 194 | "alarm_name": "osm_alarm_{}_{}_{}_{}".format( |
| 195 | ns_id, vnf_member_index, vdu_name, metric_name |
| 196 | ), |
| 197 | "metric_name": metric_name, |
| 198 | "operation": operation, |
| 199 | "severity": "critical", |
| 200 | "threshold_value": threshold, |
| 201 | "statistic": statistic, |
| Atul Agarwal | e9228cf | 2021-03-19 10:11:38 +0000 | [diff] [blame] | 202 | "action": action, |
| Rahul Kumar | bb6bdfa | 2023-11-09 08:45:38 +0000 | [diff] [blame^] | 203 | "tags": tags, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 204 | } |
| 205 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 206 | "alarm_create_request": alarm_create_request, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 207 | } |
| 208 | return msg |
| 209 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 210 | def _build_delete_alarm_payload( |
| 211 | self, |
| 212 | cor_id: int, |
| 213 | ns_id: str, |
| 214 | vdu_name: str, |
| 215 | vnf_member_index: str, |
| 216 | alarm_uuid: str, |
| 217 | ): |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 218 | alarm_delete_request = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 219 | "correlation_id": cor_id, |
| 220 | "alarm_uuid": alarm_uuid, |
| 221 | "tags": { |
| 222 | "ns_id": ns_id, |
| 223 | "vdu_name": vdu_name, |
| 224 | "vnf_member_index": vnf_member_index, |
| 225 | }, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 226 | } |
| 227 | msg = { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 228 | "alarm_delete_request": alarm_delete_request, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 229 | } |
| 230 | return msg |