khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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 | """ |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 18 | This facade handles kafka-formatted messages from the Core, extracts the kafka |
| 19 | formatting and forwards the request to the concrete handler. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 20 | """ |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 21 | import structlog |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 22 | from twisted.internet.defer import inlineCallbacks |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | from zope.interface import implementer |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 24 | from twisted.internet import reactor |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 25 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 26 | from afkak.consumer import OFFSET_LATEST, OFFSET_EARLIEST |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 27 | from python.adapters.interface import IAdapterInterface |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 28 | from python.protos.inter_container_pb2 import IntType, InterAdapterMessage, StrType, Error, ErrorCode |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 29 | from python.protos.device_pb2 import Device |
| 30 | from python.protos.openflow_13_pb2 import FlowChanges, FlowGroups, Flows, \ |
| 31 | FlowGroupChanges, ofp_packet_out |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 32 | from python.adapters.kafka.kafka_inter_container_library import IKafkaMessagingProxy, \ |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 33 | get_messaging_proxy, KAFKA_OFFSET_LATEST, KAFKA_OFFSET_EARLIEST |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 34 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 35 | log = structlog.get_logger() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | |
| 37 | class MacAddressError(BaseException): |
| 38 | def __init__(self, error): |
| 39 | self.error = error |
| 40 | |
| 41 | |
| 42 | class IDError(BaseException): |
| 43 | def __init__(self, error): |
| 44 | self.error = error |
| 45 | |
| 46 | |
| 47 | @implementer(IAdapterInterface) |
| 48 | class AdapterRequestFacade(object): |
| 49 | """ |
| 50 | Gate-keeper between CORE and device adapters. |
| 51 | |
| 52 | On one side it interacts with Core's internal model and update/dispatch |
| 53 | mechanisms. |
| 54 | |
| 55 | On the other side, it interacts with the adapters standard interface as |
| 56 | defined in |
| 57 | """ |
| 58 | |
| 59 | def __init__(self, adapter): |
| 60 | self.adapter = adapter |
| 61 | |
| 62 | @inlineCallbacks |
| 63 | def start(self): |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 64 | log.debug('starting') |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 65 | |
| 66 | @inlineCallbacks |
| 67 | def stop(self): |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 68 | log.debug('stopping') |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 69 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 70 | @inlineCallbacks |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 71 | def createKafkaDeviceTopic(self, deviceId): |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 72 | log.debug("subscribing-to-topic", device_id=deviceId) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 73 | kafka_proxy = get_messaging_proxy() |
| 74 | device_topic = kafka_proxy.get_default_topic() + "_" + deviceId |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 75 | # yield kafka_proxy.create_topic(topic=device_topic) |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 76 | yield kafka_proxy.subscribe(topic=device_topic, group_id=device_topic, target_cls=self, offset=KAFKA_OFFSET_EARLIEST) |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 77 | log.debug("subscribed-to-topic", topic=device_topic) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 78 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | def adopt_device(self, device): |
| 80 | d = Device() |
| 81 | if device: |
| 82 | device.Unpack(d) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 83 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 84 | # Start the creation of a device specific topic to handle all |
| 85 | # subsequent requests from the Core. This adapter instance will |
| 86 | # handle all requests for that device. |
| 87 | reactor.callLater(0, self.createKafkaDeviceTopic, d.id) |
| 88 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 89 | result = self.adapter.adopt_device(d) |
| 90 | # return True, self.adapter.adopt_device(d) |
| 91 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 92 | return True, result |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 93 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 94 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 95 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 96 | |
| 97 | def get_ofp_device_info(self, device): |
| 98 | d = Device() |
| 99 | if device: |
| 100 | device.Unpack(d) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 101 | return True, self.adapter.get_ofp_device_info(d) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 102 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 103 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 104 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 105 | |
| 106 | def get_ofp_port_info(self, device, port_no): |
| 107 | d = Device() |
| 108 | if device: |
| 109 | device.Unpack(d) |
| 110 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 111 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 112 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 113 | p = IntType() |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 114 | if port_no: |
| 115 | port_no.Unpack(p) |
| 116 | else: |
| 117 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 118 | reason="port-no-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 119 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 120 | return True, self.adapter.get_ofp_port_info(d, p.val) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 121 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | def reconcile_device(self, device): |
| 123 | return self.adapter.reconcile_device(device) |
| 124 | |
| 125 | def abandon_device(self, device): |
| 126 | return self.adapter.abandon_device(device) |
| 127 | |
| 128 | def disable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 129 | d = Device() |
| 130 | if device: |
| 131 | device.Unpack(d) |
| 132 | return True, self.adapter.disable_device(d) |
| 133 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 134 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 135 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 136 | |
| 137 | def reenable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 138 | d = Device() |
| 139 | if device: |
| 140 | device.Unpack(d) |
| 141 | return True, self.adapter.reenable_device(d) |
| 142 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 143 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 144 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 145 | |
| 146 | def reboot_device(self, device): |
| 147 | d = Device() |
| 148 | if device: |
| 149 | device.Unpack(d) |
| 150 | return (True, self.adapter.reboot_device(d)) |
| 151 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 152 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 153 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 154 | |
| 155 | def download_image(self, device, request): |
| 156 | return self.adapter.download_image(device, request) |
| 157 | |
| 158 | def get_image_download_status(self, device, request): |
| 159 | return self.adapter.get_image_download_status(device, request) |
| 160 | |
| 161 | def cancel_image_download(self, device, request): |
| 162 | return self.adapter.cancel_image_download(device, request) |
| 163 | |
| 164 | def activate_image_update(self, device, request): |
| 165 | return self.adapter.activate_image_update(device, request) |
| 166 | |
| 167 | def revert_image_update(self, device, request): |
| 168 | return self.adapter.revert_image_update(device, request) |
| 169 | |
| 170 | def self_test(self, device): |
| 171 | return self.adapter.self_test_device(device) |
| 172 | |
| 173 | def delete_device(self, device): |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 174 | d = Device() |
| 175 | if device: |
| 176 | device.Unpack(d) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 177 | result = self.adapter.delete_device(d) |
| 178 | # return (True, self.adapter.delete_device(d)) |
| 179 | |
| 180 | # Before we return, delete the device specific topic as we will no |
| 181 | # longer receive requests from the Core for that device |
| 182 | kafka_proxy = get_messaging_proxy() |
| 183 | device_topic = kafka_proxy.get_default_topic() + "/" + d.id |
| 184 | kafka_proxy.unsubscribe(topic=device_topic) |
| 185 | |
| 186 | return (True, result) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 187 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 188 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 189 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 190 | |
| 191 | def get_device_details(self, device): |
| 192 | return self.adapter.get_device_details(device) |
| 193 | |
| 194 | def update_flows_bulk(self, device, flows, groups): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 195 | d = Device() |
| 196 | if device: |
| 197 | device.Unpack(d) |
| 198 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 199 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 200 | reason="device-invalid") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 201 | f = Flows() |
| 202 | if flows: |
| 203 | flows.Unpack(f) |
| 204 | |
| 205 | g = FlowGroups() |
| 206 | if groups: |
| 207 | groups.Unpack(g) |
| 208 | |
| 209 | return (True, self.adapter.update_flows_bulk(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 210 | |
| 211 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 212 | d = Device() |
| 213 | if device: |
| 214 | device.Unpack(d) |
| 215 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 216 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 217 | reason="device-invalid") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 218 | f = FlowChanges() |
| 219 | if flow_changes: |
| 220 | flow_changes.Unpack(f) |
| 221 | |
| 222 | g = FlowGroupChanges() |
| 223 | if group_changes: |
| 224 | group_changes.Unpack(g) |
| 225 | |
| 226 | return (True, self.adapter.update_flows_incrementally(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 227 | |
| 228 | def suppress_alarm(self, filter): |
| 229 | return self.adapter.suppress_alarm(filter) |
| 230 | |
| 231 | def unsuppress_alarm(self, filter): |
| 232 | return self.adapter.unsuppress_alarm(filter) |
| 233 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 234 | def process_inter_adapter_message(self, msg): |
| 235 | m = InterAdapterMessage() |
| 236 | if msg: |
| 237 | msg.Unpack(m) |
| 238 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 239 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 240 | reason="msg-invalid") |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 241 | |
| 242 | return (True, self.adapter.process_inter_adapter_message(m)) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def receive_packet_out(self, deviceId, outPort, packet): |
| 246 | d_id = StrType() |
| 247 | if deviceId: |
| 248 | deviceId.Unpack(d_id) |
| 249 | else: |
| 250 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 251 | reason="deviceid-invalid") |
| 252 | |
| 253 | op = IntType |
| 254 | if outPort: |
| 255 | outPort.Unpack(op) |
| 256 | else: |
| 257 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 258 | reason="outport-invalid") |
| 259 | |
| 260 | p = ofp_packet_out() |
| 261 | if packet: |
| 262 | packet.Unpack(p) |
| 263 | else: |
| 264 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 265 | reason="packet-invalid") |
| 266 | |
| 267 | return (True, self.adapter.receive_packet_out(d_id, op, p)) |
| 268 | |