khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 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 | Adapter abstract base class |
| 19 | """ |
| 20 | |
| 21 | import structlog |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | from twisted.internet import reactor |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 23 | from zope.interface import implementer |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 24 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 25 | from interface import IAdapterInterface |
| 26 | from python.protos.adapter_pb2 import Adapter |
| 27 | from python.protos.adapter_pb2 import AdapterConfig |
| 28 | from python.protos.common_pb2 import AdminState |
| 29 | from python.protos.common_pb2 import LogLevel |
| 30 | from python.protos.device_pb2 import DeviceType, DeviceTypes |
| 31 | from python.protos.health_pb2 import HealthStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 32 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 33 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 34 | log = structlog.get_logger() |
| 35 | |
| 36 | |
| 37 | @implementer(IAdapterInterface) |
| 38 | class IAdapter(object): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 39 | def __init__(self, |
| 40 | core_proxy, |
| 41 | adapter_proxy, |
| 42 | config, |
| 43 | device_handler_class, |
| 44 | name, |
| 45 | vendor, |
| 46 | version, |
| 47 | device_type, vendor_id, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 48 | accepts_bulk_flow_update=True, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 49 | accepts_add_remove_flow_updates=False): |
| 50 | log.debug( |
| 51 | 'Initializing adapter: {} {} {}'.format(vendor, name, version)) |
| 52 | self.core_proxy = core_proxy |
| 53 | self.adapter_proxy = adapter_proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 54 | self.config = config |
| 55 | self.name = name |
| 56 | self.supported_device_types = [ |
| 57 | DeviceType( |
| 58 | id=device_type, |
| 59 | vendor_id=vendor_id, |
| 60 | adapter=name, |
| 61 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
| 62 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates |
| 63 | ) |
| 64 | ] |
| 65 | self.descriptor = Adapter( |
| 66 | id=self.name, |
| 67 | vendor=vendor, |
| 68 | version=version, |
| 69 | config=AdapterConfig(log_level=LogLevel.INFO) |
| 70 | ) |
| 71 | self.devices_handlers = dict() # device_id -> Olt/OnuHandler() |
| 72 | self.device_handler_class = device_handler_class |
| 73 | |
| 74 | def start(self): |
| 75 | log.info('Starting adapter: {}'.format(self.name)) |
| 76 | |
| 77 | def stop(self): |
| 78 | log.info('Stopping adapter: {}'.format(self.name)) |
| 79 | |
| 80 | def adapter_descriptor(self): |
| 81 | return self.descriptor |
| 82 | |
| 83 | def device_types(self): |
| 84 | return DeviceTypes(items=self.supported_device_types) |
| 85 | |
| 86 | def health(self): |
| 87 | # return HealthStatus(state=HealthStatus.HealthState.HEALTHY) |
| 88 | return HealthStatus(state=HealthStatus.HEALTHY) |
| 89 | |
| 90 | def change_master_state(self, master): |
| 91 | raise NotImplementedError() |
| 92 | |
| 93 | def get_ofp_device_info(self, device): |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 94 | log.debug('get_ofp_device_info_start', device_id=device.id) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 95 | ofp_device_info = self.devices_handlers[device.id].get_ofp_device_info( |
| 96 | device) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 97 | log.debug('get_ofp_device_info_ends', device_id=device.id) |
| 98 | return ofp_device_info |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 99 | |
| 100 | def get_ofp_port_info(self, device, port_no): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 101 | log.debug('get_ofp_port_info_start', device_id=device.id, |
| 102 | port_no=port_no) |
| 103 | ofp_port_info = self.devices_handlers[device.id].get_ofp_port_info( |
| 104 | device, port_no) |
| 105 | log.debug('get_ofp_port_info_ends', device_id=device.id, |
| 106 | port_no=port_no) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 107 | return ofp_port_info |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 108 | |
| 109 | def adopt_device(self, device): |
| 110 | log.debug('adopt_device', device_id=device.id) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 111 | self.devices_handlers[device.id] = self.device_handler_class(self, |
| 112 | device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 113 | reactor.callLater(0, self.devices_handlers[device.id].activate, device) |
| 114 | log.debug('adopt_device_done', device_id=device.id) |
| 115 | return device |
| 116 | |
| 117 | def reconcile_device(self, device): |
| 118 | raise NotImplementedError() |
| 119 | |
| 120 | def abandon_device(self, device): |
| 121 | raise NotImplementedError() |
| 122 | |
| 123 | def disable_device(self, device): |
| 124 | log.info('disable-device', device_id=device.id) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 125 | reactor.callLater(0, self.devices_handlers[device.id].disable) |
| 126 | log.debug('disable-device-done', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 127 | return device |
| 128 | |
| 129 | def reenable_device(self, device): |
| 130 | log.info('reenable-device', device_id=device.id) |
| 131 | reactor.callLater(0, self.devices_handlers[device.id].reenable) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 132 | log.info('reenable-device-done', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 133 | return device |
| 134 | |
| 135 | def reboot_device(self, device): |
| 136 | log.info('reboot-device', device_id=device.id) |
| 137 | reactor.callLater(0, self.devices_handlers[device.id].reboot) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 138 | log.info('reboot-device-done', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 139 | return device |
| 140 | |
| 141 | def download_image(self, device, request): |
| 142 | raise NotImplementedError() |
| 143 | |
| 144 | def get_image_download_status(self, device, request): |
| 145 | raise NotImplementedError() |
| 146 | |
| 147 | def cancel_image_download(self, device, request): |
| 148 | raise NotImplementedError() |
| 149 | |
| 150 | def activate_image_update(self, device, request): |
| 151 | raise NotImplementedError() |
| 152 | |
| 153 | def revert_image_update(self, device, request): |
| 154 | raise NotImplementedError() |
| 155 | |
| 156 | def self_test_device(self, device): |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 157 | log.info('self-test', device_id=device.id) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 158 | result = reactor.callLater(0, self.devices_handlers[ |
| 159 | device.id].self_test_device) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 160 | log.info('self-test-done', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | return result |
| 162 | |
| 163 | def delete_device(self, device): |
| 164 | log.info('delete-device', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 165 | reactor.callLater(0, self.devices_handlers[device.id].delete) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 166 | log.info('delete-device-done', device_id=device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 167 | return device |
| 168 | |
| 169 | def get_device_details(self, device): |
| 170 | raise NotImplementedError() |
| 171 | |
| 172 | def update_flows_bulk(self, device, flows, groups): |
| 173 | log.info('bulk-flow-update', device_id=device.id, |
| 174 | flows=flows, groups=groups) |
| 175 | assert len(groups.items) == 0 |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 176 | reactor.callLater(0, self.devices_handlers[device.id].update_flow_table, |
| 177 | flows.items) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 178 | return device |
| 179 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 180 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 181 | log.info('incremental-flow-update', device_id=device.id, |
| 182 | flows=flow_changes, groups=group_changes) |
| 183 | # For now, there is no support for group changes |
| 184 | assert len(group_changes.to_add.items) == 0 |
| 185 | assert len(group_changes.to_remove.items) == 0 |
| 186 | |
| 187 | handler = self.devices_handlers[device.id] |
| 188 | # Remove flows |
| 189 | if len(flow_changes.to_remove.items) != 0: |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 190 | reactor.callLater(0, handler.remove_from_flow_table, |
| 191 | flow_changes.to_remove.items) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | |
| 193 | # Add flows |
| 194 | if len(flow_changes.to_add.items) != 0: |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 195 | reactor.callLater(0, handler.add_to_flow_table, |
| 196 | flow_changes.to_add.items) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 197 | return device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 198 | |
| 199 | def update_pm_config(self, device, pm_config): |
| 200 | log.info("adapter-update-pm-config", device=device, |
| 201 | pm_config=pm_config) |
| 202 | handler = self.devices_handlers[device.id] |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 203 | if handler: |
| 204 | reactor.callLater(0, handler.update_pm_config, device, pm_config) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 205 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 206 | def process_inter_adapter_message(self, msg): |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 207 | raise NotImplementedError() |
| 208 | |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 209 | def receive_packet_out(self, device_id, egress_port_no, msg): |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 210 | raise NotImplementedError() |
| 211 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 212 | def suppress_alarm(self, filter): |
| 213 | raise NotImplementedError() |
| 214 | |
| 215 | def unsuppress_alarm(self, filter): |
| 216 | raise NotImplementedError() |
| 217 | |
| 218 | def _get_handler(self, device): |
| 219 | if device.id in self.devices_handlers: |
| 220 | handler = self.devices_handlers[device.id] |
| 221 | if handler is not None: |
| 222 | return handler |
| 223 | return None |
| 224 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 225 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 226 | """ |
| 227 | OLT Adapter base class |
| 228 | """ |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 229 | |
| 230 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 231 | class OltAdapter(IAdapter): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 232 | def __init__(self, |
| 233 | core_proxy, |
| 234 | adapter_proxy, |
| 235 | config, |
| 236 | device_handler_class, |
| 237 | name, |
| 238 | vendor, |
| 239 | version, device_type, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 240 | accepts_bulk_flow_update=True, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 241 | accepts_add_remove_flow_updates=False): |
| 242 | super(OltAdapter, self).__init__(core_proxy=core_proxy, |
| 243 | adapter_proxy=adapter_proxy, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 244 | config=config, |
| 245 | device_handler_class=device_handler_class, |
| 246 | name=name, |
| 247 | vendor=vendor, |
| 248 | version=version, |
| 249 | device_type=device_type, |
| 250 | vendor_id=None, |
| 251 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 252 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 253 | self.logical_device_id_to_root_device_id = dict() |
| 254 | |
| 255 | def reconcile_device(self, device): |
| 256 | try: |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 257 | self.devices_handlers[device.id] = self.device_handler_class(self, |
| 258 | device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 259 | # Work only required for devices that are in ENABLED state |
| 260 | if device.admin_state == AdminState.ENABLED: |
| 261 | reactor.callLater(0, |
| 262 | self.devices_handlers[device.id].reconcile, |
| 263 | device) |
| 264 | else: |
| 265 | # Invoke the children reconciliation which would setup the |
| 266 | # basic children data structures |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 267 | self.core_proxy.reconcile_child_devices(device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 268 | return device |
| 269 | except Exception, e: |
| 270 | log.exception('Exception', e=e) |
| 271 | |
| 272 | def send_proxied_message(self, proxy_address, msg): |
| 273 | log.info('send-proxied-message', proxy_address=proxy_address, msg=msg) |
| 274 | handler = self.devices_handlers[proxy_address.device_id] |
| 275 | handler.send_proxied_message(proxy_address, msg) |
| 276 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 277 | def process_inter_adapter_message(self, msg): |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 278 | log.debug('process-inter-adapter-message', msg=msg) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 279 | # Unpack the header to know which device needs to handle this message |
| 280 | handler = None |
| 281 | if msg.header.proxy_device_id: |
| 282 | # typical request |
| 283 | handler = self.devices_handlers[msg.header.proxy_device_id] |
| 284 | elif msg.header.to_device_id and \ |
| 285 | msg.header.to_device_id in self.devices_handlers: |
| 286 | # typical response |
| 287 | handler = self.devices_handlers[msg.header.to_device_id] |
| 288 | if handler: |
| 289 | reactor.callLater(0, handler.process_inter_adapter_message, msg) |
| 290 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 291 | def receive_packet_out(self, device_id, egress_port_no, msg): |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 292 | try: |
| 293 | log.info('receive_packet_out', device_id=device_id, |
| 294 | egress_port=egress_port_no, msg=msg) |
| 295 | handler = self.devices_handlers[device_id] |
| 296 | if handler: |
| 297 | reactor.callLater(0, handler.packet_out, egress_port_no, msg.data) |
| 298 | except Exception, e: |
| 299 | log.exception('packet-out-failure', e=e) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 300 | |
| 301 | |
| 302 | """ |
| 303 | ONU Adapter base class |
| 304 | """ |
| 305 | |
| 306 | |
| 307 | class OnuAdapter(IAdapter): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 308 | def __init__(self, |
| 309 | core_proxy, |
| 310 | adapter_proxy, |
| 311 | config, |
| 312 | device_handler_class, |
| 313 | name, |
| 314 | vendor, |
| 315 | version, |
| 316 | device_type, |
| 317 | vendor_id, |
| 318 | accepts_bulk_flow_update=True, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 319 | accepts_add_remove_flow_updates=False): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 320 | super(OnuAdapter, self).__init__(core_proxy=core_proxy, |
| 321 | adapter_proxy=adapter_proxy, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 322 | config=config, |
| 323 | device_handler_class=device_handler_class, |
| 324 | name=name, |
| 325 | vendor=vendor, |
| 326 | version=version, |
| 327 | device_type=device_type, |
| 328 | vendor_id=vendor_id, |
| 329 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 330 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 331 | |
| 332 | def reconcile_device(self, device): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 333 | self.devices_handlers[device.id] = self.device_handler_class(self, |
| 334 | device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 335 | # Reconcile only if state was ENABLED |
| 336 | if device.admin_state == AdminState.ENABLED: |
| 337 | reactor.callLater(0, |
| 338 | self.devices_handlers[device.id].reconcile, |
| 339 | device) |
| 340 | return device |
| 341 | |
| 342 | def receive_proxied_message(self, proxy_address, msg): |
| 343 | log.info('receive-proxied-message', proxy_address=proxy_address, |
| 344 | device_id=proxy_address.device_id, msg=msg) |
| 345 | # Device_id from the proxy_address is the olt device id. We need to |
| 346 | # get the onu device id using the port number in the proxy_address |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 347 | device = self.core_proxy. \ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 348 | get_child_device_with_proxy_address(proxy_address) |
| 349 | if device: |
| 350 | handler = self.devices_handlers[device.id] |
| 351 | handler.receive_message(msg) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 352 | |
| 353 | def process_inter_adapter_message(self, msg): |
| 354 | log.info('process-inter-adapter-message', msg=msg) |
| 355 | # Unpack the header to know which device needs to handle this message |
| 356 | if msg.header: |
| 357 | handler = self.devices_handlers[msg.header.to_device_id] |
| 358 | handler.process_inter_adapter_message(msg) |