| 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): |
| 40 | self.kafka_server = '{}:{}'.format(config.get('message', 'host'), |
| 41 | config.get('message', 'port')) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 42 | if not loop: |
| 43 | loop = asyncio.get_event_loop() |
| 44 | self.loop = loop |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 45 | |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 46 | async def create_alarm(self, metric_name: str, ns_id: str, vdu_name: str, vnf_member_index: str, threshold: int, |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 47 | statistic: str, operation: str): |
| 48 | cor_id = random.randint(1, 10e7) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 49 | msg = self._build_create_alarm_payload(cor_id, |
| 50 | metric_name, |
| 51 | ns_id, |
| 52 | vdu_name, |
| 53 | vnf_member_index, |
| 54 | threshold, |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 55 | statistic, |
| 56 | operation) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 57 | log.debug("Sending create_alarm_request %s", msg) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 58 | producer = AIOKafkaProducer(loop=self.loop, |
| 59 | bootstrap_servers=self.kafka_server, |
| 60 | key_serializer=str.encode, |
| 61 | value_serializer=str.encode) |
| 62 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 63 | try: |
| 64 | await producer.send_and_wait("alarm_request", key="create_alarm_request", value=json.dumps(msg)) |
| 65 | finally: |
| 66 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 67 | log.debug("Waiting for create_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 68 | consumer = AIOKafkaConsumer( |
| 69 | "alarm_response_" + str(cor_id), |
| 70 | loop=self.loop, |
| 71 | bootstrap_servers=self.kafka_server, |
| 72 | key_deserializer=bytes.decode, |
| 73 | value_deserializer=bytes.decode, |
| 74 | auto_offset_reset='earliest') |
| 75 | await consumer.start() |
| 76 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 77 | try: |
| 78 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 79 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 80 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 81 | except JSONDecodeError: |
| 82 | content = yaml.safe_load(message.value) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 83 | log.debug("Received create_alarm_response %s", content) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 84 | if content['alarm_create_response']['correlation_id'] == cor_id: |
| 85 | if not content['alarm_create_response']['status']: |
| 86 | raise ValueError("Error creating alarm in MON") |
| 87 | alarm_uuid = content['alarm_create_response']['alarm_uuid'] |
| 88 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 89 | finally: |
| 90 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 91 | if not alarm_uuid: |
| 92 | raise ValueError('No alarm deletion response from MON. Is MON up?') |
| 93 | return alarm_uuid |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 94 | |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 95 | async def delete_alarm(self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str): |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 96 | cor_id = random.randint(1, 10e7) |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 97 | msg = self._build_delete_alarm_payload(cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 98 | log.debug("Sending delete_alarm_request %s", msg) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 99 | producer = AIOKafkaProducer(loop=self.loop, |
| 100 | bootstrap_servers=self.kafka_server, |
| 101 | key_serializer=str.encode, |
| 102 | value_serializer=str.encode) |
| 103 | await producer.start() |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 104 | try: |
| 105 | await producer.send_and_wait("alarm_request", key="delete_alarm_request", value=json.dumps(msg)) |
| 106 | finally: |
| 107 | await producer.stop() |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 108 | log.debug("Waiting for delete_alarm_response...") |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 109 | consumer = AIOKafkaConsumer( |
| 110 | "alarm_response_" + str(cor_id), |
| 111 | loop=self.loop, |
| 112 | bootstrap_servers=self.kafka_server, |
| 113 | key_deserializer=bytes.decode, |
| 114 | value_deserializer=bytes.decode, |
| 115 | auto_offset_reset='earliest') |
| 116 | await consumer.start() |
| 117 | alarm_uuid = None |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 118 | try: |
| 119 | async for message in consumer: |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 120 | try: |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 121 | content = json.loads(message.value) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 122 | except JSONDecodeError: |
| 123 | content = yaml.safe_load(message.value) |
| 124 | if content['alarm_delete_response']['correlation_id'] == cor_id: |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 125 | log.debug("Received delete_alarm_response %s", content) |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 126 | if not content['alarm_delete_response']['status']: |
| 127 | raise ValueError("Error deleting alarm in MON. Response status is False.") |
| 128 | alarm_uuid = content['alarm_delete_response']['alarm_uuid'] |
| 129 | break |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 130 | finally: |
| 131 | await consumer.stop() |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 132 | if not alarm_uuid: |
| 133 | raise ValueError('No alarm deletion response from MON. Is MON up?') |
| 134 | return alarm_uuid |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 135 | |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 136 | def _build_create_alarm_payload(self, cor_id: int, |
| 137 | metric_name: str, |
| 138 | ns_id: str, |
| 139 | vdu_name: str, |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 140 | vnf_member_index: str, |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 141 | threshold: int, |
| 142 | statistic: str, |
| 143 | operation: str): |
| 144 | |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 145 | alarm_create_request = { |
| 146 | 'correlation_id': cor_id, |
| Benjamin Diaz | 127bbba | 2018-11-26 18:05:53 -0300 | [diff] [blame] | 147 | 'alarm_name': 'osm_alarm_{}_{}_{}_{}'.format(ns_id, vnf_member_index, vdu_name, metric_name), |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 148 | 'metric_name': metric_name, |
| 149 | 'ns_id': ns_id, |
| 150 | 'vdu_name': vdu_name, |
| 151 | 'vnf_member_index': vnf_member_index, |
| 152 | 'operation': operation, |
| 153 | 'severity': 'critical', |
| 154 | 'threshold_value': threshold, |
| 155 | 'statistic': statistic |
| 156 | } |
| 157 | msg = { |
| 158 | 'alarm_create_request': alarm_create_request, |
| 159 | } |
| 160 | return msg |
| 161 | |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 162 | def _build_delete_alarm_payload(self, cor_id: int, ns_id: str, vdu_name: str, |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 163 | vnf_member_index: str, alarm_uuid: str): |
| Benjamin Diaz | f35f914 | 2018-10-08 16:25:36 -0300 | [diff] [blame] | 164 | alarm_delete_request = { |
| 165 | 'correlation_id': cor_id, |
| 166 | 'alarm_uuid': alarm_uuid, |
| 167 | 'ns_id': ns_id, |
| 168 | 'vdu_name': vdu_name, |
| 169 | 'vnf_member_index': vnf_member_index |
| 170 | } |
| 171 | msg = { |
| 172 | 'alarm_delete_request': alarm_delete_request, |
| 173 | } |
| 174 | return msg |