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 |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 22 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 23 | _ = third_party |
| 24 | |
| 25 | log = structlog.get_logger() |
| 26 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 27 | class GrpcServer(object): |
| 28 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 29 | def __init__(self, port, ponsim, x_pon_sim): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 30 | self.port = port |
| 31 | self.thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 32 | self.server = grpc.server(self.thread_pool) |
| 33 | self.ponsim = ponsim |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 34 | self.x_pon_sim = x_pon_sim |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 35 | |
Shad Ansari | da0f3a4 | 2017-07-19 09:51:06 -0700 | [diff] [blame^] | 36 | ''' |
| 37 | service_list: a list of (add_xyzSimServicer_to_server, xyzServicerClass) |
| 38 | e.g. [(add_PonSimServicer_to_server, FlowUpdateHandler), |
| 39 | (add_XPonSimServicer_to_server, XPonHandler)] |
| 40 | ''' |
| 41 | def start(self, service_list): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 42 | log.debug('starting') |
Shad Ansari | da0f3a4 | 2017-07-19 09:51:06 -0700 | [diff] [blame^] | 43 | for add_x_to_server, xServiceClass in service_list: |
| 44 | x_handler = xServiceClass(self.thread_pool, self.ponsim) |
| 45 | add_x_to_server(x_handler, self.server) |
schowdhury | 9e24775 | 2017-07-14 06:56:20 -0700 | [diff] [blame] | 46 | |
| 47 | # read in key and certificate |
| 48 | try: |
| 49 | voltha_key = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.key") |
| 50 | with open(voltha_key) as f: |
| 51 | private_key = f.read() |
| 52 | |
| 53 | voltha_cert = os.path.join(os.environ.get('VOLTHA_BASE'),"pki/voltha.crt") |
| 54 | with open(voltha_cert) as f: |
| 55 | certificate_chain = f.read() |
| 56 | except Exception as e: |
| 57 | log.error('failed-to-read-cert-keys', reason=e) |
| 58 | |
| 59 | # create server credentials |
| 60 | server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain,),)) |
| 61 | self.server.add_secure_port('[::]:%s' % self.port, server_credentials) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 62 | self.server.start() |
| 63 | log.info('started') |
| 64 | |
| 65 | def stop(self, grace=0): |
| 66 | log.debug('stopping') |
| 67 | self.server.stop(grace) |
| 68 | log.info('stopped') |