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