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): |
| 86 | log.debug('get_ofp_device_info', device_id=device.id) |
| 87 | return self.devices_handlers[device.id].get_ofp_device_info(device) |
| 88 | |
| 89 | def get_ofp_port_info(self, device, port_no): |
| 90 | log.debug('get_ofp_port_info', device_id=device.id, port_no=port_no) |
| 91 | return self.devices_handlers[device.id].get_ofp_port_info(device, port_no) |
| 92 | |
| 93 | def adopt_device(self, device): |
| 94 | log.debug('adopt_device', device_id=device.id) |
| 95 | self.devices_handlers[device.id] = self.device_handler_class(self, device.id) |
| 96 | reactor.callLater(0, self.devices_handlers[device.id].activate, device) |
| 97 | log.debug('adopt_device_done', device_id=device.id) |
| 98 | return device |
| 99 | |
| 100 | def reconcile_device(self, device): |
| 101 | raise NotImplementedError() |
| 102 | |
| 103 | def abandon_device(self, device): |
| 104 | raise NotImplementedError() |
| 105 | |
| 106 | def disable_device(self, device): |
| 107 | log.info('disable-device', device_id=device.id) |
| 108 | reactor.callLater(0, self.devices_handlers[device.id].disable) |
| 109 | return device |
| 110 | |
| 111 | def reenable_device(self, device): |
| 112 | log.info('reenable-device', device_id=device.id) |
| 113 | reactor.callLater(0, self.devices_handlers[device.id].reenable) |
| 114 | return device |
| 115 | |
| 116 | def reboot_device(self, device): |
| 117 | log.info('reboot-device', device_id=device.id) |
| 118 | reactor.callLater(0, self.devices_handlers[device.id].reboot) |
| 119 | return device |
| 120 | |
| 121 | def download_image(self, device, request): |
| 122 | raise NotImplementedError() |
| 123 | |
| 124 | def get_image_download_status(self, device, request): |
| 125 | raise NotImplementedError() |
| 126 | |
| 127 | def cancel_image_download(self, device, request): |
| 128 | raise NotImplementedError() |
| 129 | |
| 130 | def activate_image_update(self, device, request): |
| 131 | raise NotImplementedError() |
| 132 | |
| 133 | def revert_image_update(self, device, request): |
| 134 | raise NotImplementedError() |
| 135 | |
| 136 | def self_test_device(self, device): |
| 137 | log.info('self-test-req', device_id=device.id) |
| 138 | result = reactor.callLater(0, self.devices_handlers[device.id].self_test_device) |
| 139 | return result |
| 140 | |
| 141 | def delete_device(self, device): |
| 142 | log.info('delete-device', device_id=device.id) |
| 143 | # TODO: Update the logical device mapping |
| 144 | reactor.callLater(0, self.devices_handlers[device.id].delete) |
| 145 | return device |
| 146 | |
| 147 | def get_device_details(self, device): |
| 148 | raise NotImplementedError() |
| 149 | |
| 150 | def update_flows_bulk(self, device, flows, groups): |
| 151 | log.info('bulk-flow-update', device_id=device.id, |
| 152 | flows=flows, groups=groups) |
| 153 | assert len(groups.items) == 0 |
| 154 | handler = self.devices_handlers[device.id] |
| 155 | return handler.update_flow_table(flows.items) |
| 156 | |
| 157 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 158 | log.info('incremental-flow-update', device_id=device.id, |
| 159 | flows=flow_changes, groups=group_changes) |
| 160 | # For now, there is no support for group changes |
| 161 | assert len(group_changes.to_add.items) == 0 |
| 162 | assert len(group_changes.to_remove.items) == 0 |
| 163 | |
| 164 | handler = self.devices_handlers[device.id] |
| 165 | # Remove flows |
| 166 | if len(flow_changes.to_remove.items) != 0: |
| 167 | handler.remove_from_flow_table(flow_changes.to_remove.items) |
| 168 | |
| 169 | # Add flows |
| 170 | if len(flow_changes.to_add.items) != 0: |
| 171 | handler.add_to_flow_table(flow_changes.to_add.items) |
| 172 | |
| 173 | def update_pm_config(self, device, pm_config): |
| 174 | log.info("adapter-update-pm-config", device=device, |
| 175 | pm_config=pm_config) |
| 176 | handler = self.devices_handlers[device.id] |
| 177 | handler.update_pm_config(device, pm_config) |
| 178 | |
| 179 | def send_proxied_message(self, proxy_address, msg): |
| 180 | raise NotImplementedError() |
| 181 | |
| 182 | def receive_proxied_message(self, proxy_address, msg): |
| 183 | raise NotImplementedError() |
| 184 | |
| 185 | def receive_packet_out(self, logical_device_id, egress_port_no, msg): |
| 186 | raise NotImplementedError() |
| 187 | |
| 188 | def receive_inter_adapter_message(self, msg): |
| 189 | raise NotImplementedError() |
| 190 | |
| 191 | def suppress_alarm(self, filter): |
| 192 | raise NotImplementedError() |
| 193 | |
| 194 | def unsuppress_alarm(self, filter): |
| 195 | raise NotImplementedError() |
| 196 | |
| 197 | def _get_handler(self, device): |
| 198 | if device.id in self.devices_handlers: |
| 199 | handler = self.devices_handlers[device.id] |
| 200 | if handler is not None: |
| 201 | return handler |
| 202 | return None |
| 203 | |
| 204 | """ |
| 205 | OLT Adapter base class |
| 206 | """ |
| 207 | class OltAdapter(IAdapter): |
| 208 | def __init__(self, adapter_agent, config, device_handler_class, name, |
| 209 | vendor, version, device_type, |
| 210 | accepts_bulk_flow_update=True, |
| 211 | accepts_add_remove_flow_updates=False, |
| 212 | core_proxy=None): |
| 213 | super(OltAdapter, self).__init__(adapter_agent=adapter_agent, |
| 214 | config=config, |
| 215 | device_handler_class=device_handler_class, |
| 216 | name=name, |
| 217 | vendor=vendor, |
| 218 | version=version, |
| 219 | device_type=device_type, |
| 220 | vendor_id=None, |
| 221 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
| 222 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates, |
| 223 | core_proxy=None) |
| 224 | self.logical_device_id_to_root_device_id = dict() |
| 225 | |
| 226 | def reconcile_device(self, device): |
| 227 | try: |
| 228 | self.devices_handlers[device.id] = self.device_handler_class(self, device.id) |
| 229 | # Work only required for devices that are in ENABLED state |
| 230 | if device.admin_state == AdminState.ENABLED: |
| 231 | reactor.callLater(0, |
| 232 | self.devices_handlers[device.id].reconcile, |
| 233 | device) |
| 234 | else: |
| 235 | # Invoke the children reconciliation which would setup the |
| 236 | # basic children data structures |
| 237 | self.adapter_agent.reconcile_child_devices(device.id) |
| 238 | return device |
| 239 | except Exception, e: |
| 240 | log.exception('Exception', e=e) |
| 241 | |
| 242 | def send_proxied_message(self, proxy_address, msg): |
| 243 | log.info('send-proxied-message', proxy_address=proxy_address, msg=msg) |
| 244 | handler = self.devices_handlers[proxy_address.device_id] |
| 245 | handler.send_proxied_message(proxy_address, msg) |
| 246 | |
| 247 | def receive_packet_out(self, logical_device_id, egress_port_no, msg): |
| 248 | def ldi_to_di(ldi): |
| 249 | di = self.logical_device_id_to_root_device_id.get(ldi) |
| 250 | if di is None: |
| 251 | logical_device = self.adapter_agent.get_logical_device(ldi) |
| 252 | di = logical_device.root_device_id |
| 253 | self.logical_device_id_to_root_device_id[ldi] = di |
| 254 | return di |
| 255 | |
| 256 | device_id = ldi_to_di(logical_device_id) |
| 257 | handler = self.devices_handlers[device_id] |
| 258 | handler.packet_out(egress_port_no, msg) |
| 259 | |
| 260 | |
| 261 | """ |
| 262 | ONU Adapter base class |
| 263 | """ |
| 264 | |
| 265 | |
| 266 | class OnuAdapter(IAdapter): |
| 267 | def __init__(self, adapter_agent, config, device_handler_class, name, |
| 268 | vendor, version, device_type, vendor_id, accepts_bulk_flow_update=True, |
| 269 | accepts_add_remove_flow_updates=False): |
| 270 | super(OnuAdapter, self).__init__(adapter_agent=adapter_agent, |
| 271 | config=config, |
| 272 | device_handler_class=device_handler_class, |
| 273 | name=name, |
| 274 | vendor=vendor, |
| 275 | version=version, |
| 276 | device_type=device_type, |
| 277 | vendor_id=vendor_id, |
| 278 | accepts_bulk_flow_update=accepts_bulk_flow_update, |
| 279 | accepts_add_remove_flow_updates=accepts_add_remove_flow_updates, |
| 280 | core_proxy=None |
| 281 | ) |
| 282 | |
| 283 | def reconcile_device(self, device): |
| 284 | self.devices_handlers[device.id] = self.device_handler_class(self, device.id) |
| 285 | # Reconcile only if state was ENABLED |
| 286 | if device.admin_state == AdminState.ENABLED: |
| 287 | reactor.callLater(0, |
| 288 | self.devices_handlers[device.id].reconcile, |
| 289 | device) |
| 290 | return device |
| 291 | |
| 292 | def receive_proxied_message(self, proxy_address, msg): |
| 293 | log.info('receive-proxied-message', proxy_address=proxy_address, |
| 294 | device_id=proxy_address.device_id, msg=msg) |
| 295 | # Device_id from the proxy_address is the olt device id. We need to |
| 296 | # get the onu device id using the port number in the proxy_address |
| 297 | device = self.adapter_agent. \ |
| 298 | get_child_device_with_proxy_address(proxy_address) |
| 299 | if device: |
| 300 | handler = self.devices_handlers[device.id] |
| 301 | handler.receive_message(msg) |