William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1 | # |
| 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 | # |
| 16 | from google.protobuf.json_format import MessageToDict |
| 17 | from google.protobuf.message import Message |
| 18 | from simplejson import dumps |
| 19 | from common.event_bus import EventBusClient |
| 20 | from voltha.protos.omci_mib_db_pb2 import OpenOmciEvent |
| 21 | from voltha.protos.omci_alarm_db_pb2 import AlarmOpenOmciEvent |
| 22 | from common.utils.json_format import MessageToDict |
| 23 | |
| 24 | |
| 25 | class 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) |