blob: 5c6786508107ee2198f96d4646df305c823b2ff5 [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16from google.protobuf.json_format import MessageToDict
17from google.protobuf.message import Message
18from simplejson import dumps
19from common.event_bus import EventBusClient
20from voltha.protos.omci_mib_db_pb2 import OpenOmciEvent
21from voltha.protos.omci_alarm_db_pb2 import AlarmOpenOmciEvent
22from common.utils.json_format import MessageToDict
23
24
25class OpenOmciEventBus(object):
26 """ Event bus for publishing OpenOMCI related events. """
27 __slots__ = (
28 '_event_bus_client', # The event bus client used to publish events.
29 '_topic' # the topic to publish to
30 )
31
32 def __init__(self):
33 self._event_bus_client = EventBusClient()
34 self._topic = 'openomci-events'
35
36 def message_to_dict(m):
37 return MessageToDict(m, True, True, False)
38
39 def advertise(self, event_type, data):
40 if isinstance(data, Message):
41 msg = dumps(MessageToDict(data, True, True))
42 elif isinstance(data, dict):
43 msg = dumps(data)
44 else:
45 msg = str(data)
46
47 event_func = AlarmOpenOmciEvent if 'AlarmSynchronizer' in msg \
48 else OpenOmciEvent
49 event = event_func(
50 type=event_type,
51 data=msg
52 )
53
54 self._event_bus_client.publish(self._topic, event)