Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 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 | import grpc |
| 17 | import structlog |
| 18 | from concurrent import futures |
| 19 | |
| 20 | from common.utils.grpc_utils import twisted_async |
| 21 | from voltha.protos import third_party |
| 22 | from voltha.protos.ponsim_pb2 import PonSimServicer, \ |
| 23 | add_PonSimServicer_to_server, PonSimDeviceInfo |
| 24 | from google.protobuf.empty_pb2 import Empty |
| 25 | |
| 26 | _ = third_party |
| 27 | |
| 28 | log = structlog.get_logger() |
| 29 | |
| 30 | |
| 31 | class FlowUpdateHandler(PonSimServicer): |
| 32 | |
| 33 | def __init__(self, thread_pool, ponsim): |
| 34 | self.thread_pool = thread_pool |
| 35 | self.ponsim = ponsim |
| 36 | |
| 37 | @twisted_async |
| 38 | def GetDeviceInfo(self, request, context): |
| 39 | log.info('get-device-info') |
| 40 | ports = self.ponsim.get_ports() |
| 41 | return PonSimDeviceInfo( |
| 42 | nni_port=ports[0], |
| 43 | uni_ports=ports[1:] |
| 44 | ) |
| 45 | |
| 46 | @twisted_async |
| 47 | def UpdateFlowTable(self, request, context): |
| 48 | log.info('flow-table-update', request=request, port=request.port) |
| 49 | if request.port == 0: |
| 50 | # by convention this is the olt port |
| 51 | self.ponsim.olt_install_flows(request.flows) |
| 52 | else: |
| 53 | self.ponsim.onu_install_flows(request.port, request.flows) |
| 54 | return Empty() |
| 55 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 56 | def GetStats(self, request, context): |
| 57 | return self.ponsim.get_stats() |
| 58 | |
| 59 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 60 | class GrpcServer(object): |
| 61 | |
| 62 | def __init__(self, port, ponsim): |
| 63 | self.port = port |
| 64 | self.thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 65 | self.server = grpc.server(self.thread_pool) |
| 66 | self.ponsim = ponsim |
| 67 | |
| 68 | def start(self): |
| 69 | log.debug('starting') |
| 70 | handler = FlowUpdateHandler(self.thread_pool, self.ponsim) |
| 71 | add_PonSimServicer_to_server(handler, self.server) |
| 72 | self.server.add_insecure_port('[::]:%s' % self.port) |
| 73 | self.server.start() |
| 74 | log.info('started') |
| 75 | |
| 76 | def stop(self, grace=0): |
| 77 | log.debug('stopping') |
| 78 | self.server.stop(grace) |
| 79 | log.info('stopped') |