blob: 91654339408db5210bb711892afe2017a0c8cacc [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
Shad Ansari2d6e4832017-07-21 16:24:14 -070022from voltha.protos.ponsim_pb2 import add_PonSimServicer_to_server
23from voltha.protos.ponsim_pb2 import add_XPonSimServicer_to_server
24from voltha.adapters.asfvolt16_olt.protos.bal_pb2 import add_BalServicer_to_server
25from ponsim_servicer import FlowUpdateHandler, XPonHandler
26from bal_servicer import BalHandler
Nikolay Titov89004ec2017-06-19 18:22:42 -040027
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080028_ = third_party
29
30log = structlog.get_logger()
31
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080032class GrpcServer(object):
33
Shad Ansari2d6e4832017-07-21 16:24:14 -070034 def __init__(self, port, ponsim, x_pon_sim, device_type):
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080035 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 Titov89004ec2017-06-19 18:22:42 -040039 self.x_pon_sim = x_pon_sim
Shad Ansari2d6e4832017-07-21 16:24:14 -070040 self.device_type = device_type
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080041
Shad Ansari2d6e4832017-07-21 16:24:14 -070042 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)
schowdhury9e247752017-07-14 06:56:20 -070051
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
rshettye4bd2ed2017-07-19 16:38:11 +053065 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 Haraszti656ecc62016-12-28 15:08:23 -080071 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')