Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 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 | """ |
| 20 | from Queue import Queue, Empty |
| 21 | import os |
| 22 | |
| 23 | from grpc import StatusCode |
| 24 | from grpc._channel import _Rendezvous |
| 25 | from structlog import get_logger |
| 26 | from twisted.internet import reactor |
| 27 | from twisted.internet import threads |
| 28 | from twisted.internet.defer import inlineCallbacks, returnValue, DeferredQueue |
| 29 | |
| 30 | from protos.voltha_pb2 import ID, VolthaServiceStub, FlowTableUpdate, \ |
| 31 | FlowGroupTableUpdate, PacketOut |
| 32 | from protos.logical_device_pb2 import LogicalPortId |
| 33 | from google.protobuf import empty_pb2 |
| 34 | |
| 35 | |
| 36 | log = get_logger() |
| 37 | |
| 38 | |
| 39 | class GrpcClient(object): |
| 40 | |
| 41 | def __init__(self, connection_manager, channel, grpc_timeout): |
| 42 | |
| 43 | self.connection_manager = connection_manager |
| 44 | self.channel = channel |
| 45 | self.grpc_timeout = grpc_timeout |
| 46 | self.local_stub = VolthaServiceStub(channel) |
| 47 | |
| 48 | self.stopped = False |
| 49 | |
| 50 | self.packet_out_queue = Queue() # queue to send out PacketOut msgs |
| 51 | self.packet_in_queue = DeferredQueue() # queue to receive PacketIn |
| 52 | self.change_event_queue = DeferredQueue() # queue change events |
| 53 | |
| 54 | def start(self): |
| 55 | log.debug('starting', grpc_timeout=self.grpc_timeout) |
| 56 | self.start_packet_out_stream() |
| 57 | self.start_packet_in_stream() |
| 58 | self.start_change_event_in_stream() |
| 59 | reactor.callLater(0, self.packet_in_forwarder_loop) |
| 60 | reactor.callLater(0, self.change_event_processing_loop) |
| 61 | log.info('started') |
| 62 | return self |
| 63 | |
| 64 | def stop(self): |
| 65 | log.debug('stopping') |
| 66 | self.stopped = True |
| 67 | log.info('stopped') |
| 68 | |
| 69 | def start_packet_out_stream(self): |
| 70 | |
| 71 | def packet_generator(): |
| 72 | while 1: |
| 73 | try: |
| 74 | packet = self.packet_out_queue.get(block=True, timeout=1.0) |
| 75 | except Empty: |
| 76 | if self.stopped: |
| 77 | return |
| 78 | else: |
| 79 | yield packet |
| 80 | |
| 81 | def stream_packets_out(): |
| 82 | generator = packet_generator() |
| 83 | try: |
| 84 | self.local_stub.StreamPacketsOut(generator) |
| 85 | except _Rendezvous, e: |
| 86 | log.error('grpc-exception', status=e.code()) |
| 87 | if e.code() == StatusCode.UNAVAILABLE: |
| 88 | os.system("kill -15 {}".format(os.getpid())) |
| 89 | |
| 90 | reactor.callInThread(stream_packets_out) |
| 91 | |
| 92 | def start_packet_in_stream(self): |
| 93 | |
| 94 | def receive_packet_in_stream(): |
| 95 | streaming_rpc_method = self.local_stub.ReceivePacketsIn |
| 96 | iterator = streaming_rpc_method(empty_pb2.Empty()) |
| 97 | try: |
| 98 | for packet_in in iterator: |
| 99 | reactor.callFromThread(self.packet_in_queue.put, |
| 100 | packet_in) |
| 101 | log.debug('enqued-packet-in', |
| 102 | packet_in=packet_in, |
| 103 | queue_len=len(self.packet_in_queue.pending)) |
| 104 | except _Rendezvous, e: |
| 105 | log.error('grpc-exception', status=e.code()) |
| 106 | if e.code() == StatusCode.UNAVAILABLE: |
| 107 | os.system("kill -15 {}".format(os.getpid())) |
| 108 | |
| 109 | reactor.callInThread(receive_packet_in_stream) |
| 110 | |
| 111 | def start_change_event_in_stream(self): |
| 112 | |
| 113 | def receive_change_events(): |
| 114 | streaming_rpc_method = self.local_stub.ReceiveChangeEvents |
| 115 | iterator = streaming_rpc_method(empty_pb2.Empty()) |
| 116 | try: |
| 117 | for event in iterator: |
| 118 | reactor.callFromThread(self.change_event_queue.put, event) |
| 119 | log.debug('enqued-change-event', |
| 120 | change_event=event, |
| 121 | queue_len=len(self.change_event_queue.pending)) |
| 122 | except _Rendezvous, e: |
| 123 | log.error('grpc-exception', status=e.code()) |
| 124 | if e.code() == StatusCode.UNAVAILABLE: |
| 125 | os.system("kill -15 {}".format(os.getpid())) |
| 126 | |
| 127 | reactor.callInThread(receive_change_events) |
| 128 | |
| 129 | @inlineCallbacks |
| 130 | def change_event_processing_loop(self): |
| 131 | while True: |
| 132 | try: |
| 133 | event = yield self.change_event_queue.get() |
| 134 | device_id = event.id |
| 135 | self.connection_manager.forward_change_event(device_id, event) |
| 136 | except Exception, e: |
| 137 | log.exception('failed-in-packet-in-handler', e=e) |
| 138 | if self.stopped: |
| 139 | break |
| 140 | |
| 141 | @inlineCallbacks |
| 142 | def packet_in_forwarder_loop(self): |
| 143 | while True: |
| 144 | packet_in = yield self.packet_in_queue.get() |
| 145 | device_id = packet_in.id |
| 146 | ofp_packet_in = packet_in.packet_in |
| 147 | self.connection_manager.forward_packet_in(device_id, ofp_packet_in) |
| 148 | if self.stopped: |
| 149 | break |
| 150 | |
| 151 | def send_packet_out(self, device_id, packet_out): |
| 152 | packet_out = PacketOut(id=device_id, packet_out=packet_out) |
| 153 | self.packet_out_queue.put(packet_out) |
| 154 | |
| 155 | @inlineCallbacks |
| 156 | def get_port(self, device_id, port_id): |
| 157 | req = LogicalPortId(id=device_id, port_id=port_id) |
| 158 | res = yield threads.deferToThread( |
| 159 | self.local_stub.GetLogicalDevicePort, req, timeout=self.grpc_timeout) |
| 160 | returnValue(res) |
| 161 | |
| 162 | @inlineCallbacks |
| 163 | def get_port_list(self, device_id): |
| 164 | req = ID(id=device_id) |
| 165 | res = yield threads.deferToThread( |
| 166 | self.local_stub.ListLogicalDevicePorts, req, timeout=self.grpc_timeout) |
| 167 | returnValue(res.items) |
| 168 | |
| 169 | @inlineCallbacks |
| 170 | def enable_port(self, device_id, port_id): |
| 171 | req = LogicalPortId( |
| 172 | id=device_id, |
| 173 | port_id=port_id |
| 174 | ) |
| 175 | res = yield threads.deferToThread( |
| 176 | self.local_stub.EnableLogicalDevicePort, req, timeout=self.grpc_timeout) |
| 177 | returnValue(res) |
| 178 | |
| 179 | @inlineCallbacks |
| 180 | def disable_port(self, device_id, port_id): |
| 181 | req = LogicalPortId( |
| 182 | id=device_id, |
| 183 | port_id=port_id |
| 184 | ) |
| 185 | res = yield threads.deferToThread( |
| 186 | self.local_stub.DisableLogicalDevicePort, req, timeout=self.grpc_timeout) |
| 187 | returnValue(res) |
| 188 | |
| 189 | @inlineCallbacks |
| 190 | def get_device_info(self, device_id): |
| 191 | req = ID(id=device_id) |
| 192 | res = yield threads.deferToThread( |
| 193 | self.local_stub.GetLogicalDevice, req, timeout=self.grpc_timeout) |
| 194 | returnValue(res) |
| 195 | |
| 196 | @inlineCallbacks |
| 197 | def update_flow_table(self, device_id, flow_mod): |
| 198 | req = FlowTableUpdate( |
| 199 | id=device_id, |
| 200 | flow_mod=flow_mod |
| 201 | ) |
| 202 | res = yield threads.deferToThread( |
| 203 | self.local_stub.UpdateLogicalDeviceFlowTable, req, timeout=self.grpc_timeout) |
| 204 | returnValue(res) |
| 205 | |
| 206 | @inlineCallbacks |
| 207 | def update_group_table(self, device_id, group_mod): |
| 208 | req = FlowGroupTableUpdate( |
| 209 | id=device_id, |
| 210 | group_mod=group_mod |
| 211 | ) |
| 212 | res = yield threads.deferToThread( |
| 213 | self.local_stub.UpdateLogicalDeviceFlowGroupTable, req, timeout=self.grpc_timeout) |
| 214 | returnValue(res) |
| 215 | |
| 216 | @inlineCallbacks |
| 217 | def list_flows(self, device_id): |
| 218 | req = ID(id=device_id) |
| 219 | res = yield threads.deferToThread( |
| 220 | self.local_stub.ListLogicalDeviceFlows, req, timeout=self.grpc_timeout) |
| 221 | returnValue(res.items) |
| 222 | |
| 223 | @inlineCallbacks |
| 224 | def list_groups(self, device_id): |
| 225 | req = ID(id=device_id) |
| 226 | res = yield threads.deferToThread( |
| 227 | self.local_stub.ListLogicalDeviceFlowGroups, req, timeout=self.grpc_timeout) |
| 228 | returnValue(res.items) |
| 229 | |
| 230 | @inlineCallbacks |
| 231 | def list_ports(self, device_id): |
| 232 | req = ID(id=device_id) |
| 233 | res = yield threads.deferToThread( |
| 234 | self.local_stub.ListLogicalDevicePorts, req, timeout=self.grpc_timeout) |
| 235 | returnValue(res.items) |
| 236 | |
| 237 | @inlineCallbacks |
| 238 | def list_logical_devices(self): |
| 239 | res = yield threads.deferToThread( |
| 240 | self.local_stub.ListLogicalDevices, empty_pb2.Empty(), timeout=self.grpc_timeout) |
| 241 | returnValue(res.items) |
| 242 | |
| 243 | @inlineCallbacks |
| 244 | def subscribe(self, subscriber): |
| 245 | res = yield threads.deferToThread( |
| 246 | self.local_stub.Subscribe, subscriber, timeout=self.grpc_timeout) |
| 247 | returnValue(res) |