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 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 26 | from voltha.protos.ponsim_pb2 import XPonSimServicer, add_XPonSimServicer_to_server |
| 27 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 28 | _ = third_party |
| 29 | |
| 30 | log = structlog.get_logger() |
| 31 | |
| 32 | |
| 33 | class FlowUpdateHandler(PonSimServicer): |
| 34 | |
| 35 | def __init__(self, thread_pool, ponsim): |
| 36 | self.thread_pool = thread_pool |
| 37 | self.ponsim = ponsim |
| 38 | |
| 39 | @twisted_async |
| 40 | def GetDeviceInfo(self, request, context): |
| 41 | log.info('get-device-info') |
| 42 | ports = self.ponsim.get_ports() |
| 43 | return PonSimDeviceInfo( |
| 44 | nni_port=ports[0], |
| 45 | uni_ports=ports[1:] |
| 46 | ) |
| 47 | |
| 48 | @twisted_async |
| 49 | def UpdateFlowTable(self, request, context): |
| 50 | log.info('flow-table-update', request=request, port=request.port) |
| 51 | if request.port == 0: |
| 52 | # by convention this is the olt port |
| 53 | self.ponsim.olt_install_flows(request.flows) |
| 54 | else: |
| 55 | self.ponsim.onu_install_flows(request.port, request.flows) |
| 56 | return Empty() |
| 57 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 58 | def GetStats(self, request, context): |
| 59 | return self.ponsim.get_stats() |
| 60 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 61 | class XPonHandler(XPonSimServicer): |
| 62 | |
| 63 | def __init__(self, thread_pool, x_pon_sim): |
| 64 | self.thread_pool = thread_pool |
| 65 | self.x_pon_sim = x_pon_sim |
| 66 | |
| 67 | def CreateInterface(self, request, context): |
| 68 | self.x_pon_sim.CreateInterface(request) |
| 69 | return Empty() |
| 70 | |
| 71 | def UpdateInterface(self, request, context): |
| 72 | self.x_pon_sim.UpdateInterface(request) |
| 73 | return Empty() |
| 74 | |
| 75 | def RemoveInterface(self, request, context): |
| 76 | self.x_pon_sim.RemoveInterface(request) |
| 77 | return Empty() |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 78 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 79 | class GrpcServer(object): |
| 80 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 81 | def __init__(self, port, ponsim, x_pon_sim): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 82 | self.port = port |
| 83 | self.thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 84 | self.server = grpc.server(self.thread_pool) |
| 85 | self.ponsim = ponsim |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 86 | self.x_pon_sim = x_pon_sim |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 87 | |
| 88 | def start(self): |
| 89 | log.debug('starting') |
| 90 | handler = FlowUpdateHandler(self.thread_pool, self.ponsim) |
| 91 | add_PonSimServicer_to_server(handler, self.server) |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 92 | x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim) |
| 93 | add_XPonSimServicer_to_server(x_pon_handler, self.server) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 94 | self.server.add_insecure_port('[::]:%s' % self.port) |
| 95 | self.server.start() |
| 96 | log.info('started') |
| 97 | |
| 98 | def stop(self, grace=0): |
| 99 | log.debug('stopping') |
| 100 | self.server.stop(grace) |
| 101 | log.info('stopped') |