Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | # |
| 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 | # |
| 16 | |
| 17 | """ |
| 18 | The gRPC client layer for the OpenFlow agent |
| 19 | """ |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 20 | from Queue import Queue, Empty |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 21 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 22 | from grpc import StatusCode |
| 23 | from grpc._channel import _Rendezvous |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 24 | from structlog import get_logger |
| 25 | from twisted.internet import reactor |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 26 | from twisted.internet import threads |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 27 | from twisted.internet.defer import inlineCallbacks, returnValue, DeferredQueue |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 28 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 29 | from protos.voltha_pb2 import ID, VolthaLogicalLayerStub, FlowTableUpdate, \ |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 30 | GroupTableUpdate, NullMessage, PacketOut |
| 31 | |
| 32 | |
| 33 | log = get_logger() |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | class GrpcClient(object): |
| 37 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 38 | def __init__(self, connection_manager, channel): |
| 39 | |
| 40 | self.connection_manager = connection_manager |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 41 | self.channel = channel |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 42 | self.logical_stub = VolthaLogicalLayerStub(channel) |
| 43 | |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 44 | self.stopped = False |
| 45 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 46 | self.packet_out_queue = Queue() # queue to send out PacketOut msgs |
| 47 | self.packet_in_queue = DeferredQueue() # queue to receive PacketIn |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 48 | |
| 49 | def start(self): |
| 50 | log.debug('starting') |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 51 | self.start_packet_out_stream() |
| 52 | self.start_packet_in_stream() |
| 53 | reactor.callLater(0, self.packet_in_forwarder_loop) |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 54 | log.info('started') |
| 55 | return self |
| 56 | |
| 57 | def stop(self): |
| 58 | log.debug('stopping') |
| 59 | self.stopped = True |
| 60 | log.info('stopped') |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 61 | |
| 62 | def start_packet_out_stream(self): |
| 63 | |
| 64 | def packet_generator(): |
| 65 | while 1: |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 66 | try: |
| 67 | packet = self.packet_out_queue.get(block=True, timeout=1.0) |
| 68 | except Empty: |
| 69 | if self.stopped: |
| 70 | return |
| 71 | else: |
| 72 | yield packet |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 73 | |
| 74 | def stream_packets_out(): |
| 75 | generator = packet_generator() |
| 76 | self.logical_stub.StreamPacketsOut(generator) |
| 77 | |
| 78 | reactor.callInThread(stream_packets_out) |
| 79 | |
| 80 | def start_packet_in_stream(self): |
| 81 | |
| 82 | def receive_packet_in_stream(): |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 83 | streaming_rpc_method = self.logical_stub.ReceivePacketsIn |
| 84 | iterator = streaming_rpc_method(NullMessage()) |
| 85 | for packet_in in iterator: |
| 86 | reactor.callFromThread(self.packet_in_queue.put, |
| 87 | packet_in) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 88 | log.debug('enqued-packet-in', |
| 89 | packet_in=packet_in, |
| 90 | queue_len=len(self.packet_in_queue.pending)) |
| 91 | |
| 92 | reactor.callInThread(receive_packet_in_stream) |
| 93 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 94 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 95 | def packet_in_forwarder_loop(self): |
| 96 | while True: |
| 97 | packet_in = yield self.packet_in_queue.get() |
| 98 | device_id = packet_in.id |
| 99 | ofp_packet_in = packet_in.packet_in |
| 100 | self.connection_manager.forward_packet_in(device_id, ofp_packet_in) |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 101 | if self.stopped: |
| 102 | break |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 103 | |
| 104 | def send_packet_out(self, device_id, packet_out): |
| 105 | packet_out = PacketOut(id=device_id, packet_out=packet_out) |
| 106 | self.packet_out_queue.put(packet_out) |
| 107 | |
| 108 | @inlineCallbacks |
| 109 | def get_port_list(self, device_id): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 110 | req = ID(id=device_id) |
| 111 | res = yield threads.deferToThread( |
| 112 | self.logical_stub.ListLogicalDevicePorts, req) |
| 113 | returnValue(res.items) |
| 114 | |
| 115 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 116 | def get_device_info(self, device_id): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 117 | req = ID(id=device_id) |
| 118 | res = yield threads.deferToThread( |
| 119 | self.logical_stub.GetLogicalDevice, req) |
| 120 | returnValue(res) |
| 121 | |
| 122 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 123 | def update_flow_table(self, device_id, flow_mod): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 124 | req = FlowTableUpdate( |
| 125 | id=device_id, |
| 126 | flow_mod=flow_mod |
| 127 | ) |
| 128 | res = yield threads.deferToThread( |
| 129 | self.logical_stub.UpdateFlowTable, req) |
| 130 | returnValue(res) |
| 131 | |
| 132 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 133 | def update_group_table(self, device_id, group_mod): |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 134 | req = GroupTableUpdate( |
| 135 | id=device_id, |
| 136 | group_mod=group_mod |
| 137 | ) |
| 138 | res = yield threads.deferToThread( |
| 139 | self.logical_stub.UpdateGroupTable, req) |
| 140 | returnValue(res) |
| 141 | |
| 142 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 143 | def list_flows(self, device_id): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 144 | req = ID(id=device_id) |
| 145 | res = yield threads.deferToThread( |
| 146 | self.logical_stub.ListDeviceFlows, req) |
| 147 | returnValue(res.items) |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 148 | |
| 149 | @inlineCallbacks |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 150 | def list_groups(self, device_id): |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 151 | req = ID(id=device_id) |
| 152 | res = yield threads.deferToThread( |
| 153 | self.logical_stub.ListDeviceFlowGroups, req) |
| 154 | returnValue(res.items) |