blob: 962a6f9cedd816626371275224b600221fa74506 [file] [log] [blame]
Benjamin Diazde3d5702018-11-22 17:27:35 -03001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
Benjamin Diaz51f44862018-11-15 10:27:12 -03004# *************************************************************
Benjamin Diazde3d5702018-11-22 17:27:35 -03005
Benjamin Diaz51f44862018-11-15 10:27:12 -03006# This file is part of OSM Monitoring module
Benjamin Diazde3d5702018-11-22 17:27:35 -03007# 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 Diaz51f44862018-11-15 10:27:12 -030013# http://www.apache.org/licenses/LICENSE-2.0
Benjamin Diazde3d5702018-11-22 17:27:35 -030014
Benjamin Diaz51f44862018-11-15 10:27:12 -030015# Unless required by applicable law or agreed to in writing, software
Benjamin Diazde3d5702018-11-22 17:27:35 -030016# 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 Diaz51f44862018-11-15 10:27:12 -030020# For those usages not covered by the Apache License, Version 2.0 please
Benjamin Diazde3d5702018-11-22 17:27:35 -030021# contact: bdiaz@whitestack.com or glavado@whitestack.com
22##
Benjamin Diaza97bdb32019-04-10 15:22:22 -030023"""
24MON component in charge of CRUD operations for vim_accounts and alarms. It uses the message bus to communicate.
25"""
Benjamin Diaz274a6e92018-11-26 13:14:33 -030026import asyncio
Benjamin Diaz51f44862018-11-15 10:27:12 -030027import json
28import logging
Atul Agarwala353daa2021-06-14 10:46:09 +000029import time
Benjamin Diaz51f44862018-11-15 10:27:12 -030030
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030031from osm_mon.core.config import Config
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030032from osm_mon.core.message_bus_client import MessageBusClient
Benjamin Diaz51f44862018-11-15 10:27:12 -030033from osm_mon.core.response import ResponseBuilder
Benjamin Diaza97bdb32019-04-10 15:22:22 -030034from osm_mon.server.service import ServerService
Benjamin Diaz51f44862018-11-15 10:27:12 -030035
36log = logging.getLogger(__name__)
37
38
39class Server:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030040 def __init__(self, config: Config, loop=None):
41 self.conf = config
Benjamin Diaz274a6e92018-11-26 13:14:33 -030042 if not loop:
43 loop = asyncio.get_event_loop()
44 self.loop = loop
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030045 self.msg_bus = MessageBusClient(config)
Benjamin Diaza97bdb32019-04-10 15:22:22 -030046 self.service = ServerService(config)
bravof088fbd32021-05-10 11:05:20 -040047 self.service.populate_prometheus()
Benjamin Diaz51f44862018-11-15 10:27:12 -030048
49 def run(self):
Benjamin Diaz274a6e92018-11-26 13:14:33 -030050 self.loop.run_until_complete(self.start())
Benjamin Diaz51f44862018-11-15 10:27:12 -030051
Atul Agarwala353daa2021-06-14 10:46:09 +000052 async def start(self, wait_time=5):
garciadeblas8e4179f2021-05-14 16:47:03 +020053 topics = ["alarm_request"]
Atul Agarwala353daa2021-06-14 10:46:09 +000054 while True:
55 try:
56 await self.msg_bus.aioread(topics, self._process_msg)
57 log.info("Sucessfully subscribed to kafka topic(s) %s", str(topics))
58 break
59 except Exception as e:
60 # Failed to subscribe to kafka topic
61 log.error("Error when subscribing to topic(s) %s", str(topics))
62 log.exception("Exception %s", str(e))
63 # Wait for some time for kaka to stabilize and then reattempt to subscribe again
64 time.sleep(wait_time)
65 log.info("Retrying to subscribe the kafka topic(s) %s", str(topics))
Benjamin Diaz51f44862018-11-15 10:27:12 -030066
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030067 async def _process_msg(self, topic, key, values):
68 log.info("Message arrived: %s", values)
Benjamin Diaz51f44862018-11-15 10:27:12 -030069 try:
Benjamin Diaz090df142019-01-30 13:01:54 -030070
Benjamin Diaz416a7532019-07-29 12:00:38 -030071 if topic == "alarm_request":
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030072 if key == "create_alarm_request":
garciadeblas8e4179f2021-05-14 16:47:03 +020073 alarm_details = values["alarm_create_request"]
74 cor_id = alarm_details["correlation_id"]
Benjamin Diaz51f44862018-11-15 10:27:12 -030075 response_builder = ResponseBuilder()
76 try:
Benjamin Diaza97bdb32019-04-10 15:22:22 -030077 alarm = self.service.create_alarm(
garciadeblas8e4179f2021-05-14 16:47:03 +020078 alarm_details["alarm_name"],
79 alarm_details["threshold_value"],
80 alarm_details["operation"].lower(),
81 alarm_details["severity"].lower(),
82 alarm_details["statistic"].lower(),
83 alarm_details["metric_name"],
Atul Agarwal927a5842021-03-18 07:54:40 +000084 alarm_details["action"],
garciadeblas8e4179f2021-05-14 16:47:03 +020085 alarm_details["tags"],
Benjamin Diaz51f44862018-11-15 10:27:12 -030086 )
garciadeblas8e4179f2021-05-14 16:47:03 +020087 response = response_builder.generate_response(
88 "create_alarm_response",
89 cor_id=cor_id,
90 status=True,
91 alarm_id=alarm.uuid,
92 )
Benjamin Diaz51f44862018-11-15 10:27:12 -030093 except Exception:
94 log.exception("Error creating alarm: ")
garciadeblas8e4179f2021-05-14 16:47:03 +020095 response = response_builder.generate_response(
96 "create_alarm_response",
97 cor_id=cor_id,
98 status=False,
99 alarm_id=None,
100 )
101 await self._publish_response(
102 "alarm_response_" + str(cor_id),
103 "create_alarm_response",
104 response,
105 )
Benjamin Diaz274a6e92018-11-26 13:14:33 -0300106
Benjamin Diaz5ac7c082019-02-06 11:58:00 -0300107 if key == "delete_alarm_request":
garciadeblas8e4179f2021-05-14 16:47:03 +0200108 alarm_details = values["alarm_delete_request"]
109 alarm_uuid = alarm_details["alarm_uuid"]
Benjamin Diazde3d5702018-11-22 17:27:35 -0300110 response_builder = ResponseBuilder()
garciadeblas8e4179f2021-05-14 16:47:03 +0200111 cor_id = alarm_details["correlation_id"]
Benjamin Diaz51f44862018-11-15 10:27:12 -0300112 try:
Benjamin Diaza97bdb32019-04-10 15:22:22 -0300113 self.service.delete_alarm(alarm_uuid)
garciadeblas8e4179f2021-05-14 16:47:03 +0200114 response = response_builder.generate_response(
115 "delete_alarm_response",
116 cor_id=cor_id,
117 status=True,
118 alarm_id=alarm_uuid,
119 )
Benjamin Diaz51f44862018-11-15 10:27:12 -0300120 except Exception:
Benjamin Diazde3d5702018-11-22 17:27:35 -0300121 log.exception("Error deleting alarm: ")
garciadeblas8e4179f2021-05-14 16:47:03 +0200122 response = response_builder.generate_response(
123 "delete_alarm_response",
124 cor_id=cor_id,
125 status=False,
126 alarm_id=alarm_uuid,
127 )
128 await self._publish_response(
129 "alarm_response_" + str(cor_id),
130 "delete_alarm_response",
131 response,
132 )
Benjamin Diaz51f44862018-11-15 10:27:12 -0300133
134 except Exception:
135 log.exception("Exception processing message: ")
136
Benjamin Diaz274a6e92018-11-26 13:14:33 -0300137 async def _publish_response(self, topic: str, key: str, msg: dict):
garciadeblas8e4179f2021-05-14 16:47:03 +0200138 log.info(
139 "Sending response %s to topic %s with key %s", json.dumps(msg), topic, key
140 )
Benjamin Diaz5ac7c082019-02-06 11:58:00 -0300141 await self.msg_bus.aiowrite(topic, key, msg)