Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 1 | # |
| 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 Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 17 | import sys |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 18 | import structlog |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 19 | import grpc |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 20 | from multiprocessing import Process |
| 21 | from confluent_kafka import Producer |
| 22 | from simplejson import dumps |
| 23 | from google.protobuf.json_format import MessageToJson |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 24 | |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 25 | from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2 |
| 26 | |
| 27 | |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 28 | log = structlog.get_logger() |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 29 | |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 30 | |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 31 | def kafka_send_pb(p, topic, ind): |
| 32 | p.produce(topic, dumps(MessageToJson( |
| 33 | ind, including_default_value_fields=True))) |
| 34 | |
| 35 | |
| 36 | def process_indications(broker, host_and_port): |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 37 | channel = grpc.insecure_channel(host_and_port) |
| 38 | stub = openolt_pb2_grpc.OpenoltStub(channel) |
| 39 | stream = stub.EnableIndication(openolt_pb2.Empty()) |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 40 | |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 41 | default_topic = 'openolt.ind-{}'.format(host_and_port.split(':')[0]) |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame] | 42 | pktin_topic = 'openolt.pktin-{}'.format(host_and_port.split(':')[0]) |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 43 | |
| 44 | conf = {'bootstrap.servers': broker} |
| 45 | |
| 46 | p = Producer(**conf) |
Shad Ansari | fc3c4e1 | 2019-04-19 16:54:01 -0700 | [diff] [blame] | 47 | |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 48 | while True: |
| 49 | try: |
| 50 | # get the next indication from olt |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 51 | print('waiting for indication...') |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 52 | 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 Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 57 | kafka_send_pb(p, default_topic, ind) |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 58 | break |
| 59 | else: |
Shad Ansari | a03b133 | 2019-04-24 23:31:36 -0700 | [diff] [blame] | 60 | log.debug("openolt grpc rx indication", indication=ind) |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame] | 61 | if ind.HasField('pkt_ind'): |
| 62 | kafka_send_pb(p, pktin_topic, ind) |
| 63 | else: |
| 64 | kafka_send_pb(p, default_topic, ind) |
Shad Ansari | 0cc9230 | 2019-04-03 11:34:49 -0700 | [diff] [blame] | 65 | |
Shad Ansari | 2fbda7b | 2019-04-20 22:56:02 -0700 | [diff] [blame] | 66 | |
| 67 | if __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 Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 75 | 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 Ansari | 3b1bfaf | 2019-04-24 22:06:58 -0700 | [diff] [blame] | 86 | |
Shad Ansari | ea2e6c2 | 2019-05-20 20:21:55 +0000 | [diff] [blame] | 87 | try: |
| 88 | indications_process.join() |
| 89 | except KeyboardInterrupt: |
| 90 | indications_process.terminate() |