blob: 20b5dabb37cbedefe99ba9047c42ceefc22c8535 [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
18from concurrent import futures
19
20from common.utils.grpc_utils import twisted_async
21from voltha.protos import third_party
22from voltha.protos.ponsim_pb2 import PonSimServicer, \
23 add_PonSimServicer_to_server, PonSimDeviceInfo
24from google.protobuf.empty_pb2 import Empty
25
26_ = third_party
27
28log = structlog.get_logger()
29
30
31class FlowUpdateHandler(PonSimServicer):
32
33 def __init__(self, thread_pool, ponsim):
34 self.thread_pool = thread_pool
35 self.ponsim = ponsim
36
37 @twisted_async
38 def GetDeviceInfo(self, request, context):
39 log.info('get-device-info')
40 ports = self.ponsim.get_ports()
41 return PonSimDeviceInfo(
42 nni_port=ports[0],
43 uni_ports=ports[1:]
44 )
45
46 @twisted_async
47 def UpdateFlowTable(self, request, context):
48 log.info('flow-table-update', request=request, port=request.port)
49 if request.port == 0:
50 # by convention this is the olt port
51 self.ponsim.olt_install_flows(request.flows)
52 else:
53 self.ponsim.onu_install_flows(request.port, request.flows)
54 return Empty()
55
56class GrpcServer(object):
57
58 def __init__(self, port, ponsim):
59 self.port = port
60 self.thread_pool = futures.ThreadPoolExecutor(max_workers=10)
61 self.server = grpc.server(self.thread_pool)
62 self.ponsim = ponsim
63
64 def start(self):
65 log.debug('starting')
66 handler = FlowUpdateHandler(self.thread_pool, self.ponsim)
67 add_PonSimServicer_to_server(handler, self.server)
68 self.server.add_insecure_port('[::]:%s' % self.port)
69 self.server.start()
70 log.info('started')
71
72 def stop(self, grace=0):
73 log.debug('stopping')
74 self.server.stop(grace)
75 log.info('stopped')