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 | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | |
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 |
| 24 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 25 | from python.adapters.interface import IAdapterInterface |
| 26 | from python.protos.core_adapter_pb2 import IntType, InterAdapterMessage, StrType, Error, ErrorCode |
| 27 | from python.protos.device_pb2 import Device |
| 28 | from python.protos.openflow_13_pb2 import FlowChanges, FlowGroups, Flows, \ |
| 29 | FlowGroupChanges, ofp_packet_out |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class MacAddressError(BaseException): |
| 33 | def __init__(self, error): |
| 34 | self.error = error |
| 35 | |
| 36 | |
| 37 | class IDError(BaseException): |
| 38 | def __init__(self, error): |
| 39 | self.error = error |
| 40 | |
| 41 | |
| 42 | @implementer(IAdapterInterface) |
| 43 | class AdapterRequestFacade(object): |
| 44 | """ |
| 45 | Gate-keeper between CORE and device adapters. |
| 46 | |
| 47 | On one side it interacts with Core's internal model and update/dispatch |
| 48 | mechanisms. |
| 49 | |
| 50 | On the other side, it interacts with the adapters standard interface as |
| 51 | defined in |
| 52 | """ |
| 53 | |
| 54 | def __init__(self, adapter): |
| 55 | self.adapter = adapter |
| 56 | |
| 57 | @inlineCallbacks |
| 58 | def start(self): |
| 59 | self.log.debug('starting') |
| 60 | |
| 61 | @inlineCallbacks |
| 62 | def stop(self): |
| 63 | self.log.debug('stopping') |
| 64 | |
| 65 | def adopt_device(self, device): |
| 66 | d = Device() |
| 67 | if device: |
| 68 | device.Unpack(d) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 69 | return True, self.adapter.adopt_device(d) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 70 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 71 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 72 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | |
| 74 | def get_ofp_device_info(self, device): |
| 75 | d = Device() |
| 76 | if device: |
| 77 | device.Unpack(d) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 78 | return True, self.adapter.get_ofp_device_info(d) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 80 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 81 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | |
| 83 | def get_ofp_port_info(self, device, port_no): |
| 84 | d = Device() |
| 85 | if device: |
| 86 | device.Unpack(d) |
| 87 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 88 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 89 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 90 | p = IntType() |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 91 | if port_no: |
| 92 | port_no.Unpack(p) |
| 93 | else: |
| 94 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 95 | reason="port-no-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 96 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 97 | return True, self.adapter.get_ofp_port_info(d, p.val) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 99 | def reconcile_device(self, device): |
| 100 | return self.adapter.reconcile_device(device) |
| 101 | |
| 102 | def abandon_device(self, device): |
| 103 | return self.adapter.abandon_device(device) |
| 104 | |
| 105 | def disable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 106 | d = Device() |
| 107 | if device: |
| 108 | device.Unpack(d) |
| 109 | return True, self.adapter.disable_device(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 | |
| 114 | def reenable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 115 | d = Device() |
| 116 | if device: |
| 117 | device.Unpack(d) |
| 118 | return True, self.adapter.reenable_device(d) |
| 119 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 120 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 121 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | |
| 123 | def reboot_device(self, device): |
| 124 | d = Device() |
| 125 | if device: |
| 126 | device.Unpack(d) |
| 127 | return (True, self.adapter.reboot_device(d)) |
| 128 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 129 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 130 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 131 | |
| 132 | def download_image(self, device, request): |
| 133 | return self.adapter.download_image(device, request) |
| 134 | |
| 135 | def get_image_download_status(self, device, request): |
| 136 | return self.adapter.get_image_download_status(device, request) |
| 137 | |
| 138 | def cancel_image_download(self, device, request): |
| 139 | return self.adapter.cancel_image_download(device, request) |
| 140 | |
| 141 | def activate_image_update(self, device, request): |
| 142 | return self.adapter.activate_image_update(device, request) |
| 143 | |
| 144 | def revert_image_update(self, device, request): |
| 145 | return self.adapter.revert_image_update(device, request) |
| 146 | |
| 147 | def self_test(self, device): |
| 148 | return self.adapter.self_test_device(device) |
| 149 | |
| 150 | def delete_device(self, device): |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 151 | d = Device() |
| 152 | if device: |
| 153 | device.Unpack(d) |
| 154 | return (True, self.adapter.delete_device(d)) |
| 155 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 156 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 157 | reason="device-invalid") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 158 | |
| 159 | def get_device_details(self, device): |
| 160 | return self.adapter.get_device_details(device) |
| 161 | |
| 162 | def update_flows_bulk(self, device, flows, groups): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 163 | d = Device() |
| 164 | if device: |
| 165 | device.Unpack(d) |
| 166 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 167 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 168 | reason="device-invalid") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 169 | f = Flows() |
| 170 | if flows: |
| 171 | flows.Unpack(f) |
| 172 | |
| 173 | g = FlowGroups() |
| 174 | if groups: |
| 175 | groups.Unpack(g) |
| 176 | |
| 177 | return (True, self.adapter.update_flows_bulk(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 178 | |
| 179 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 180 | d = Device() |
| 181 | if device: |
| 182 | device.Unpack(d) |
| 183 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 184 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 185 | reason="device-invalid") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 186 | f = FlowChanges() |
| 187 | if flow_changes: |
| 188 | flow_changes.Unpack(f) |
| 189 | |
| 190 | g = FlowGroupChanges() |
| 191 | if group_changes: |
| 192 | group_changes.Unpack(g) |
| 193 | |
| 194 | return (True, self.adapter.update_flows_incrementally(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 195 | |
| 196 | def suppress_alarm(self, filter): |
| 197 | return self.adapter.suppress_alarm(filter) |
| 198 | |
| 199 | def unsuppress_alarm(self, filter): |
| 200 | return self.adapter.unsuppress_alarm(filter) |
| 201 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 202 | def process_inter_adapter_message(self, msg): |
| 203 | m = InterAdapterMessage() |
| 204 | if msg: |
| 205 | msg.Unpack(m) |
| 206 | else: |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 207 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 208 | reason="msg-invalid") |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 209 | |
| 210 | return (True, self.adapter.process_inter_adapter_message(m)) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def receive_packet_out(self, deviceId, outPort, packet): |
| 214 | d_id = StrType() |
| 215 | if deviceId: |
| 216 | deviceId.Unpack(d_id) |
| 217 | else: |
| 218 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 219 | reason="deviceid-invalid") |
| 220 | |
| 221 | op = IntType |
| 222 | if outPort: |
| 223 | outPort.Unpack(op) |
| 224 | else: |
| 225 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 226 | reason="outport-invalid") |
| 227 | |
| 228 | p = ofp_packet_out() |
| 229 | if packet: |
| 230 | packet.Unpack(p) |
| 231 | else: |
| 232 | return False, Error(code=ErrorCode.INVALID_PARAMETERS, |
| 233 | reason="packet-invalid") |
| 234 | |
| 235 | return (True, self.adapter.receive_packet_out(d_id, op, p)) |
| 236 | |