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 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 21 | from voltha.protos import third_party |
Shad Ansari | 2d6e483 | 2017-07-21 16:24:14 -0700 | [diff] [blame] | 22 | from voltha.protos.ponsim_pb2 import add_PonSimServicer_to_server |
| 23 | from voltha.protos.ponsim_pb2 import add_XPonSimServicer_to_server |
| 24 | from voltha.adapters.asfvolt16_olt.protos.bal_pb2 import add_BalServicer_to_server |
| 25 | from ponsim_servicer import FlowUpdateHandler, XPonHandler |
| 26 | from bal_servicer import BalHandler |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 27 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 28 | _ = third_party |
| 29 | |
| 30 | log = structlog.get_logger() |
| 31 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 32 | class GrpcServer(object): |
| 33 | |
Shad Ansari | 2d6e483 | 2017-07-21 16:24:14 -0700 | [diff] [blame] | 34 | def __init__(self, port, ponsim, x_pon_sim, device_type): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 35 | self.port = port |
| 36 | self.thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 37 | self.server = grpc.server(self.thread_pool) |
| 38 | self.ponsim = ponsim |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 39 | self.x_pon_sim = x_pon_sim |
Shad Ansari | 2d6e483 | 2017-07-21 16:24:14 -0700 | [diff] [blame] | 40 | self.device_type = device_type |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 41 | |
Shad Ansari | 2d6e483 | 2017-07-21 16:24:14 -0700 | [diff] [blame] | 42 | def start(self): |
| 43 | if self.device_type == 'ponsim': |
| 44 | handler = FlowUpdateHandler(self.thread_pool, self.ponsim) |
| 45 | add_PonSimServicer_to_server(handler, self.server) |
| 46 | x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim) |
| 47 | add_XPonSimServicer_to_server(x_pon_handler, self.server) |
| 48 | else: |
| 49 | handler = BalHandler(self.thread_pool, self.ponsim) |
| 50 | add_BalServicer_to_server(handler, self.server) |
schowdhury | 9e24775 | 2017-07-14 06:56:20 -0700 | [diff] [blame] | 51 | |
| 52 | # read in key and certificate |
| 53 | try: |
| 54 | voltha_key = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.key") |
| 55 | with open(voltha_key) as f: |
| 56 | private_key = f.read() |
| 57 | |
| 58 | voltha_cert = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.crt") |
| 59 | with open(voltha_cert) as f: |
| 60 | certificate_chain = f.read() |
| 61 | except Exception as e: |
| 62 | log.error('failed-to-read-cert-keys', reason=e) |
| 63 | |
| 64 | # create server credentials |
rshetty | e4bd2ed | 2017-07-19 16:38:11 +0530 | [diff] [blame] | 65 | if self.device_type == 'ponsim': |
| 66 | server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain,),)) |
| 67 | self.server.add_secure_port('[::]:%s' % self.port, server_credentials) |
| 68 | else: |
| 69 | self.server.add_insecure_port('[::]:%s' % self.port) |
| 70 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 71 | self.server.start() |
| 72 | log.info('started') |
| 73 | |
| 74 | def stop(self, grace=0): |
| 75 | log.debug('stopping') |
| 76 | self.server.stop(grace) |
| 77 | log.info('stopped') |