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