blob: 42440637fd497ec85797787fd96394a096d46043 [file] [log] [blame]
Shad Ansari2825d012018-02-22 23:57:46 +00001#
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
17import structlog
18import threading
19import grpc
nick47b74372018-05-25 18:22:49 -040020import time
Shad Ansari2825d012018-02-22 23:57:46 +000021
Shad Ansari15928d12018-04-17 02:42:13 +000022from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000023from scapy.layers.l2 import Ether, Dot1Q
24import binascii
Shad Ansari22efe832018-05-19 05:37:03 +000025from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000026
Shad Ansari2825d012018-02-22 23:57:46 +000027from voltha.protos.device_pb2 import Port, Device
28from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
29from voltha.protos.logical_device_pb2 import LogicalDevice
Shad Ansarif9d2d102018-06-13 02:15:26 +000030from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
31 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, \
32 OFPC_TABLE_STATS, OFPC_FLOW_STATS, ofp_switch_features, ofp_port
33from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.core.logical_device_agent import mac_str_to_tuple
35from voltha.registry import registry
36from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040037from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
38from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
39from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
Shad Ansari2825d012018-02-22 23:57:46 +000040import voltha.core.flow_decomposer as fd
41
Shad Ansari22920932018-05-17 00:33:34 +000042import openolt_platform as platform
Shad Ansari2dda4f32018-05-17 07:16:07 +000043from openolt_flow_mgr import OpenOltFlowMgr
Shad Ansari801f7372018-04-27 02:15:41 +000044
nick47b74372018-05-25 18:22:49 -040045MAX_HEARTBEAT_MISS = 3
46HEARTBEAT_PERIOD = 1
47GRPC_TIMEOUT = 5
Shad Ansari2825d012018-02-22 23:57:46 +000048
49"""
50OpenoltDevice represents an OLT.
51"""
Shad Ansarif9d2d102018-06-13 02:15:26 +000052
53
Shad Ansari2825d012018-02-22 23:57:46 +000054class OpenoltDevice(object):
55
Shad Ansari22efe832018-05-19 05:37:03 +000056 states = ['up', 'down']
57 transitions = [
Shad Ansarif9d2d102018-06-13 02:15:26 +000058 {'trigger': 'olt_up', 'source': 'down', 'dest': 'up',
59 'before': 'olt_indication_up'},
60 {'trigger': 'olt_down', 'source': 'up', 'dest': 'down',
61 'before': 'olt_indication_down'}
Shad Ansari22efe832018-05-19 05:37:03 +000062 ]
63
Shad Ansari2825d012018-02-22 23:57:46 +000064 def __init__(self, **kwargs):
65 super(OpenoltDevice, self).__init__()
66
67 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000068 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000069 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040070 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000071 self.device_id = device.id
72 self.host_and_port = device.host_and_port
Shad Ansarif9d2d102018-06-13 02:15:26 +000073 self.log = structlog.get_logger(id=self.device_id,
74 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040075 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +000076
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040077 # Device already set in the event of reconciliation
78 if not is_reconciliation:
79 # It is a new device
80 # Update device
81 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +000082 device.serial_number = self.host_and_port # FIXME
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040083 device.connect_status = ConnectStatus.REACHABLE
84 device.oper_status = OperStatus.ACTIVATING
85 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +000086
Shad Ansari22efe832018-05-19 05:37:03 +000087 # Initialize the OLT state machine
88 self.machine = Machine(model=self, states=OpenoltDevice.states,
Shad Ansarif9d2d102018-06-13 02:15:26 +000089 transitions=OpenoltDevice.transitions,
90 send_event=True, initial='down',
91 ignore_invalid_triggers=True)
92 self.machine.add_transition(trigger='olt_ind_up', source='down',
93 dest='up')
94 self.machine.add_transition(trigger='olt_ind_loss', source='up',
95 dest='down')
Shad Ansari22efe832018-05-19 05:37:03 +000096
Shad Ansari2825d012018-02-22 23:57:46 +000097 # Initialize gRPC
98 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +000099 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400100 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
101
102 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub)
Shad Ansari2825d012018-02-22 23:57:46 +0000103
nick369a5062018-05-29 17:11:06 -0400104 # Indications thread plcaholder (started by heartbeat thread)
105 self.indications_thread = None
106 self.indications_thread_active = False
Shad Ansari2825d012018-02-22 23:57:46 +0000107
nick47b74372018-05-25 18:22:49 -0400108 # Start heartbeat thread
109 self.heartbeat_thread = threading.Thread(target=self.heartbeat)
110 self.heartbeat_thread.setDaemon(True)
111 self.heartbeat_thread_active = True
112 self.heartbeat_miss = 0
113 self.heartbeat_signature = None
114 self.heartbeat_thread.start()
115
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400116 self.log.debug('openolt-device-created', device_id=self.device_id)
117
Shad Ansari8f1b2532018-04-21 07:51:39 +0000118 def process_indications(self):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000119
nick47b74372018-05-25 18:22:49 -0400120 self.log.debug('starting-indications-thread')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000121
Shad Ansari15928d12018-04-17 02:42:13 +0000122 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000123
nick47b74372018-05-25 18:22:49 -0400124 while self.indications_thread_active:
125 try:
126 # get the next indication from olt
127 ind = next(self.indications)
128 except Exception as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000129 self.log.warn('GRPC-connection-lost-stoping-indication-thread',
130 error=e)
nick47b74372018-05-25 18:22:49 -0400131 self.indications_thread_active = False
132 else:
133 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000134
nick47b74372018-05-25 18:22:49 -0400135 # indication handlers run in the main event loop
136 if ind.HasField('olt_ind'):
137 reactor.callFromThread(self.olt_indication, ind.olt_ind)
138 elif ind.HasField('intf_ind'):
139 reactor.callFromThread(self.intf_indication, ind.intf_ind)
140 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000141 reactor.callFromThread(self.intf_oper_indication,
142 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400143 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000144 reactor.callFromThread(self.onu_discovery_indication,
145 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400146 elif ind.HasField('onu_ind'):
147 reactor.callFromThread(self.onu_indication, ind.onu_ind)
148 elif ind.HasField('omci_ind'):
149 reactor.callFromThread(self.omci_indication, ind.omci_ind)
150 elif ind.HasField('pkt_ind'):
151 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400152 elif ind.HasField('port_stats'):
153 reactor.callFromThread(self.port_statistics_indication,
154 ind.port_stats)
155 elif ind.HasField('flow_stats'):
156 reactor.callFromThread(self.flow_statistics_indication,
157 ind.flow_stats)
158 else:
159 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400160
161 self.log.debug('stopping-indications-thread', device_id=self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000162
163 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000164 if olt_indication.oper_state == "up":
165 self.olt_up(ind=olt_indication)
166 elif olt_indication.oper_state == "down":
167 self.olt_down(ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000168
Shad Ansari22efe832018-05-19 05:37:03 +0000169 def olt_indication_up(self, event):
170 olt_indication = event.kwargs.get('ind', None)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400171 self.log.debug("olt indication", olt_ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000172
nick47b74372018-05-25 18:22:49 -0400173 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000174
nick47b74372018-05-25 18:22:49 -0400175 # If logical device does not exist create it
176 if len(device.parent_id) == 0:
177
178 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
179
180 # Create logical OF device
181 ld = LogicalDevice(
182 root_device_id=self.device_id,
183 switch_features=ofp_switch_features(
184 n_buffers=256, # TODO fake for now
185 n_tables=2, # TODO ditto
186 capabilities=( # TODO and ditto
187 OFPC_FLOW_STATS
188 | OFPC_TABLE_STATS
189 | OFPC_PORT_STATS
190 | OFPC_GROUP_STATS
191 )
Jonathan Hart7687e0a2018-05-16 10:54:47 -0700192 )
193 )
Shad Ansarif9d2d102018-06-13 02:15:26 +0000194 ld_init = self.adapter_agent.create_logical_device(ld,
195 dpid=dpid)
196 self.logical_device_id = ld_init.id
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400197 else:
198 # logical device already exists
199 self.logical_device_id = device.parent_id
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000200
201 # Update phys OF device
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000202 device.parent_id = self.logical_device_id
203 device.oper_status = OperStatus.ACTIVE
204 self.adapter_agent.update_device(device)
205
Shad Ansari22efe832018-05-19 05:37:03 +0000206 def olt_indication_down(self, event):
207 olt_indication = event.kwargs.get('ind', None)
nick47b74372018-05-25 18:22:49 -0400208 new_admin_state = event.kwargs.get('admin_state', None)
209 new_oper_state = event.kwargs.get('oper_state', None)
210 new_connect_state = event.kwargs.get('connect_state', None)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000211 self.log.debug("olt indication", olt_ind=olt_indication,
212 admin_state=new_admin_state, oper_state=new_oper_state,
nick47b74372018-05-25 18:22:49 -0400213 connect_state=new_connect_state)
214
nick369a5062018-05-29 17:11:06 -0400215 # Propagating to the children
216
217 # Children ports
218 child_devices = self.adapter_agent.get_child_devices(self.device_id)
219 for onu_device in child_devices:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000220 uni_no = platform.mk_uni_port_num(
221 onu_device.proxy_address.channel_id,
222 onu_device.proxy_address.onu_id)
223 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
224 serial_number=onu_device.serial_number)
nick369a5062018-05-29 17:11:06 -0400225
226 self.onu_ports_down(onu_device, uni_no, uni_name, new_oper_state)
227 # Children devices
Shad Ansarif9d2d102018-06-13 02:15:26 +0000228 self.adapter_agent.update_child_devices_state(
229 self.device_id, oper_status=new_oper_state,
230 connect_status=ConnectStatus.UNREACHABLE,
231 admin_state=new_admin_state)
nick369a5062018-05-29 17:11:06 -0400232 # Device Ports
Shad Ansarif9d2d102018-06-13 02:15:26 +0000233 device_ports = self.adapter_agent.get_ports(self.device_id,
234 Port.ETHERNET_NNI)
nick369a5062018-05-29 17:11:06 -0400235 logical_ports_ids = [port.label for port in device_ports]
Shad Ansarif9d2d102018-06-13 02:15:26 +0000236 device_ports += self.adapter_agent.get_ports(self.device_id,
237 Port.PON_OLT)
nick369a5062018-05-29 17:11:06 -0400238
239 for port in device_ports:
240 if new_admin_state is not None:
241 port.admin_state = new_admin_state
242 if new_oper_state is not None:
243 port.oper_status = new_oper_state
244 self.adapter_agent.add_port(self.device_id, port)
245
246 # Device logical port
247 for logical_port_id in logical_ports_ids:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000248 logical_port = self.adapter_agent.get_logical_port(
249 self.logical_device_id, logical_port_id)
nick369a5062018-05-29 17:11:06 -0400250 logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000251 self.adapter_agent.update_logical_port(self.logical_device_id,
252 logical_port)
nick369a5062018-05-29 17:11:06 -0400253
254 # Device
nick47b74372018-05-25 18:22:49 -0400255 device = self.adapter_agent.get_device(self.device_id)
nick369a5062018-05-29 17:11:06 -0400256 if new_admin_state is not None:
nick47b74372018-05-25 18:22:49 -0400257 device.admin_state = new_admin_state
258 if new_oper_state is not None:
259 device.oper_status = new_oper_state
260 if new_connect_state is not None:
261 device.connect_status = new_connect_state
262
263 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000264
265 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400266 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000267 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000268
269 if intf_indication.oper_state == "up":
270 oper_status = OperStatus.ACTIVE
271 else:
272 oper_status = OperStatus.DISCOVERED
273
nick47b74372018-05-25 18:22:49 -0400274 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000275 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
276
277 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000278 self.log.debug("Received interface oper state change indication",
279 intf_id=intf_oper_indication.intf_id,
280 type=intf_oper_indication.type,
281 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000282
283 if intf_oper_indication.oper_state == "up":
284 oper_state = OperStatus.ACTIVE
285 else:
286 oper_state = OperStatus.DISCOVERED
287
288 if intf_oper_indication.type == "nni":
289
Shad Ansari0346f0d2018-04-26 06:54:09 +0000290 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000291 if intf_oper_indication.intf_id != 0:
292 return
293
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400294 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000295 port_no, label = self.add_port(intf_oper_indication.intf_id,
296 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400297 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000298 self.add_logical_port(port_no, intf_oper_indication.intf_id,
299 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000300
301 elif intf_oper_indication.type == "pon":
302 # FIXME - handle PON oper state change
303 pass
304
305 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000306 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000307 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000308
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400309 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000310
Shad Ansarif9d2d102018-06-13 02:15:26 +0000311 self.log.debug("onu discovery indication", intf_id=intf_id,
312 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400313
Shad Ansarif9d2d102018-06-13 02:15:26 +0000314 onu_device = self.adapter_agent.get_child_device(
315 self.device_id,
316 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400317
318 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000319 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000320 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000321 self.add_onu_device(
322 intf_id,
323 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
324 onu_id, serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400325 self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000326 serial_number=serial_number_str)
327 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
328 serial_number=serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400329 self.stub.ActivateOnu(onu)
330 except Exception as e:
331 self.log.exception('onu-activation-failed', e=e)
332
Shad Ansari2825d012018-02-22 23:57:46 +0000333 else:
nick47b74372018-05-25 18:22:49 -0400334 if onu_device.connect_status != ConnectStatus.REACHABLE:
335 onu_device.connect_status = ConnectStatus.REACHABLE
336 self.adapter_agent.update_device(onu_device)
337
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400338 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000339 if onu_device.oper_status == OperStatus.DISCOVERED \
340 or onu_device.oper_status == OperStatus.ACTIVATING:
341 self.log.debug("ignore onu discovery indication, \
342 the onu has been discovered and should be \
343 activating shorlty", intf_id=intf_id,
344 onu_id=onu_id, state=onu_device.oper_status)
345 elif onu_device.oper_status == OperStatus.ACTIVE:
346 self.log.warn("onu discovery indication whereas onu is \
347 supposed to be active",
348 intf_id=intf_id, onu_id=onu_id,
349 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400350 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000351 self.log.info("onu in unknown state, recovering from olt \
352 reboot, activate onu", intf_id=intf_id,
353 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400354
355 onu_device.oper_status = OperStatus.DISCOVERED
356 self.adapter_agent.update_device(onu_device)
357
Shad Ansarif9d2d102018-06-13 02:15:26 +0000358 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
359 serial_number=serial_number)
nick47b74372018-05-25 18:22:49 -0400360 self.stub.ActivateOnu(onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400361 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000362 self.log.warn('unexpected state', onu_id=onu_id,
363 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000364
Shad Ansari2825d012018-02-22 23:57:46 +0000365 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000366 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000367 onu_id=onu_indication.onu_id,
368 serial_number=onu_indication.serial_number,
369 oper_state=onu_indication.oper_state,
370 admin_state=onu_indication.admin_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000371
Shad Ansaria0b37892018-06-12 21:34:30 +0000372 if onu_indication.serial_number:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400373 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000374 self.device_id,
375 serial_number=self.stringify_serial_number(
376 onu_indication.serial_number))
Shad Ansarif9d2d102018-06-13 02:15:26 +0000377 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400378 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000379 self.device_id,
380 parent_port_no=platform.intf_id_to_port_no(
381 onu_indication.intf_id, Port.PON_OLT),
382 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400383
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400384 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000385 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000386 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000387 return
388
nick47b74372018-05-25 18:22:49 -0400389 if onu_device.connect_status != ConnectStatus.REACHABLE:
390 onu_device.connect_status = ConnectStatus.REACHABLE
391 self.adapter_agent.update_device(onu_device)
392
Shad Ansarif9d2d102018-06-13 02:15:26 +0000393 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
394 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400395 self.log.warn('ONU-is-on-a-different-intf-id-now',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000396 previous_intf_id=platform.intf_id_from_pon_port_no(
397 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400398 current_intf_id=onu_indication.intf_id)
399 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000400
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400401 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
402 # FIXME - handle onu id mismatch
Shad Ansarif9d2d102018-06-13 02:15:26 +0000403 self.log.warn('ONU-id-mismatch',
404 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400405 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000406
Shad Ansarif9d2d102018-06-13 02:15:26 +0000407 uni_no = platform.mk_uni_port_num(onu_indication.intf_id,
408 onu_indication.onu_id)
409 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
410 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000411
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400412 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000413
Shad Ansarif9d2d102018-06-13 02:15:26 +0000414 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400415 if onu_indication.admin_state == 'down':
416 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000417 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
418 oper_state=onu_indication.oper_state)
419 # Forcing the oper state change code to execute
420 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400421
422 if onu_device.admin_state != AdminState.DISABLED:
423 onu_device.admin_state = AdminState.DISABLED
424 self.adapter_agent.update(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000425 self.log.debug('putting-onu-in-disabled-state',
426 onu_serial_number=onu_device.serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400427
Shad Ansarif9d2d102018-06-13 02:15:26 +0000428 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400429
430 elif onu_indication.admin_state == 'up':
431 if onu_device.admin_state != AdminState.ENABLED:
432 onu_device.admin_state = AdminState.ENABLED
433 self.adapter_agent.update(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000434 self.log.debug('putting-onu-in-enabled-state',
435 onu_serial_number=onu_device.serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400436
437 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000438 self.log.warn('Invalid-or-not-implemented-admin-state',
439 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400440
441 self.log.debug('admin-state-dealt-with')
442
Shad Ansarif9d2d102018-06-13 02:15:26 +0000443 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400444 if onu_indication.oper_state == 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000445 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400446 self.log.debug('onu-oper-state-is-down')
447
448 if onu_device.oper_status != OperStatus.DISCOVERED:
449 onu_device.oper_status = OperStatus.DISCOVERED
450 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000451 # Set port oper state to Discovered
452 self.onu_ports_down(onu_device, uni_no, uni_name,
453 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400454
455 elif onu_indication.oper_state == 'up':
456
457 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000458 self.log.debug("ignore onu indication",
459 intf_id=onu_indication.intf_id,
460 onu_id=onu_indication.onu_id,
461 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400462 msg_oper_state=onu_indication.oper_state)
463 return
464
Shad Ansarif9d2d102018-06-13 02:15:26 +0000465 # Device was in Discovered state, setting it to active
466 onu_adapter_agent = \
467 registry('adapter_loader').get_agent(onu_device.adapter)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400468 if onu_adapter_agent is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000469 self.log.error('onu_adapter_agent-could-not-be-retrieved',
470 onu_device=onu_device)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400471 return
472
Shad Ansarif9d2d102018-06-13 02:15:26 +0000473 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400474
475 # onu initialization, base configuration (bridge setup ...)
476 def onu_initialization():
477
Shad Ansarif9d2d102018-06-13 02:15:26 +0000478 # FIXME: that's definitely cheating
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400479 if onu_device.adapter == 'broadcom_onu':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000480 onu_adapter_agent.adapter.devices_handlers[onu_device.id] \
481 .message_exchange()
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400482 self.log.debug('broadcom-message-exchange-started')
483
484 # tcont creation (onu)
485 tcont = TcontsConfigData()
486 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
487
488 # gem port creation
489 gem_port = GemportsConfigData()
490 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
491
Shad Ansarif9d2d102018-06-13 02:15:26 +0000492 # ports creation/update
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400493 def port_config():
494
495 # "v_enet" creation (olt)
496
Shad Ansarif9d2d102018-06-13 02:15:26 +0000497 # add_port update port when it exists
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400498 self.adapter_agent.add_port(
499 self.device_id,
500 Port(
501 port_no=uni_no,
502 label=uni_name,
503 type=Port.ETHERNET_UNI,
504 admin_state=AdminState.ENABLED,
505 oper_status=OperStatus.ACTIVE))
506
507 # v_enet creation (onu)
508
509 venet = VEnetConfig(name=uni_name)
510 venet.interface.name = uni_name
511 onu_adapter_agent.create_interface(onu_device, venet)
512
513 # ONU device status update in the datastore
514 def onu_update_oper_status():
515 onu_device.oper_status = OperStatus.ACTIVE
516 onu_device.connect_status = ConnectStatus.REACHABLE
517 self.adapter_agent.update_device(onu_device)
518
519 # FIXME : the asynchronicity has to be taken care of properly
520 onu_initialization()
Shad Ansarif9d2d102018-06-13 02:15:26 +0000521 reactor.callLater(10, onu_adapter_agent.create_tcont,
522 device=onu_device, tcont_data=tcont,
523 traffic_descriptor_data=None)
524 reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device,
525 gem_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400526 reactor.callLater(12, port_config)
527 reactor.callLater(12, onu_update_oper_status)
528
529 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000530 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
531 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000532
nick47b74372018-05-25 18:22:49 -0400533 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
534 # Set port oper state to Discovered
535 # add port will update port if it exists
536 self.adapter_agent.add_port(
537 self.device_id,
538 Port(
539 port_no=uni_no,
540 label=uni_name,
541 type=Port.ETHERNET_UNI,
542 admin_state=onu_device.admin_state,
543 oper_status=oper_state))
544
545 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400546 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
547 onu_port_id = None
548 for onu_port in onu_ports:
549 if onu_port.port_no == uni_no:
550 onu_port_id = onu_port.label
551 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000552 self.log.error('matching-onu-port-label-not-found',
553 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400554 onu_ports=onu_ports)
555 return
556 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000557 onu_logical_port = self.adapter_agent.get_logical_port(
558 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400559 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000560 self.adapter_agent.update_logical_port(
561 logical_device_id=self.logical_device_id,
562 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400563 self.log.debug('cascading-oper-state-to-port-and-logical-port')
564 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000565 self.log.error('matching-onu-port-label-invalid',
566 onu_id=onu_device.id, olt_id=self.device_id,
567 onu_ports=onu_ports, onu_port_id=onu_port_id,
568 error=e)
nick47b74372018-05-25 18:22:49 -0400569
Shad Ansari2825d012018-02-22 23:57:46 +0000570 def omci_indication(self, omci_indication):
571
572 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000573 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000574
Shad Ansarif9d2d102018-06-13 02:15:26 +0000575 onu_device = self.adapter_agent.get_child_device(
576 self.device_id, onu_id=omci_indication.onu_id)
Shad Ansari0efa6512018-04-28 06:42:54 +0000577
578 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000579 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000580
Shad Ansari42db7342018-04-25 21:39:46 +0000581 def packet_indication(self, pkt_indication):
582
583 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000584 gemport_id=pkt_indication.gemport_id,
585 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000586
Shad Ansari22920932018-05-17 00:33:34 +0000587 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000588 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
589 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000590
591 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000592 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000593 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000594 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
595
nick47b74372018-05-25 18:22:49 -0400596 def olt_reachable(self):
597 device = self.adapter_agent.get_device(self.device_id)
598 device.connect_status = ConnectStatus.REACHABLE
599 self.adapter_agent.update_device(device)
600 # Not changing its child devices state, we cannot guaranty that
601
602 def heartbeat(self):
603
Shad Ansarif9d2d102018-06-13 02:15:26 +0000604 # block till gRPC connection is complete
605 self.channel_ready_future.result()
nick369a5062018-05-29 17:11:06 -0400606
nick47b74372018-05-25 18:22:49 -0400607 while self.heartbeat_thread_active:
608
609 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000610 heartbeat = self.stub.HeartbeatCheck(openolt_pb2.Empty(),
611 timeout=GRPC_TIMEOUT)
nick47b74372018-05-25 18:22:49 -0400612 except Exception as e:
613 self.heartbeat_miss += 1
Shad Ansarif9d2d102018-06-13 02:15:26 +0000614 self.log.warn('heartbeat-miss',
615 missed_heartbeat=self.heartbeat_miss, error=e)
nick47b74372018-05-25 18:22:49 -0400616 if self.heartbeat_miss == MAX_HEARTBEAT_MISS:
617 self.log.error('lost-connectivity-to-olt')
Shad Ansarif9d2d102018-06-13 02:15:26 +0000618 # TODO : send alarm/notify monitoring system
nick47b74372018-05-25 18:22:49 -0400619 # Using reactor to synchronize update
620 # flagging it as unreachable and in unknow state
Shad Ansarif9d2d102018-06-13 02:15:26 +0000621 reactor.callFromThread(
622 self.olt_down,
623 oper_state=OperStatus.UNKNOWN,
624 connect_state=ConnectStatus.UNREACHABLE)
nick47b74372018-05-25 18:22:49 -0400625
626 else:
627 # heartbeat received
628 if self.heartbeat_signature is None:
629 # Initialize heartbeat signature
630 self.heartbeat_signature = heartbeat.heartbeat_signature
Shad Ansarif9d2d102018-06-13 02:15:26 +0000631 self.log.debug(
632 'heartbeat-signature',
633 device_id=self.device_id,
634 heartbeat_signature=self.heartbeat_signature)
nick47b74372018-05-25 18:22:49 -0400635 # Check if signature is different
636 if self.heartbeat_signature != heartbeat.heartbeat_signature:
637 # OLT has rebooted
638 self.log.warn('OLT-was-rebooted', device_id=self.device_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000639 # TODO: notify monitoring system
nick47b74372018-05-25 18:22:49 -0400640 self.heartbeat_signature = heartbeat.heartbeat_signature
641
642 else:
643 self.log.debug('valid-heartbeat-received')
644
645 if self.heartbeat_miss > MAX_HEARTBEAT_MISS:
646 self.log.info('OLT-connection-restored')
Shad Ansarif9d2d102018-06-13 02:15:26 +0000647 # TODO : suppress alarm/notify monitoring system
nick47b74372018-05-25 18:22:49 -0400648 # flagging it as reachable again
nick369a5062018-05-29 17:11:06 -0400649 reactor.callFromThread(self.olt_reachable)
nick47b74372018-05-25 18:22:49 -0400650
651 if not self.indications_thread_active:
nick369a5062018-05-29 17:11:06 -0400652 self.log.info('(re)starting-indications-thread')
nick47b74372018-05-25 18:22:49 -0400653 # reset indications thread
Shad Ansarif9d2d102018-06-13 02:15:26 +0000654 self.indications_thread = threading.Thread(
655 target=self.process_indications)
nick47b74372018-05-25 18:22:49 -0400656 self.indications_thread.setDaemon(True)
657 self.indications_thread_active = True
658 self.indications_thread.start()
659
660 self.heartbeat_miss = 0
661
662 time.sleep(HEARTBEAT_PERIOD)
663
664 self.log.debug('stopping-heartbeat-thread', device_id=self.device_id)
665
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400666 def port_statistics_indication(self, port_stats):
667 # TODO: send to kafka
668 # TODO : update ONOS counters
669 self.log.info('port-stats-collected', stats=port_stats)
670
671 def flow_statistics_indication(self, flow_stats):
672 # TODO: send to kafka
673 # TODO : update ONOS counters
674 self.log.info('flow-stats-collected', stats=flow_stats)
675
Shad Ansari42db7342018-04-25 21:39:46 +0000676 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000677 pkt = Ether(msg)
678 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000679 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400680
681 # Find port type
682 egress_port_type = self.port_type(egress_port)
683
684 if egress_port_type == Port.ETHERNET_UNI:
685
686 if pkt.haslayer(Dot1Q):
687 outer_shim = pkt.getlayer(Dot1Q)
688 if isinstance(outer_shim.payload, Dot1Q):
689 # If double tag, remove the outer tag
690 payload = (
691 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
692 outer_shim.payload
693 )
694 else:
695 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000696 else:
697 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400698
699 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
700
Shad Ansarif9d2d102018-06-13 02:15:26 +0000701 self.log.info(
702 'sending-packet-to-ONU', egress_port=egress_port,
703 intf_id=platform.intf_id_from_pon_port_no(egress_port),
704 onu_id=platform.onu_id_from_port_num(egress_port),
705 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400706
Shad Ansarif9d2d102018-06-13 02:15:26 +0000707 onu_pkt = openolt_pb2.OnuPacket(
708 intf_id=platform.intf_id_from_pon_port_no(egress_port),
709 onu_id=platform.onu_id_from_port_num(egress_port),
710 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400711
712 self.stub.OnuPacketOut(onu_pkt)
713
714 elif egress_port_type == Port.ETHERNET_NNI:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000715 self.log.info('sending-packet-to-uplink', egress_port=egress_port,
716 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400717
718 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
719
Shad Ansarif9d2d102018-06-13 02:15:26 +0000720 uplink_pkt = openolt_pb2.UplinkPacket(
721 intf_id=platform.intf_id_from_nni_port_num(egress_port),
722 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400723
724 self.stub.UplinkPacketOut(uplink_pkt)
725
Shad Ansari0346f0d2018-04-26 06:54:09 +0000726 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000727 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
728 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400729 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000730
Shad Ansari2825d012018-02-22 23:57:46 +0000731 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000732 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
733 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000734 self.stub.OmciMsgOut(omci)
735
736 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000737 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000738 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000739
740 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000741 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000742 channel_id=intf_id, onu_id=onu_id,
743 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000744
745 self.log.info("Adding ONU", proxy_address=proxy_address)
746
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400747 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000748
Shad Ansarif9d2d102018-06-13 02:15:26 +0000749 self.adapter_agent.add_onu_device(
750 parent_device_id=self.device_id, parent_port_no=port_no,
751 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
752 root=True, serial_number=serial_number_str,
753 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000754
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000755 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000756 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400757 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000758 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000759 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000760 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400761 if serial_number is not None:
762 return serial_number
763 else:
764 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000765
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400766 def port_type(self, port_no):
767 ports = self.adapter_agent.get_ports(self.device_id)
768 for port in ports:
769 if port.port_no == port_no:
770 return port.type
771 return None
772
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400773 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000774 self.log.info('adding-logical-port', port_no=port_no)
775
776 label = self.port_name(port_no, Port.ETHERNET_NNI)
777
778 cap = OFPPF_1GB_FD | OFPPF_FIBER
779 curr_speed = OFPPF_1GB_FD
780 max_speed = OFPPF_1GB_FD
781
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400782 if oper_state == OperStatus.ACTIVE:
783 of_oper_state = OFPPS_LIVE
784 else:
785 of_oper_state = OFPPS_LINK_DOWN
786
Shad Ansarif9d2d102018-06-13 02:15:26 +0000787 ofp = ofp_port(
788 port_no=port_no,
789 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
790 name=label, config=0, state=of_oper_state, curr=cap,
791 advertised=cap, peer=cap, curr_speed=curr_speed,
792 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000793
Shad Ansarif9d2d102018-06-13 02:15:26 +0000794 logical_port = LogicalPort(
795 id=label, ofp_port=ofp, device_id=self.device_id,
796 device_port_no=port_no, root_port=True)
Shad Ansari2825d012018-02-22 23:57:46 +0000797
Shad Ansarif9d2d102018-06-13 02:15:26 +0000798 self.adapter_agent.add_logical_port(self.logical_device_id,
799 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000800
801 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000802 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000803
Shad Ansari4a232ca2018-05-05 05:24:17 +0000804 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000805
Shad Ansari0efa6512018-04-28 06:42:54 +0000806 self.log.info('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000807 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000808
809 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000810 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000811
Shad Ansari2825d012018-02-22 23:57:46 +0000812 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000813
Shad Ansari2825d012018-02-22 23:57:46 +0000814 return port_no, label
815
Shad Ansari2825d012018-02-22 23:57:46 +0000816 def new_onu_id(self, intf_id):
817 onu_id = None
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400818 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000819 for i in range(1, 512):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400820 id_not_taken = True
821 for child_device in onu_devices:
822 if child_device.proxy_address.onu_id == i:
823 id_not_taken = False
824 break
825 if id_not_taken:
Shad Ansari2825d012018-02-22 23:57:46 +0000826 onu_id = i
827 break
828 return onu_id
829
830 def stringify_vendor_specific(self, vendor_specific):
831 return ''.join(str(i) for i in [
Shad Ansarif9d2d102018-06-13 02:15:26 +0000832 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000833 hex(ord(vendor_specific[0]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000834 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000835 hex(ord(vendor_specific[1]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000836 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000837 hex(ord(vendor_specific[2]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000838 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000839 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000840
Shad Ansari2825d012018-02-22 23:57:46 +0000841 def update_flow_table(self, flows):
842 device = self.adapter_agent.get_device(self.device_id)
Shad Ansaricd2e8ff2018-05-11 20:26:22 +0000843 self.log.debug('update flow table')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000844 in_port = None
Shad Ansari2825d012018-02-22 23:57:46 +0000845
846 for flow in flows:
Shad Ansari2825d012018-02-22 23:57:46 +0000847 is_down_stream = None
Shad Ansari2dda4f32018-05-17 07:16:07 +0000848 in_port = fd.get_in_port(flow)
849 assert in_port is not None
Shad Ansarif9d2d102018-06-13 02:15:26 +0000850 # Right now there is only one NNI port. Get the NNI PORT and
851 # compare with IN_PUT port number. Need to find better way.
Shad Ansari2dda4f32018-05-17 07:16:07 +0000852 ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI)
Shad Ansari2825d012018-02-22 23:57:46 +0000853
Shad Ansari2dda4f32018-05-17 07:16:07 +0000854 for port in ports:
855 if (port.port_no == in_port):
856 self.log.info('downstream-flow')
857 is_down_stream = True
858 break
859 if is_down_stream is None:
860 is_down_stream = False
861 self.log.info('upstream-flow')
Shad Ansari2825d012018-02-22 23:57:46 +0000862
Shad Ansari2dda4f32018-05-17 07:16:07 +0000863 for flow in flows:
864 try:
865 self.flow_mgr.add_flow(flow, is_down_stream)
866 except Exception as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000867 self.log.exception('failed-to-install-flow', e=e,
868 flow=flow)
Shad Ansari89b09d52018-05-21 07:28:14 +0000869
870 # There has to be a better way to do this
871 def ip_hex(self, ip):
872 octets = ip.split(".")
873 hex_ip = []
874 for octet in octets:
875 octet_hex = hex(int(octet))
876 octet_hex = octet_hex.split('0x')[1]
877 octet_hex = octet_hex.rjust(2, '0')
878 hex_ip.append(octet_hex)
879 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400880
881 def stringify_serial_number(self, serial_number):
882 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000883 self.stringify_vendor_specific(
884 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400885
886 def disable(self):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000887 self.log.info('sending-deactivate-olt-message',
888 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400889
890 # Send grpc call
Shad Ansarif9d2d102018-06-13 02:15:26 +0000891 # self.stub.DeactivateOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400892
Shad Ansarif9d2d102018-06-13 02:15:26 +0000893 # Soft deactivate. Turning down hardware should remove flows,
894 # but we are not doing that presently
Jonathan Davis0f917a22018-05-30 14:39:45 -0400895
896 # Bring OLT down
Shad Ansarif9d2d102018-06-13 02:15:26 +0000897 self.olt_down(oper_state=OperStatus.UNKNOWN,
898 admin_state=AdminState.DISABLED,
899 connect_state=ConnectStatus.UNREACHABLE)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400900
901 def delete(self):
902 self.log.info('delete-olt', device_id=self.device_id)
903
904 # Stop the grpc communication threads
905 self.log.info('stopping-grpc-threads', device_id=self.device_id)
906 self.indications_thread_active = False
907 self.heartbeat_thread_active = False
908
909 # Close the grpc channel
910 # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id)
911 # self.channel.unsubscribe()
912
913 self.log.info('successfully-deleted-olt', device_id=self.device_id)
914
915 def reenable(self):
916 self.log.info('reenable-olt', device_id=self.device_id)
917
918 # Bring up OLT
919 self.olt_up()
920
921 # Enable all child devices
922 self.log.info('enabling-child-devices', device_id=self.device_id)
923 self.log.info('enabling-child-devices', olt_device_id=self.device_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000924 self.adapter_agent.update_child_devices_state(
925 parent_device_id=self.device_id,
926 admin_state=AdminState.ENABLED)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400927
928 # Set all ports to enabled
929 self.log.info('enabling-all-ports', device_id=self.device_id)
Shad Ansaria0b37892018-06-12 21:34:30 +0000930 self.adapter_agent.enable_all_ports(self.device_id)