blob: 13d0ab97878f7ee748d5b7950616027e3e3d11e2 [file] [log] [blame]
Zsolt Haraszti656ecc62016-12-28 15:08:23 -08001#
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#
16import grpc
17import structlog
schowdhury9e247752017-07-14 06:56:20 -070018import os
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080019from concurrent import futures
20
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080021from voltha.protos import third_party
Nikolay Titov89004ec2017-06-19 18:22:42 -040022
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080023_ = third_party
24
25log = structlog.get_logger()
26
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080027class GrpcServer(object):
28
Nikolay Titov89004ec2017-06-19 18:22:42 -040029 def __init__(self, port, ponsim, x_pon_sim):
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080030 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 Titov89004ec2017-06-19 18:22:42 -040034 self.x_pon_sim = x_pon_sim
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080035
Shad Ansarida0f3a42017-07-19 09:51:06 -070036 '''
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 Haraszti656ecc62016-12-28 15:08:23 -080042 log.debug('starting')
Shad Ansarida0f3a42017-07-19 09:51:06 -070043 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)
schowdhury9e247752017-07-14 06:56:20 -070046
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 Haraszti656ecc62016-12-28 15:08:23 -080062 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')