Resolved Bug 1569 - updated log information
[osm/MON.git] / osm_mon / server / server.py
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 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 """
24 MON component in charge of CRUD operations for vim_accounts and alarms. It uses the message bus to communicate.
25 """
26 import asyncio
27 import json
28 import logging
29 import time
30
31 from osm_mon.core.config import Config
32 from osm_mon.core.message_bus_client import MessageBusClient
33 from osm_mon.core.response import ResponseBuilder
34 from osm_mon.server.service import ServerService
35
36 log = logging.getLogger(__name__)
37
38
39 class Server:
40 def __init__(self, config: Config, loop=None):
41 self.conf = config
42 if not loop:
43 loop = asyncio.get_event_loop()
44 self.loop = loop
45 self.msg_bus = MessageBusClient(config)
46 self.service = ServerService(config)
47 self.service.populate_prometheus()
48
49 def run(self):
50 self.loop.run_until_complete(self.start())
51
52 async def start(self, wait_time=5):
53 topics = ["alarm_request"]
54 while True:
55 try:
56 await self.msg_bus.aioread(topics, self._process_msg)
57 break
58 except Exception as e:
59 # Failed to subscribe to kafka topic
60 log.error("Error when subscribing to topic(s) %s", str(topics))
61 log.exception("Exception %s", str(e))
62 # Wait for some time for kaka to stabilize and then reattempt to subscribe again
63 time.sleep(wait_time)
64 log.info("Retrying to subscribe the kafka topic(s) %s", str(topics))
65
66 async def _process_msg(self, topic, key, values):
67 log.info("Message arrived: %s", values)
68 try:
69
70 if topic == "alarm_request":
71 if key == "create_alarm_request":
72 alarm_details = values["alarm_create_request"]
73 cor_id = alarm_details["correlation_id"]
74 response_builder = ResponseBuilder()
75 try:
76 alarm = self.service.create_alarm(
77 alarm_details["alarm_name"],
78 alarm_details["threshold_value"],
79 alarm_details["operation"].lower(),
80 alarm_details["severity"].lower(),
81 alarm_details["statistic"].lower(),
82 alarm_details["metric_name"],
83 alarm_details["action"],
84 alarm_details["tags"],
85 )
86 response = response_builder.generate_response(
87 "create_alarm_response",
88 cor_id=cor_id,
89 status=True,
90 alarm_id=alarm.uuid,
91 )
92 except Exception:
93 log.exception("Error creating alarm: ")
94 response = response_builder.generate_response(
95 "create_alarm_response",
96 cor_id=cor_id,
97 status=False,
98 alarm_id=None,
99 )
100 await self._publish_response(
101 "alarm_response_" + str(cor_id),
102 "create_alarm_response",
103 response,
104 )
105
106 if key == "delete_alarm_request":
107 alarm_details = values["alarm_delete_request"]
108 alarm_uuid = alarm_details["alarm_uuid"]
109 response_builder = ResponseBuilder()
110 cor_id = alarm_details["correlation_id"]
111 try:
112 self.service.delete_alarm(alarm_uuid)
113 response = response_builder.generate_response(
114 "delete_alarm_response",
115 cor_id=cor_id,
116 status=True,
117 alarm_id=alarm_uuid,
118 )
119 except Exception:
120 log.exception("Error deleting alarm: ")
121 response = response_builder.generate_response(
122 "delete_alarm_response",
123 cor_id=cor_id,
124 status=False,
125 alarm_id=alarm_uuid,
126 )
127 await self._publish_response(
128 "alarm_response_" + str(cor_id),
129 "delete_alarm_response",
130 response,
131 )
132
133 except Exception:
134 log.exception("Exception processing message: ")
135
136 async def _publish_response(self, topic: str, key: str, msg: dict):
137 log.info(
138 "Sending response %s to topic %s with key %s", json.dumps(msg), topic, key
139 )
140 await self.msg_bus.aiowrite(topic, key, msg)