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 |
schowdhury | 9e24775 | 2017-07-14 06:56:20 -0700 | [diff] [blame] | 18 | import os |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 19 | from concurrent import futures |
| 20 | |
| 21 | from common.utils.grpc_utils import twisted_async |
| 22 | from voltha.protos import third_party |
| 23 | from voltha.protos.ponsim_pb2 import PonSimServicer, \ |
| 24 | add_PonSimServicer_to_server, PonSimDeviceInfo |
| 25 | from google.protobuf.empty_pb2 import Empty |
| 26 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 27 | from voltha.protos.ponsim_pb2 import XPonSimServicer, add_XPonSimServicer_to_server |
| 28 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 29 | _ = third_party |
| 30 | |
| 31 | log = structlog.get_logger() |
| 32 | |
| 33 | |
| 34 | class FlowUpdateHandler(PonSimServicer): |
| 35 | |
| 36 | def __init__(self, thread_pool, ponsim): |
| 37 | self.thread_pool = thread_pool |
| 38 | self.ponsim = ponsim |
| 39 | |
| 40 | @twisted_async |
| 41 | def GetDeviceInfo(self, request, context): |
| 42 | log.info('get-device-info') |
| 43 | ports = self.ponsim.get_ports() |
| 44 | return PonSimDeviceInfo( |
| 45 | nni_port=ports[0], |
| 46 | uni_ports=ports[1:] |
| 47 | ) |
| 48 | |
| 49 | @twisted_async |
| 50 | def UpdateFlowTable(self, request, context): |
| 51 | log.info('flow-table-update', request=request, port=request.port) |
| 52 | if request.port == 0: |
| 53 | # by convention this is the olt port |
| 54 | self.ponsim.olt_install_flows(request.flows) |
| 55 | else: |
| 56 | self.ponsim.onu_install_flows(request.port, request.flows) |
| 57 | return Empty() |
| 58 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 59 | def GetStats(self, request, context): |
| 60 | return self.ponsim.get_stats() |
| 61 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 62 | class XPonHandler(XPonSimServicer): |
| 63 | |
| 64 | def __init__(self, thread_pool, x_pon_sim): |
| 65 | self.thread_pool = thread_pool |
| 66 | self.x_pon_sim = x_pon_sim |
| 67 | |
| 68 | def CreateInterface(self, request, context): |
| 69 | self.x_pon_sim.CreateInterface(request) |
| 70 | return Empty() |
| 71 | |
| 72 | def UpdateInterface(self, request, context): |
| 73 | self.x_pon_sim.UpdateInterface(request) |
| 74 | return Empty() |
| 75 | |
| 76 | def RemoveInterface(self, request, context): |
| 77 | self.x_pon_sim.RemoveInterface(request) |
| 78 | return Empty() |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 79 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 80 | class GrpcServer(object): |
| 81 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 82 | def __init__(self, port, ponsim, x_pon_sim): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 83 | self.port = port |
| 84 | self.thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 85 | self.server = grpc.server(self.thread_pool) |
| 86 | self.ponsim = ponsim |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 87 | self.x_pon_sim = x_pon_sim |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 88 | |
| 89 | def start(self): |
| 90 | log.debug('starting') |
| 91 | handler = FlowUpdateHandler(self.thread_pool, self.ponsim) |
| 92 | add_PonSimServicer_to_server(handler, self.server) |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 93 | x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim) |
| 94 | add_XPonSimServicer_to_server(x_pon_handler, self.server) |
schowdhury | 9e24775 | 2017-07-14 06:56:20 -0700 | [diff] [blame] | 95 | |
| 96 | # read in key and certificate |
| 97 | try: |
| 98 | voltha_key = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.key") |
| 99 | with open(voltha_key) as f: |
| 100 | private_key = f.read() |
| 101 | |
| 102 | voltha_cert = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.crt") |
| 103 | with open(voltha_cert) as f: |
| 104 | certificate_chain = f.read() |
| 105 | except Exception as e: |
| 106 | log.error('failed-to-read-cert-keys', reason=e) |
| 107 | |
| 108 | # create server credentials |
| 109 | server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain,),)) |
| 110 | self.server.add_secure_port('[::]:%s' % self.port, server_credentials) |
| 111 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 112 | self.server.start() |
| 113 | log.info('started') |
| 114 | |
| 115 | def stop(self, grace=0): |
| 116 | log.debug('stopping') |
| 117 | self.server.stop(grace) |
| 118 | log.info('stopped') |