Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2019 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 | import structlog |
| 17 | import socket |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 18 | from scapy.layers.l2 import Ether |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 19 | import voltha.core.flow_decomposer as fd |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 20 | from voltha.adapters.openolt.openolt_utils import OpenoltUtils |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 21 | from voltha.protos.device_pb2 import Port, Device |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 22 | from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \ |
| 23 | OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_PORT_STATS, OFPC_TABLE_STATS, \ |
| 24 | OFPC_FLOW_STATS, OFPC_GROUP_STATS, ofp_port, ofp_port_stats, ofp_desc, \ |
| 25 | ofp_switch_features |
| 26 | from voltha.core.logical_device_agent import mac_str_to_tuple |
| 27 | from voltha.protos.logical_device_pb2 import LogicalPort |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 28 | from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 29 | from voltha.protos.logical_device_pb2 import LogicalDevice |
| 30 | from voltha.registry import registry |
| 31 | |
| 32 | |
| 33 | class OpenOltDataModel(object): |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 34 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 35 | def __init__(self, device, adapter_agent, platform): |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 36 | self.log = structlog.get_logger() |
| 37 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 38 | self.device = device |
| 39 | self.adapter_agent = adapter_agent |
| 40 | self.platform = platform |
| 41 | self.logical_device_id = None |
| 42 | |
| 43 | self.device.root = True |
| 44 | self.device.connect_status = ConnectStatus.UNREACHABLE |
| 45 | self.device.oper_status = OperStatus.ACTIVATING |
| 46 | |
| 47 | self.adapter_agent.update_device(device) |
| 48 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 49 | self.nni_intf_id = None |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 50 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 51 | def reconcile(self): |
| 52 | assert self.logical_device_id is not None |
| 53 | self.adapter_agent.reconcile_logical_device( |
| 54 | self.logical_device_id) |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 55 | # Update device cache |
| 56 | self.device = self.get_device(self.device.id) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 57 | |
| 58 | def olt_create(self, device_info): |
| 59 | if self.logical_device_id is not None: |
| 60 | return |
| 61 | |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 62 | dpid = device_info.device_id |
| 63 | serial_number = device_info.device_serial_number |
| 64 | |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 65 | if dpid is None or dpid == '': |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 66 | uri = self.device.host_and_port.split(":")[0] |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 67 | try: |
| 68 | socket.inet_pton(socket.AF_INET, uri) |
| 69 | dpid = '00:00:' + OpenoltUtils.ip_hex(uri) |
| 70 | except socket.error: |
| 71 | # this is not an IP |
| 72 | dpid = OpenoltUtils.str_to_mac(uri) |
| 73 | |
| 74 | self.log.info('creating-openolt-logical-device', dp_id=dpid, |
| 75 | serial_number=serial_number) |
| 76 | |
| 77 | hw_desc = device_info.model |
| 78 | if device_info.hardware_version: |
| 79 | hw_desc += '-' + device_info.hardware_version |
| 80 | |
| 81 | # Create logical OF device |
| 82 | ld = LogicalDevice( |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 83 | root_device_id=self.device.id, |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 84 | switch_features=ofp_switch_features( |
| 85 | n_buffers=256, # TODO fake for now |
| 86 | n_tables=2, # TODO ditto |
| 87 | capabilities=( # TODO and ditto |
| 88 | OFPC_FLOW_STATS |
| 89 | | OFPC_TABLE_STATS |
| 90 | | OFPC_PORT_STATS |
| 91 | | OFPC_GROUP_STATS |
| 92 | ) |
| 93 | ), |
| 94 | desc=ofp_desc( |
| 95 | serial_num=serial_number |
| 96 | ) |
| 97 | ) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 98 | self.logical_device_id = \ |
| 99 | self.adapter_agent.create_logical_device(ld, dpid=dpid).id |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 100 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 101 | self.device.vendor = device_info.vendor |
| 102 | self.device.model = device_info.model |
| 103 | self.device.hardware_version = device_info.hardware_version |
| 104 | self.device.firmware_version = device_info.firmware_version |
| 105 | self.device.connect_status = ConnectStatus.REACHABLE |
| 106 | self.device.serial_number = serial_number |
| 107 | |
| 108 | self.adapter_agent.update_device(self.device) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 109 | |
| 110 | self.log.info('created-openolt-logical-device', |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 111 | logical_device_id=self.logical_device_id) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 112 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 113 | return self.logical_device_id |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 114 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 115 | def olt_oper_up(self): |
| 116 | self.device.parent_id = self.logical_device_id |
| 117 | self.device.oper_status = OperStatus.ACTIVE |
| 118 | self.adapter_agent.update_device(self.device) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 119 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 120 | def olt_oper_down(self): |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 121 | self.__disable_logical_device() |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 122 | |
| 123 | def olt_delete(self): |
| 124 | ld = self.adapter_agent.get_logical_device(self.logical_device_id) |
| 125 | self.adapter_agent.delete_logical_device(ld) |
| 126 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 127 | def olt_port_add_update(self, intf_id, intf_type, oper): |
| 128 | if oper == "up": |
| 129 | oper_status = OperStatus.ACTIVE |
| 130 | else: |
| 131 | oper_status = OperStatus.DISCOVERED |
| 132 | |
| 133 | if intf_type == "nni": |
| 134 | port_type = Port.ETHERNET_NNI |
| 135 | elif intf_type == "pon": |
| 136 | port_type = Port.PON_OLT |
| 137 | |
| 138 | port_no, label = self.__add_port(intf_id, port_type, oper_status) |
| 139 | |
| 140 | if intf_type == "nni": |
| 141 | self.__add_logical_port(port_no, intf_id, oper_status) |
| 142 | self.nni_intf_id = intf_id |
| 143 | |
| 144 | def olt_nni_intf_id(self): |
| 145 | if self.nni_intf_id is not None: |
| 146 | return self.nni_intf_id |
| 147 | |
| 148 | port_list = self.adapter_agent.get_ports(self.device.id, |
| 149 | Port.ETHERNET_NNI) |
| 150 | logical_port = self.adapter_agent.get_logical_port( |
| 151 | self.logical_device_id, port_list[0].label) |
| 152 | self.nni_intf_id = self.platform.intf_id_from_nni_port_num( |
| 153 | logical_port.ofp_port.port_no) |
| 154 | self.log.debug("nni-intf-d ", nni_intf_id=self.nni_intf_id) |
| 155 | return self.nni_intf_id |
| 156 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 157 | def onu_create(self, intf_id, onu_id, serial_number): |
| 158 | onu_device = self.adapter_agent.get_child_device( |
| 159 | self.device.id, |
| 160 | serial_number=serial_number) |
| 161 | |
| 162 | if onu_device: |
| 163 | self.log.debug("data_model onu update", intf_id=intf_id, |
| 164 | onu_id=onu_id, serial_number=serial_number) |
| 165 | onu_device.oper_status = OperStatus.DISCOVERED |
| 166 | onu_device.connect_status = ConnectStatus.REACHABLE |
| 167 | self.adapter_agent.update_device(onu_device) |
| 168 | return |
| 169 | |
| 170 | self.log.debug("data_model onu create", intf_id=intf_id, |
| 171 | onu_id=onu_id, serial_number=serial_number) |
| 172 | |
| 173 | # NOTE - channel_id of onu is set to intf_id |
| 174 | proxy_address = Device.ProxyAddress(device_id=self.device.id, |
| 175 | channel_id=intf_id, onu_id=onu_id, |
| 176 | onu_session_id=onu_id) |
| 177 | port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT) |
| 178 | vendor_id = serial_number[:4] |
| 179 | self.adapter_agent.add_onu_device( |
| 180 | parent_device_id=self.device.id, parent_port_no=port_no, |
| 181 | vendor_id=vendor_id, proxy_address=proxy_address, |
| 182 | root=False, serial_number=serial_number, |
| 183 | admin_state=AdminState.ENABLED, |
| 184 | connect_status=ConnectStatus.REACHABLE |
| 185 | ) |
| 186 | |
| 187 | def onu_delete(self, serial_number): |
| 188 | onu_device = self.adapter_agent.get_child_device( |
| 189 | self.device.id, |
| 190 | serial_number=serial_number) |
| 191 | try: |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 192 | self.adapter_agent.delete_child_device(self.device.id, |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 193 | onu_device.id, onu_device) |
| 194 | except Exception as e: |
| 195 | self.log.error('adapter_agent error', error=e) |
| 196 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 197 | ofp_port_name = self.__get_uni_ofp_port_name(onu_device) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 198 | if ofp_port_name is None: |
| 199 | self.log.exception("uni-ofp-port-not-found") |
| 200 | return |
| 201 | |
| 202 | try: |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 203 | self.__delete_logical_port(onu_device) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 204 | except Exception as e: |
| 205 | self.log.error('logical_port delete error', error=e) |
| 206 | try: |
| 207 | self.delete_port(onu_device.serial_number) |
| 208 | except Exception as e: |
| 209 | self.log.error('port delete error', error=e) |
| 210 | |
| 211 | def onu_id(self, serial_number): |
| 212 | onu_device = self.adapter_agent.get_child_device( |
| 213 | self.device.id, |
| 214 | serial_number=serial_number) |
| 215 | |
| 216 | if onu_device: |
| 217 | return onu_device.proxy_address.onu_id |
| 218 | else: |
| 219 | return 0 # Invalid onu id |
| 220 | |
| 221 | def onu_oper_down(self, intf_id, onu_id): |
| 222 | |
| 223 | onu_device = self.adapter_agent.get_child_device( |
| 224 | self.device.id, |
| 225 | parent_port_no=self.platform.intf_id_to_port_no(intf_id, |
| 226 | Port.PON_OLT), |
| 227 | onu_id=onu_id) |
| 228 | |
| 229 | if onu_device is None: |
| 230 | self.log.error('onu not found', intf_id=intf_id, onu_id=onu_id) |
| 231 | return |
| 232 | |
| 233 | onu_adapter_agent = \ |
| 234 | registry('adapter_loader').get_agent(onu_device.adapter) |
| 235 | if onu_adapter_agent is None: |
| 236 | self.log.error('onu_adapter_agent-could-not-be-retrieved', |
| 237 | onu_device=onu_device) |
| 238 | return |
| 239 | |
| 240 | if onu_device.connect_status != ConnectStatus.UNREACHABLE: |
| 241 | onu_device.connect_status = ConnectStatus.UNREACHABLE |
| 242 | self.adapter_agent.update_device(onu_device) |
| 243 | |
| 244 | # Move to discovered state |
| 245 | self.log.debug('onu-oper-state-is-down') |
| 246 | |
| 247 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 248 | onu_device.oper_status = OperStatus.DISCOVERED |
| 249 | self.adapter_agent.update_device(onu_device) |
| 250 | # Set port oper state to Discovered |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 251 | self.__onu_ports_down(onu_device) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 252 | |
| 253 | onu_adapter_agent.update_interface(onu_device, |
| 254 | {'oper_state': 'down'}) |
| 255 | |
| 256 | def onu_oper_up(self, intf_id, onu_id): |
| 257 | |
| 258 | class _OnuIndication: |
| 259 | def __init__(self, intf_id, onu_id): |
| 260 | self.intf_id = intf_id |
| 261 | self.onu_id = onu_id |
| 262 | |
| 263 | onu_device = self.adapter_agent.get_child_device( |
| 264 | self.device.id, |
| 265 | parent_port_no=self.platform.intf_id_to_port_no(intf_id, |
| 266 | Port.PON_OLT), |
| 267 | onu_id=onu_id) |
| 268 | |
| 269 | if onu_device is None: |
| 270 | self.log.error('onu not found', intf_id=intf_id, onu_id=onu_id) |
| 271 | return |
| 272 | |
| 273 | onu_adapter_agent = \ |
| 274 | registry('adapter_loader').get_agent(onu_device.adapter) |
| 275 | if onu_adapter_agent is None: |
| 276 | self.log.error('onu_adapter_agent-could-not-be-retrieved', |
| 277 | onu_device=onu_device) |
| 278 | return |
| 279 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 280 | onu_device.connect_status = ConnectStatus.REACHABLE |
| 281 | self.adapter_agent.update_device(onu_device) |
| 282 | |
| 283 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 284 | self.log.debug("ignore onu indication", |
| 285 | intf_id=intf_id, |
| 286 | onu_id=onu_id, |
| 287 | state=onu_device.oper_status, |
| 288 | msg_oper_state="up") |
| 289 | return |
| 290 | |
| 291 | onu_adapter_agent.create_interface(onu_device, |
| 292 | _OnuIndication(intf_id, onu_id)) |
| 293 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 294 | def onu_download_tech_profile(self, intf_id, onu_id, uni_id, tp_path): |
| 295 | onu_device = self.adapter_agent.get_child_device( |
| 296 | self.device.id, |
| 297 | parent_port_no=self.platform.intf_id_to_port_no(intf_id, |
| 298 | Port.PON_OLT), |
| 299 | onu_id=onu_id) |
| 300 | msg = {'proxy_address': onu_device.proxy_address, |
| 301 | 'uni_id': uni_id, 'event': 'download_tech_profile', |
| 302 | 'event_data': tp_path} |
| 303 | |
| 304 | # Send the event message to the ONU adapter |
| 305 | self.adapter_agent.publish_inter_adapter_message( |
| 306 | onu_device.id, msg) |
| 307 | |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 308 | def onu_omci_rx(self, intf_id, onu_id, pkt): |
| 309 | onu_device = self.adapter_agent.get_child_device( |
| 310 | self.device.id, |
| 311 | parent_port_no=self.platform.intf_id_to_port_no(intf_id, |
| 312 | Port.PON_OLT), |
| 313 | onu_id=onu_id) |
| 314 | self.adapter_agent.receive_proxied_message(onu_device.proxy_address, |
| 315 | pkt) |
| 316 | |
| 317 | def onu_send_packet_in(self, intf_type, intf_id, port_no, pkt): |
| 318 | if intf_type == "pon": |
| 319 | if not port_no: |
| 320 | raise ValueError("invalid port_no") |
| 321 | logical_port_num = port_no |
| 322 | elif intf_type == "nni": |
| 323 | logical_port_num = self.platform.intf_id_to_port_no( |
| 324 | intf_id, |
| 325 | Port.ETHERNET_NNI) |
| 326 | |
| 327 | ether_pkt = Ether(pkt) |
| 328 | |
| 329 | self.adapter_agent.send_packet_in( |
| 330 | logical_device_id=self.logical_device_id, |
| 331 | logical_port_no=logical_port_num, |
| 332 | packet=str(ether_pkt)) |
| 333 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 334 | # ####################################################################### |
| 335 | # Flow decomposer utility functions |
| 336 | # |
| 337 | # Flow related functions that are used by the OpenOLT flow decomposer. |
| 338 | # These are all prefixed with _ to denote that they will likely be removed |
| 339 | # once OpenOLT adapter transitions back to using core's flow decomposer. |
| 340 | # ####################################################################### |
| 341 | |
| 342 | def _flow_extract_info(self, flow, flow_direction): |
| 343 | uni_port_no = None |
| 344 | child_device_id = None |
| 345 | if flow_direction == "upstream": |
| 346 | for field in fd.get_ofb_fields(flow): |
| 347 | if field.type == fd.IN_PORT: |
| 348 | is_uni, child_device_id = self.__is_uni_port(field.port) |
| 349 | if is_uni: |
| 350 | uni_port_no = field.port |
| 351 | elif flow_direction == "downstream": |
| 352 | for field in fd.get_ofb_fields(flow): |
| 353 | if field.type == fd.METADATA: |
| 354 | uni_port = field.table_metadata & 0xFFFFFFFF |
| 355 | is_uni, child_device_id = self.__is_uni_port(uni_port) |
| 356 | if is_uni: |
| 357 | uni_port_no = field.port |
| 358 | |
| 359 | if uni_port_no is None: |
| 360 | for action in fd.get_actions(flow): |
| 361 | if action.type == fd.OUTPUT: |
| 362 | is_uni, child_device_id = \ |
| 363 | self.__is_uni_port(action.output.port) |
| 364 | if is_uni: |
| 365 | uni_port_no = action.output.port |
| 366 | |
| 367 | if child_device_id: |
| 368 | child_device = self.adapter_agent.get_device(child_device_id) |
| 369 | pon_intf = child_device.proxy_address.channel_id |
| 370 | onu_id = child_device.proxy_address.onu_id |
| 371 | uni_id = self.platform.uni_id_from_port_num(uni_port_no) \ |
| 372 | if uni_port_no is not None else None |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 373 | else: |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 374 | raise ValueError |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 375 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 376 | return pon_intf, onu_id, uni_id |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 377 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 378 | def _get_ofp_port_name(self, intf_id, onu_id, uni_id): |
| 379 | parent_port_no = self.platform.intf_id_to_port_no(intf_id, |
| 380 | Port.PON_OLT) |
| 381 | child_device = self.adapter_agent.get_child_device( |
| 382 | self.device.id, parent_port_no=parent_port_no, onu_id=onu_id) |
| 383 | if child_device is None: |
| 384 | self.log.error("could-not-find-child-device", |
| 385 | parent_port_no=intf_id, onu_id=onu_id) |
| 386 | return (None, None) |
| 387 | ports = self.adapter_agent.get_ports(child_device.id, |
| 388 | Port.ETHERNET_UNI) |
| 389 | logical_port = self.adapter_agent.get_logical_port( |
| 390 | self.logical_device_id, ports[uni_id].label) |
| 391 | ofp_port_name = (logical_port.ofp_port.name, |
| 392 | logical_port.ofp_port.port_no) |
| 393 | return ofp_port_name |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 394 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 395 | # ####################################################################### |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 396 | # Methods used by Alarm and Statistics Manager (TODO - re-visit) |
| 397 | # ####################################################################### |
| 398 | |
| 399 | def _adapter_name(self): |
| 400 | return self.adapter_agent.adapter_name |
| 401 | |
| 402 | def _device_id(self): |
| 403 | return self.device.id |
| 404 | |
| 405 | def _resolve_onu_id(self, onu_id, port_intf_id): |
| 406 | try: |
| 407 | onu_device = None |
| 408 | onu_device = self.adapter_agent.get_child_device( |
| 409 | self.device_id, |
| 410 | parent_port_no=self.platform.intf_id_to_port_no( |
| 411 | port_intf_id, Port.PON_OLT), |
| 412 | onu_id=onu_id) |
| 413 | except Exception as inner: |
| 414 | self.log.exception('resolve-onu-id', errmsg=inner.message) |
| 415 | |
| 416 | return onu_device |
| 417 | |
| 418 | def create_alarm(self, **kwargs): |
| 419 | self.adapter_agent.create_alarm(kwargs) |
| 420 | |
| 421 | def submit_alarm(self, alarm_event): |
| 422 | self.adapter_agent.submit_alarm(self.device.id, alarm_event) |
| 423 | |
| 424 | # ####################################################################### |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 425 | # Private functions |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 426 | # |
| 427 | # These functions are prefixed with __ to denote that they are private |
| 428 | # to openolt_data_model and should not be called directly from the adapter. |
| 429 | # ####################################################################### |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 430 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 431 | def __disable_logical_device(self): |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 432 | oper_state = OperStatus.UNKNOWN |
| 433 | connect_state = ConnectStatus.UNREACHABLE |
| 434 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 435 | onu_devices = self.adapter_agent.get_child_devices(self.device.id) |
| 436 | for onu_device in onu_devices: |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 437 | onu_adapter_agent = \ |
| 438 | registry('adapter_loader').get_agent(onu_device.adapter) |
| 439 | onu_adapter_agent.update_interface(onu_device, |
| 440 | {'oper_state': 'down'}) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 441 | self.onu_ports_down(onu_device) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 442 | |
| 443 | # Children devices |
| 444 | self.adapter_agent.update_child_devices_state( |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 445 | self.device.id, oper_status=oper_state, |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 446 | connect_status=connect_state) |
| 447 | # Device Ports |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 448 | device_ports = self.adapter_agent.get_ports(self.device.id, |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 449 | Port.ETHERNET_NNI) |
| 450 | logical_ports_ids = [port.label for port in device_ports] |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 451 | device_ports += self.adapter_agent.get_ports(self.device.id, |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 452 | Port.PON_OLT) |
| 453 | |
| 454 | for port in device_ports: |
| 455 | port.oper_status = oper_state |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 456 | self.adapter_agent.add_port(self.device.id, port) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 457 | |
| 458 | # Device logical port |
| 459 | for logical_port_id in logical_ports_ids: |
| 460 | logical_port = self.adapter_agent.get_logical_port( |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 461 | self.logical_device_id, logical_port_id) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 462 | logical_port.ofp_port.state = OFPPS_LINK_DOWN |
| 463 | self.adapter_agent.update_logical_port(self.logical_device_id, |
| 464 | logical_port) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 465 | self.device.oper_status = oper_state |
| 466 | self.device.connect_status = connect_state |
| 467 | self.adapter_agent.update_device(self.device) |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 468 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 469 | def __add_logical_port(self, port_no, intf_id, oper_state): |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 470 | self.log.info('adding-logical-port', port_no=port_no) |
| 471 | |
| 472 | label = OpenoltUtils.port_name(port_no, Port.ETHERNET_NNI) |
| 473 | |
| 474 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
| 475 | curr_speed = OFPPF_1GB_FD |
| 476 | max_speed = OFPPF_1GB_FD |
| 477 | |
| 478 | if oper_state == OperStatus.ACTIVE: |
| 479 | of_oper_state = OFPPS_LIVE |
| 480 | else: |
| 481 | of_oper_state = OFPPS_LINK_DOWN |
| 482 | |
| 483 | ofp = ofp_port( |
| 484 | port_no=port_no, |
| 485 | hw_addr=mac_str_to_tuple( |
| 486 | OpenoltUtils.make_mac_from_port_no(port_no)), |
| 487 | name=label, config=0, state=of_oper_state, curr=cap, |
| 488 | advertised=cap, peer=cap, curr_speed=curr_speed, |
| 489 | max_speed=max_speed) |
| 490 | |
| 491 | ofp_stats = ofp_port_stats(port_no=port_no) |
| 492 | |
| 493 | logical_port = LogicalPort( |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 494 | id=label, ofp_port=ofp, device_id=self.device.id, |
Shad Ansari | 134947d | 2019-02-14 23:45:03 -0800 | [diff] [blame] | 495 | device_port_no=port_no, root_port=True, |
| 496 | ofp_port_stats=ofp_stats) |
| 497 | |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 498 | self.adapter_agent.add_logical_port(self.logical_device_id, |
| 499 | logical_port) |
| 500 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 501 | def __delete_logical_port(self, child_device): |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 502 | logical_ports = self.proxy.get('/logical_devices/{}/ports'.format( |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 503 | self.logical_device_id)) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 504 | for logical_port in logical_ports: |
| 505 | if logical_port.device_id == child_device.id: |
| 506 | self.log.debug('delete-logical-port', |
| 507 | onu_device_id=child_device.id, |
| 508 | logical_port=logical_port) |
| 509 | self.flow_mgr.clear_flows_and_scheduler_for_logical_port( |
| 510 | child_device, logical_port) |
| 511 | self.adapter_agent.delete_logical_port( |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 512 | self.logical_device_id, logical_port) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 513 | return |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 514 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 515 | def __onu_ports_down(self, onu_device): |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 516 | onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id)) |
| 517 | for onu_port in onu_ports: |
| 518 | self.log.debug('onu-ports-down', onu_port=onu_port) |
| 519 | onu_port_id = onu_port.label |
| 520 | try: |
| 521 | onu_logical_port = self.adapter_agent.get_logical_port( |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 522 | logical_device_id=self.logical_device_id, |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 523 | port_id=onu_port_id) |
| 524 | onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN |
| 525 | self.adapter_agent.update_logical_port( |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 526 | logical_device_id=self.logical_device_id, |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 527 | port=onu_logical_port) |
| 528 | self.log.debug('cascading-oper-state-to-port-and-logical-port') |
| 529 | except KeyError as e: |
| 530 | self.log.error('matching-onu-port-label-invalid', |
| 531 | onu_id=onu_device.id, olt_id=self.device.id, |
| 532 | onu_ports=onu_ports, onu_port_id=onu_port_id, |
| 533 | error=e) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 534 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 535 | def __add_port(self, intf_id, port_type, oper_status): |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 536 | port_no = self.platform.intf_id_to_port_no(intf_id, port_type) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 537 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 538 | label = OpenoltUtils.port_name(port_no, port_type, intf_id) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 539 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 540 | self.log.debug('adding-port', port_no=port_no, label=label, |
| 541 | port_type=port_type) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 542 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 543 | port = Port(port_no=port_no, label=label, type=port_type, |
| 544 | admin_state=AdminState.ENABLED, oper_status=oper_status) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 545 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 546 | self.adapter_agent.add_port(self.device.id, port) |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 547 | |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 548 | return port_no, label |
Shad Ansari | d557797 | 2019-02-22 09:35:03 -0800 | [diff] [blame] | 549 | |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 550 | def __get_uni_ofp_port_name(self, child_device): |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 551 | logical_ports = self.proxy.get('/logical_devices/{}/ports'.format( |
Shad Ansari | 8d06864 | 2019-02-28 23:09:03 -0800 | [diff] [blame^] | 552 | self.logical_device_id)) |
Shad Ansari | ef2029b | 2019-02-25 09:45:54 -0800 | [diff] [blame] | 553 | for logical_port in logical_ports: |
| 554 | if logical_port.device_id == child_device.id: |
| 555 | return logical_port.ofp_port.name |
| 556 | return None |
Shad Ansari | f9b9b89 | 2019-02-27 17:10:27 -0800 | [diff] [blame] | 557 | |
| 558 | def __is_uni_port(self, port_no): |
| 559 | try: |
| 560 | port = self.adapter_agent.get_logical_port( |
| 561 | self.logical_device_id, 'uni-{}'.format(port_no)) |
| 562 | if port is not None: |
| 563 | return (not port.root_port), port.device_id |
| 564 | else: |
| 565 | return False, None |
| 566 | except Exception as e: |
| 567 | self.log.error("error-retrieving-port", e=e) |
| 568 | return False, None |