blob: 2d04723b7fef0e28687c9aeb8b7e21e1abfc012d [file] [log] [blame]
Shad Ansari0cc92302019-04-03 11:34:49 -07001#
2# Copyright 2019 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
Shad Ansari2fbda7b2019-04-20 22:56:02 -070017import sys
Shad Ansariea2e6c22019-05-20 20:21:55 +000018import structlog
Shad Ansari0cc92302019-04-03 11:34:49 -070019import grpc
Shad Ansariea2e6c22019-05-20 20:21:55 +000020from multiprocessing import Process
21from confluent_kafka import Producer
22from simplejson import dumps
23from google.protobuf.json_format import MessageToJson
Shad Ansari2fbda7b2019-04-20 22:56:02 -070024
Shad Ansari0cc92302019-04-03 11:34:49 -070025from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
26
27
Shad Ansariea2e6c22019-05-20 20:21:55 +000028log = structlog.get_logger()
Shad Ansari0cc92302019-04-03 11:34:49 -070029
Shad Ansari0cc92302019-04-03 11:34:49 -070030
Shad Ansariea2e6c22019-05-20 20:21:55 +000031def kafka_send_pb(p, topic, ind):
32 p.produce(topic, dumps(MessageToJson(
33 ind, including_default_value_fields=True)))
34
35
36def process_indications(broker, host_and_port):
Shad Ansari2fbda7b2019-04-20 22:56:02 -070037 channel = grpc.insecure_channel(host_and_port)
38 stub = openolt_pb2_grpc.OpenoltStub(channel)
39 stream = stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari0cc92302019-04-03 11:34:49 -070040
Shad Ansariea2e6c22019-05-20 20:21:55 +000041 default_topic = 'openolt.ind-{}'.format(host_and_port.split(':')[0])
Shad Ansari12acc9a2019-05-20 21:46:28 +000042 pktin_topic = 'openolt.pktin-{}'.format(host_and_port.split(':')[0])
Shad Ansariea2e6c22019-05-20 20:21:55 +000043
44 conf = {'bootstrap.servers': broker}
45
46 p = Producer(**conf)
Shad Ansarifc3c4e12019-04-19 16:54:01 -070047
Shad Ansari2fbda7b2019-04-20 22:56:02 -070048 while True:
49 try:
50 # get the next indication from olt
Shad Ansariea2e6c22019-05-20 20:21:55 +000051 print('waiting for indication...')
Shad Ansari2fbda7b2019-04-20 22:56:02 -070052 ind = next(stream)
53 except Exception as e:
54 log.warn('openolt grpc connection lost', error=e)
55 ind = openolt_pb2.Indication()
56 ind.olt_ind.oper_state = 'down'
Shad Ansariea2e6c22019-05-20 20:21:55 +000057 kafka_send_pb(p, default_topic, ind)
Shad Ansari2fbda7b2019-04-20 22:56:02 -070058 break
59 else:
Shad Ansaria03b1332019-04-24 23:31:36 -070060 log.debug("openolt grpc rx indication", indication=ind)
Shad Ansari12acc9a2019-05-20 21:46:28 +000061 if ind.HasField('pkt_ind'):
62 kafka_send_pb(p, pktin_topic, ind)
63 else:
64 kafka_send_pb(p, default_topic, ind)
Shad Ansari0cc92302019-04-03 11:34:49 -070065
Shad Ansari2fbda7b2019-04-20 22:56:02 -070066
67if __name__ == '__main__':
68 if len(sys.argv) < 2:
69 sys.stderr.write('Usage: %s <olt hostname or ip>\n\n' % sys.argv[0])
70 sys.exit(1)
71
72 broker = sys.argv[1]
73 host = sys.argv[2]
74
Shad Ansariea2e6c22019-05-20 20:21:55 +000075 try:
76 # Start indications_process
77 log.debug('openolt grpc starting')
78 indications_process = Process(
79 target=process_indications,
80 args=(broker, host,))
81 indications_process.start()
82 except Exception as e:
83 log.exception('indication start failed', e=e)
84 else:
85 log.debug('openolt grpc started')
Shad Ansari3b1bfaf2019-04-24 22:06:58 -070086
Shad Ansariea2e6c22019-05-20 20:21:55 +000087 try:
88 indications_process.join()
89 except KeyboardInterrupt:
90 indications_process.terminate()