William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [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 | import threading |
| 17 | import binascii |
| 18 | import grpc |
| 19 | import socket |
| 20 | import re |
| 21 | import structlog |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 22 | import time |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 23 | from twisted.internet import reactor |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 24 | from twisted.internet.defer import inlineCallbacks, returnValue |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 25 | from scapy.layers.l2 import Ether, Dot1Q |
| 26 | from transitions import Machine |
| 27 | |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 28 | from pyvoltha.protos import openolt_pb2_grpc, openolt_pb2 |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 29 | |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 30 | from pyvoltha.adapters.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 31 | |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 32 | from pyvoltha.common.utils.nethelpers import mac_str_to_tuple |
| 33 | from pyvoltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \ |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 34 | OFPPS_LINK_DOWN, OFPPF_1GB_FD, \ |
| 35 | OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS, \ |
| 36 | ofp_switch_features, ofp_port, ofp_port_stats, ofp_desc |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 37 | from pyvoltha.common.utils.registry import registry |
| 38 | from pyvoltha.protos import third_party |
| 39 | from pyvoltha.protos.common_pb2 import AdminState, OperStatus, ConnectStatus |
| 40 | from pyvoltha.protos.common_pb2 import LogLevel |
| 41 | from pyvoltha.protos.device_pb2 import Port, Device |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 42 | |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 43 | from pyvoltha.protos.logical_device_pb2 import LogicalDevice, LogicalPort |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 44 | |
| 45 | class OpenoltDevice(object): |
| 46 | """ |
| 47 | OpenoltDevice state machine: |
| 48 | |
| 49 | null ----> init ------> connected -----> up -----> down |
| 50 | ^ ^ | ^ | | |
| 51 | | | | | | | |
| 52 | | +-------------+ +---------+ | |
| 53 | | | |
| 54 | +-----------------------------------------+ |
| 55 | """ |
| 56 | # pylint: disable=too-many-instance-attributes |
| 57 | # pylint: disable=R0904 |
| 58 | states = [ |
| 59 | 'state_null', |
| 60 | 'state_init', |
| 61 | 'state_connected', |
| 62 | 'state_up', |
| 63 | 'state_down'] |
| 64 | |
| 65 | transitions = [ |
| 66 | {'trigger': 'go_state_init', |
| 67 | 'source': ['state_null', 'state_connected', 'state_down'], |
| 68 | 'dest': 'state_init', |
| 69 | 'before': 'do_state_init', |
| 70 | 'after': 'post_init'}, |
| 71 | {'trigger': 'go_state_connected', |
| 72 | 'source': 'state_init', |
| 73 | 'dest': 'state_connected', |
| 74 | 'before': 'do_state_connected'}, |
| 75 | {'trigger': 'go_state_up', |
| 76 | 'source': ['state_connected', 'state_down'], |
| 77 | 'dest': 'state_up', |
| 78 | 'before': 'do_state_up'}, |
| 79 | {'trigger': 'go_state_down', |
| 80 | 'source': ['state_up'], |
| 81 | 'dest': 'state_down', |
| 82 | 'before': 'do_state_down', |
| 83 | 'after': 'post_down'}] |
| 84 | |
| 85 | def __init__(self, **kwargs): |
| 86 | super(OpenoltDevice, self).__init__() |
| 87 | |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 88 | self.adapter_proxy = kwargs['adapter_proxy'] |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 89 | self.adapter_agent = kwargs['adapter_agent'] |
| 90 | self.device_num = kwargs['device_num'] |
| 91 | device = kwargs['device'] |
| 92 | |
| 93 | self.platform_class = kwargs['support_classes']['platform'] |
| 94 | self.resource_mgr_class = kwargs['support_classes']['resource_mgr'] |
| 95 | self.flow_mgr_class = kwargs['support_classes']['flow_mgr'] |
| 96 | self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr'] |
| 97 | self.stats_mgr_class = kwargs['support_classes']['stats_mgr'] |
| 98 | self.bw_mgr_class = kwargs['support_classes']['bw_mgr'] |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 99 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 100 | is_reconciliation = kwargs.get('reconciliation', False) |
| 101 | self.device_id = device.id |
| 102 | self.host_and_port = device.host_and_port |
| 103 | self.extra_args = device.extra_args |
| 104 | self.log = structlog.get_logger(id=self.device_id, |
| 105 | ip=self.host_and_port) |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 106 | #self.proxy = registry('core').get_proxy('/') |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 107 | |
| 108 | self.log.info('openolt-device-init') |
| 109 | |
| 110 | # default device id and device serial number. If device_info provides better results, they will be updated |
| 111 | self.dpid = kwargs.get('dp_id') |
| 112 | self.serial_number = self.host_and_port # FIXME |
| 113 | |
| 114 | # Device already set in the event of reconciliation |
| 115 | if not is_reconciliation: |
| 116 | self.log.info('updating-device') |
| 117 | # It is a new device |
| 118 | # Update device |
| 119 | device.root = True |
| 120 | device.connect_status = ConnectStatus.UNREACHABLE |
| 121 | device.oper_status = OperStatus.ACTIVATING |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 122 | self.adapter_agent.device_update(device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 123 | |
| 124 | # If logical device does exist use it, else create one after connecting to device |
| 125 | if device.parent_id: |
| 126 | # logical device already exists |
| 127 | self.logical_device_id = device.parent_id |
| 128 | if is_reconciliation: |
| 129 | self.adapter_agent.reconcile_logical_device( |
| 130 | self.logical_device_id) |
| 131 | |
| 132 | # Initialize the OLT state machine |
| 133 | self.machine = Machine(model=self, states=OpenoltDevice.states, |
| 134 | transitions=OpenoltDevice.transitions, |
| 135 | send_event=True, initial='state_null') |
| 136 | self.go_state_init() |
| 137 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 138 | def create_logical_device(self, device_info): |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 139 | dpid = device_info.device_id |
| 140 | serial_number = device_info.device_serial_number |
| 141 | |
| 142 | if dpid is None: dpid = self.dpid |
| 143 | if serial_number is None: serial_number = self.serial_number |
| 144 | |
| 145 | if dpid == None or dpid == '': |
| 146 | uri = self.host_and_port.split(":")[0] |
| 147 | try: |
| 148 | socket.inet_pton(socket.AF_INET, uri) |
| 149 | dpid = '00:00:' + self.ip_hex(uri) |
| 150 | except socket.error: |
| 151 | # this is not an IP |
| 152 | dpid = self.stringToMacAddr(uri) |
| 153 | |
| 154 | if serial_number == None or serial_number == '': |
| 155 | serial_number = self.host_and_port |
| 156 | |
| 157 | self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number) |
| 158 | |
| 159 | mfr_desc = device_info.vendor |
| 160 | sw_desc = device_info.firmware_version |
| 161 | hw_desc = device_info.model |
| 162 | if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 163 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 164 | # Create logical OF device |
| 165 | ld = LogicalDevice( |
| 166 | root_device_id=self.device_id, |
| 167 | switch_features=ofp_switch_features( |
| 168 | n_buffers=256, # TODO fake for now |
| 169 | n_tables=2, # TODO ditto |
| 170 | capabilities=( # TODO and ditto |
| 171 | OFPC_FLOW_STATS |
| 172 | | OFPC_TABLE_STATS |
| 173 | | OFPC_PORT_STATS |
| 174 | | OFPC_GROUP_STATS |
| 175 | ) |
| 176 | ), |
| 177 | desc=ofp_desc( |
| 178 | serial_num=serial_number |
| 179 | ) |
| 180 | ) |
| 181 | ld_init = self.adapter_agent.create_logical_device(ld, |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 182 | dpid=dpid) |
| 183 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 184 | self.logical_device_id = ld_init.id |
| 185 | |
| 186 | device = self.adapter_agent.get_device(self.device_id) |
| 187 | device.serial_number = serial_number |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 188 | self.adapter_agent.update_device(device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 189 | |
| 190 | self.dpid = dpid |
| 191 | self.serial_number = serial_number |
| 192 | |
| 193 | self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id) |
| 194 | |
| 195 | def stringToMacAddr(self, uri): |
| 196 | regex = re.compile('[^a-zA-Z]') |
| 197 | uri = regex.sub('', uri) |
| 198 | |
| 199 | l = len(uri) |
| 200 | if l > 6: |
| 201 | uri = uri[0:6] |
| 202 | else: |
| 203 | uri = uri + uri[0:6 - l] |
| 204 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 205 | return ":".join([hex(ord(x))[-2:] for x in uri]) |
| 206 | |
| 207 | def do_state_init(self, event): |
| 208 | # Initialize gRPC |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 209 | self.log.debug("grpc-host-port", self.host_and_port) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 210 | self.channel = grpc.insecure_channel(self.host_and_port) |
| 211 | self.channel_ready_future = grpc.channel_ready_future(self.channel) |
| 212 | |
| 213 | self.log.info('openolt-device-created', device_id=self.device_id) |
| 214 | |
| 215 | def post_init(self, event): |
| 216 | self.log.debug('post_init') |
| 217 | |
| 218 | # We have reached init state, starting the indications thread |
| 219 | |
| 220 | # Catch RuntimeError exception |
| 221 | try: |
| 222 | # Start indications thread |
| 223 | self.indications_thread_handle = threading.Thread( |
| 224 | target=self.indications_thread) |
| 225 | # Old getter/setter API for daemon; use it directly as a |
| 226 | # property instead. The Jinkins error will happon on the reason of |
| 227 | # Exception in thread Thread-1 (most likely raised # during |
| 228 | # interpreter shutdown) |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 229 | self.log.debug('starting indications thread') |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 230 | self.indications_thread_handle.setDaemon(True) |
| 231 | self.indications_thread_handle.start() |
| 232 | except Exception as e: |
| 233 | self.log.exception('post_init failed', e=e) |
| 234 | |
| 235 | def do_state_connected(self, event): |
| 236 | self.log.debug("do_state_connected") |
| 237 | |
| 238 | device = self.adapter_agent.get_device(self.device_id) |
| 239 | |
| 240 | self.stub = openolt_pb2_grpc.OpenoltStub(self.channel) |
| 241 | |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 242 | delay = 1 |
| 243 | while True: |
| 244 | try: |
| 245 | device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty()) |
| 246 | break |
| 247 | except Exception as e: |
| 248 | reraise = True |
| 249 | if delay > 120: |
| 250 | self.log.error("gRPC failure too many times") |
| 251 | else: |
| 252 | self.log.warn("gRPC failure, retry in %ds: %s" |
| 253 | % (delay, repr(e))) |
| 254 | time.sleep(delay) |
| 255 | delay += delay |
| 256 | reraise = False |
| 257 | |
| 258 | if reraise: |
| 259 | raise |
| 260 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 261 | self.log.info('Device connected', device_info=device_info) |
| 262 | |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 263 | # self.create_logical_device(device_info) |
| 264 | self.logical_device_id = 0 |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 265 | device.serial_number = self.serial_number |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 266 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 267 | self.resource_mgr = self.resource_mgr_class(self.device_id, |
| 268 | self.host_and_port, |
| 269 | self.extra_args, |
| 270 | device_info) |
| 271 | self.platform = self.platform_class(self.log, self.resource_mgr) |
| 272 | self.flow_mgr = self.flow_mgr_class(self.adapter_agent, self.log, |
| 273 | self.stub, self.device_id, |
| 274 | self.logical_device_id, |
| 275 | self.platform, self.resource_mgr) |
| 276 | |
| 277 | self.alarm_mgr = self.alarm_mgr_class(self.log, self.adapter_agent, |
| 278 | self.device_id, |
| 279 | self.logical_device_id, |
| 280 | self.platform) |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 281 | self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform) |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 282 | self.bw_mgr = self.bw_mgr_class(self.log, self.adapter_agent) |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 283 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 284 | device.vendor = device_info.vendor |
| 285 | device.model = device_info.model |
| 286 | device.hardware_version = device_info.hardware_version |
| 287 | device.firmware_version = device_info.firmware_version |
| 288 | |
| 289 | # TODO: check for uptime and reboot if too long (VOL-1192) |
| 290 | |
| 291 | device.connect_status = ConnectStatus.REACHABLE |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 292 | device.mac_address = "AA:BB:CC:DD:EE:FF" |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 293 | self.adapter_agent.device_update(device) |
William Kurkian | 3a341a2 | 2019-02-13 18:23:44 -0500 | [diff] [blame] | 294 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 295 | def do_state_up(self, event): |
| 296 | self.log.debug("do_state_up") |
| 297 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 298 | self.adapter_agent.device_state_update(self.device_id, |
| 299 | connect_status=ConnectStatus.REACHABLE, |
| 300 | oper_status=OperStatus.ACTIVE) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 301 | |
| 302 | def do_state_down(self, event): |
| 303 | self.log.debug("do_state_down") |
| 304 | oper_state = OperStatus.UNKNOWN |
| 305 | connect_state = ConnectStatus.UNREACHABLE |
| 306 | |
| 307 | # Propagating to the children |
| 308 | |
| 309 | # Children ports |
| 310 | child_devices = self.adapter_agent.get_child_devices(self.device_id) |
| 311 | for onu_device in child_devices: |
| 312 | onu_adapter_agent = \ |
| 313 | registry('adapter_loader').get_agent(onu_device.adapter) |
| 314 | onu_adapter_agent.update_interface(onu_device, |
| 315 | {'oper_state': 'down'}) |
| 316 | self.onu_ports_down(onu_device, oper_state) |
| 317 | |
| 318 | # Children devices |
| 319 | self.adapter_agent.update_child_devices_state( |
| 320 | self.device_id, oper_status=oper_state, |
| 321 | connect_status=connect_state) |
| 322 | # Device Ports |
| 323 | device_ports = self.adapter_agent.get_ports(self.device_id, |
| 324 | Port.ETHERNET_NNI) |
| 325 | logical_ports_ids = [port.label for port in device_ports] |
| 326 | device_ports += self.adapter_agent.get_ports(self.device_id, |
| 327 | Port.PON_OLT) |
| 328 | |
| 329 | for port in device_ports: |
| 330 | port.oper_status = oper_state |
| 331 | self.adapter_agent.add_port(self.device_id, port) |
| 332 | |
| 333 | # Device logical port |
| 334 | for logical_port_id in logical_ports_ids: |
| 335 | logical_port = self.adapter_agent.get_logical_port( |
| 336 | self.logical_device_id, logical_port_id) |
| 337 | logical_port.ofp_port.state = OFPPS_LINK_DOWN |
| 338 | self.adapter_agent.update_logical_port(self.logical_device_id, |
| 339 | logical_port) |
| 340 | |
| 341 | # Device |
| 342 | device = self.adapter_agent.get_device(self.device_id) |
| 343 | device.oper_status = oper_state |
| 344 | device.connect_status = connect_state |
| 345 | |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 346 | reactor.callLater(2, self.adapter_agent.device_update, device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 347 | |
| 348 | # def post_up(self, event): |
| 349 | # self.log.debug('post-up') |
| 350 | # self.flow_mgr.reseed_flows() |
| 351 | |
| 352 | def post_down(self, event): |
| 353 | self.log.debug('post_down') |
| 354 | self.flow_mgr.reset_flows() |
| 355 | |
| 356 | def indications_thread(self): |
| 357 | self.log.debug('starting-indications-thread') |
| 358 | self.log.debug('connecting to olt', device_id=self.device_id) |
| 359 | self.channel_ready_future.result() # blocking call |
| 360 | self.log.info('connected to olt', device_id=self.device_id) |
| 361 | self.go_state_connected() |
| 362 | |
| 363 | self.indications = self.stub.EnableIndication(openolt_pb2.Empty()) |
| 364 | |
| 365 | while True: |
| 366 | try: |
| 367 | # get the next indication from olt |
| 368 | ind = next(self.indications) |
| 369 | except Exception as e: |
| 370 | self.log.warn('gRPC connection lost', error=e) |
| 371 | reactor.callFromThread(self.go_state_down) |
| 372 | reactor.callFromThread(self.go_state_init) |
| 373 | break |
| 374 | else: |
| 375 | self.log.debug("rx indication", indication=ind) |
| 376 | |
| 377 | # indication handlers run in the main event loop |
| 378 | if ind.HasField('olt_ind'): |
| 379 | reactor.callFromThread(self.olt_indication, ind.olt_ind) |
| 380 | elif ind.HasField('intf_ind'): |
| 381 | reactor.callFromThread(self.intf_indication, ind.intf_ind) |
| 382 | elif ind.HasField('intf_oper_ind'): |
| 383 | reactor.callFromThread(self.intf_oper_indication, |
| 384 | ind.intf_oper_ind) |
| 385 | elif ind.HasField('onu_disc_ind'): |
| 386 | reactor.callFromThread(self.onu_discovery_indication, |
| 387 | ind.onu_disc_ind) |
| 388 | elif ind.HasField('onu_ind'): |
| 389 | reactor.callFromThread(self.onu_indication, ind.onu_ind) |
| 390 | elif ind.HasField('omci_ind'): |
| 391 | reactor.callFromThread(self.omci_indication, ind.omci_ind) |
| 392 | elif ind.HasField('pkt_ind'): |
| 393 | reactor.callFromThread(self.packet_indication, ind.pkt_ind) |
| 394 | elif ind.HasField('port_stats'): |
| 395 | reactor.callFromThread( |
| 396 | self.stats_mgr.port_statistics_indication, |
| 397 | ind.port_stats) |
| 398 | elif ind.HasField('flow_stats'): |
| 399 | reactor.callFromThread( |
| 400 | self.stats_mgr.flow_statistics_indication, |
| 401 | ind.flow_stats) |
| 402 | elif ind.HasField('alarm_ind'): |
| 403 | reactor.callFromThread(self.alarm_mgr.process_alarms, |
| 404 | ind.alarm_ind) |
| 405 | else: |
| 406 | self.log.warn('unknown indication type') |
| 407 | |
| 408 | def olt_indication(self, olt_indication): |
| 409 | if olt_indication.oper_state == "up": |
| 410 | self.go_state_up() |
| 411 | elif olt_indication.oper_state == "down": |
| 412 | self.go_state_down() |
| 413 | |
| 414 | def intf_indication(self, intf_indication): |
| 415 | self.log.debug("intf indication", intf_id=intf_indication.intf_id, |
| 416 | oper_state=intf_indication.oper_state) |
| 417 | |
| 418 | if intf_indication.oper_state == "up": |
| 419 | oper_status = OperStatus.ACTIVE |
| 420 | else: |
| 421 | oper_status = OperStatus.DISCOVERED |
| 422 | |
| 423 | # add_port update the port if it exists |
| 424 | self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status) |
| 425 | |
| 426 | def intf_oper_indication(self, intf_oper_indication): |
| 427 | self.log.debug("Received interface oper state change indication", |
| 428 | intf_id=intf_oper_indication.intf_id, |
| 429 | type=intf_oper_indication.type, |
| 430 | oper_state=intf_oper_indication.oper_state) |
| 431 | |
| 432 | if intf_oper_indication.oper_state == "up": |
| 433 | oper_state = OperStatus.ACTIVE |
| 434 | else: |
| 435 | oper_state = OperStatus.DISCOVERED |
| 436 | |
| 437 | if intf_oper_indication.type == "nni": |
| 438 | |
| 439 | # add_(logical_)port update the port if it exists |
| 440 | port_no, label = self.add_port(intf_oper_indication.intf_id, |
| 441 | Port.ETHERNET_NNI, oper_state) |
| 442 | self.log.debug("int_oper_indication", port_no=port_no, label=label) |
| 443 | self.add_logical_port(port_no, intf_oper_indication.intf_id, |
| 444 | oper_state) |
| 445 | |
| 446 | elif intf_oper_indication.type == "pon": |
| 447 | # FIXME - handle PON oper state change |
| 448 | pass |
| 449 | |
| 450 | def onu_discovery_indication(self, onu_disc_indication): |
| 451 | intf_id = onu_disc_indication.intf_id |
| 452 | serial_number = onu_disc_indication.serial_number |
| 453 | |
| 454 | serial_number_str = self.stringify_serial_number(serial_number) |
| 455 | |
| 456 | self.log.debug("onu discovery indication", intf_id=intf_id, |
| 457 | serial_number=serial_number_str) |
| 458 | |
| 459 | # Post ONU Discover alarm 20180809_0805 |
| 460 | try: |
| 461 | OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id, |
| 462 | serial_number=serial_number_str).raise_alarm() |
| 463 | except Exception as disc_alarm_error: |
| 464 | self.log.exception("onu-discovery-alarm-error", |
| 465 | errmsg=disc_alarm_error.message) |
| 466 | # continue for now. |
| 467 | |
| 468 | onu_device = self.adapter_agent.get_child_device( |
| 469 | self.device_id, |
| 470 | serial_number=serial_number_str) |
| 471 | |
| 472 | if onu_device is None: |
| 473 | try: |
| 474 | onu_id = self.resource_mgr.get_onu_id(intf_id) |
| 475 | if onu_id is None: |
| 476 | raise Exception("onu-id-unavailable") |
| 477 | |
| 478 | self.add_onu_device( |
| 479 | intf_id, |
| 480 | self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT), |
| 481 | onu_id, serial_number) |
| 482 | self.activate_onu(intf_id, onu_id, serial_number, |
| 483 | serial_number_str) |
| 484 | except Exception as e: |
| 485 | self.log.exception('onu-activation-failed', e=e) |
| 486 | |
| 487 | else: |
| 488 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 489 | onu_device.connect_status = ConnectStatus.REACHABLE |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 490 | self.adapter_agent.device_update(onu_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 491 | |
| 492 | onu_id = onu_device.proxy_address.onu_id |
| 493 | if onu_device.oper_status == OperStatus.DISCOVERED \ |
| 494 | or onu_device.oper_status == OperStatus.ACTIVATING: |
| 495 | self.log.debug("ignore onu discovery indication, \ |
| 496 | the onu has been discovered and should be \ |
| 497 | activating shorlty", intf_id=intf_id, |
| 498 | onu_id=onu_id, state=onu_device.oper_status) |
| 499 | elif onu_device.oper_status == OperStatus.ACTIVE: |
| 500 | self.log.warn("onu discovery indication whereas onu is \ |
| 501 | supposed to be active", |
| 502 | intf_id=intf_id, onu_id=onu_id, |
| 503 | state=onu_device.oper_status) |
| 504 | elif onu_device.oper_status == OperStatus.UNKNOWN: |
| 505 | self.log.info("onu in unknown state, recovering from olt \ |
| 506 | reboot probably, activate onu", intf_id=intf_id, |
| 507 | onu_id=onu_id, serial_number=serial_number_str) |
| 508 | |
| 509 | onu_device.oper_status = OperStatus.DISCOVERED |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 510 | self.adapter_agent.device_update(onu_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 511 | try: |
| 512 | self.activate_onu(intf_id, onu_id, serial_number, |
| 513 | serial_number_str) |
| 514 | except Exception as e: |
| 515 | self.log.error('onu-activation-error', |
| 516 | serial_number=serial_number_str, error=e) |
| 517 | else: |
| 518 | self.log.warn('unexpected state', onu_id=onu_id, |
| 519 | onu_device_oper_state=onu_device.oper_status) |
| 520 | |
| 521 | def onu_indication(self, onu_indication): |
| 522 | self.log.debug("onu indication", intf_id=onu_indication.intf_id, |
| 523 | onu_id=onu_indication.onu_id, |
| 524 | serial_number=onu_indication.serial_number, |
| 525 | oper_state=onu_indication.oper_state, |
| 526 | admin_state=onu_indication.admin_state) |
| 527 | try: |
| 528 | serial_number_str = self.stringify_serial_number( |
| 529 | onu_indication.serial_number) |
| 530 | except Exception as e: |
| 531 | serial_number_str = None |
| 532 | |
| 533 | if serial_number_str is not None: |
| 534 | onu_device = self.adapter_agent.get_child_device( |
| 535 | self.device_id, |
| 536 | serial_number=serial_number_str) |
| 537 | else: |
| 538 | onu_device = self.adapter_agent.get_child_device( |
| 539 | self.device_id, |
| 540 | parent_port_no=self.platform.intf_id_to_port_no( |
| 541 | onu_indication.intf_id, Port.PON_OLT), |
| 542 | onu_id=onu_indication.onu_id) |
| 543 | |
| 544 | if onu_device is None: |
| 545 | self.log.error('onu not found', intf_id=onu_indication.intf_id, |
| 546 | onu_id=onu_indication.onu_id) |
| 547 | return |
| 548 | |
| 549 | if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \ |
| 550 | != onu_indication.intf_id: |
| 551 | self.log.warn('ONU-is-on-a-different-intf-id-now', |
| 552 | previous_intf_id=self.platform.intf_id_from_pon_port_no( |
| 553 | onu_device.parent_port_no), |
| 554 | current_intf_id=onu_indication.intf_id) |
| 555 | # FIXME - handle intf_id mismatch (ONU move?) |
| 556 | |
| 557 | if onu_device.proxy_address.onu_id != onu_indication.onu_id: |
| 558 | # FIXME - handle onu id mismatch |
| 559 | self.log.warn('ONU-id-mismatch, can happen if both voltha and ' |
| 560 | 'the olt rebooted', |
| 561 | expected_onu_id=onu_device.proxy_address.onu_id, |
| 562 | received_onu_id=onu_indication.onu_id) |
| 563 | |
| 564 | # Admin state |
| 565 | if onu_indication.admin_state == 'down': |
| 566 | if onu_indication.oper_state != 'down': |
| 567 | self.log.error('ONU-admin-state-down-and-oper-status-not-down', |
| 568 | oper_state=onu_indication.oper_state) |
| 569 | # Forcing the oper state change code to execute |
| 570 | onu_indication.oper_state = 'down' |
| 571 | |
| 572 | # Port and logical port update is taken care of by oper state block |
| 573 | |
| 574 | elif onu_indication.admin_state == 'up': |
| 575 | pass |
| 576 | |
| 577 | else: |
| 578 | self.log.warn('Invalid-or-not-implemented-admin-state', |
| 579 | received_admin_state=onu_indication.admin_state) |
| 580 | |
| 581 | self.log.debug('admin-state-dealt-with') |
| 582 | |
| 583 | onu_adapter_agent = \ |
| 584 | registry('adapter_loader').get_agent(onu_device.adapter) |
| 585 | if onu_adapter_agent is None: |
| 586 | self.log.error('onu_adapter_agent-could-not-be-retrieved', |
| 587 | onu_device=onu_device) |
| 588 | return |
| 589 | |
| 590 | # Operating state |
| 591 | if onu_indication.oper_state == 'down': |
| 592 | |
| 593 | if onu_device.connect_status != ConnectStatus.UNREACHABLE: |
| 594 | onu_device.connect_status = ConnectStatus.UNREACHABLE |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 595 | self.adapter_agent.device_update(onu_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 596 | |
| 597 | # Move to discovered state |
| 598 | self.log.debug('onu-oper-state-is-down') |
| 599 | |
| 600 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 601 | onu_device.oper_status = OperStatus.DISCOVERED |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 602 | self.adapter_agent.device_update(onu_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 603 | # Set port oper state to Discovered |
| 604 | self.onu_ports_down(onu_device, OperStatus.DISCOVERED) |
| 605 | |
| 606 | onu_adapter_agent.update_interface(onu_device, |
| 607 | {'oper_state': 'down'}) |
| 608 | |
| 609 | elif onu_indication.oper_state == 'up': |
| 610 | |
| 611 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 612 | onu_device.connect_status = ConnectStatus.REACHABLE |
William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame] | 613 | self.adapter_agent.device_update(onu_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 614 | |
| 615 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 616 | self.log.debug("ignore onu indication", |
| 617 | intf_id=onu_indication.intf_id, |
| 618 | onu_id=onu_indication.onu_id, |
| 619 | state=onu_device.oper_status, |
| 620 | msg_oper_state=onu_indication.oper_state) |
| 621 | return |
| 622 | |
| 623 | # Device was in Discovered state, setting it to active |
| 624 | |
| 625 | # Prepare onu configuration |
| 626 | |
| 627 | onu_adapter_agent.create_interface(onu_device, onu_indication) |
| 628 | |
| 629 | else: |
| 630 | self.log.warn('Not-implemented-or-invalid-value-of-oper-state', |
| 631 | oper_state=onu_indication.oper_state) |
| 632 | |
| 633 | def onu_ports_down(self, onu_device, oper_state): |
| 634 | # Set port oper state to Discovered |
| 635 | # add port will update port if it exists |
| 636 | # self.adapter_agent.add_port( |
| 637 | # self.device_id, |
| 638 | # Port( |
| 639 | # port_no=uni_no, |
| 640 | # label=uni_name, |
| 641 | # type=Port.ETHERNET_UNI, |
| 642 | # admin_state=onu_device.admin_state, |
| 643 | # oper_status=oper_state)) |
| 644 | # TODO this should be downning ports in onu adatper |
| 645 | |
| 646 | # Disable logical port |
| 647 | onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id)) |
| 648 | for onu_port in onu_ports: |
| 649 | self.log.debug('onu-ports-down', onu_port=onu_port) |
| 650 | onu_port_id = onu_port.label |
| 651 | try: |
| 652 | onu_logical_port = self.adapter_agent.get_logical_port( |
| 653 | logical_device_id=self.logical_device_id, port_id=onu_port_id) |
| 654 | onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN |
| 655 | self.adapter_agent.update_logical_port( |
| 656 | logical_device_id=self.logical_device_id, |
| 657 | port=onu_logical_port) |
| 658 | self.log.debug('cascading-oper-state-to-port-and-logical-port') |
| 659 | except KeyError as e: |
| 660 | self.log.error('matching-onu-port-label-invalid', |
| 661 | onu_id=onu_device.id, olt_id=self.device_id, |
| 662 | onu_ports=onu_ports, onu_port_id=onu_port_id, |
| 663 | error=e) |
| 664 | |
| 665 | def omci_indication(self, omci_indication): |
| 666 | |
| 667 | self.log.debug("omci indication", intf_id=omci_indication.intf_id, |
| 668 | onu_id=omci_indication.onu_id) |
| 669 | |
| 670 | onu_device = self.adapter_agent.get_child_device( |
| 671 | self.device_id, onu_id=omci_indication.onu_id, |
| 672 | parent_port_no=self.platform.intf_id_to_port_no( |
| 673 | omci_indication.intf_id, Port.PON_OLT), ) |
| 674 | |
| 675 | self.adapter_agent.receive_proxied_message(onu_device.proxy_address, |
| 676 | omci_indication.pkt) |
| 677 | |
| 678 | def packet_indication(self, pkt_indication): |
| 679 | |
| 680 | self.log.debug("packet indication", |
| 681 | intf_type=pkt_indication.intf_type, |
| 682 | intf_id=pkt_indication.intf_id, |
| 683 | port_no=pkt_indication.port_no, |
| 684 | cookie=pkt_indication.cookie, |
| 685 | gemport_id=pkt_indication.gemport_id, |
| 686 | flow_id=pkt_indication.flow_id) |
| 687 | |
| 688 | if pkt_indication.intf_type == "pon": |
| 689 | if pkt_indication.port_no: |
| 690 | logical_port_num = pkt_indication.port_no |
| 691 | else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change |
| 692 | try: |
| 693 | onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id, |
| 694 | pkt_indication.gemport_id) |
| 695 | onu_id = int(onu_id_uni_id[0]) |
| 696 | uni_id = int(onu_id_uni_id[1]) |
| 697 | self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id) |
| 698 | if onu_id is None: |
| 699 | raise Exception("onu-id-none") |
| 700 | if uni_id is None: |
| 701 | raise Exception("uni-id-none") |
| 702 | logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id) |
| 703 | except Exception as e: |
| 704 | self.log.error("no-onu-reference-for-gem", |
| 705 | gemport_id=pkt_indication.gemport_id, e=e) |
| 706 | return |
| 707 | |
| 708 | |
| 709 | elif pkt_indication.intf_type == "nni": |
| 710 | logical_port_num = self.platform.intf_id_to_port_no( |
| 711 | pkt_indication.intf_id, |
| 712 | Port.ETHERNET_NNI) |
| 713 | |
| 714 | pkt = Ether(pkt_indication.pkt) |
| 715 | |
| 716 | self.log.debug("packet indication", |
| 717 | logical_device_id=self.logical_device_id, |
| 718 | logical_port_no=logical_port_num) |
| 719 | |
| 720 | self.adapter_agent.send_packet_in( |
| 721 | logical_device_id=self.logical_device_id, |
| 722 | logical_port_no=logical_port_num, |
| 723 | packet=str(pkt)) |
| 724 | |
| 725 | def packet_out(self, egress_port, msg): |
| 726 | pkt = Ether(msg) |
| 727 | self.log.debug('packet out', egress_port=egress_port, |
| 728 | device_id=self.device_id, |
| 729 | logical_device_id=self.logical_device_id, |
| 730 | packet=str(pkt).encode("HEX")) |
| 731 | |
| 732 | # Find port type |
| 733 | egress_port_type = self.platform.intf_id_to_port_type_name(egress_port) |
| 734 | if egress_port_type == Port.ETHERNET_UNI: |
| 735 | |
| 736 | if pkt.haslayer(Dot1Q): |
| 737 | outer_shim = pkt.getlayer(Dot1Q) |
| 738 | if isinstance(outer_shim.payload, Dot1Q): |
| 739 | # If double tag, remove the outer tag |
| 740 | payload = ( |
| 741 | Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) / |
| 742 | outer_shim.payload |
| 743 | ) |
| 744 | else: |
| 745 | payload = pkt |
| 746 | else: |
| 747 | payload = pkt |
| 748 | |
| 749 | send_pkt = binascii.unhexlify(str(payload).encode("HEX")) |
| 750 | |
| 751 | self.log.debug( |
| 752 | 'sending-packet-to-ONU', egress_port=egress_port, |
| 753 | intf_id=self.platform.intf_id_from_uni_port_num(egress_port), |
| 754 | onu_id=self.platform.onu_id_from_port_num(egress_port), |
| 755 | uni_id=self.platform.uni_id_from_port_num(egress_port), |
| 756 | port_no=egress_port, |
| 757 | packet=str(payload).encode("HEX")) |
| 758 | |
| 759 | onu_pkt = openolt_pb2.OnuPacket( |
| 760 | intf_id=self.platform.intf_id_from_uni_port_num(egress_port), |
| 761 | onu_id=self.platform.onu_id_from_port_num(egress_port), |
| 762 | port_no=egress_port, |
| 763 | pkt=send_pkt) |
| 764 | |
| 765 | self.stub.OnuPacketOut(onu_pkt) |
| 766 | |
| 767 | elif egress_port_type == Port.ETHERNET_NNI: |
| 768 | self.log.debug('sending-packet-to-uplink', egress_port=egress_port, |
| 769 | packet=str(pkt).encode("HEX")) |
| 770 | |
| 771 | send_pkt = binascii.unhexlify(str(pkt).encode("HEX")) |
| 772 | |
| 773 | uplink_pkt = openolt_pb2.UplinkPacket( |
| 774 | intf_id=self.platform.intf_id_from_nni_port_num(egress_port), |
| 775 | pkt=send_pkt) |
| 776 | |
| 777 | self.stub.UplinkPacketOut(uplink_pkt) |
| 778 | |
| 779 | else: |
| 780 | self.log.warn('Packet-out-to-this-interface-type-not-implemented', |
| 781 | egress_port=egress_port, |
| 782 | port_type=egress_port_type) |
| 783 | |
| 784 | def send_proxied_message(self, proxy_address, msg): |
| 785 | onu_device = self.adapter_agent.get_child_device( |
| 786 | self.device_id, onu_id=proxy_address.onu_id, |
| 787 | parent_port_no=self.platform.intf_id_to_port_no( |
| 788 | proxy_address.channel_id, Port.PON_OLT) |
| 789 | ) |
| 790 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 791 | self.log.debug('ONU is not reachable, cannot send OMCI', |
| 792 | serial_number=onu_device.serial_number, |
| 793 | intf_id=onu_device.proxy_address.channel_id, |
| 794 | onu_id=onu_device.proxy_address.onu_id) |
| 795 | return |
| 796 | omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id, |
| 797 | onu_id=proxy_address.onu_id, pkt=str(msg)) |
| 798 | self.stub.OmciMsgOut(omci) |
| 799 | |
| 800 | def add_onu_device(self, intf_id, port_no, onu_id, serial_number): |
| 801 | self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id, |
| 802 | serial_number=serial_number) |
| 803 | |
| 804 | # NOTE - channel_id of onu is set to intf_id |
| 805 | proxy_address = Device.ProxyAddress(device_id=self.device_id, |
| 806 | channel_id=intf_id, onu_id=onu_id, |
| 807 | onu_session_id=onu_id) |
| 808 | |
| 809 | self.log.debug("Adding ONU", proxy_address=proxy_address) |
| 810 | |
| 811 | serial_number_str = self.stringify_serial_number(serial_number) |
| 812 | |
| 813 | self.adapter_agent.add_onu_device( |
| 814 | parent_device_id=self.device_id, parent_port_no=port_no, |
| 815 | vendor_id=serial_number.vendor_id, proxy_address=proxy_address, |
| 816 | root=True, serial_number=serial_number_str, |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 817 | admin_state=AdminState.ENABLED |
| 818 | # , **{'vlan':4091} # magic still maps to brcm_openomci_onu.pon_port.BRDCM_DEFAULT_VLAN |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 819 | ) |
| 820 | |
| 821 | def port_name(self, port_no, port_type, intf_id=None, serial_number=None): |
| 822 | if port_type is Port.ETHERNET_NNI: |
| 823 | return "nni-" + str(port_no) |
| 824 | elif port_type is Port.PON_OLT: |
| 825 | return "pon" + str(intf_id) |
| 826 | elif port_type is Port.ETHERNET_UNI: |
| 827 | assert False, 'local UNI management not supported' |
| 828 | |
| 829 | def add_logical_port(self, port_no, intf_id, oper_state): |
| 830 | self.log.info('adding-logical-port', port_no=port_no) |
| 831 | |
| 832 | label = self.port_name(port_no, Port.ETHERNET_NNI) |
| 833 | |
| 834 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
| 835 | curr_speed = OFPPF_1GB_FD |
| 836 | max_speed = OFPPF_1GB_FD |
| 837 | |
| 838 | if oper_state == OperStatus.ACTIVE: |
| 839 | of_oper_state = OFPPS_LIVE |
| 840 | else: |
| 841 | of_oper_state = OFPPS_LINK_DOWN |
| 842 | |
| 843 | ofp = ofp_port( |
| 844 | port_no=port_no, |
| 845 | hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)), |
| 846 | name=label, config=0, state=of_oper_state, curr=cap, |
| 847 | advertised=cap, peer=cap, curr_speed=curr_speed, |
| 848 | max_speed=max_speed) |
| 849 | |
| 850 | ofp_stats = ofp_port_stats(port_no=port_no) |
| 851 | |
| 852 | logical_port = LogicalPort( |
| 853 | id=label, ofp_port=ofp, device_id=self.device_id, |
| 854 | device_port_no=port_no, root_port=True, |
| 855 | ofp_port_stats=ofp_stats) |
| 856 | |
| 857 | self.adapter_agent.add_logical_port(self.logical_device_id, |
| 858 | logical_port) |
| 859 | |
| 860 | def _get_mac_form_port_no(self, port_no): |
| 861 | mac = '' |
| 862 | for i in range(4): |
| 863 | mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac |
| 864 | return '00:00' + mac |
| 865 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 866 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 867 | def add_port(self, intf_id, port_type, oper_status): |
| 868 | port_no = self.platform.intf_id_to_port_no(intf_id, port_type) |
| 869 | |
| 870 | label = self.port_name(port_no, port_type, intf_id) |
| 871 | |
| 872 | self.log.debug('adding-port', port_no=port_no, label=label, |
| 873 | port_type=port_type) |
| 874 | |
| 875 | port = Port(port_no=port_no, label=label, type=port_type, |
| 876 | admin_state=AdminState.ENABLED, oper_status=oper_status) |
| 877 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 878 | yield self.adapter_agent.port_created(self.device_id, port) |
| 879 | #self.adapter_agent.add_port(self.device_id, port) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 880 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame^] | 881 | returnValue(port_no, label) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 882 | |
| 883 | def delete_logical_port(self, child_device): |
| 884 | logical_ports = self.proxy.get('/logical_devices/{}/ports'.format( |
| 885 | self.logical_device_id)) |
| 886 | for logical_port in logical_ports: |
| 887 | if logical_port.device_id == child_device.id: |
| 888 | self.log.debug('delete-logical-port', |
| 889 | onu_device_id=child_device.id, |
| 890 | logical_port=logical_port) |
| 891 | self.flow_mgr.clear_flows_and_scheduler_for_logical_port( |
| 892 | child_device, logical_port) |
| 893 | self.adapter_agent.delete_logical_port( |
| 894 | self.logical_device_id, logical_port) |
| 895 | return |
| 896 | |
| 897 | def delete_port(self, child_serial_number): |
| 898 | ports = self.proxy.get('/devices/{}/ports'.format( |
| 899 | self.device_id)) |
| 900 | for port in ports: |
| 901 | if port.label == child_serial_number: |
| 902 | self.log.debug('delete-port', |
| 903 | onu_serial_number=child_serial_number, |
| 904 | port=port) |
| 905 | self.adapter_agent.delete_port(self.device_id, port) |
| 906 | return |
| 907 | |
| 908 | def update_flow_table(self, flows): |
| 909 | self.log.debug('No updates here now, all is done in logical flows ' |
| 910 | 'update') |
| 911 | |
| 912 | def update_logical_flows(self, flows_to_add, flows_to_remove, |
| 913 | device_rules_map): |
| 914 | if not self.is_state_up(): |
| 915 | self.log.info('The OLT is not up, we cannot update flows', |
| 916 | flows_to_add=[f.id for f in flows_to_add], |
| 917 | flows_to_remove=[f.id for f in flows_to_remove]) |
| 918 | return |
| 919 | |
| 920 | try: |
| 921 | self.flow_mgr.update_children_flows(device_rules_map) |
| 922 | except Exception as e: |
| 923 | self.log.error('Error updating children flows', error=e) |
| 924 | |
| 925 | self.log.debug('logical flows update', flows_to_add=flows_to_add, |
| 926 | flows_to_remove=flows_to_remove) |
| 927 | |
| 928 | for flow in flows_to_add: |
| 929 | |
| 930 | try: |
| 931 | self.flow_mgr.add_flow(flow) |
| 932 | except Exception as e: |
| 933 | self.log.error('failed to add flow', flow=flow, e=e) |
| 934 | |
| 935 | for flow in flows_to_remove: |
| 936 | |
| 937 | try: |
| 938 | self.flow_mgr.remove_flow(flow) |
| 939 | except Exception as e: |
| 940 | self.log.error('failed to remove flow', flow=flow, e=e) |
| 941 | |
| 942 | self.flow_mgr.repush_all_different_flows() |
| 943 | |
| 944 | # There has to be a better way to do this |
| 945 | def ip_hex(self, ip): |
| 946 | octets = ip.split(".") |
| 947 | hex_ip = [] |
| 948 | for octet in octets: |
| 949 | octet_hex = hex(int(octet)) |
| 950 | octet_hex = octet_hex.split('0x')[1] |
| 951 | octet_hex = octet_hex.rjust(2, '0') |
| 952 | hex_ip.append(octet_hex) |
| 953 | return ":".join(hex_ip) |
| 954 | |
| 955 | def stringify_vendor_specific(self, vendor_specific): |
| 956 | return ''.join(str(i) for i in [ |
| 957 | hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:], |
| 958 | hex(ord(vendor_specific[0]) & 0x0f)[2:], |
| 959 | hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:], |
| 960 | hex(ord(vendor_specific[1]) & 0x0f)[2:], |
| 961 | hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:], |
| 962 | hex(ord(vendor_specific[2]) & 0x0f)[2:], |
| 963 | hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:], |
| 964 | hex(ord(vendor_specific[3]) & 0x0f)[2:]]) |
| 965 | |
| 966 | def stringify_serial_number(self, serial_number): |
| 967 | return ''.join([serial_number.vendor_id, |
| 968 | self.stringify_vendor_specific( |
| 969 | serial_number.vendor_specific)]) |
| 970 | |
| 971 | def destringify_serial_number(self, serial_number_str): |
| 972 | serial_number = openolt_pb2.SerialNumber( |
| 973 | vendor_id=serial_number_str[:4].encode('utf-8'), |
| 974 | vendor_specific=binascii.unhexlify(serial_number_str[4:])) |
| 975 | return serial_number |
| 976 | |
| 977 | def disable(self): |
| 978 | self.log.debug('sending-deactivate-olt-message', |
| 979 | device_id=self.device_id) |
| 980 | |
| 981 | try: |
| 982 | # Send grpc call |
| 983 | self.stub.DisableOlt(openolt_pb2.Empty()) |
| 984 | # The resulting indication will bring the OLT down |
| 985 | # self.go_state_down() |
| 986 | self.log.info('openolt device disabled') |
| 987 | except Exception as e: |
| 988 | self.log.error('Failure to disable openolt device', error=e) |
| 989 | |
| 990 | def delete(self): |
| 991 | self.log.info('deleting-olt', device_id=self.device_id, |
| 992 | logical_device_id=self.logical_device_id) |
| 993 | |
| 994 | # Clears up the data from the resource manager KV store |
| 995 | # for the device |
| 996 | del self.resource_mgr |
| 997 | |
| 998 | try: |
| 999 | # Rebooting to reset the state |
| 1000 | self.reboot() |
| 1001 | # Removing logical device |
| 1002 | ld = self.adapter_agent.get_logical_device(self.logical_device_id) |
| 1003 | self.adapter_agent.delete_logical_device(ld) |
| 1004 | except Exception as e: |
| 1005 | self.log.error('Failure to delete openolt device', error=e) |
| 1006 | raise e |
| 1007 | else: |
| 1008 | self.log.info('successfully-deleted-olt', device_id=self.device_id) |
| 1009 | |
| 1010 | def reenable(self): |
| 1011 | self.log.debug('reenabling-olt', device_id=self.device_id) |
| 1012 | |
| 1013 | try: |
| 1014 | self.stub.ReenableOlt(openolt_pb2.Empty()) |
| 1015 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1016 | except Exception as e: |
| 1017 | self.log.error('Failure to reenable openolt device', error=e) |
| 1018 | else: |
| 1019 | self.log.info('openolt device reenabled') |
| 1020 | |
| 1021 | def activate_onu(self, intf_id, onu_id, serial_number, |
| 1022 | serial_number_str): |
| 1023 | pir = self.bw_mgr.pir(serial_number_str) |
| 1024 | self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id, |
| 1025 | serial_number_str=serial_number_str, |
| 1026 | serial_number=serial_number, pir=pir) |
| 1027 | onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id, |
| 1028 | serial_number=serial_number, pir=pir) |
| 1029 | self.stub.ActivateOnu(onu) |
| 1030 | self.log.info('onu-activated', serial_number=serial_number_str) |
| 1031 | |
| 1032 | def delete_child_device(self, child_device): |
| 1033 | self.log.debug('sending-deactivate-onu', |
| 1034 | olt_device_id=self.device_id, |
| 1035 | onu_device=child_device, |
| 1036 | onu_serial_number=child_device.serial_number) |
| 1037 | try: |
| 1038 | self.adapter_agent.delete_child_device(self.device_id, |
| 1039 | child_device.id, |
| 1040 | child_device) |
| 1041 | except Exception as e: |
| 1042 | self.log.error('adapter_agent error', error=e) |
| 1043 | try: |
| 1044 | self.delete_logical_port(child_device) |
| 1045 | except Exception as e: |
| 1046 | self.log.error('logical_port delete error', error=e) |
| 1047 | try: |
| 1048 | self.delete_port(child_device.serial_number) |
| 1049 | except Exception as e: |
| 1050 | self.log.error('port delete error', error=e) |
| 1051 | serial_number = self.destringify_serial_number( |
| 1052 | child_device.serial_number) |
| 1053 | # TODO FIXME - For each uni. |
| 1054 | # TODO FIXME - Flows are not deleted |
| 1055 | uni_id = 0 # FIXME |
| 1056 | self.flow_mgr.delete_tech_profile_instance( |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 1057 | child_device.proxy_address.channel_id, |
| 1058 | child_device.proxy_address.onu_id, |
| 1059 | uni_id |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1060 | ) |
| 1061 | pon_intf_id_onu_id = (child_device.proxy_address.channel_id, |
| 1062 | child_device.proxy_address.onu_id, |
| 1063 | uni_id) |
| 1064 | # Free any PON resources that were reserved for the ONU |
| 1065 | self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id) |
| 1066 | |
| 1067 | onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id, |
| 1068 | onu_id=child_device.proxy_address.onu_id, |
| 1069 | serial_number=serial_number) |
| 1070 | self.stub.DeleteOnu(onu) |
| 1071 | |
| 1072 | def reboot(self): |
| 1073 | self.log.debug('rebooting openolt device', device_id=self.device_id) |
| 1074 | try: |
| 1075 | self.stub.Reboot(openolt_pb2.Empty()) |
| 1076 | except Exception as e: |
| 1077 | self.log.error('something went wrong with the reboot', error=e) |
| 1078 | else: |
| 1079 | self.log.info('device rebooted') |
| 1080 | |
| 1081 | def trigger_statistics_collection(self): |
| 1082 | try: |
| 1083 | self.stub.CollectStatistics(openolt_pb2.Empty()) |
| 1084 | except Exception as e: |
| 1085 | self.log.error('Error while triggering statistics collection', |
| 1086 | error=e) |
| 1087 | else: |
| 1088 | self.log.info('statistics requested') |
| 1089 | |
| 1090 | def simulate_alarm(self, alarm): |
| 1091 | self.alarm_mgr.simulate_alarm(alarm) |