blob: ad87387b88b282ae085a6dffc8e20d0337e8d312 [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07003#
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#
16
17"""
18The gRPC client layer for the OpenFlow agent
19"""
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070020from Queue import Queue, Empty
alshabib06b449c2017-01-15 17:33:16 -060021import os
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070022
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070023from grpc import StatusCode
24from grpc._channel import _Rendezvous
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070025from structlog import get_logger
26from twisted.internet import reactor
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070027from twisted.internet import threads
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070028from twisted.internet.defer import inlineCallbacks, returnValue, DeferredQueue
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070029
Koray Kokten8592a232018-08-27 07:41:14 +000030from protos.voltha_pb2 import ID, VolthaLocalServiceStub, FlowTableUpdate, MeterModUpdate, \
Jonathan Hart398e4072018-05-30 16:54:00 -070031 FlowGroupTableUpdate, PacketOut
32from protos.logical_device_pb2 import LogicalPortId
Zsolt Haraszti7eeb2b32016-11-06 14:04:55 -080033from google.protobuf import empty_pb2
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070034
35
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070036class GrpcClient(object):
37
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040038 def __init__(self, connection_manager, channel, grpc_timeout):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070039
Zack Williams0ad0a392018-11-14 10:41:08 -070040 self.log = get_logger()
41
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070042 self.connection_manager = connection_manager
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070043 self.channel = channel
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040044 self.grpc_timeout = grpc_timeout
Zsolt Haraszti66862032016-11-28 14:28:39 -080045 self.local_stub = VolthaLocalServiceStub(channel)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070046
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070047 self.stopped = False
48
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070049 self.packet_out_queue = Queue() # queue to send out PacketOut msgs
50 self.packet_in_queue = DeferredQueue() # queue to receive PacketIn
Zsolt Haraszti217a12e2016-12-19 16:37:55 -080051 self.change_event_queue = DeferredQueue() # queue change events
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070052
53 def start(self):
Zack Williams0ad0a392018-11-14 10:41:08 -070054 self.log.debug('starting', grpc_timeout=self.grpc_timeout)
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070055 self.start_packet_out_stream()
56 self.start_packet_in_stream()
Zsolt Haraszti217a12e2016-12-19 16:37:55 -080057 self.start_change_event_in_stream()
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070058 reactor.callLater(0, self.packet_in_forwarder_loop)
Zsolt Haraszti217a12e2016-12-19 16:37:55 -080059 reactor.callLater(0, self.change_event_processing_loop)
Zack Williams0ad0a392018-11-14 10:41:08 -070060 self.log.info('started')
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070061 return self
62
63 def stop(self):
Zack Williams0ad0a392018-11-14 10:41:08 -070064 self.log.debug('stopping')
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070065 self.stopped = True
Zack Williams0ad0a392018-11-14 10:41:08 -070066 self.log.info('stopped')
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070067
68 def start_packet_out_stream(self):
69
70 def packet_generator():
71 while 1:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070072 try:
73 packet = self.packet_out_queue.get(block=True, timeout=1.0)
74 except Empty:
75 if self.stopped:
76 return
77 else:
78 yield packet
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070079
80 def stream_packets_out():
81 generator = packet_generator()
alshabib06b449c2017-01-15 17:33:16 -060082 try:
83 self.local_stub.StreamPacketsOut(generator)
84 except _Rendezvous, e:
Zack Williams0ad0a392018-11-14 10:41:08 -070085 self.log.error('grpc-exception', status=e.code())
alshabib06b449c2017-01-15 17:33:16 -060086 if e.code() == StatusCode.UNAVAILABLE:
87 os.system("kill -15 {}".format(os.getpid()))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070088
89 reactor.callInThread(stream_packets_out)
90
91 def start_packet_in_stream(self):
92
93 def receive_packet_in_stream():
Zsolt Haraszti66862032016-11-28 14:28:39 -080094 streaming_rpc_method = self.local_stub.ReceivePacketsIn
Zsolt Haraszti9ed54292017-01-09 18:28:32 -080095 iterator = streaming_rpc_method(empty_pb2.Empty())
alshabib06b449c2017-01-15 17:33:16 -060096 try:
97 for packet_in in iterator:
98 reactor.callFromThread(self.packet_in_queue.put,
99 packet_in)
Zack Williams0ad0a392018-11-14 10:41:08 -0700100 self.log.debug('enqued-packet-in',
alshabib06b449c2017-01-15 17:33:16 -0600101 packet_in=packet_in,
102 queue_len=len(self.packet_in_queue.pending))
103 except _Rendezvous, e:
Zack Williams0ad0a392018-11-14 10:41:08 -0700104 self.log.error('grpc-exception', status=e.code())
alshabib06b449c2017-01-15 17:33:16 -0600105 if e.code() == StatusCode.UNAVAILABLE:
106 os.system("kill -15 {}".format(os.getpid()))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700107
108 reactor.callInThread(receive_packet_in_stream)
109
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800110 def start_change_event_in_stream(self):
111
112 def receive_change_events():
113 streaming_rpc_method = self.local_stub.ReceiveChangeEvents
Zsolt Haraszti9ed54292017-01-09 18:28:32 -0800114 iterator = streaming_rpc_method(empty_pb2.Empty())
alshabib06b449c2017-01-15 17:33:16 -0600115 try:
116 for event in iterator:
117 reactor.callFromThread(self.change_event_queue.put, event)
Zack Williams0ad0a392018-11-14 10:41:08 -0700118 self.log.debug('enqued-change-event',
alshabib06b449c2017-01-15 17:33:16 -0600119 change_event=event,
120 queue_len=len(self.change_event_queue.pending))
121 except _Rendezvous, e:
Zack Williams0ad0a392018-11-14 10:41:08 -0700122 self.log.error('grpc-exception', status=e.code())
alshabib06b449c2017-01-15 17:33:16 -0600123 if e.code() == StatusCode.UNAVAILABLE:
124 os.system("kill -15 {}".format(os.getpid()))
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800125
126 reactor.callInThread(receive_change_events)
127
128 @inlineCallbacks
129 def change_event_processing_loop(self):
130 while True:
131 try:
132 event = yield self.change_event_queue.get()
133 device_id = event.id
134 self.connection_manager.forward_change_event(device_id, event)
135 except Exception, e:
Zack Williams0ad0a392018-11-14 10:41:08 -0700136 self.log.exception('failed-in-packet-in-handler', e=e)
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800137 if self.stopped:
138 break
139
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700140 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700141 def packet_in_forwarder_loop(self):
142 while True:
143 packet_in = yield self.packet_in_queue.get()
144 device_id = packet_in.id
145 ofp_packet_in = packet_in.packet_in
146 self.connection_manager.forward_packet_in(device_id, ofp_packet_in)
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700147 if self.stopped:
148 break
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700149
150 def send_packet_out(self, device_id, packet_out):
151 packet_out = PacketOut(id=device_id, packet_out=packet_out)
152 self.packet_out_queue.put(packet_out)
153
154 @inlineCallbacks
Jonathan Hart8d21c322018-04-17 07:42:02 -0700155 def get_port(self, device_id, port_id):
156 req = LogicalPortId(id=device_id, port_id=port_id)
157 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400158 self.local_stub.GetLogicalDevicePort, req, timeout=self.grpc_timeout)
Jonathan Hart8d21c322018-04-17 07:42:02 -0700159 returnValue(res)
160
161 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700162 def get_port_list(self, device_id):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700163 req = ID(id=device_id)
164 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400165 self.local_stub.ListLogicalDevicePorts, req, timeout=self.grpc_timeout)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700166 returnValue(res.items)
167
168 @inlineCallbacks
Jonathan Hart8d21c322018-04-17 07:42:02 -0700169 def enable_port(self, device_id, port_id):
170 req = LogicalPortId(
171 id=device_id,
172 port_id=port_id
173 )
174 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400175 self.local_stub.EnableLogicalDevicePort, req, timeout=self.grpc_timeout)
Jonathan Hart8d21c322018-04-17 07:42:02 -0700176 returnValue(res)
177
178 @inlineCallbacks
179 def disable_port(self, device_id, port_id):
180 req = LogicalPortId(
181 id=device_id,
182 port_id=port_id
183 )
184 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400185 self.local_stub.DisableLogicalDevicePort, req, timeout=self.grpc_timeout)
Jonathan Hart8d21c322018-04-17 07:42:02 -0700186 returnValue(res)
187
188 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700189 def get_device_info(self, device_id):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700190 req = ID(id=device_id)
191 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400192 self.local_stub.GetLogicalDevice, req, timeout=self.grpc_timeout)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700193 returnValue(res)
194
195 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700196 def update_flow_table(self, device_id, flow_mod):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700197 req = FlowTableUpdate(
198 id=device_id,
199 flow_mod=flow_mod
200 )
201 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400202 self.local_stub.UpdateLogicalDeviceFlowTable, req, timeout=self.grpc_timeout)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700203 returnValue(res)
204
205 @inlineCallbacks
Koray Kokten8592a232018-08-27 07:41:14 +0000206 def update_meter_mod_table(self, device_id, meter_mod):
207 req = MeterModUpdate(
208 id=device_id,
209 meter_mod=meter_mod
210 )
211 res = yield threads.deferToThread(
212 self.local_stub.UpdateLogicalDeviceMeterTable, req)
213 returnValue(res)
214
215 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700216 def update_group_table(self, device_id, group_mod):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800217 req = FlowGroupTableUpdate(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700218 id=device_id,
219 group_mod=group_mod
220 )
221 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400222 self.local_stub.UpdateLogicalDeviceFlowGroupTable, req, timeout=self.grpc_timeout)
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700223 returnValue(res)
224
225 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700226 def list_flows(self, device_id):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700227 req = ID(id=device_id)
228 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400229 self.local_stub.ListLogicalDeviceFlows, req, timeout=self.grpc_timeout)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700230 returnValue(res.items)
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700231
232 @inlineCallbacks
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700233 def list_groups(self, device_id):
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700234 req = ID(id=device_id)
235 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400236 self.local_stub.ListLogicalDeviceFlowGroups, req, timeout=self.grpc_timeout)
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700237 returnValue(res.items)
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -0400238
239 @inlineCallbacks
240 def list_ports(self, device_id):
241 req = ID(id=device_id)
242 res = yield threads.deferToThread(
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400243 self.local_stub.ListLogicalDevicePorts, req, timeout=self.grpc_timeout)
244 returnValue(res.items)
245
246 @inlineCallbacks
247 def list_logical_devices(self):
248 res = yield threads.deferToThread(
249 self.local_stub.ListLogicalDevices, empty_pb2.Empty(), timeout=self.grpc_timeout)
250 returnValue(res.items)
251
252 @inlineCallbacks
Nicolas Palpacuer75ba77f2018-08-27 17:26:57 -0400253 def list_reachable_logical_devices(self):
254 res = yield threads.deferToThread(
255 self.local_stub.ListReachableLogicalDevices, empty_pb2.Empty(),
256 timeout=self.grpc_timeout)
257 returnValue(res.items)
258
259 @inlineCallbacks
Richard Jankowskidb9a86e2018-09-17 13:33:29 -0400260 def subscribe(self, subscriber):
261 res = yield threads.deferToThread(
262 self.local_stub.Subscribe, subscriber, timeout=self.grpc_timeout)
263 returnValue(res)
Koray Kokten8592a232018-08-27 07:41:14 +0000264
Koray Kokten8592a232018-08-27 07:41:14 +0000265 @inlineCallbacks
266 def get_meter_stats(self, device_id):
267 req = ID(id=device_id)
268 res = yield threads.deferToThread(
269 self.local_stub.GetMeterStatsOfLogicalDevice, req)
270 returnValue(res.items)