blob: c5f5cc2a24bc8dbdcbed2e0377cde81256dd432e [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 Agarwal0e7722a2021-06-15 05:54:05 +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:
40
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030041 def __init__(self, config: Config, loop=None):
42 self.conf = config
Benjamin Diaz274a6e92018-11-26 13:14:33 -030043 if not loop:
44 loop = asyncio.get_event_loop()
45 self.loop = loop
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030046 self.msg_bus = MessageBusClient(config)
Benjamin Diaza97bdb32019-04-10 15:22:22 -030047 self.service = ServerService(config)
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 Agarwal0e7722a2021-06-15 05:54:05 +000052 async def start(self, wait_time=5):
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030053 topics = [
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030054 "alarm_request"
55 ]
Atul Agarwal0e7722a2021-06-15 05:54:05 +000056 while True:
57 try:
58 await self.msg_bus.aioread(topics, self._process_msg)
59 break
60 except Exception as e:
61 # Failed to subscribe to kafka topic
62 log.exception("Error when subscribing to topics %s", str(topics))
63 log.exception("Exception %s", str(e))
64 # Wait for some time for kaka to stabilize and then reattempt to subscribe again
65 time.sleep(wait_time)
66 log.info("Retrying to subscribe the kafka topic(s) %s", str(topics))
Benjamin Diaz51f44862018-11-15 10:27:12 -030067
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030068 async def _process_msg(self, topic, key, values):
69 log.info("Message arrived: %s", values)
Benjamin Diaz51f44862018-11-15 10:27:12 -030070 try:
Benjamin Diaz090df142019-01-30 13:01:54 -030071
Benjamin Diaz416a7532019-07-29 12:00:38 -030072 if topic == "alarm_request":
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030073 if key == "create_alarm_request":
Benjamin Diaz51f44862018-11-15 10:27:12 -030074 alarm_details = values['alarm_create_request']
Benjamin Diaz274a6e92018-11-26 13:14:33 -030075 cor_id = alarm_details['correlation_id']
Benjamin Diaz51f44862018-11-15 10:27:12 -030076 response_builder = ResponseBuilder()
77 try:
Benjamin Diaza97bdb32019-04-10 15:22:22 -030078 alarm = self.service.create_alarm(
Benjamin Diaz51f44862018-11-15 10:27:12 -030079 alarm_details['alarm_name'],
80 alarm_details['threshold_value'],
81 alarm_details['operation'].lower(),
82 alarm_details['severity'].lower(),
83 alarm_details['statistic'].lower(),
84 alarm_details['metric_name'],
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030085 alarm_details['tags']
Benjamin Diaz51f44862018-11-15 10:27:12 -030086 )
87 response = response_builder.generate_response('create_alarm_response',
Benjamin Diaz274a6e92018-11-26 13:14:33 -030088 cor_id=cor_id,
Benjamin Diaz51f44862018-11-15 10:27:12 -030089 status=True,
Benjamin Diazde3d5702018-11-22 17:27:35 -030090 alarm_id=alarm.uuid)
Benjamin Diaz51f44862018-11-15 10:27:12 -030091 except Exception:
92 log.exception("Error creating alarm: ")
93 response = response_builder.generate_response('create_alarm_response',
Benjamin Diaz274a6e92018-11-26 13:14:33 -030094 cor_id=cor_id,
Benjamin Diaz51f44862018-11-15 10:27:12 -030095 status=False,
96 alarm_id=None)
Benjamin Diaz274a6e92018-11-26 13:14:33 -030097 await self._publish_response('alarm_response_' + str(cor_id), 'create_alarm_response', response)
98
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030099 if key == "delete_alarm_request":
Benjamin Diaz51f44862018-11-15 10:27:12 -0300100 alarm_details = values['alarm_delete_request']
Benjamin Diaz51f44862018-11-15 10:27:12 -0300101 alarm_uuid = alarm_details['alarm_uuid']
Benjamin Diazde3d5702018-11-22 17:27:35 -0300102 response_builder = ResponseBuilder()
Benjamin Diaz51f44862018-11-15 10:27:12 -0300103 cor_id = alarm_details['correlation_id']
104 try:
Benjamin Diaza97bdb32019-04-10 15:22:22 -0300105 self.service.delete_alarm(alarm_uuid)
Benjamin Diazde3d5702018-11-22 17:27:35 -0300106 response = response_builder.generate_response('delete_alarm_response',
Benjamin Diaz51f44862018-11-15 10:27:12 -0300107 cor_id=cor_id,
108 status=True,
109 alarm_id=alarm_uuid)
110 except Exception:
Benjamin Diazde3d5702018-11-22 17:27:35 -0300111 log.exception("Error deleting alarm: ")
112 response = response_builder.generate_response('delete_alarm_response',
Benjamin Diaz51f44862018-11-15 10:27:12 -0300113 cor_id=cor_id,
114 status=False,
115 alarm_id=alarm_uuid)
Benjamin Diaz274a6e92018-11-26 13:14:33 -0300116 await self._publish_response('alarm_response_' + str(cor_id), 'delete_alarm_response', response)
Benjamin Diaz51f44862018-11-15 10:27:12 -0300117
118 except Exception:
119 log.exception("Exception processing message: ")
120
Benjamin Diaz274a6e92018-11-26 13:14:33 -0300121 async def _publish_response(self, topic: str, key: str, msg: dict):
Benjamin Diaz274a6e92018-11-26 13:14:33 -0300122 log.info("Sending response %s to topic %s with key %s", json.dumps(msg), topic, key)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -0300123 await self.msg_bus.aiowrite(topic, key, msg)