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