blob: 7ed70d59ffbc5175c57798f87994c4468b5afa8c [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
Nikolay Titov89004ec2017-06-19 18:22:42 -040026from voltha.protos.ponsim_pb2 import XPonSimServicer, add_XPonSimServicer_to_server
27
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080028_ = third_party
29
30log = structlog.get_logger()
31
32
33class FlowUpdateHandler(PonSimServicer):
34
35 def __init__(self, thread_pool, ponsim):
36 self.thread_pool = thread_pool
37 self.ponsim = ponsim
38
39 @twisted_async
40 def GetDeviceInfo(self, request, context):
41 log.info('get-device-info')
42 ports = self.ponsim.get_ports()
43 return PonSimDeviceInfo(
44 nni_port=ports[0],
45 uni_ports=ports[1:]
46 )
47
48 @twisted_async
49 def UpdateFlowTable(self, request, context):
50 log.info('flow-table-update', request=request, port=request.port)
51 if request.port == 0:
52 # by convention this is the olt port
53 self.ponsim.olt_install_flows(request.flows)
54 else:
55 self.ponsim.onu_install_flows(request.port, request.flows)
56 return Empty()
57
Sergio Slobodrian98eff412017-03-15 14:46:30 -040058 def GetStats(self, request, context):
59 return self.ponsim.get_stats()
60
Nikolay Titov89004ec2017-06-19 18:22:42 -040061class XPonHandler(XPonSimServicer):
62
63 def __init__(self, thread_pool, x_pon_sim):
64 self.thread_pool = thread_pool
65 self.x_pon_sim = x_pon_sim
66
67 def CreateInterface(self, request, context):
68 self.x_pon_sim.CreateInterface(request)
69 return Empty()
70
71 def UpdateInterface(self, request, context):
72 self.x_pon_sim.UpdateInterface(request)
73 return Empty()
74
75 def RemoveInterface(self, request, context):
76 self.x_pon_sim.RemoveInterface(request)
77 return Empty()
Sergio Slobodrian98eff412017-03-15 14:46:30 -040078
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080079class GrpcServer(object):
80
Nikolay Titov89004ec2017-06-19 18:22:42 -040081 def __init__(self, port, ponsim, x_pon_sim):
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080082 self.port = port
83 self.thread_pool = futures.ThreadPoolExecutor(max_workers=10)
84 self.server = grpc.server(self.thread_pool)
85 self.ponsim = ponsim
Nikolay Titov89004ec2017-06-19 18:22:42 -040086 self.x_pon_sim = x_pon_sim
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080087
88 def start(self):
89 log.debug('starting')
90 handler = FlowUpdateHandler(self.thread_pool, self.ponsim)
91 add_PonSimServicer_to_server(handler, self.server)
Nikolay Titov89004ec2017-06-19 18:22:42 -040092 x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim)
93 add_XPonSimServicer_to_server(x_pon_handler, self.server)
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080094 self.server.add_insecure_port('[::]:%s' % self.port)
95 self.server.start()
96 log.info('started')
97
98 def stop(self, grace=0):
99 log.debug('stopping')
100 self.server.stop(grace)
101 log.info('stopped')