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): |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 478 | self.log.debug("onu indication with retry", |
| 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 | |
| 490 | if serial_number_str is not None: |
Matt Jeanneret | bad3d98 | 2019-03-11 16:06:10 -0400 | [diff] [blame] | 491 | onu_device = yield self.core_proxy.get_child_device( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 492 | self.device_id, serial_number=serial_number_str) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 493 | else: |
Matt Jeanneret | bad3d98 | 2019-03-11 16:06:10 -0400 | [diff] [blame] | 494 | onu_device = yield self.core_proxy.get_child_device( |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 495 | self.device_id, |
| 496 | parent_port_no=self.platform.intf_id_to_port_no( |
| 497 | onu_indication.intf_id, Port.PON_OLT), |
| 498 | onu_id=onu_indication.onu_id) |
| 499 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 500 | onu_key = self.form_onu_key(onu_indication.intf_id, |
| 501 | onu_indication.onu_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 502 | if onu_device is None: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 503 | if serial_number_str in self.seen_discovery_indications: |
| 504 | if onu_key not in self.onu_cache: |
| 505 | # The ONU is probably getting added. Lets retry again |
| 506 | if self.indication_retries < 10: |
| 507 | self.indication_retries += 1 |
| 508 | self.log.error( |
| 509 | 'onu in discovery indications but not found in core. Retrying again', |
| 510 | intf_id=onu_indication.intf_id, |
| 511 | onu_id=onu_indication.onu_id) |
| 512 | reactor.callLater(3, self.onu_indication, |
| 513 | onu_indication) |
| 514 | else: |
| 515 | self.log.error('ONU not found', |
| 516 | intf_id=onu_indication.intf_id, |
| 517 | onu_id=onu_indication.onu_id) |
| 518 | else: |
| 519 | # The ONU device is in the ONU cache |
| 520 | self.log.debug('ONU in cache') |
| 521 | self.indication_retries = 0 |
| 522 | onu_device = self.onu_cache[onu_key] |
| 523 | self.log.debug('ONU device found in the cache', onu_device=onu_device) |
| 524 | else: |
| 525 | self.log.error('onu not found', |
| 526 | intf_id=onu_indication.intf_id, |
| 527 | onu_id=onu_indication.onu_id) |
| 528 | if onu_device is None: |
| 529 | return |
| 530 | |
| 531 | self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type, |
| 532 | serial_number_str, |
| 533 | onu_indication.onu_id, |
| 534 | onu_indication.intf_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 535 | if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \ |
| 536 | != onu_indication.intf_id: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 537 | self.log.warn( |
| 538 | 'ONU-is-on-a-different-intf-id-now', |
| 539 | previous_intf_id=self.platform.intf_id_from_pon_port_no( |
| 540 | onu_device.parent_port_no), |
| 541 | current_intf_id=onu_indication.intf_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 542 | # FIXME - handle intf_id mismatch (ONU move?) |
| 543 | |
| 544 | if onu_device.proxy_address.onu_id != onu_indication.onu_id: |
| 545 | # FIXME - handle onu id mismatch |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 546 | self.log.warn( |
| 547 | 'ONU-id-mismatch, can happen if both voltha and ' |
| 548 | 'the olt rebooted', |
| 549 | expected_onu_id=onu_device.proxy_address.onu_id, |
| 550 | received_onu_id=onu_indication.onu_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 551 | |
| 552 | # Admin state |
| 553 | if onu_indication.admin_state == 'down': |
| 554 | if onu_indication.oper_state != 'down': |
| 555 | self.log.error('ONU-admin-state-down-and-oper-status-not-down', |
| 556 | oper_state=onu_indication.oper_state) |
| 557 | # Forcing the oper state change code to execute |
| 558 | onu_indication.oper_state = 'down' |
| 559 | |
| 560 | # Port and logical port update is taken care of by oper state block |
| 561 | |
| 562 | elif onu_indication.admin_state == 'up': |
| 563 | pass |
| 564 | |
| 565 | else: |
| 566 | self.log.warn('Invalid-or-not-implemented-admin-state', |
| 567 | received_admin_state=onu_indication.admin_state) |
| 568 | |
| 569 | self.log.debug('admin-state-dealt-with') |
| 570 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 571 | # Operating state |
| 572 | if onu_indication.oper_state == 'down': |
| 573 | |
| 574 | if onu_device.connect_status != ConnectStatus.UNREACHABLE: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 575 | yield self.core_proxy.device_state_update( |
| 576 | onu_device.id, connect_status=ConnectStatus.UNREACHABLE) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 577 | |
| 578 | # Move to discovered state |
| 579 | self.log.debug('onu-oper-state-is-down') |
| 580 | |
| 581 | if onu_device.oper_status != OperStatus.DISCOVERED: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 582 | yield self.core_proxy.device_state_update( |
| 583 | onu_device.id, oper_status=OperStatus.DISCOVERED) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 584 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 585 | self.log.debug('inter-adapter-send-onu-ind', |
| 586 | onu_indication=onu_indication) |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 587 | |
| 588 | # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
| 589 | yield self.adapter_proxy.send_inter_adapter_message( |
| 590 | msg=onu_indication, |
| 591 | type=InterAdapterMessageType.ONU_IND_REQUEST, |
| 592 | from_adapter="openolt", |
| 593 | to_adapter=onu_device.type, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 594 | to_device_id=onu_device.id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 595 | |
| 596 | elif onu_indication.oper_state == 'up': |
| 597 | |
| 598 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 599 | yield self.core_proxy.device_state_update( |
| 600 | onu_device.id, connect_status=ConnectStatus.REACHABLE) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 601 | |
| 602 | if onu_device.oper_status != OperStatus.DISCOVERED: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 603 | if serial_number_str not in self.seen_discovery_indications: |
| 604 | self.log.debug("Ignore ONU indication", |
| 605 | intf_id=onu_indication.intf_id, |
| 606 | onu_id=onu_indication.onu_id, |
| 607 | state=onu_device.oper_status, |
| 608 | msg_oper_state=onu_indication.oper_state) |
| 609 | return |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 610 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 611 | self.log.debug('inter-adapter-send-onu-ind', |
| 612 | onu_indication=onu_indication) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 613 | |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 614 | # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
| 615 | yield self.adapter_proxy.send_inter_adapter_message( |
| 616 | msg=onu_indication, |
| 617 | type=InterAdapterMessageType.ONU_IND_REQUEST, |
| 618 | from_adapter="openolt", |
| 619 | to_adapter=onu_device.type, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 620 | to_device_id=onu_device.id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 621 | |
| 622 | else: |
| 623 | self.log.warn('Not-implemented-or-invalid-value-of-oper-state', |
| 624 | oper_state=onu_indication.oper_state) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 625 | |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 626 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 627 | def onu_ports_down(self, onu_device, oper_state): |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 628 | pass |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 629 | # Set port oper state to Discovered |
| 630 | # add port will update port if it exists |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 631 | # yield self.core_proxy.add_port( |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 632 | # self.device_id, |
| 633 | # Port( |
| 634 | # port_no=uni_no, |
| 635 | # label=uni_name, |
| 636 | # type=Port.ETHERNET_UNI, |
| 637 | # admin_state=onu_device.admin_state, |
| 638 | # oper_status=oper_state)) |
| 639 | # TODO this should be downning ports in onu adatper |
| 640 | |
Matt Jeanneret | d2f155b | 2019-02-22 13:49:09 -0500 | [diff] [blame] | 641 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 642 | def omci_indication(self, omci_indication): |
| 643 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 644 | self.log.debug("omci indication", |
| 645 | intf_id=omci_indication.intf_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 646 | onu_id=omci_indication.onu_id) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 647 | onu_in_cache = self.onus.get( |
| 648 | self.form_onu_key(omci_indication.intf_id, omci_indication.onu_id), |
| 649 | None) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 650 | if onu_in_cache is None: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 651 | self.log.debug('omci indication for a device not in cache.', |
| 652 | intf_id=omci_indication.intf_id, |
| 653 | onu_id=omci_indication.onu_id) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 654 | onu_device = yield self.core_proxy.get_child_device( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 655 | self.device_id, |
| 656 | onu_id=omci_indication.onu_id, |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 657 | parent_port_no=self.platform.intf_id_to_port_no( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 658 | omci_indication.intf_id, Port.PON_OLT), |
| 659 | ) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 660 | onu_device_type = onu_device.type |
| 661 | onu_device_id = onu_device.id |
| 662 | try: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 663 | serial_number_str = self.stringify_serial_number( |
| 664 | omci_indication.serial_number) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 665 | except Exception as e: |
| 666 | serial_number_str = None |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 667 | |
| 668 | #if not exist in cache, then add to cache. |
| 669 | onu_key = self.form_onu_key(omci_indication.intf_id, |
| 670 | omci_indication.onu_id) |
| 671 | self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type, |
| 672 | serial_number_str, |
| 673 | omci_indication.onu_id, |
| 674 | omci_indication.intf_id) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 675 | else: |
| 676 | onu_device_type = onu_in_cache.device_type |
| 677 | onu_device_id = onu_in_cache.device_id |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 678 | |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 679 | omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt) |
| 680 | |
| 681 | self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg) |
| 682 | |
| 683 | # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
| 684 | yield self.adapter_proxy.send_inter_adapter_message( |
| 685 | msg=omci_msg, |
| 686 | type=InterAdapterMessageType.OMCI_REQUEST, |
| 687 | from_adapter="openolt", |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 688 | to_adapter=onu_device_type, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 689 | to_device_id=onu_device_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 690 | |
Matt Jeanneret | d2f155b | 2019-02-22 13:49:09 -0500 | [diff] [blame] | 691 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 692 | def packet_indication(self, pkt_indication): |
| 693 | |
| 694 | self.log.debug("packet indication", |
| 695 | intf_type=pkt_indication.intf_type, |
| 696 | intf_id=pkt_indication.intf_id, |
| 697 | port_no=pkt_indication.port_no, |
| 698 | cookie=pkt_indication.cookie, |
| 699 | gemport_id=pkt_indication.gemport_id, |
| 700 | flow_id=pkt_indication.flow_id) |
| 701 | |
| 702 | if pkt_indication.intf_type == "pon": |
| 703 | if pkt_indication.port_no: |
Matt Jeanneret | a591ab8 | 2019-04-13 15:54:28 -0400 | [diff] [blame] | 704 | port_num = pkt_indication.port_no |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 705 | else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change |
| 706 | try: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 707 | onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport( |
| 708 | pkt_indication.intf_id, pkt_indication.gemport_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 709 | onu_id = int(onu_id_uni_id[0]) |
| 710 | uni_id = int(onu_id_uni_id[1]) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 711 | self.log.debug("packet indication-kv", |
| 712 | onu_id=onu_id, |
| 713 | uni_id=uni_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 714 | if onu_id is None: |
| 715 | raise Exception("onu-id-none") |
| 716 | if uni_id is None: |
| 717 | raise Exception("uni-id-none") |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 718 | port_num = self.platform.mk_uni_port_num( |
| 719 | pkt_indication.intf_id, onu_id, uni_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 720 | except Exception as e: |
| 721 | self.log.error("no-onu-reference-for-gem", |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 722 | gemport_id=pkt_indication.gemport_id, |
| 723 | e=e) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 724 | return |
| 725 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 726 | elif pkt_indication.intf_type == "nni": |
Matt Jeanneret | a591ab8 | 2019-04-13 15:54:28 -0400 | [diff] [blame] | 727 | port_num = self.platform.intf_id_to_port_no( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 728 | pkt_indication.intf_id, Port.ETHERNET_NNI) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 729 | |
| 730 | pkt = Ether(pkt_indication.pkt) |
| 731 | |
| 732 | self.log.debug("packet indication", |
Matt Jeanneret | a591ab8 | 2019-04-13 15:54:28 -0400 | [diff] [blame] | 733 | device_id=self.device_id, |
| 734 | port_num=port_num) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 735 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 736 | yield self.core_proxy.send_packet_in(device_id=self.device_id, |
| 737 | port=port_num, |
| 738 | packet=str(pkt)) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 739 | |
| 740 | def packet_out(self, egress_port, msg): |
| 741 | pkt = Ether(msg) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 742 | self.log.debug('packet out', |
| 743 | egress_port=egress_port, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 744 | device_id=self.device_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 745 | packet=str(pkt).encode("HEX")) |
| 746 | |
| 747 | # Find port type |
| 748 | egress_port_type = self.platform.intf_id_to_port_type_name(egress_port) |
| 749 | if egress_port_type == Port.ETHERNET_UNI: |
| 750 | |
| 751 | if pkt.haslayer(Dot1Q): |
| 752 | outer_shim = pkt.getlayer(Dot1Q) |
| 753 | if isinstance(outer_shim.payload, Dot1Q): |
| 754 | # If double tag, remove the outer tag |
| 755 | payload = ( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 756 | Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) / |
| 757 | outer_shim.payload) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 758 | else: |
| 759 | payload = pkt |
| 760 | else: |
| 761 | payload = pkt |
| 762 | |
| 763 | send_pkt = binascii.unhexlify(str(payload).encode("HEX")) |
| 764 | |
| 765 | self.log.debug( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 766 | 'sending-packet-to-ONU', |
| 767 | egress_port=egress_port, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 768 | intf_id=self.platform.intf_id_from_uni_port_num(egress_port), |
| 769 | onu_id=self.platform.onu_id_from_port_num(egress_port), |
| 770 | uni_id=self.platform.uni_id_from_port_num(egress_port), |
| 771 | port_no=egress_port, |
| 772 | packet=str(payload).encode("HEX")) |
| 773 | |
| 774 | onu_pkt = openolt_pb2.OnuPacket( |
| 775 | intf_id=self.platform.intf_id_from_uni_port_num(egress_port), |
| 776 | onu_id=self.platform.onu_id_from_port_num(egress_port), |
| 777 | port_no=egress_port, |
| 778 | pkt=send_pkt) |
| 779 | |
| 780 | self.stub.OnuPacketOut(onu_pkt) |
| 781 | |
| 782 | elif egress_port_type == Port.ETHERNET_NNI: |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 783 | self.log.debug('sending-packet-to-uplink', |
| 784 | egress_port=egress_port, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 785 | packet=str(pkt).encode("HEX")) |
| 786 | |
| 787 | send_pkt = binascii.unhexlify(str(pkt).encode("HEX")) |
| 788 | |
| 789 | uplink_pkt = openolt_pb2.UplinkPacket( |
| 790 | intf_id=self.platform.intf_id_from_nni_port_num(egress_port), |
| 791 | pkt=send_pkt) |
| 792 | |
| 793 | self.stub.UplinkPacketOut(uplink_pkt) |
| 794 | |
| 795 | else: |
| 796 | self.log.warn('Packet-out-to-this-interface-type-not-implemented', |
| 797 | egress_port=egress_port, |
| 798 | port_type=egress_port_type) |
| 799 | |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 800 | def process_inter_adapter_message(self, request): |
| 801 | self.log.debug('process-inter-adapter-message', msg=request) |
| 802 | try: |
| 803 | if request.header.type == InterAdapterMessageType.OMCI_REQUEST: |
| 804 | omci_msg = InterAdapterOmciMessage() |
| 805 | request.body.Unpack(omci_msg) |
| 806 | self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg) |
| 807 | |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 808 | #onu_device_id = request.header.to_device_id |
| 809 | #onu_device = yield self.core_proxy.get_device(onu_device_id) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 810 | self.send_proxied_message(omci_msg) |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 811 | |
| 812 | else: |
| 813 | self.log.error("inter-adapter-unhandled-type", request=request) |
| 814 | |
| 815 | except Exception as e: |
| 816 | self.log.exception("error-processing-inter-adapter-message", e=e) |
| 817 | |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 818 | def send_proxied_message(self, omci_msg): |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 819 | |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 820 | if omci_msg.connect_status != ConnectStatus.REACHABLE: |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 821 | self.log.debug('ONU is not reachable, cannot send OMCI', |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 822 | intf_id=omci_msg.proxy_address.channel_id, |
| 823 | onu_id=omci_msg.proxy_address.onu_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 824 | return |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 825 | |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 826 | omci = openolt_pb2.OmciMsg(intf_id=omci_msg.proxy_address.channel_id, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 827 | onu_id=omci_msg.proxy_address.onu_id, |
| 828 | pkt=str(omci_msg.message)) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 829 | self.stub.OmciMsgOut(omci) |
| 830 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 831 | self.log.debug("omci-message-sent", |
| 832 | intf_id=omci_msg.proxy_address.channel_id, |
| 833 | onu_id=omci_msg.proxy_address.onu_id, |
| 834 | pkt=str(omci_msg.message)) |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 835 | |
Matt Jeanneret | 6e31509 | 2019-02-20 10:42:57 -0500 | [diff] [blame] | 836 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 837 | 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] | 838 | self.log.info("adding-onu", |
| 839 | port_no=port_no, |
| 840 | onu_id=onu_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 841 | serial_number=serial_number) |
| 842 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 843 | serial_number_str = self.stringify_serial_number(serial_number) |
| 844 | |
Matt Jeanneret | b428b95 | 2019-03-07 05:14:17 -0500 | [diff] [blame] | 845 | # TODO NEW CORE dont hardcode child device type. find some way of determining by vendor in serial number |
Matt Jeanneret | 2133c3b | 2019-07-03 11:34:20 -0400 | [diff] [blame] | 846 | onu_device = yield self.core_proxy.child_device_detected( |
Matt Jeanneret | 6e31509 | 2019-02-20 10:42:57 -0500 | [diff] [blame] | 847 | parent_device_id=self.device_id, |
| 848 | parent_port_no=port_no, |
| 849 | child_device_type='brcm_openomci_onu', |
Matt Jeanneret | 5ba87e3 | 2019-02-28 11:35:49 -0500 | [diff] [blame] | 850 | channel_id=intf_id, |
| 851 | vendor_id=serial_number.vendor_id, |
| 852 | serial_number=serial_number_str, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 853 | onu_id=onu_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 854 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 855 | self.log.debug("onu-added", |
| 856 | onu_id=onu_id, |
| 857 | port_no=port_no, |
| 858 | serial_number=serial_number_str) |
Matt Jeanneret | 5ba87e3 | 2019-02-28 11:35:49 -0500 | [diff] [blame] | 859 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 860 | yield self.core_proxy.device_state_update( |
| 861 | onu_device.id, |
| 862 | oper_status=OperStatus.DISCOVERED, |
| 863 | connect_status=ConnectStatus.REACHABLE) |
Matt Jeanneret | 2133c3b | 2019-07-03 11:34:20 -0400 | [diff] [blame] | 864 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 865 | self.log.debug("set-onu-discovered", |
| 866 | onu_id=onu_id, |
| 867 | port_no=port_no, |
| 868 | serial_number=serial_number_str, |
Matt Jeanneret | 2133c3b | 2019-07-03 11:34:20 -0400 | [diff] [blame] | 869 | onu_device=onu_device) |
| 870 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 871 | self.log.debug('Adding ONU device to the cache', |
| 872 | intf_id=intf_id, |
| 873 | onu_id=onu_id) |
| 874 | onu_key = self.form_onu_key(intf_id, onu_id) |
| 875 | self.onu_cache[onu_key] = onu_device |
| 876 | return |
| 877 | |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 878 | def get_ofp_device_info(self, device): |
| 879 | self.log.info('get_ofp_device_info', device_id=device.id) |
| 880 | |
| 881 | mfr_desc = self.device_info.vendor |
| 882 | sw_desc = self.device_info.firmware_version |
| 883 | hw_desc = self.device_info.model |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 884 | if self.device_info.hardware_version: |
| 885 | hw_desc += '-' + self.device_info.hardware_version |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 886 | |
| 887 | return SwitchCapability( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 888 | desc=ofp_desc(hw_desc=hw_desc, |
| 889 | sw_desc=sw_desc, |
| 890 | serial_num=device.serial_number), |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 891 | switch_features=ofp_switch_features( |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 892 | n_buffers= |
| 893 | 256, # Max packets buffered at once # TODO fake for now |
| 894 | n_tables= |
| 895 | 2, # Number of tables supported by datapath # TODO fake for now |
| 896 | capabilities= |
| 897 | ( #Bitmap of support "ofp_capabilities" # TODO fake for now |
| 898 | OFPC_FLOW_STATS |
| 899 | | OFPC_TABLE_STATS |
| 900 | | OFPC_PORT_STATS |
| 901 | | OFPC_GROUP_STATS))) |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 902 | |
| 903 | def get_ofp_port_info(self, device, port_no): |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 904 | self.log.info('get_ofp_port_info', |
| 905 | port_no=port_no, |
| 906 | device_id=device.id) |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 907 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 908 | return PortCapability(port=LogicalPort(ofp_port=ofp_port( |
| 909 | hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)), |
| 910 | config=0, |
| 911 | state=OFPPS_LIVE, |
| 912 | curr=cap, |
| 913 | advertised=cap, |
| 914 | peer=cap, |
| 915 | curr_speed=OFPPF_1GB_FD, |
| 916 | max_speed=OFPPF_1GB_FD), |
| 917 | device_id=device.id, |
| 918 | device_port_no=port_no)) |
Matt Jeanneret | e33a709 | 2019-03-12 21:54:14 -0400 | [diff] [blame] | 919 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 920 | def port_name(self, port_no, port_type, intf_id=None, serial_number=None): |
| 921 | if port_type is Port.ETHERNET_NNI: |
| 922 | return "nni-" + str(port_no) |
| 923 | elif port_type is Port.PON_OLT: |
| 924 | return "pon" + str(intf_id) |
| 925 | elif port_type is Port.ETHERNET_UNI: |
| 926 | assert False, 'local UNI management not supported' |
| 927 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 928 | def _get_mac_form_port_no(self, port_no): |
| 929 | mac = '' |
| 930 | for i in range(4): |
| 931 | mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac |
| 932 | return '00:00' + mac |
| 933 | |
William Kurkian | 92bd712 | 2019-02-14 15:26:59 -0500 | [diff] [blame] | 934 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 935 | def add_port(self, intf_id, port_type, oper_status): |
| 936 | port_no = self.platform.intf_id_to_port_no(intf_id, port_type) |
| 937 | |
| 938 | label = self.port_name(port_no, port_type, intf_id) |
| 939 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 940 | self.log.debug('adding-port', |
| 941 | port_no=port_no, |
| 942 | label=label, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 943 | port_type=port_type) |
| 944 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 945 | port = Port(port_no=port_no, |
| 946 | label=label, |
| 947 | type=port_type, |
| 948 | admin_state=AdminState.ENABLED, |
| 949 | oper_status=oper_status) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 950 | |
Matt Jeanneret | bad3d98 | 2019-03-11 16:06:10 -0400 | [diff] [blame] | 951 | yield self.core_proxy.port_created(self.device_id, port) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 952 | |
Matt Jeanneret | d2f155b | 2019-02-22 13:49:09 -0500 | [diff] [blame] | 953 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 954 | def delete_port(self, child_serial_number): |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 955 | ports = self.proxy.get('/devices/{}/ports'.format(self.device_id)) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 956 | for port in ports: |
| 957 | if port.label == child_serial_number: |
| 958 | self.log.debug('delete-port', |
| 959 | onu_serial_number=child_serial_number, |
| 960 | port=port) |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 961 | yield self.core_proxy.port_removed(self.device_id, port) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 962 | return |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 963 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 964 | def update_flow_table(self, flow_changes): |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 965 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 966 | self.log.debug("update_flow_table", flow_changes=flow_changes) |
| 967 | |
| 968 | flows_to_add = flow_changes.to_add.items |
| 969 | flows_to_remove = flow_changes.to_remove.items |
| 970 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 971 | if not self.is_state_up(): |
| 972 | self.log.info('The OLT is not up, we cannot update flows', |
| 973 | flows_to_add=[f.id for f in flows_to_add], |
| 974 | flows_to_remove=[f.id for f in flows_to_remove]) |
| 975 | return |
| 976 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 977 | self.log.debug('flows update', |
| 978 | flows_to_add=flows_to_add, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 979 | flows_to_remove=flows_to_remove) |
| 980 | |
| 981 | for flow in flows_to_add: |
| 982 | |
| 983 | try: |
| 984 | self.flow_mgr.add_flow(flow) |
| 985 | except Exception as e: |
| 986 | self.log.error('failed to add flow', flow=flow, e=e) |
| 987 | |
| 988 | for flow in flows_to_remove: |
| 989 | |
| 990 | try: |
| 991 | self.flow_mgr.remove_flow(flow) |
| 992 | except Exception as e: |
| 993 | self.log.error('failed to remove flow', flow=flow, e=e) |
| 994 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 995 | # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot! |
| 996 | #self.flow_mgr.repush_all_different_flows() |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 997 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 998 | # There has to be a better way to do this |
| 999 | def ip_hex(self, ip): |
| 1000 | octets = ip.split(".") |
| 1001 | hex_ip = [] |
| 1002 | for octet in octets: |
| 1003 | octet_hex = hex(int(octet)) |
| 1004 | octet_hex = octet_hex.split('0x')[1] |
| 1005 | octet_hex = octet_hex.rjust(2, '0') |
| 1006 | hex_ip.append(octet_hex) |
| 1007 | return ":".join(hex_ip) |
| 1008 | |
| 1009 | def stringify_vendor_specific(self, vendor_specific): |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1010 | return ''.join( |
| 1011 | str(i) for i in [ |
| 1012 | hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:], |
| 1013 | hex(ord(vendor_specific[0]) & 0x0f)[2:], |
| 1014 | hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:], |
| 1015 | hex(ord(vendor_specific[1]) & 0x0f)[2:], |
| 1016 | hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:], |
| 1017 | hex(ord(vendor_specific[2]) & 0x0f)[2:], |
| 1018 | hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:], |
| 1019 | hex(ord(vendor_specific[3]) & 0x0f)[2:] |
| 1020 | ]) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1021 | |
| 1022 | def stringify_serial_number(self, serial_number): |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1023 | return ''.join([ |
| 1024 | serial_number.vendor_id, |
| 1025 | self.stringify_vendor_specific(serial_number.vendor_specific) |
| 1026 | ]) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1027 | |
| 1028 | def destringify_serial_number(self, serial_number_str): |
| 1029 | serial_number = openolt_pb2.SerialNumber( |
| 1030 | vendor_id=serial_number_str[:4].encode('utf-8'), |
| 1031 | vendor_specific=binascii.unhexlify(serial_number_str[4:])) |
| 1032 | return serial_number |
| 1033 | |
| 1034 | def disable(self): |
| 1035 | self.log.debug('sending-deactivate-olt-message', |
| 1036 | device_id=self.device_id) |
| 1037 | |
| 1038 | try: |
| 1039 | # Send grpc call |
| 1040 | self.stub.DisableOlt(openolt_pb2.Empty()) |
| 1041 | # The resulting indication will bring the OLT down |
| 1042 | # self.go_state_down() |
| 1043 | self.log.info('openolt device disabled') |
| 1044 | except Exception as e: |
| 1045 | self.log.error('Failure to disable openolt device', error=e) |
| 1046 | |
| 1047 | def delete(self): |
Matt Jeanneret | aa36091 | 2019-04-22 16:23:12 -0400 | [diff] [blame] | 1048 | self.log.info('deleting-olt', device_id=self.device_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1049 | |
| 1050 | # Clears up the data from the resource manager KV store |
| 1051 | # for the device |
| 1052 | del self.resource_mgr |
| 1053 | |
| 1054 | try: |
| 1055 | # Rebooting to reset the state |
| 1056 | self.reboot() |
| 1057 | # Removing logical device |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1058 | except Exception as e: |
| 1059 | self.log.error('Failure to delete openolt device', error=e) |
| 1060 | raise e |
| 1061 | else: |
| 1062 | self.log.info('successfully-deleted-olt', device_id=self.device_id) |
| 1063 | |
| 1064 | def reenable(self): |
| 1065 | self.log.debug('reenabling-olt', device_id=self.device_id) |
| 1066 | |
| 1067 | try: |
| 1068 | self.stub.ReenableOlt(openolt_pb2.Empty()) |
| 1069 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1070 | except Exception as e: |
| 1071 | self.log.error('Failure to reenable openolt device', error=e) |
| 1072 | else: |
| 1073 | self.log.info('openolt device reenabled') |
| 1074 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1075 | 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] | 1076 | pir = self.bw_mgr.pir(serial_number_str) |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1077 | self.log.debug("activating-onu", |
| 1078 | intf_id=intf_id, |
| 1079 | onu_id=onu_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1080 | serial_number_str=serial_number_str, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1081 | serial_number=serial_number, |
| 1082 | pir=pir) |
| 1083 | onu = openolt_pb2.Onu(intf_id=intf_id, |
| 1084 | onu_id=onu_id, |
| 1085 | serial_number=serial_number, |
| 1086 | pir=pir) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1087 | self.stub.ActivateOnu(onu) |
| 1088 | self.log.info('onu-activated', serial_number=serial_number_str) |
| 1089 | |
Matt Jeanneret | d2f155b | 2019-02-22 13:49:09 -0500 | [diff] [blame] | 1090 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1091 | def delete_child_device(self, child_device): |
| 1092 | self.log.debug('sending-deactivate-onu', |
| 1093 | olt_device_id=self.device_id, |
| 1094 | onu_device=child_device, |
| 1095 | onu_serial_number=child_device.serial_number) |
| 1096 | try: |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 1097 | yield self.core_proxy.child_device_removed(self.device_id, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1098 | child_device.id, |
| 1099 | child_device) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1100 | except Exception as e: |
William Kurkian | 23047b9 | 2019-05-01 11:02:35 -0400 | [diff] [blame] | 1101 | self.log.error('core_proxy error', error=e) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1102 | try: |
| 1103 | self.delete_logical_port(child_device) |
| 1104 | except Exception as e: |
| 1105 | self.log.error('logical_port delete error', error=e) |
| 1106 | try: |
| 1107 | self.delete_port(child_device.serial_number) |
| 1108 | except Exception as e: |
| 1109 | self.log.error('port delete error', error=e) |
| 1110 | serial_number = self.destringify_serial_number( |
| 1111 | child_device.serial_number) |
| 1112 | # TODO FIXME - For each uni. |
| 1113 | # TODO FIXME - Flows are not deleted |
| 1114 | uni_id = 0 # FIXME |
| 1115 | self.flow_mgr.delete_tech_profile_instance( |
Matt Jeanneret | 7906d23 | 2019-02-14 14:57:38 -0500 | [diff] [blame] | 1116 | 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 | pon_intf_id_onu_id = (child_device.proxy_address.channel_id, |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1119 | child_device.proxy_address.onu_id, uni_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1120 | # Free any PON resources that were reserved for the ONU |
| 1121 | self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id) |
| 1122 | |
| 1123 | onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id, |
| 1124 | onu_id=child_device.proxy_address.onu_id, |
| 1125 | serial_number=serial_number) |
| 1126 | self.stub.DeleteOnu(onu) |
| 1127 | |
| 1128 | def reboot(self): |
| 1129 | self.log.debug('rebooting openolt device', device_id=self.device_id) |
| 1130 | try: |
| 1131 | self.stub.Reboot(openolt_pb2.Empty()) |
| 1132 | except Exception as e: |
| 1133 | self.log.error('something went wrong with the reboot', error=e) |
| 1134 | else: |
| 1135 | self.log.info('device rebooted') |
| 1136 | |
| 1137 | def trigger_statistics_collection(self): |
| 1138 | try: |
| 1139 | self.stub.CollectStatistics(openolt_pb2.Empty()) |
| 1140 | except Exception as e: |
| 1141 | self.log.error('Error while triggering statistics collection', |
| 1142 | error=e) |
| 1143 | else: |
| 1144 | self.log.info('statistics requested') |
| 1145 | |
| 1146 | def simulate_alarm(self, alarm): |
| 1147 | self.alarm_mgr.simulate_alarm(alarm) |
Mahir Gunyel | 4d45150 | 2019-05-16 14:27:13 -0700 | [diff] [blame] | 1148 | |
| 1149 | def form_onu_key(self, intf_id, onu_id): |
| 1150 | return str(intf_id) + "." + str(onu_id) |
| 1151 | |
| 1152 | |
| 1153 | class OnuDevice(object): |
| 1154 | def __init__(self, device_id, device_type, serialnumber, onu_id, intf_id): |
| 1155 | self.device_id = device_id |
| 1156 | self.device_type = device_type |
| 1157 | self.serialnumber = serialnumber |
| 1158 | self.onu_id = onu_id |
| 1159 | self.intf_id = intf_id |