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