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