| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Whitestack, LLC |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 4 | # ************************************************************* |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 5 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 6 | # This file is part of OSM Monitoring module |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 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 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 14 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 15 | # Unless required by applicable law or agreed to in writing, software |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 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. |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 21 | # contact: bdiaz@whitestack.com or glavado@whitestack.com |
| 22 | ## |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 23 | """ |
| 24 | MON component in charge of CRUD operations for vim_accounts and alarms. It uses the message bus to communicate. |
| 25 | """ |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 26 | import asyncio |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 27 | import json |
| 28 | import logging |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 29 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 30 | from osm_mon.core.config import Config |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 31 | from osm_mon.core.message_bus_client import MessageBusClient |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 32 | from osm_mon.core.response import ResponseBuilder |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 33 | from osm_mon.server.service import ServerService |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 34 | |
| 35 | log = logging.getLogger(__name__) |
| 36 | |
| 37 | |
| 38 | class Server: |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 39 | def __init__(self, config: Config, loop=None): |
| 40 | self.conf = config |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 41 | if not loop: |
| 42 | loop = asyncio.get_event_loop() |
| 43 | self.loop = loop |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 44 | self.msg_bus = MessageBusClient(config) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 45 | self.service = ServerService(config) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 46 | |
| 47 | def run(self): |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 48 | self.loop.run_until_complete(self.start()) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 49 | |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 50 | async def start(self): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 51 | topics = ["alarm_request"] |
| palsus | a949ff9 | 2021-04-06 11:47:21 +0000 | [diff] [blame] | 52 | try: |
| 53 | await self.msg_bus.aioread(topics, self._process_msg) |
| 54 | except Exception as e: |
| 55 | # Failed to subscribe to kafka topic |
| 56 | log.exception("Error when subscribing to topics %s", str(topics)) |
| 57 | raise e |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 58 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 59 | async def _process_msg(self, topic, key, values): |
| 60 | log.info("Message arrived: %s", values) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 61 | try: |
| Benjamin Diaz | 090df14 | 2019-01-30 13:01:54 -0300 | [diff] [blame] | 62 | |
| Benjamin Diaz | 416a753 | 2019-07-29 12:00:38 -0300 | [diff] [blame] | 63 | if topic == "alarm_request": |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 64 | if key == "create_alarm_request": |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 65 | alarm_details = values["alarm_create_request"] |
| 66 | cor_id = alarm_details["correlation_id"] |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 67 | response_builder = ResponseBuilder() |
| 68 | try: |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 69 | alarm = self.service.create_alarm( |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 70 | alarm_details["alarm_name"], |
| 71 | alarm_details["threshold_value"], |
| 72 | alarm_details["operation"].lower(), |
| 73 | alarm_details["severity"].lower(), |
| 74 | alarm_details["statistic"].lower(), |
| 75 | alarm_details["metric_name"], |
| 76 | alarm_details["tags"], |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 77 | ) |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 78 | response = response_builder.generate_response( |
| 79 | "create_alarm_response", |
| 80 | cor_id=cor_id, |
| 81 | status=True, |
| 82 | alarm_id=alarm.uuid, |
| 83 | ) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 84 | except Exception: |
| 85 | log.exception("Error creating alarm: ") |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 86 | response = response_builder.generate_response( |
| 87 | "create_alarm_response", |
| 88 | cor_id=cor_id, |
| 89 | status=False, |
| 90 | alarm_id=None, |
| 91 | ) |
| 92 | await self._publish_response( |
| 93 | "alarm_response_" + str(cor_id), |
| 94 | "create_alarm_response", |
| 95 | response, |
| 96 | ) |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 97 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 98 | if key == "delete_alarm_request": |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 99 | alarm_details = values["alarm_delete_request"] |
| 100 | alarm_uuid = alarm_details["alarm_uuid"] |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 101 | response_builder = ResponseBuilder() |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 102 | cor_id = alarm_details["correlation_id"] |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 103 | try: |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 104 | self.service.delete_alarm(alarm_uuid) |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 105 | response = response_builder.generate_response( |
| 106 | "delete_alarm_response", |
| 107 | cor_id=cor_id, |
| 108 | status=True, |
| 109 | alarm_id=alarm_uuid, |
| 110 | ) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 111 | except Exception: |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 112 | log.exception("Error deleting alarm: ") |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 113 | response = response_builder.generate_response( |
| 114 | "delete_alarm_response", |
| 115 | cor_id=cor_id, |
| 116 | status=False, |
| 117 | alarm_id=alarm_uuid, |
| 118 | ) |
| 119 | await self._publish_response( |
| 120 | "alarm_response_" + str(cor_id), |
| 121 | "delete_alarm_response", |
| 122 | response, |
| 123 | ) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 124 | |
| 125 | except Exception: |
| 126 | log.exception("Exception processing message: ") |
| 127 | |
| Benjamin Diaz | 274a6e9 | 2018-11-26 13:14:33 -0300 | [diff] [blame] | 128 | async def _publish_response(self, topic: str, key: str, msg: dict): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame^] | 129 | log.info( |
| 130 | "Sending response %s to topic %s with key %s", json.dumps(msg), topic, key |
| 131 | ) |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 132 | await self.msg_bus.aiowrite(topic, key, msg) |