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 | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | from adapters.interface import IAdapterInterface |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 26 | from adapters.protos.core_adapter_pb2 import IntType, InterAdapterMessage |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | from adapters.protos.device_pb2 import Device |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 28 | from adapters.protos.openflow_13_pb2 import FlowChanges, FlowGroups, Flows, \ |
| 29 | FlowGroupChanges |
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 | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 71 | return False, d |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | |
| 73 | def get_ofp_device_info(self, device): |
| 74 | d = Device() |
| 75 | if device: |
| 76 | device.Unpack(d) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 77 | return True, self.adapter.get_ofp_device_info(d) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 78 | else: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 79 | return False, d |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | |
| 81 | def get_ofp_port_info(self, device, port_no): |
| 82 | d = Device() |
| 83 | if device: |
| 84 | device.Unpack(d) |
| 85 | else: |
| 86 | return (False, d) |
| 87 | |
| 88 | p = IntType() |
| 89 | port_no.Unpack(p) |
| 90 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 91 | return True, self.adapter.get_ofp_port_info(d, p.val) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 92 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 93 | def reconcile_device(self, device): |
| 94 | return self.adapter.reconcile_device(device) |
| 95 | |
| 96 | def abandon_device(self, device): |
| 97 | return self.adapter.abandon_device(device) |
| 98 | |
| 99 | def disable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 100 | d = Device() |
| 101 | if device: |
| 102 | device.Unpack(d) |
| 103 | return True, self.adapter.disable_device(d) |
| 104 | else: |
| 105 | return False, d |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 106 | |
| 107 | def reenable_device(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 108 | d = Device() |
| 109 | if device: |
| 110 | device.Unpack(d) |
| 111 | return True, self.adapter.reenable_device(d) |
| 112 | else: |
| 113 | return False, d |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 114 | |
| 115 | def reboot_device(self, device): |
| 116 | d = Device() |
| 117 | if device: |
| 118 | device.Unpack(d) |
| 119 | return (True, self.adapter.reboot_device(d)) |
| 120 | else: |
| 121 | return (False, d) |
| 122 | |
| 123 | def download_image(self, device, request): |
| 124 | return self.adapter.download_image(device, request) |
| 125 | |
| 126 | def get_image_download_status(self, device, request): |
| 127 | return self.adapter.get_image_download_status(device, request) |
| 128 | |
| 129 | def cancel_image_download(self, device, request): |
| 130 | return self.adapter.cancel_image_download(device, request) |
| 131 | |
| 132 | def activate_image_update(self, device, request): |
| 133 | return self.adapter.activate_image_update(device, request) |
| 134 | |
| 135 | def revert_image_update(self, device, request): |
| 136 | return self.adapter.revert_image_update(device, request) |
| 137 | |
| 138 | def self_test(self, device): |
| 139 | return self.adapter.self_test_device(device) |
| 140 | |
| 141 | def delete_device(self, device): |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 142 | d = Device() |
| 143 | if device: |
| 144 | device.Unpack(d) |
| 145 | return (True, self.adapter.delete_device(d)) |
| 146 | else: |
| 147 | return (False, d) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 148 | |
| 149 | def get_device_details(self, device): |
| 150 | return self.adapter.get_device_details(device) |
| 151 | |
| 152 | def update_flows_bulk(self, device, flows, groups): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 153 | d = Device() |
| 154 | if device: |
| 155 | device.Unpack(d) |
| 156 | else: |
| 157 | return (False, d) |
| 158 | |
| 159 | f = Flows() |
| 160 | if flows: |
| 161 | flows.Unpack(f) |
| 162 | |
| 163 | g = FlowGroups() |
| 164 | if groups: |
| 165 | groups.Unpack(g) |
| 166 | |
| 167 | return (True, self.adapter.update_flows_bulk(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 168 | |
| 169 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 170 | d = Device() |
| 171 | if device: |
| 172 | device.Unpack(d) |
| 173 | else: |
| 174 | return (False, d) |
| 175 | |
| 176 | f = FlowChanges() |
| 177 | if flow_changes: |
| 178 | flow_changes.Unpack(f) |
| 179 | |
| 180 | g = FlowGroupChanges() |
| 181 | if group_changes: |
| 182 | group_changes.Unpack(g) |
| 183 | |
| 184 | return (True, self.adapter.update_flows_incrementally(d, f, g)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 185 | |
| 186 | def suppress_alarm(self, filter): |
| 187 | return self.adapter.suppress_alarm(filter) |
| 188 | |
| 189 | def unsuppress_alarm(self, filter): |
| 190 | return self.adapter.unsuppress_alarm(filter) |
| 191 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 192 | def process_inter_adapter_message(self, msg): |
| 193 | m = InterAdapterMessage() |
| 194 | if msg: |
| 195 | msg.Unpack(m) |
| 196 | else: |
| 197 | return (False, m) |
| 198 | |
| 199 | return (True, self.adapter.process_inter_adapter_message(m)) |