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