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