blob: fb0834666895e3c72bd4f56801a80db20b23c9fe [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
21from common.utils.grpc_utils import twisted_async
22from voltha.protos import third_party
23from voltha.protos.ponsim_pb2 import PonSimServicer, \
24 add_PonSimServicer_to_server, PonSimDeviceInfo
25from google.protobuf.empty_pb2 import Empty
26
Nikolay Titov89004ec2017-06-19 18:22:42 -040027from voltha.protos.ponsim_pb2 import XPonSimServicer, add_XPonSimServicer_to_server
28
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080029_ = third_party
30
31log = structlog.get_logger()
32
33
34class 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 Slobodrian98eff412017-03-15 14:46:30 -040059 def GetStats(self, request, context):
60 return self.ponsim.get_stats()
61
Nikolay Titov89004ec2017-06-19 18:22:42 -040062class 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 Slobodrian98eff412017-03-15 14:46:30 -040079
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080080class GrpcServer(object):
81
Nikolay Titov89004ec2017-06-19 18:22:42 -040082 def __init__(self, port, ponsim, x_pon_sim):
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080083 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 Titov89004ec2017-06-19 18:22:42 -040087 self.x_pon_sim = x_pon_sim
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080088
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 Titov89004ec2017-06-19 18:22:42 -040093 x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim)
94 add_XPonSimServicer_to_server(x_pon_handler, self.server)
schowdhury9e247752017-07-14 06:56:20 -070095
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 Haraszti656ecc62016-12-28 15:08:23 -0800112 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')