Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [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 | |
| 17 | import structlog |
| 18 | import threading |
| 19 | import grpc |
| 20 | import collections |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 21 | import time |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 22 | |
Shad Ansari | 15928d1 | 2018-04-17 02:42:13 +0000 | [diff] [blame] | 23 | from twisted.internet import reactor |
Shad Ansari | 0346f0d | 2018-04-26 06:54:09 +0000 | [diff] [blame] | 24 | from scapy.layers.l2 import Ether, Dot1Q |
| 25 | import binascii |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 26 | from transitions import Machine |
Shad Ansari | 15928d1 | 2018-04-17 02:42:13 +0000 | [diff] [blame] | 27 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 28 | from voltha.protos.device_pb2 import Port, Device |
| 29 | from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus |
| 30 | from voltha.protos.logical_device_pb2 import LogicalDevice |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 31 | from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, OFPPS_LINK_DOWN, \ |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 32 | OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, \ |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 33 | OFPC_FLOW_STATS, ofp_switch_features, ofp_port |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 34 | from voltha.protos.logical_device_pb2 import LogicalPort, LogicalDevice |
| 35 | from voltha.core.logical_device_agent import mac_str_to_tuple |
| 36 | from voltha.registry import registry |
| 37 | from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2 |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 38 | from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData |
| 39 | from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData |
| 40 | from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 41 | import voltha.core.flow_decomposer as fd |
| 42 | |
Shad Ansari | 2292093 | 2018-05-17 00:33:34 +0000 | [diff] [blame] | 43 | import openolt_platform as platform |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 44 | from openolt_flow_mgr import OpenOltFlowMgr |
Shad Ansari | 801f737 | 2018-04-27 02:15:41 +0000 | [diff] [blame] | 45 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 46 | MAX_HEARTBEAT_MISS = 3 |
| 47 | HEARTBEAT_PERIOD = 1 |
| 48 | GRPC_TIMEOUT = 5 |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 49 | |
| 50 | """ |
| 51 | OpenoltDevice represents an OLT. |
| 52 | """ |
| 53 | class OpenoltDevice(object): |
| 54 | |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 55 | states = ['up', 'down'] |
| 56 | transitions = [ |
| 57 | { 'trigger': 'olt_up', 'source': 'down', 'dest': 'up', 'before': 'olt_indication_up' }, |
| 58 | { 'trigger': 'olt_down', 'source': 'up', 'dest': 'down', 'before': 'olt_indication_down' } |
| 59 | ] |
| 60 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 61 | def __init__(self, **kwargs): |
| 62 | super(OpenoltDevice, self).__init__() |
| 63 | |
| 64 | self.adapter_agent = kwargs['adapter_agent'] |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 65 | self.device_num = kwargs['device_num'] |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 66 | device = kwargs['device'] |
Nicolas Palpacuer | 253461f | 2018-06-01 12:01:45 -0400 | [diff] [blame] | 67 | is_reconciliation = kwargs.get('reconciliation', False) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 68 | self.device_id = device.id |
| 69 | self.host_and_port = device.host_and_port |
| 70 | self.log = structlog.get_logger(id=self.device_id, ip=self.host_and_port) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 71 | self.proxy = registry('core').get_proxy('/') |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 72 | |
Nicolas Palpacuer | 253461f | 2018-06-01 12:01:45 -0400 | [diff] [blame] | 73 | # Device already set in the event of reconciliation |
| 74 | if not is_reconciliation: |
| 75 | # It is a new device |
| 76 | # Update device |
| 77 | device.root = True |
| 78 | device.serial_number = self.host_and_port # FIXME |
| 79 | device.connect_status = ConnectStatus.REACHABLE |
| 80 | device.oper_status = OperStatus.ACTIVATING |
| 81 | self.adapter_agent.update_device(device) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 82 | |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 83 | # Initialize the OLT state machine |
| 84 | self.machine = Machine(model=self, states=OpenoltDevice.states, |
| 85 | transitions=OpenoltDevice.transitions, |
| 86 | send_event=True, initial='down', ignore_invalid_triggers=True) |
| 87 | self.machine.add_transition(trigger='olt_ind_up', source='down', dest='up') |
| 88 | self.machine.add_transition(trigger='olt_ind_loss', source='up', dest='down') |
| 89 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 90 | # Initialize gRPC |
| 91 | self.channel = grpc.insecure_channel(self.host_and_port) |
Shad Ansari | 8f1b253 | 2018-04-21 07:51:39 +0000 | [diff] [blame] | 92 | self.channel_ready_future = grpc.channel_ready_future(self.channel) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 93 | self.stub = openolt_pb2_grpc.OpenoltStub(self.channel) |
| 94 | |
| 95 | self.flow_mgr = OpenOltFlowMgr(self.log, self.stub) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 96 | |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 97 | # Indications thread plcaholder (started by heartbeat thread) |
| 98 | self.indications_thread = None |
| 99 | self.indications_thread_active = False |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 100 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 101 | # Start heartbeat thread |
| 102 | self.heartbeat_thread = threading.Thread(target=self.heartbeat) |
| 103 | self.heartbeat_thread.setDaemon(True) |
| 104 | self.heartbeat_thread_active = True |
| 105 | self.heartbeat_miss = 0 |
| 106 | self.heartbeat_signature = None |
| 107 | self.heartbeat_thread.start() |
| 108 | |
Nicolas Palpacuer | 253461f | 2018-06-01 12:01:45 -0400 | [diff] [blame] | 109 | self.log.debug('openolt-device-created', device_id=self.device_id) |
| 110 | |
| 111 | |
Shad Ansari | 8f1b253 | 2018-04-21 07:51:39 +0000 | [diff] [blame] | 112 | def process_indications(self): |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 113 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 114 | self.log.debug('starting-indications-thread') |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 115 | |
Shad Ansari | 15928d1 | 2018-04-17 02:42:13 +0000 | [diff] [blame] | 116 | self.indications = self.stub.EnableIndication(openolt_pb2.Empty()) |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 117 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 118 | while self.indications_thread_active: |
| 119 | try: |
| 120 | # get the next indication from olt |
| 121 | ind = next(self.indications) |
| 122 | except Exception as e: |
| 123 | self.log.warn('GRPC-connection-lost-stoping-indications-thread', error=e) |
| 124 | self.indications_thread_active = False |
| 125 | else: |
| 126 | self.log.debug("rx indication", indication=ind) |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 127 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 128 | # indication handlers run in the main event loop |
| 129 | if ind.HasField('olt_ind'): |
| 130 | reactor.callFromThread(self.olt_indication, ind.olt_ind) |
| 131 | elif ind.HasField('intf_ind'): |
| 132 | reactor.callFromThread(self.intf_indication, ind.intf_ind) |
| 133 | elif ind.HasField('intf_oper_ind'): |
| 134 | reactor.callFromThread(self.intf_oper_indication, ind.intf_oper_ind) |
| 135 | elif ind.HasField('onu_disc_ind'): |
| 136 | reactor.callFromThread(self.onu_discovery_indication, ind.onu_disc_ind) |
| 137 | elif ind.HasField('onu_ind'): |
| 138 | reactor.callFromThread(self.onu_indication, ind.onu_ind) |
| 139 | elif ind.HasField('omci_ind'): |
| 140 | reactor.callFromThread(self.omci_indication, ind.omci_ind) |
| 141 | elif ind.HasField('pkt_ind'): |
| 142 | reactor.callFromThread(self.packet_indication, ind.pkt_ind) |
| 143 | |
| 144 | self.log.debug('stopping-indications-thread', device_id=self.device_id) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 145 | |
| 146 | def olt_indication(self, olt_indication): |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 147 | if olt_indication.oper_state == "up": |
| 148 | self.olt_up(ind=olt_indication) |
| 149 | elif olt_indication.oper_state == "down": |
| 150 | self.olt_down(ind=olt_indication) |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 151 | |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 152 | def olt_indication_up(self, event): |
| 153 | olt_indication = event.kwargs.get('ind', None) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 154 | self.log.debug("olt indication", olt_ind=olt_indication) |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 155 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 156 | device = self.adapter_agent.get_device(self.device_id) |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 157 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 158 | # If logical device does not exist create it |
| 159 | if len(device.parent_id) == 0: |
| 160 | |
| 161 | dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0]) |
| 162 | |
| 163 | # Create logical OF device |
| 164 | ld = LogicalDevice( |
| 165 | root_device_id=self.device_id, |
| 166 | switch_features=ofp_switch_features( |
| 167 | n_buffers=256, # TODO fake for now |
| 168 | n_tables=2, # TODO ditto |
| 169 | capabilities=( # TODO and ditto |
| 170 | OFPC_FLOW_STATS |
| 171 | | OFPC_TABLE_STATS |
| 172 | | OFPC_PORT_STATS |
| 173 | | OFPC_GROUP_STATS |
| 174 | ) |
Jonathan Hart | 7687e0a | 2018-05-16 10:54:47 -0700 | [diff] [blame] | 175 | ) |
| 176 | ) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 177 | ld_initialized = self.adapter_agent.create_logical_device(ld, dpid=dpid) |
| 178 | self.logical_device_id = ld_initialized.id |
Nicolas Palpacuer | cf735ac | 2018-06-06 11:12:53 -0400 | [diff] [blame] | 179 | else: |
| 180 | # logical device already exists |
| 181 | self.logical_device_id = device.parent_id |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 182 | |
| 183 | # Update phys OF device |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 184 | device.parent_id = self.logical_device_id |
| 185 | device.oper_status = OperStatus.ACTIVE |
| 186 | self.adapter_agent.update_device(device) |
| 187 | |
Shad Ansari | 22efe83 | 2018-05-19 05:37:03 +0000 | [diff] [blame] | 188 | def olt_indication_down(self, event): |
| 189 | olt_indication = event.kwargs.get('ind', None) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 190 | new_admin_state = event.kwargs.get('admin_state', None) |
| 191 | new_oper_state = event.kwargs.get('oper_state', None) |
| 192 | new_connect_state = event.kwargs.get('connect_state', None) |
| 193 | self.log.debug("olt indication", olt_ind=olt_indication, admin_state=new_admin_state, oper_state=new_oper_state, |
| 194 | connect_state=new_connect_state) |
| 195 | |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 196 | # Propagating to the children |
| 197 | |
| 198 | # Children ports |
| 199 | child_devices = self.adapter_agent.get_child_devices(self.device_id) |
| 200 | for onu_device in child_devices: |
| 201 | uni_no = platform.mk_uni_port_num(onu_device.proxy_address.channel_id, onu_device.proxy_address.onu_id) |
| 202 | uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=onu_device.serial_number) |
| 203 | |
| 204 | self.onu_ports_down(onu_device, uni_no, uni_name, new_oper_state) |
| 205 | # Children devices |
| 206 | self.adapter_agent.update_child_devices_state(self.device_id, oper_status=new_oper_state, |
| 207 | connect_status=ConnectStatus.UNREACHABLE, |
| 208 | admin_state=new_admin_state) |
| 209 | # Device Ports |
| 210 | device_ports = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI) |
| 211 | logical_ports_ids = [port.label for port in device_ports] |
| 212 | device_ports += self.adapter_agent.get_ports(self.device_id, Port.PON_OLT) |
| 213 | |
| 214 | for port in device_ports: |
| 215 | if new_admin_state is not None: |
| 216 | port.admin_state = new_admin_state |
| 217 | if new_oper_state is not None: |
| 218 | port.oper_status = new_oper_state |
| 219 | self.adapter_agent.add_port(self.device_id, port) |
| 220 | |
| 221 | # Device logical port |
| 222 | for logical_port_id in logical_ports_ids: |
| 223 | logical_port = self.adapter_agent.get_logical_port(self.logical_device_id, logical_port_id) |
| 224 | logical_port.ofp_port.state = OFPPS_LINK_DOWN |
| 225 | self.adapter_agent.update_logical_port(self.logical_device_id, logical_port) |
| 226 | |
| 227 | # Device |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 228 | device = self.adapter_agent.get_device(self.device_id) |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 229 | if new_admin_state is not None: |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 230 | device.admin_state = new_admin_state |
| 231 | if new_oper_state is not None: |
| 232 | device.oper_status = new_oper_state |
| 233 | if new_connect_state is not None: |
| 234 | device.connect_status = new_connect_state |
| 235 | |
| 236 | self.adapter_agent.update_device(device) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 237 | |
| 238 | def intf_indication(self, intf_indication): |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 239 | self.log.debug("intf indication", intf_id=intf_indication.intf_id, |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 240 | oper_state=intf_indication.oper_state) |
| 241 | |
| 242 | if intf_indication.oper_state == "up": |
| 243 | oper_status = OperStatus.ACTIVE |
| 244 | else: |
| 245 | oper_status = OperStatus.DISCOVERED |
| 246 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 247 | # add_port update the port if it exists |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 248 | self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status) |
| 249 | |
| 250 | def intf_oper_indication(self, intf_oper_indication): |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 251 | self.log.debug("Received interface oper state change indication", intf_id=intf_oper_indication.intf_id, |
| 252 | type=intf_oper_indication.type, oper_state=intf_oper_indication.oper_state) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 253 | |
| 254 | if intf_oper_indication.oper_state == "up": |
| 255 | oper_state = OperStatus.ACTIVE |
| 256 | else: |
| 257 | oper_state = OperStatus.DISCOVERED |
| 258 | |
| 259 | if intf_oper_indication.type == "nni": |
| 260 | |
Shad Ansari | 0346f0d | 2018-04-26 06:54:09 +0000 | [diff] [blame] | 261 | # FIXME - creating logical port for 2nd interface throws exception! |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 262 | if intf_oper_indication.intf_id != 0: |
| 263 | return |
| 264 | |
Nicolas Palpacuer | cf735ac | 2018-06-06 11:12:53 -0400 | [diff] [blame] | 265 | # add_(logical_)port update the port if it exists |
| 266 | port_no, label = self.add_port(intf_oper_indication.intf_id, Port.ETHERNET_NNI, oper_state) |
| 267 | self.log.debug("int_oper_indication", port_no=port_no, label=label) |
| 268 | self.add_logical_port(port_no, intf_oper_indication.intf_id, oper_state) |
| 269 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 270 | |
| 271 | elif intf_oper_indication.type == "pon": |
| 272 | # FIXME - handle PON oper state change |
| 273 | pass |
| 274 | |
| 275 | def onu_discovery_indication(self, onu_disc_indication): |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 276 | intf_id = onu_disc_indication.intf_id |
| 277 | serial_number=onu_disc_indication.serial_number |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 278 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 279 | serial_number_str = self.stringify_serial_number(serial_number) |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 280 | |
Nicolas Palpacuer | 36a9344 | 2018-05-23 17:38:57 -0400 | [diff] [blame] | 281 | self.log.debug("onu discovery indication", intf_id=intf_id, serial_number=serial_number_str) |
| 282 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 283 | onu_device = self.adapter_agent.get_child_device(self.device_id, serial_number=serial_number_str) |
| 284 | |
| 285 | if onu_device is None: |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 286 | onu_id = self.new_onu_id(intf_id) |
Shad Ansari | 15928d1 | 2018-04-17 02:42:13 +0000 | [diff] [blame] | 287 | try: |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 288 | self.add_onu_device(intf_id, |
Shad Ansari | 2292093 | 2018-05-17 00:33:34 +0000 | [diff] [blame] | 289 | platform.intf_id_to_port_no(intf_id, Port.PON_OLT), |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 290 | onu_id, serial_number) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 291 | self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id, |
Nicolas Palpacuer | 36a9344 | 2018-05-23 17:38:57 -0400 | [diff] [blame] | 292 | serial_number=serial_number_str) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 293 | onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,serial_number=serial_number) |
| 294 | self.stub.ActivateOnu(onu) |
| 295 | except Exception as e: |
| 296 | self.log.exception('onu-activation-failed', e=e) |
| 297 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 298 | else: |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 299 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 300 | onu_device.connect_status = ConnectStatus.REACHABLE |
| 301 | self.adapter_agent.update_device(onu_device) |
| 302 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 303 | onu_id = onu_device.proxy_address.onu_id |
Nicolas Palpacuer | 36a9344 | 2018-05-23 17:38:57 -0400 | [diff] [blame] | 304 | if onu_device.oper_status == OperStatus.DISCOVERED or onu_device.oper_status == OperStatus.ACTIVATING: |
| 305 | self.log.debug("ignore onu discovery indication, the onu has been discovered and should be \ |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 306 | activating shorlty", intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status) |
| 307 | elif onu_device.oper_status== OperStatus.ACTIVE: |
| 308 | self.log.warn("onu discovery indication whereas onu is supposed to be active", |
| 309 | intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 310 | elif onu_device.oper_status == OperStatus.UNKNOWN: |
| 311 | self.log.info("onu-in-unknow-state-recovering-form-olt-reboot-activate-onu", intf_id=intf_id, onu_id=onu_id, |
| 312 | serial_number=serial_number_str) |
| 313 | |
| 314 | onu_device.oper_status = OperStatus.DISCOVERED |
| 315 | self.adapter_agent.update_device(onu_device) |
| 316 | |
| 317 | onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id, serial_number=serial_number) |
| 318 | self.stub.ActivateOnu(onu) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 319 | else: |
| 320 | self.log.warn('unexpected state', onu_id=onu_id, onu_device_oper_state=onu_device.oper_status) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 321 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 322 | def onu_indication(self, onu_indication): |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 323 | self.log.debug("onu-indication", intf_id=onu_indication.intf_id, |
| 324 | onu_id=onu_indication.onu_id, serial_number=onu_indication.serial_number, |
| 325 | oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 326 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 327 | serial_number_str = self.stringify_serial_number(onu_indication.serial_number) |
| 328 | |
| 329 | if serial_number_str == '000000000000': |
| 330 | self.log.debug('serial-number-was-not-provided-or-default-serial-number-provided-identifying-onu-by-onu_id') |
| 331 | #FIXME: if multiple PON ports onu_id is not a sufficient key |
| 332 | onu_device = self.adapter_agent.get_child_device( |
| 333 | self.device_id, |
| 334 | onu_id=onu_indication.onu_id) |
| 335 | else : |
| 336 | onu_device = self.adapter_agent.get_child_device( |
| 337 | self.device_id, |
| 338 | serial_number=serial_number_str) |
| 339 | |
| 340 | self.log.debug('onu-device', olt_device_id=self.device_id, device=onu_device) |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 341 | |
| 342 | # FIXME - handle serial_number mismatch |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 343 | # assert key is not None |
| 344 | # assert onu_device is not None |
| 345 | if onu_device is None: |
| 346 | self.log.warn('onu-device-is-none-invalid-message') |
Shad Ansari | 803900a | 2018-05-02 06:26:00 +0000 | [diff] [blame] | 347 | return |
| 348 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 349 | if onu_device.connect_status != ConnectStatus.REACHABLE: |
| 350 | onu_device.connect_status = ConnectStatus.REACHABLE |
| 351 | self.adapter_agent.update_device(onu_device) |
| 352 | |
Nicolas Palpacuer | 36a9344 | 2018-05-23 17:38:57 -0400 | [diff] [blame] | 353 | if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) != onu_indication.intf_id: |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 354 | self.log.warn('ONU-is-on-a-different-intf-id-now', |
Nicolas Palpacuer | 36a9344 | 2018-05-23 17:38:57 -0400 | [diff] [blame] | 355 | previous_intf_id=platform.intf_id_from_pon_port_no(onu_device.parent_port_no), |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 356 | current_intf_id=onu_indication.intf_id) |
| 357 | # FIXME - handle intf_id mismatch (ONU move?) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 358 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 359 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 360 | if onu_device.proxy_address.onu_id != onu_indication.onu_id: |
| 361 | # FIXME - handle onu id mismatch |
| 362 | self.log.warn('ONU-id-mismatch', expected_onu_id=onu_device.proxy_address.onu_id, |
| 363 | received_onu_id=onu_indication.onu_id) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 364 | |
Shad Ansari | 2292093 | 2018-05-17 00:33:34 +0000 | [diff] [blame] | 365 | uni_no = platform.mk_uni_port_num(onu_indication.intf_id, onu_indication.onu_id) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 366 | uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=serial_number_str) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 367 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 368 | self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 369 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 370 | #Admin state |
| 371 | if onu_indication.admin_state == 'down': |
| 372 | if onu_indication.oper_state != 'down': |
| 373 | self.log.error('ONU-admin-state-down-and-oper-status-not-down', oper_state=onu_indication.oper_state) |
| 374 | onu_indication.oper_state = 'down' # Forcing the oper state change code to execute |
| 375 | |
| 376 | if onu_device.admin_state != AdminState.DISABLED: |
| 377 | onu_device.admin_state = AdminState.DISABLED |
| 378 | self.adapter_agent.update(onu_device) |
| 379 | self.log.debug('putting-onu-in-disabled-state', onu_serial_number=onu_device.serial_number) |
| 380 | |
| 381 | #Port and logical port update is taken care of by oper state block |
| 382 | |
| 383 | elif onu_indication.admin_state == 'up': |
| 384 | if onu_device.admin_state != AdminState.ENABLED: |
| 385 | onu_device.admin_state = AdminState.ENABLED |
| 386 | self.adapter_agent.update(onu_device) |
| 387 | self.log.debug('putting-onu-in-enabled-state', onu_serial_number=onu_device.serial_number) |
| 388 | |
| 389 | else: |
| 390 | self.log.warn('Invalid-or-not-implemented-admin-state', received_admin_state=onu_indication.admin_state) |
| 391 | |
| 392 | self.log.debug('admin-state-dealt-with') |
| 393 | |
| 394 | #Operating state |
| 395 | if onu_indication.oper_state == 'down': |
| 396 | #Move to discovered state |
| 397 | self.log.debug('onu-oper-state-is-down') |
| 398 | |
| 399 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 400 | onu_device.oper_status = OperStatus.DISCOVERED |
| 401 | self.adapter_agent.update_device(onu_device) |
| 402 | #Set port oper state to Discovered |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 403 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 404 | self.onu_ports_down(onu_device, uni_no, uni_name, OperStatus.DISCOVERED) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 405 | |
| 406 | elif onu_indication.oper_state == 'up': |
| 407 | |
| 408 | if onu_device.oper_status != OperStatus.DISCOVERED: |
| 409 | self.log.debug("ignore onu indication", intf_id=onu_indication.intf_id, |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 410 | onu_id=onu_indication.onu_id, state=onu_device.oper_status, |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 411 | msg_oper_state=onu_indication.oper_state) |
| 412 | return |
| 413 | |
| 414 | #Device was in Discovered state, setting it to active |
| 415 | |
| 416 | |
| 417 | |
| 418 | onu_adapter_agent = registry('adapter_loader').get_agent(onu_device.adapter) |
| 419 | if onu_adapter_agent is None: |
| 420 | self.log.error('onu_adapter_agent-could-not-be-retrieved', onu_device=onu_device) |
| 421 | return |
| 422 | |
| 423 | #Prepare onu configuration |
| 424 | |
| 425 | # onu initialization, base configuration (bridge setup ...) |
| 426 | def onu_initialization(): |
| 427 | |
| 428 | #FIXME: that's definitely cheating |
| 429 | if onu_device.adapter == 'broadcom_onu': |
| 430 | onu_adapter_agent.adapter.devices_handlers[onu_device.id].message_exchange() |
| 431 | self.log.debug('broadcom-message-exchange-started') |
| 432 | |
| 433 | # tcont creation (onu) |
| 434 | tcont = TcontsConfigData() |
| 435 | tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id) |
| 436 | |
| 437 | # gem port creation |
| 438 | gem_port = GemportsConfigData() |
| 439 | gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id) |
| 440 | |
| 441 | #ports creation/update |
| 442 | def port_config(): |
| 443 | |
| 444 | # "v_enet" creation (olt) |
| 445 | |
| 446 | #add_port update port when it exists |
| 447 | self.adapter_agent.add_port( |
| 448 | self.device_id, |
| 449 | Port( |
| 450 | port_no=uni_no, |
| 451 | label=uni_name, |
| 452 | type=Port.ETHERNET_UNI, |
| 453 | admin_state=AdminState.ENABLED, |
| 454 | oper_status=OperStatus.ACTIVE)) |
| 455 | |
| 456 | # v_enet creation (onu) |
| 457 | |
| 458 | venet = VEnetConfig(name=uni_name) |
| 459 | venet.interface.name = uni_name |
| 460 | onu_adapter_agent.create_interface(onu_device, venet) |
| 461 | |
| 462 | # ONU device status update in the datastore |
| 463 | def onu_update_oper_status(): |
| 464 | onu_device.oper_status = OperStatus.ACTIVE |
| 465 | onu_device.connect_status = ConnectStatus.REACHABLE |
| 466 | self.adapter_agent.update_device(onu_device) |
| 467 | |
| 468 | # FIXME : the asynchronicity has to be taken care of properly |
| 469 | onu_initialization() |
| 470 | reactor.callLater(10, onu_adapter_agent.create_tcont, device=onu_device, |
| 471 | tcont_data=tcont, traffic_descriptor_data=None) |
| 472 | reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device, gem_port) |
| 473 | reactor.callLater(12, port_config) |
| 474 | reactor.callLater(12, onu_update_oper_status) |
| 475 | |
| 476 | else: |
| 477 | self.log.warn('Not-implemented-or-invalid-value-of-oper-state', oper_state=onu_indication.oper_state) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 478 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 479 | def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state): |
| 480 | # Set port oper state to Discovered |
| 481 | # add port will update port if it exists |
| 482 | self.adapter_agent.add_port( |
| 483 | self.device_id, |
| 484 | Port( |
| 485 | port_no=uni_no, |
| 486 | label=uni_name, |
| 487 | type=Port.ETHERNET_UNI, |
| 488 | admin_state=onu_device.admin_state, |
| 489 | oper_status=oper_state)) |
| 490 | |
| 491 | # Disable logical port |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 492 | onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id)) |
| 493 | onu_port_id = None |
| 494 | for onu_port in onu_ports: |
| 495 | if onu_port.port_no == uni_no: |
| 496 | onu_port_id = onu_port.label |
| 497 | if onu_port_id is None: |
| 498 | self.log.error('matching-onu-port-label-not-found', onu_id=onu_device.id, olt_id=self.device_id, |
| 499 | onu_ports=onu_ports) |
| 500 | return |
| 501 | try: |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 502 | onu_logical_port = self.adapter_agent.get_logical_port(logical_device_id=self.logical_device_id, |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 503 | port_id=onu_port_id) |
| 504 | onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 505 | self.adapter_agent.update_logical_port(logical_device_id=self.logical_device_id, port=onu_logical_port) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 506 | self.log.debug('cascading-oper-state-to-port-and-logical-port') |
| 507 | except KeyError as e: |
| 508 | self.log.error('matching-onu-port-label-invalid', onu_id=onu_device.id, olt_id=self.device_id, |
| 509 | onu_ports=onu_ports, onu_port_id=onu_port_id, error=e) |
| 510 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 511 | def omci_indication(self, omci_indication): |
| 512 | |
| 513 | self.log.debug("omci indication", intf_id=omci_indication.intf_id, |
| 514 | onu_id=omci_indication.onu_id) |
| 515 | |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 516 | onu_device = self.adapter_agent.get_child_device(self.device_id, |
| 517 | onu_id=omci_indication.onu_id) |
| 518 | |
| 519 | self.adapter_agent.receive_proxied_message(onu_device.proxy_address, |
| 520 | omci_indication.pkt) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 521 | |
Shad Ansari | 42db734 | 2018-04-25 21:39:46 +0000 | [diff] [blame] | 522 | def packet_indication(self, pkt_indication): |
| 523 | |
| 524 | self.log.debug("packet indication", intf_id=pkt_indication.intf_id, |
| 525 | gemport_id=pkt_indication.gemport_id, |
| 526 | flow_id=pkt_indication.flow_id) |
| 527 | |
Shad Ansari | 2292093 | 2018-05-17 00:33:34 +0000 | [diff] [blame] | 528 | onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id) |
| 529 | logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id, onu_id) |
Shad Ansari | 42db734 | 2018-04-25 21:39:46 +0000 | [diff] [blame] | 530 | |
| 531 | pkt = Ether(pkt_indication.pkt) |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 532 | kw = dict(logical_device_id=self.logical_device_id, |
| 533 | logical_port_no=logical_port_num) |
Shad Ansari | 42db734 | 2018-04-25 21:39:46 +0000 | [diff] [blame] | 534 | self.adapter_agent.send_packet_in(packet=str(pkt), **kw) |
| 535 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 536 | def olt_reachable(self): |
| 537 | device = self.adapter_agent.get_device(self.device_id) |
| 538 | device.connect_status = ConnectStatus.REACHABLE |
| 539 | self.adapter_agent.update_device(device) |
| 540 | # Not changing its child devices state, we cannot guaranty that |
| 541 | |
| 542 | def heartbeat(self): |
| 543 | |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 544 | self.channel_ready_future.result() # blocks till gRPC connection is complete |
| 545 | |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 546 | while self.heartbeat_thread_active: |
| 547 | |
| 548 | try: |
| 549 | heartbeat = self.stub.HeartbeatCheck(openolt_pb2.Empty(), timeout=GRPC_TIMEOUT) |
| 550 | except Exception as e: |
| 551 | self.heartbeat_miss += 1 |
| 552 | self.log.warn('heartbeat-miss', missed_heartbeat=self.heartbeat_miss, error=e) |
| 553 | if self.heartbeat_miss == MAX_HEARTBEAT_MISS: |
| 554 | self.log.error('lost-connectivity-to-olt') |
| 555 | #TODO : send alarm/notify monitoring system |
| 556 | # Using reactor to synchronize update |
| 557 | # flagging it as unreachable and in unknow state |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 558 | reactor.callFromThread(self.olt_down, oper_state=OperStatus.UNKNOWN, |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 559 | connect_state=ConnectStatus.UNREACHABLE) |
| 560 | |
| 561 | else: |
| 562 | # heartbeat received |
| 563 | if self.heartbeat_signature is None: |
| 564 | # Initialize heartbeat signature |
| 565 | self.heartbeat_signature = heartbeat.heartbeat_signature |
| 566 | self.log.debug('heartbeat-signature', device_id=self.device_id, |
| 567 | heartbeat_signature=self.heartbeat_signature) |
| 568 | # Check if signature is different |
| 569 | if self.heartbeat_signature != heartbeat.heartbeat_signature: |
| 570 | # OLT has rebooted |
| 571 | self.log.warn('OLT-was-rebooted', device_id=self.device_id) |
| 572 | #TODO: notify monitoring system |
| 573 | self.heartbeat_signature = heartbeat.heartbeat_signature |
| 574 | |
| 575 | else: |
| 576 | self.log.debug('valid-heartbeat-received') |
| 577 | |
| 578 | if self.heartbeat_miss > MAX_HEARTBEAT_MISS: |
| 579 | self.log.info('OLT-connection-restored') |
| 580 | #TODO : suppress alarm/notify monitoring system |
| 581 | # flagging it as reachable again |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 582 | reactor.callFromThread(self.olt_reachable) |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 583 | |
| 584 | if not self.indications_thread_active: |
nick | 369a506 | 2018-05-29 17:11:06 -0400 | [diff] [blame] | 585 | self.log.info('(re)starting-indications-thread') |
nick | 47b7437 | 2018-05-25 18:22:49 -0400 | [diff] [blame] | 586 | # reset indications thread |
| 587 | self.indications_thread = threading.Thread(target=self.process_indications) |
| 588 | self.indications_thread.setDaemon(True) |
| 589 | self.indications_thread_active = True |
| 590 | self.indications_thread.start() |
| 591 | |
| 592 | self.heartbeat_miss = 0 |
| 593 | |
| 594 | time.sleep(HEARTBEAT_PERIOD) |
| 595 | |
| 596 | self.log.debug('stopping-heartbeat-thread', device_id=self.device_id) |
| 597 | |
| 598 | |
| 599 | |
Shad Ansari | 42db734 | 2018-04-25 21:39:46 +0000 | [diff] [blame] | 600 | def packet_out(self, egress_port, msg): |
Shad Ansari | 0346f0d | 2018-04-26 06:54:09 +0000 | [diff] [blame] | 601 | pkt = Ether(msg) |
| 602 | self.log.info('packet out', egress_port=egress_port, |
| 603 | packet=str(pkt).encode("HEX")) |
| 604 | |
Nicolas Palpacuer | 7d90281 | 2018-06-07 16:17:09 -0400 | [diff] [blame^] | 605 | |
| 606 | # Find port type |
| 607 | egress_port_type = self.port_type(egress_port) |
| 608 | |
| 609 | if egress_port_type == Port.ETHERNET_UNI: |
| 610 | |
| 611 | if pkt.haslayer(Dot1Q): |
| 612 | outer_shim = pkt.getlayer(Dot1Q) |
| 613 | if isinstance(outer_shim.payload, Dot1Q): |
| 614 | # If double tag, remove the outer tag |
| 615 | payload = ( |
| 616 | Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) / |
| 617 | outer_shim.payload |
| 618 | ) |
| 619 | else: |
| 620 | payload = pkt |
Shad Ansari | 0346f0d | 2018-04-26 06:54:09 +0000 | [diff] [blame] | 621 | else: |
| 622 | payload = pkt |
Nicolas Palpacuer | 7d90281 | 2018-06-07 16:17:09 -0400 | [diff] [blame^] | 623 | |
| 624 | send_pkt = binascii.unhexlify(str(payload).encode("HEX")) |
| 625 | |
| 626 | self.log.info('sending-packet-to-ONU', egress_port=egress_port, |
| 627 | intf_id=platform.intf_id_from_pon_port_no(egress_port), |
| 628 | onu_id=platform.onu_id_from_port_num(egress_port), |
| 629 | packet=str(payload).encode("HEX")) |
| 630 | |
| 631 | onu_pkt = openolt_pb2.OnuPacket(intf_id=platform.intf_id_from_pon_port_no(egress_port), |
| 632 | onu_id=platform.onu_id_from_port_num(egress_port), pkt=send_pkt) |
| 633 | |
| 634 | self.stub.OnuPacketOut(onu_pkt) |
| 635 | |
| 636 | elif egress_port_type == Port.ETHERNET_NNI: |
| 637 | self.log.info('sending-packet-to-uplink', egress_port=egress_port, packet=str(pkt).encode("HEX")) |
| 638 | |
| 639 | send_pkt = binascii.unhexlify(str(pkt).encode("HEX")) |
| 640 | |
| 641 | uplink_pkt = openolt_pb2.UplinkPacket(intf_id=platform.intf_id_from_nni_port_num(egress_port), pkt=send_pkt) |
| 642 | |
| 643 | self.stub.UplinkPacketOut(uplink_pkt) |
| 644 | |
Shad Ansari | 0346f0d | 2018-04-26 06:54:09 +0000 | [diff] [blame] | 645 | else: |
Nicolas Palpacuer | 7d90281 | 2018-06-07 16:17:09 -0400 | [diff] [blame^] | 646 | self.log.warn('Packet-out-to-this-interface-type-not-implemented', egress_port=egress_port, |
| 647 | port_type=egress_port_type) |
Shad Ansari | 42db734 | 2018-04-25 21:39:46 +0000 | [diff] [blame] | 648 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 649 | def send_proxied_message(self, proxy_address, msg): |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 650 | omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id, # intf_id |
| 651 | onu_id=proxy_address.onu_id, pkt=str(msg)) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 652 | self.stub.OmciMsgOut(omci) |
| 653 | |
| 654 | def add_onu_device(self, intf_id, port_no, onu_id, serial_number): |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 655 | self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id, |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 656 | serial_number=serial_number) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 657 | |
| 658 | # NOTE - channel_id of onu is set to intf_id |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 659 | proxy_address = Device.ProxyAddress(device_id=self.device_id, |
| 660 | channel_id=intf_id, onu_id=onu_id, onu_session_id=onu_id) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 661 | |
| 662 | self.log.info("Adding ONU", proxy_address=proxy_address) |
| 663 | |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 664 | serial_number_str = self.stringify_serial_number(serial_number) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 665 | |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 666 | self.adapter_agent.add_onu_device(parent_device_id=self.device_id, |
| 667 | parent_port_no=port_no, vendor_id=serial_number.vendor_id, |
| 668 | proxy_address=proxy_address, root=True, |
| 669 | serial_number=serial_number_str, admin_state=AdminState.ENABLED) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 670 | |
Shad Ansari | 1fd9eb2 | 2018-05-15 05:13:49 +0000 | [diff] [blame] | 671 | def port_name(self, port_no, port_type, intf_id=None, serial_number=None): |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 672 | if port_type is Port.ETHERNET_NNI: |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 673 | return "nni-" + str(port_no) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 674 | elif port_type is Port.PON_OLT: |
Shad Ansari | 4a232ca | 2018-05-05 05:24:17 +0000 | [diff] [blame] | 675 | return "pon" + str(intf_id) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 676 | elif port_type is Port.ETHERNET_UNI: |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 677 | if serial_number is not None: |
| 678 | return serial_number |
| 679 | else: |
| 680 | return "uni-{}".format(port_no) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 681 | |
Nicolas Palpacuer | 7d90281 | 2018-06-07 16:17:09 -0400 | [diff] [blame^] | 682 | |
| 683 | def port_type(self, port_no): |
| 684 | ports = self.adapter_agent.get_ports(self.device_id) |
| 685 | for port in ports: |
| 686 | if port.port_no == port_no: |
| 687 | return port.type |
| 688 | return None |
| 689 | |
| 690 | |
Nicolas Palpacuer | cf735ac | 2018-06-06 11:12:53 -0400 | [diff] [blame] | 691 | def add_logical_port(self, port_no, intf_id, oper_state): |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 692 | self.log.info('adding-logical-port', port_no=port_no) |
| 693 | |
| 694 | label = self.port_name(port_no, Port.ETHERNET_NNI) |
| 695 | |
| 696 | cap = OFPPF_1GB_FD | OFPPF_FIBER |
| 697 | curr_speed = OFPPF_1GB_FD |
| 698 | max_speed = OFPPF_1GB_FD |
| 699 | |
Nicolas Palpacuer | cf735ac | 2018-06-06 11:12:53 -0400 | [diff] [blame] | 700 | if oper_state == OperStatus.ACTIVE: |
| 701 | of_oper_state = OFPPS_LIVE |
| 702 | else: |
| 703 | of_oper_state = OFPPS_LINK_DOWN |
| 704 | |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 705 | ofp = ofp_port(port_no=port_no, |
| 706 | hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no), |
Nicolas Palpacuer | cf735ac | 2018-06-06 11:12:53 -0400 | [diff] [blame] | 707 | name=label, config=0, state=of_oper_state, curr=cap, |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 708 | advertised=cap, peer=cap, curr_speed=curr_speed, |
| 709 | max_speed=max_speed) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 710 | |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 711 | logical_port = LogicalPort(id=label, ofp_port=ofp, |
| 712 | device_id=self.device_id, device_port_no=port_no, |
| 713 | root_port=True) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 714 | |
| 715 | self.adapter_agent.add_logical_port(self.logical_device_id, logical_port) |
| 716 | |
| 717 | def add_port(self, intf_id, port_type, oper_status): |
Shad Ansari | 2292093 | 2018-05-17 00:33:34 +0000 | [diff] [blame] | 718 | port_no = platform.intf_id_to_port_no(intf_id, port_type) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 719 | |
Shad Ansari | 4a232ca | 2018-05-05 05:24:17 +0000 | [diff] [blame] | 720 | label = self.port_name(port_no, port_type, intf_id) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 721 | |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 722 | self.log.info('adding-port', port_no=port_no, label=label, |
| 723 | port_type=port_type) |
| 724 | |
| 725 | port = Port(port_no=port_no, label=label, type=port_type, |
| 726 | admin_state=AdminState.ENABLED, oper_status=oper_status) |
| 727 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 728 | self.adapter_agent.add_port(self.device_id, port) |
Shad Ansari | 0efa651 | 2018-04-28 06:42:54 +0000 | [diff] [blame] | 729 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 730 | return port_no, label |
| 731 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 732 | def new_onu_id(self, intf_id): |
| 733 | onu_id = None |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 734 | onu_devices = self.adapter_agent.get_child_devices(self.device_id) |
Shad Ansari | 5dbc9c8 | 2018-05-10 03:29:31 +0000 | [diff] [blame] | 735 | for i in range(1, 512): |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 736 | id_not_taken = True |
| 737 | for child_device in onu_devices: |
| 738 | if child_device.proxy_address.onu_id == i: |
| 739 | id_not_taken = False |
| 740 | break |
| 741 | if id_not_taken: |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 742 | onu_id = i |
| 743 | break |
| 744 | return onu_id |
| 745 | |
| 746 | def stringify_vendor_specific(self, vendor_specific): |
| 747 | return ''.join(str(i) for i in [ |
Shad Ansari | 1fd9eb2 | 2018-05-15 05:13:49 +0000 | [diff] [blame] | 748 | hex(ord(vendor_specific[0])>>4 & 0x0f)[2:], |
| 749 | hex(ord(vendor_specific[0]) & 0x0f)[2:], |
| 750 | hex(ord(vendor_specific[1])>>4 & 0x0f)[2:], |
| 751 | hex(ord(vendor_specific[1]) & 0x0f)[2:], |
| 752 | hex(ord(vendor_specific[2])>>4 & 0x0f)[2:], |
| 753 | hex(ord(vendor_specific[2]) & 0x0f)[2:], |
| 754 | hex(ord(vendor_specific[3])>>4 & 0x0f)[2:], |
| 755 | hex(ord(vendor_specific[3]) & 0x0f)[2:]]) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 756 | |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 757 | |
| 758 | def update_flow_table(self, flows): |
| 759 | device = self.adapter_agent.get_device(self.device_id) |
Shad Ansari | cd2e8ff | 2018-05-11 20:26:22 +0000 | [diff] [blame] | 760 | self.log.debug('update flow table') |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 761 | in_port = None |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 762 | |
| 763 | for flow in flows: |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 764 | is_down_stream = None |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 765 | in_port = fd.get_in_port(flow) |
| 766 | assert in_port is not None |
| 767 | # Right now there is only one NNI port. Get the NNI PORT and compare |
| 768 | # with IN_PUT port number. Need to find better way. |
| 769 | ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI) |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 770 | |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 771 | for port in ports: |
| 772 | if (port.port_no == in_port): |
| 773 | self.log.info('downstream-flow') |
| 774 | is_down_stream = True |
| 775 | break |
| 776 | if is_down_stream is None: |
| 777 | is_down_stream = False |
| 778 | self.log.info('upstream-flow') |
Shad Ansari | 2825d01 | 2018-02-22 23:57:46 +0000 | [diff] [blame] | 779 | |
Shad Ansari | 2dda4f3 | 2018-05-17 07:16:07 +0000 | [diff] [blame] | 780 | for flow in flows: |
| 781 | try: |
| 782 | self.flow_mgr.add_flow(flow, is_down_stream) |
| 783 | except Exception as e: |
| 784 | self.log.exception('failed-to-install-flow', e=e, flow=flow) |
Shad Ansari | 89b09d5 | 2018-05-21 07:28:14 +0000 | [diff] [blame] | 785 | |
| 786 | # There has to be a better way to do this |
| 787 | def ip_hex(self, ip): |
| 788 | octets = ip.split(".") |
| 789 | hex_ip = [] |
| 790 | for octet in octets: |
| 791 | octet_hex = hex(int(octet)) |
| 792 | octet_hex = octet_hex.split('0x')[1] |
| 793 | octet_hex = octet_hex.rjust(2, '0') |
| 794 | hex_ip.append(octet_hex) |
| 795 | return ":".join(hex_ip) |
Nicolas Palpacuer | 65de6a4 | 2018-05-22 17:28:29 -0400 | [diff] [blame] | 796 | |
| 797 | def stringify_serial_number(self, serial_number): |
| 798 | return ''.join([serial_number.vendor_id, |
Shad Ansari | 5c596a5 | 2018-05-23 19:49:57 +0000 | [diff] [blame] | 799 | self.stringify_vendor_specific(serial_number.vendor_specific)]) |
Jonathan Davis | 0f917a2 | 2018-05-30 14:39:45 -0400 | [diff] [blame] | 800 | |
| 801 | def disable(self): |
| 802 | self.log.info('sending-deactivate-olt-message', device_id=self.device_id) |
| 803 | |
| 804 | # Send grpc call |
| 805 | #self.stub.DeactivateOlt(openolt_pb2.Empty()) |
| 806 | |
| 807 | # Soft deactivate. Turning down hardware should remove flows, but we are not doing that presently |
| 808 | |
| 809 | # Bring OLT down |
| 810 | self.olt_down(oper_state=OperStatus.UNKNOWN, admin_state=AdminState.DISABLED, |
| 811 | connect_state = ConnectStatus.UNREACHABLE) |
| 812 | |
| 813 | |
| 814 | def delete(self): |
| 815 | self.log.info('delete-olt', device_id=self.device_id) |
| 816 | |
| 817 | # Stop the grpc communication threads |
| 818 | self.log.info('stopping-grpc-threads', device_id=self.device_id) |
| 819 | self.indications_thread_active = False |
| 820 | self.heartbeat_thread_active = False |
| 821 | |
| 822 | # Close the grpc channel |
| 823 | # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id) |
| 824 | # self.channel.unsubscribe() |
| 825 | |
| 826 | self.log.info('successfully-deleted-olt', device_id=self.device_id) |
| 827 | |
| 828 | def reenable(self): |
| 829 | self.log.info('reenable-olt', device_id=self.device_id) |
| 830 | |
| 831 | # Bring up OLT |
| 832 | self.olt_up() |
| 833 | |
| 834 | # Enable all child devices |
| 835 | self.log.info('enabling-child-devices', device_id=self.device_id) |
| 836 | self.log.info('enabling-child-devices', olt_device_id=self.device_id) |
| 837 | self.adapter_agent.update_child_devices_state(parent_device_id=self.device_id, |
| 838 | admin_state=AdminState.ENABLED) |
| 839 | |
| 840 | # Set all ports to enabled |
| 841 | self.log.info('enabling-all-ports', device_id=self.device_id) |
| 842 | self.adapter_agent.enable_all_ports(self.device_id) |