Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 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 | Fully simulated OLT/ONU adapter. |
| 19 | """ |
| 20 | |
| 21 | import structlog |
| 22 | from twisted.internet import reactor |
| 23 | from twisted.internet.defer import DeferredQueue, inlineCallbacks |
| 24 | from zope.interface import implementer |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 25 | from common.utils.asleep import asleep |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 26 | |
| 27 | from voltha.adapters.interface import IAdapterInterface |
| 28 | from voltha.core.logical_device_agent import mac_str_to_tuple |
| 29 | from voltha.protos import third_party |
| 30 | from voltha.protos.adapter_pb2 import Adapter |
| 31 | from voltha.protos.adapter_pb2 import AdapterConfig |
| 32 | from voltha.protos.common_pb2 import LogLevel, OperStatus, ConnectStatus, \ |
| 33 | AdminState |
| 34 | from voltha.protos.device_pb2 import DeviceType, DeviceTypes, Port |
| 35 | from voltha.protos.health_pb2 import HealthStatus |
| 36 | from voltha.protos.logical_device_pb2 import LogicalPort |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 37 | from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \ |
| 38 | OFPPF_1GB_FD, OFPPC_NO_RECV |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 39 | from voltha.protos.openflow_13_pb2 import ofp_port |
| 40 | from voltha.protos.ponsim_pb2 import FlowTable |
| 41 | |
| 42 | _ = third_party |
| 43 | log = structlog.get_logger() |
| 44 | |
| 45 | |
| 46 | @implementer(IAdapterInterface) |
| 47 | class PonSimOnuAdapter(object): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 48 | name = 'ponsim_onu' |
| 49 | |
| 50 | supported_device_types = [ |
| 51 | DeviceType( |
| 52 | id=name, |
| 53 | adapter=name, |
| 54 | accepts_bulk_flow_update=True |
| 55 | ) |
| 56 | ] |
| 57 | |
| 58 | def __init__(self, adapter_agent, config): |
| 59 | self.adapter_agent = adapter_agent |
| 60 | self.config = config |
| 61 | self.descriptor = Adapter( |
| 62 | id=self.name, |
| 63 | vendor='Voltha project', |
| 64 | version='0.4', |
| 65 | config=AdapterConfig(log_level=LogLevel.INFO) |
| 66 | ) |
| 67 | self.devices_handlers = dict() # device_id -> PonSimOltHandler() |
| 68 | |
| 69 | def start(self): |
| 70 | log.debug('starting') |
| 71 | log.info('started') |
| 72 | |
| 73 | def stop(self): |
| 74 | log.debug('stopping') |
| 75 | log.info('stopped') |
| 76 | |
| 77 | def adapter_descriptor(self): |
| 78 | return self.descriptor |
| 79 | |
| 80 | def device_types(self): |
| 81 | return DeviceTypes(items=self.supported_device_types) |
| 82 | |
| 83 | def health(self): |
| 84 | return HealthStatus(state=HealthStatus.HealthState.HEALTHY) |
| 85 | |
| 86 | def change_master_state(self, master): |
| 87 | raise NotImplementedError() |
| 88 | |
Sergio Slobodrian | ec864c6 | 2017-03-09 11:41:43 -0500 | [diff] [blame] | 89 | def update_pm_config(self, device, pm_configs): |
| 90 | raise NotImplementedError() |
| 91 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 92 | def adopt_device(self, device): |
| 93 | self.devices_handlers[device.id] = PonSimOnuHandler(self, device.id) |
| 94 | reactor.callLater(0, self.devices_handlers[device.id].activate, device) |
| 95 | return device |
| 96 | |
| 97 | def abandon_device(self, device): |
| 98 | raise NotImplementedError() |
| 99 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 100 | def disable_device(self, device): |
| 101 | log.info('disable-device', device_id=device.id) |
| 102 | reactor.callLater(0, self.devices_handlers[device.id].disable) |
| 103 | return device |
| 104 | |
| 105 | def reenable_device(self, device): |
| 106 | log.info('reenable-device', device_id=device.id) |
| 107 | reactor.callLater(0, self.devices_handlers[device.id].reenable) |
| 108 | return device |
| 109 | |
| 110 | def reboot_device(self, device): |
| 111 | log.info('rebooting', device_id=device.id) |
| 112 | reactor.callLater(0, self.devices_handlers[device.id].reboot) |
| 113 | return device |
| 114 | |
| 115 | def delete_device(self, device): |
| 116 | log.info('delete-device', device_id=device.id) |
| 117 | reactor.callLater(0, self.devices_handlers[device.id].delete) |
| 118 | return device |
| 119 | |
| 120 | def get_device_details(self, device): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 121 | raise NotImplementedError() |
| 122 | |
| 123 | def update_flows_bulk(self, device, flows, groups): |
| 124 | log.info('bulk-flow-update', device_id=device.id, |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 125 | flows=flows, groups=groups) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 126 | assert len(groups.items) == 0 |
| 127 | handler = self.devices_handlers[device.id] |
| 128 | return handler.update_flow_table(flows.items) |
| 129 | |
| 130 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 131 | raise NotImplementedError() |
| 132 | |
| 133 | def send_proxied_message(self, proxy_address, msg): |
| 134 | log.info('send-proxied-message', proxy_address=proxy_address, msg=msg) |
| 135 | |
| 136 | def receive_proxied_message(self, proxy_address, msg): |
| 137 | log.info('receive-proxied-message', proxy_address=proxy_address, |
| 138 | device_id=proxy_address.device_id, msg=msg) |
| 139 | handler = self.devices_handlers[proxy_address.device_id] |
| 140 | handler.receive_message(msg) |
| 141 | |
| 142 | def receive_packet_out(self, logical_device_id, egress_port_no, msg): |
| 143 | log.info('packet-out', logical_device_id=logical_device_id, |
| 144 | egress_port_no=egress_port_no, msg_len=len(msg)) |
| 145 | |
Peter Shafik | b2ff5a6 | 2017-05-02 15:54:39 -0400 | [diff] [blame] | 146 | def receive_inter_adapter_message(self, msg): |
| 147 | raise NotImplementedError() |
| 148 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 149 | |
| 150 | class PonSimOnuHandler(object): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 151 | def __init__(self, adapter, device_id): |
| 152 | self.adapter = adapter |
| 153 | self.adapter_agent = adapter.adapter_agent |
| 154 | self.device_id = device_id |
| 155 | self.log = structlog.get_logger(device_id=device_id) |
| 156 | self.incoming_messages = DeferredQueue() |
| 157 | self.proxy_address = None |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 158 | # reference of uni_port is required when re-enabling the device if |
| 159 | # it was disabled previously |
| 160 | self.uni_port = None |
| 161 | self.pon_port = None |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 162 | |
| 163 | def receive_message(self, msg): |
| 164 | self.incoming_messages.put(msg) |
| 165 | |
| 166 | def activate(self, device): |
| 167 | self.log.info('activating') |
| 168 | |
| 169 | # first we verify that we got parent reference and proxy info |
| 170 | assert device.parent_id |
| 171 | assert device.proxy_address.device_id |
| 172 | assert device.proxy_address.channel_id |
| 173 | |
| 174 | # register for proxied messages right away |
| 175 | self.proxy_address = device.proxy_address |
| 176 | self.adapter_agent.register_for_proxied_messages(device.proxy_address) |
| 177 | |
| 178 | # populate device info |
| 179 | device.root = True |
| 180 | device.vendor = 'ponsim' |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 181 | device.model = 'n/a' |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 182 | device.connect_status = ConnectStatus.REACHABLE |
| 183 | self.adapter_agent.update_device(device) |
| 184 | |
| 185 | # register physical ports |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 186 | self.uni_port = Port( |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 187 | port_no=2, |
| 188 | label='UNI facing Ethernet port', |
| 189 | type=Port.ETHERNET_UNI, |
| 190 | admin_state=AdminState.ENABLED, |
| 191 | oper_status=OperStatus.ACTIVE |
| 192 | ) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 193 | self.pon_port = Port( |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 194 | port_no=1, |
| 195 | label='PON port', |
| 196 | type=Port.PON_ONU, |
| 197 | admin_state=AdminState.ENABLED, |
| 198 | oper_status=OperStatus.ACTIVE, |
| 199 | peers=[ |
| 200 | Port.PeerPort( |
| 201 | device_id=device.parent_id, |
| 202 | port_no=device.parent_port_no |
| 203 | ) |
| 204 | ] |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 205 | ) |
| 206 | self.adapter_agent.add_port(device.id, self.uni_port) |
| 207 | self.adapter_agent.add_port(device.id, self.pon_port) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 208 | |
| 209 | # add uni port to logical device |
| 210 | parent_device = self.adapter_agent.get_device(device.parent_id) |
| 211 | logical_device_id = parent_device.parent_id |
| 212 | assert logical_device_id |
| 213 | port_no = device.proxy_address.channel_id |
| 214 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
| 215 | self.adapter_agent.add_logical_port(logical_device_id, LogicalPort( |
| 216 | id='uni-{}'.format(port_no), |
| 217 | ofp_port=ofp_port( |
| 218 | port_no=port_no, |
| 219 | hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no), |
| 220 | name='uni-{}'.format(port_no), |
| 221 | config=0, |
| 222 | state=OFPPS_LIVE, |
| 223 | curr=cap, |
| 224 | advertised=cap, |
| 225 | peer=cap, |
| 226 | curr_speed=OFPPF_1GB_FD, |
| 227 | max_speed=OFPPF_1GB_FD |
| 228 | ), |
| 229 | device_id=device.id, |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 230 | device_port_no=self.uni_port.port_no |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 231 | )) |
| 232 | |
| 233 | device = self.adapter_agent.get_device(device.id) |
| 234 | device.oper_status = OperStatus.ACTIVE |
| 235 | self.adapter_agent.update_device(device) |
| 236 | |
| 237 | @inlineCallbacks |
| 238 | def update_flow_table(self, flows): |
| 239 | |
| 240 | # we need to proxy through the OLT to get to the ONU |
| 241 | |
| 242 | # reset response queue |
| 243 | while self.incoming_messages.pending: |
| 244 | yield self.incoming_messages.get() |
| 245 | |
| 246 | msg = FlowTable( |
| 247 | port=self.proxy_address.channel_id, |
| 248 | flows=flows |
| 249 | ) |
| 250 | self.adapter_agent.send_proxied_message(self.proxy_address, msg) |
| 251 | |
| 252 | yield self.incoming_messages.get() |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 253 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 254 | @inlineCallbacks |
| 255 | def reboot(self): |
| 256 | self.log.info('rebooting', device_id=self.device_id) |
| 257 | |
| 258 | # Update the operational status to ACTIVATING and connect status to |
| 259 | # UNREACHABLE |
| 260 | device = self.adapter_agent.get_device(self.device_id) |
| 261 | previous_oper_status = device.oper_status |
| 262 | previous_conn_status = device.connect_status |
| 263 | device.oper_status = OperStatus.ACTIVATING |
| 264 | device.connect_status = ConnectStatus.UNREACHABLE |
| 265 | self.adapter_agent.update_device(device) |
| 266 | |
| 267 | # Sleep 10 secs, simulating a reboot |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 268 | # TODO: send alert and clear alert after the reboot |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 269 | yield asleep(10) |
| 270 | |
| 271 | # Change the operational status back to its previous state. With a |
| 272 | # real OLT the operational state should be the state the device is |
| 273 | # after a reboot. |
| 274 | # Get the latest device reference |
| 275 | device = self.adapter_agent.get_device(self.device_id) |
| 276 | device.oper_status = previous_oper_status |
| 277 | device.connect_status = previous_conn_status |
| 278 | self.adapter_agent.update_device(device) |
| 279 | self.log.info('rebooted', device_id=self.device_id) |
| 280 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 281 | def disable(self): |
| 282 | self.log.info('disabling', device_id=self.device_id) |
| 283 | |
| 284 | # Get the latest device reference |
| 285 | device = self.adapter_agent.get_device(self.device_id) |
| 286 | |
| 287 | # Disable all ports on that device |
| 288 | self.adapter_agent.disable_all_ports(self.device_id) |
| 289 | |
| 290 | # Update the device operational status to UNKNOWN |
| 291 | device.oper_status = OperStatus.UNKNOWN |
| 292 | device.connect_status = ConnectStatus.UNREACHABLE |
| 293 | self.adapter_agent.update_device(device) |
| 294 | |
| 295 | # Remove the uni logical port from the OLT, if still present |
| 296 | parent_device = self.adapter_agent.get_device(device.parent_id) |
| 297 | assert parent_device |
| 298 | logical_device_id = parent_device.parent_id |
| 299 | assert logical_device_id |
| 300 | port_no = device.proxy_address.channel_id |
| 301 | port_id = 'uni-{}'.format(port_no) |
| 302 | try: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 303 | port = self.adapter_agent.get_logical_port(logical_device_id, |
| 304 | port_id) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 305 | self.adapter_agent.delete_logical_port(logical_device_id, port) |
| 306 | except KeyError: |
| 307 | self.log.info('logical-port-not-found', device_id=self.device_id, |
| 308 | portid=port_id) |
| 309 | |
| 310 | # Remove pon port from parent |
| 311 | self.adapter_agent.delete_port_reference_from_parent(self.device_id, |
| 312 | self.pon_port) |
| 313 | |
| 314 | # Just updating the port status may be an option as well |
| 315 | # port.ofp_port.config = OFPPC_NO_RECV |
| 316 | # yield self.adapter_agent.update_logical_port(logical_device_id, |
| 317 | # port) |
| 318 | # Unregister for proxied message |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 319 | self.adapter_agent.unregister_for_proxied_messages( |
| 320 | device.proxy_address) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 321 | |
| 322 | # TODO: |
| 323 | # 1) Remove all flows from the device |
| 324 | # 2) Remove the device from ponsim |
| 325 | |
| 326 | self.log.info('disabled', device_id=device.id) |
| 327 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 328 | def reenable(self): |
| 329 | self.log.info('re-enabling', device_id=self.device_id) |
| 330 | |
| 331 | # Get the latest device reference |
| 332 | device = self.adapter_agent.get_device(self.device_id) |
| 333 | |
| 334 | # First we verify that we got parent reference and proxy info |
| 335 | assert self.uni_port |
| 336 | assert device.parent_id |
| 337 | assert device.proxy_address.device_id |
| 338 | assert device.proxy_address.channel_id |
| 339 | |
| 340 | # Re-register for proxied messages right away |
| 341 | self.proxy_address = device.proxy_address |
| 342 | self.adapter_agent.register_for_proxied_messages(device.proxy_address) |
| 343 | |
| 344 | # Re-enable the ports on that device |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 345 | self.adapter_agent.enable_all_ports(self.device_id) |
| 346 | |
| 347 | # Add the pon port reference to the parent |
| 348 | self.adapter_agent.add_port_reference_to_parent(device.id, |
| 349 | self.pon_port) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 350 | |
| 351 | # Update the connect status to REACHABLE |
| 352 | device.connect_status = ConnectStatus.REACHABLE |
| 353 | self.adapter_agent.update_device(device) |
| 354 | |
| 355 | # re-add uni port to logical device |
| 356 | parent_device = self.adapter_agent.get_device(device.parent_id) |
| 357 | logical_device_id = parent_device.parent_id |
| 358 | assert logical_device_id |
| 359 | port_no = device.proxy_address.channel_id |
| 360 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
| 361 | self.adapter_agent.add_logical_port(logical_device_id, LogicalPort( |
| 362 | id='uni-{}'.format(port_no), |
| 363 | ofp_port=ofp_port( |
| 364 | port_no=port_no, |
| 365 | hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no), |
| 366 | name='uni-{}'.format(port_no), |
| 367 | config=0, |
| 368 | state=OFPPS_LIVE, |
| 369 | curr=cap, |
| 370 | advertised=cap, |
| 371 | peer=cap, |
| 372 | curr_speed=OFPPF_1GB_FD, |
| 373 | max_speed=OFPPF_1GB_FD |
| 374 | ), |
| 375 | device_id=device.id, |
| 376 | device_port_no=self.uni_port.port_no |
| 377 | )) |
| 378 | |
| 379 | device = self.adapter_agent.get_device(device.id) |
| 380 | device.oper_status = OperStatus.ACTIVE |
| 381 | self.adapter_agent.update_device(device) |
| 382 | |
| 383 | self.log.info('re-enabled', device_id=device.id) |
| 384 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 385 | def delete(self): |
| 386 | self.log.info('deleting', device_id=self.device_id) |
| 387 | |
| 388 | # A delete request may be received when an OLT is dsiabled |
| 389 | |
| 390 | # TODO: |
| 391 | # 1) Remove all flows from the device |
| 392 | # 2) Remove the device from ponsim |
| 393 | |
| 394 | self.log.info('deleted', device_id=self.device_id) |