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