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] |
| 203 | handler.update_pm_config(device, pm_config) |
| 204 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 205 | def process_inter_adapter_message(self, msg): |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 206 | raise NotImplementedError() |
| 207 | |
| 208 | def receive_packet_out(self, logical_device_id, egress_port_no, msg): |
| 209 | raise NotImplementedError() |
| 210 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 211 | def suppress_alarm(self, filter): |
| 212 | raise NotImplementedError() |
| 213 | |
| 214 | def unsuppress_alarm(self, filter): |
| 215 | raise NotImplementedError() |
| 216 | |
| 217 | def _get_handler(self, device): |
| 218 | if device.id in self.devices_handlers: |
| 219 | handler = self.devices_handlers[device.id] |
| 220 | if handler is not None: |
| 221 | return handler |
| 222 | return None |
| 223 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 224 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 225 | """ |
| 226 | OLT Adapter base class |
| 227 | """ |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 228 | |
| 229 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 230 | class OltAdapter(IAdapter): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 231 | def __init__(self, |
| 232 | core_proxy, |
| 233 | adapter_proxy, |
| 234 | config, |
| 235 | device_handler_class, |
| 236 | name, |
| 237 | vendor, |
| 238 | version, device_type, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 239 | accepts_bulk_flow_update=True, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 240 | accepts_add_remove_flow_updates=False): |
| 241 | super(OltAdapter, self).__init__(core_proxy=core_proxy, |
| 242 | adapter_proxy=adapter_proxy, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 243 | config=config, |
| 244 | device_handler_class=device_handler_class, |
| 245 | name=name, |
| 246 | vendor=vendor, |
| 247 | version=version, |
| 248 | device_type=device_type, |
| 249 | vendor_id=None, |
| 250 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 251 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 252 | self.logical_device_id_to_root_device_id = dict() |
| 253 | |
| 254 | def reconcile_device(self, device): |
| 255 | try: |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 256 | self.devices_handlers[device.id] = self.device_handler_class(self, |
| 257 | device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 258 | # Work only required for devices that are in ENABLED state |
| 259 | if device.admin_state == AdminState.ENABLED: |
| 260 | reactor.callLater(0, |
| 261 | self.devices_handlers[device.id].reconcile, |
| 262 | device) |
| 263 | else: |
| 264 | # Invoke the children reconciliation which would setup the |
| 265 | # basic children data structures |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 266 | self.core_proxy.reconcile_child_devices(device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 267 | return device |
| 268 | except Exception, e: |
| 269 | log.exception('Exception', e=e) |
| 270 | |
| 271 | def send_proxied_message(self, proxy_address, msg): |
| 272 | log.info('send-proxied-message', proxy_address=proxy_address, msg=msg) |
| 273 | handler = self.devices_handlers[proxy_address.device_id] |
| 274 | handler.send_proxied_message(proxy_address, msg) |
| 275 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 276 | def process_inter_adapter_message(self, msg): |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 277 | log.debug('process-inter-adapter-message', msg=msg) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 278 | # Unpack the header to know which device needs to handle this message |
| 279 | handler = None |
| 280 | if msg.header.proxy_device_id: |
| 281 | # typical request |
| 282 | handler = self.devices_handlers[msg.header.proxy_device_id] |
| 283 | elif msg.header.to_device_id and \ |
| 284 | msg.header.to_device_id in self.devices_handlers: |
| 285 | # typical response |
| 286 | handler = self.devices_handlers[msg.header.to_device_id] |
| 287 | if handler: |
| 288 | reactor.callLater(0, handler.process_inter_adapter_message, msg) |
| 289 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 290 | def receive_packet_out(self, device_id, egress_port_no, msg): |
| 291 | log.info('receive_packet_out', device_id=device_id, |
| 292 | egress_port=egress_port_no, msg=msg) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 293 | handler = self.devices_handlers[device_id] |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 294 | handler.packet_out(egress_port_no, msg.data) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 295 | |
| 296 | |
| 297 | """ |
| 298 | ONU Adapter base class |
| 299 | """ |
| 300 | |
| 301 | |
| 302 | class OnuAdapter(IAdapter): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 303 | def __init__(self, |
| 304 | core_proxy, |
| 305 | adapter_proxy, |
| 306 | config, |
| 307 | device_handler_class, |
| 308 | name, |
| 309 | vendor, |
| 310 | version, |
| 311 | device_type, |
| 312 | vendor_id, |
| 313 | accepts_bulk_flow_update=True, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 314 | accepts_add_remove_flow_updates=False): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 315 | super(OnuAdapter, self).__init__(core_proxy=core_proxy, |
| 316 | adapter_proxy=adapter_proxy, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 317 | config=config, |
| 318 | device_handler_class=device_handler_class, |
| 319 | name=name, |
| 320 | vendor=vendor, |
| 321 | version=version, |
| 322 | device_type=device_type, |
| 323 | vendor_id=vendor_id, |
| 324 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 325 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 326 | |
| 327 | def reconcile_device(self, device): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 328 | self.devices_handlers[device.id] = self.device_handler_class(self, |
| 329 | device.id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 330 | # Reconcile only if state was ENABLED |
| 331 | if device.admin_state == AdminState.ENABLED: |
| 332 | reactor.callLater(0, |
| 333 | self.devices_handlers[device.id].reconcile, |
| 334 | device) |
| 335 | return device |
| 336 | |
| 337 | def receive_proxied_message(self, proxy_address, msg): |
| 338 | log.info('receive-proxied-message', proxy_address=proxy_address, |
| 339 | device_id=proxy_address.device_id, msg=msg) |
| 340 | # Device_id from the proxy_address is the olt device id. We need to |
| 341 | # get the onu device id using the port number in the proxy_address |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 342 | device = self.core_proxy. \ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 343 | get_child_device_with_proxy_address(proxy_address) |
| 344 | if device: |
| 345 | handler = self.devices_handlers[device.id] |
| 346 | handler.receive_message(msg) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 347 | |
| 348 | def process_inter_adapter_message(self, msg): |
| 349 | log.info('process-inter-adapter-message', msg=msg) |
| 350 | # Unpack the header to know which device needs to handle this message |
| 351 | if msg.header: |
| 352 | handler = self.devices_handlers[msg.header.to_device_id] |
| 353 | handler.process_inter_adapter_message(msg) |