blob: 8dfa42bdd29e86e63bcb9a7522e7b3d282e61c1f [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
20import collections
nick47b74372018-05-25 18:22:49 -040021import time
Shad Ansari2825d012018-02-22 23:57:46 +000022
Shad Ansari15928d12018-04-17 02:42:13 +000023from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000024from scapy.layers.l2 import Ether, Dot1Q
25import binascii
Shad Ansari22efe832018-05-19 05:37:03 +000026from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000027
Shad Ansari2825d012018-02-22 23:57:46 +000028from voltha.protos.device_pb2 import Port, Device
29from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
30from voltha.protos.logical_device_pb2 import LogicalDevice
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040031from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, OFPPS_LINK_DOWN, \
Shad Ansari2825d012018-02-22 23:57:46 +000032 OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, \
Shad Ansari2dda4f32018-05-17 07:16:07 +000033 OFPC_FLOW_STATS, ofp_switch_features, ofp_port
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.protos.logical_device_pb2 import LogicalPort, LogicalDevice
35from voltha.core.logical_device_agent import mac_str_to_tuple
36from voltha.registry import registry
37from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040038from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
39from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
40from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
Shad Ansari2825d012018-02-22 23:57:46 +000041import voltha.core.flow_decomposer as fd
42
Shad Ansari22920932018-05-17 00:33:34 +000043import openolt_platform as platform
Shad Ansari2dda4f32018-05-17 07:16:07 +000044from openolt_flow_mgr import OpenOltFlowMgr
Shad Ansari801f7372018-04-27 02:15:41 +000045
nick47b74372018-05-25 18:22:49 -040046MAX_HEARTBEAT_MISS = 3
47HEARTBEAT_PERIOD = 1
48GRPC_TIMEOUT = 5
Shad Ansari2825d012018-02-22 23:57:46 +000049
50"""
51OpenoltDevice represents an OLT.
52"""
53class OpenoltDevice(object):
54
Shad Ansari22efe832018-05-19 05:37:03 +000055 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 Ansari2825d012018-02-22 23:57:46 +000061 def __init__(self, **kwargs):
62 super(OpenoltDevice, self).__init__()
63
64 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000065 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000066 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040067 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000068 self.device_id = device.id
69 self.host_and_port = device.host_and_port
70 self.log = structlog.get_logger(id=self.device_id, ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040071 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +000072
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040073 # Device already set in the event of reconciliation
74 if not is_reconciliation:
75 # It is a new device
76 # Update device
77 device.root = True
78 device.serial_number = self.host_and_port # FIXME
79 device.connect_status = ConnectStatus.REACHABLE
80 device.oper_status = OperStatus.ACTIVATING
81 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +000082
Shad Ansari22efe832018-05-19 05:37:03 +000083 # Initialize the OLT state machine
84 self.machine = Machine(model=self, states=OpenoltDevice.states,
85 transitions=OpenoltDevice.transitions,
86 send_event=True, initial='down', ignore_invalid_triggers=True)
87 self.machine.add_transition(trigger='olt_ind_up', source='down', dest='up')
88 self.machine.add_transition(trigger='olt_ind_loss', source='up', dest='down')
89
Shad Ansari2825d012018-02-22 23:57:46 +000090 # Initialize gRPC
91 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +000092 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -040093 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
94
95 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub)
Shad Ansari2825d012018-02-22 23:57:46 +000096
nick369a5062018-05-29 17:11:06 -040097 # Indications thread plcaholder (started by heartbeat thread)
98 self.indications_thread = None
99 self.indications_thread_active = False
Shad Ansari2825d012018-02-22 23:57:46 +0000100
nick47b74372018-05-25 18:22:49 -0400101 # Start heartbeat thread
102 self.heartbeat_thread = threading.Thread(target=self.heartbeat)
103 self.heartbeat_thread.setDaemon(True)
104 self.heartbeat_thread_active = True
105 self.heartbeat_miss = 0
106 self.heartbeat_signature = None
107 self.heartbeat_thread.start()
108
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400109 self.log.debug('openolt-device-created', device_id=self.device_id)
110
111
Shad Ansari8f1b2532018-04-21 07:51:39 +0000112 def process_indications(self):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000113
nick47b74372018-05-25 18:22:49 -0400114 self.log.debug('starting-indications-thread')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000115
Shad Ansari15928d12018-04-17 02:42:13 +0000116 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000117
nick47b74372018-05-25 18:22:49 -0400118 while self.indications_thread_active:
119 try:
120 # get the next indication from olt
121 ind = next(self.indications)
122 except Exception as e:
123 self.log.warn('GRPC-connection-lost-stoping-indications-thread', error=e)
124 self.indications_thread_active = False
125 else:
126 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000127
nick47b74372018-05-25 18:22:49 -0400128 # indication handlers run in the main event loop
129 if ind.HasField('olt_ind'):
130 reactor.callFromThread(self.olt_indication, ind.olt_ind)
131 elif ind.HasField('intf_ind'):
132 reactor.callFromThread(self.intf_indication, ind.intf_ind)
133 elif ind.HasField('intf_oper_ind'):
134 reactor.callFromThread(self.intf_oper_indication, ind.intf_oper_ind)
135 elif ind.HasField('onu_disc_ind'):
136 reactor.callFromThread(self.onu_discovery_indication, ind.onu_disc_ind)
137 elif ind.HasField('onu_ind'):
138 reactor.callFromThread(self.onu_indication, ind.onu_ind)
139 elif ind.HasField('omci_ind'):
140 reactor.callFromThread(self.omci_indication, ind.omci_ind)
141 elif ind.HasField('pkt_ind'):
142 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
143
144 self.log.debug('stopping-indications-thread', device_id=self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000145
146 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000147 if olt_indication.oper_state == "up":
148 self.olt_up(ind=olt_indication)
149 elif olt_indication.oper_state == "down":
150 self.olt_down(ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000151
Shad Ansari22efe832018-05-19 05:37:03 +0000152 def olt_indication_up(self, event):
153 olt_indication = event.kwargs.get('ind', None)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400154 self.log.debug("olt indication", olt_ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000155
nick47b74372018-05-25 18:22:49 -0400156 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000157
nick47b74372018-05-25 18:22:49 -0400158 # If logical device does not exist create it
159 if len(device.parent_id) == 0:
160
161 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
162
163 # Create logical OF device
164 ld = LogicalDevice(
165 root_device_id=self.device_id,
166 switch_features=ofp_switch_features(
167 n_buffers=256, # TODO fake for now
168 n_tables=2, # TODO ditto
169 capabilities=( # TODO and ditto
170 OFPC_FLOW_STATS
171 | OFPC_TABLE_STATS
172 | OFPC_PORT_STATS
173 | OFPC_GROUP_STATS
174 )
Jonathan Hart7687e0a2018-05-16 10:54:47 -0700175 )
176 )
nick47b74372018-05-25 18:22:49 -0400177 ld_initialized = self.adapter_agent.create_logical_device(ld, dpid=dpid)
178 self.logical_device_id = ld_initialized.id
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400179 else:
180 # logical device already exists
181 self.logical_device_id = device.parent_id
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000182
183 # Update phys OF device
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000184 device.parent_id = self.logical_device_id
185 device.oper_status = OperStatus.ACTIVE
186 self.adapter_agent.update_device(device)
187
Shad Ansari22efe832018-05-19 05:37:03 +0000188 def olt_indication_down(self, event):
189 olt_indication = event.kwargs.get('ind', None)
nick47b74372018-05-25 18:22:49 -0400190 new_admin_state = event.kwargs.get('admin_state', None)
191 new_oper_state = event.kwargs.get('oper_state', None)
192 new_connect_state = event.kwargs.get('connect_state', None)
193 self.log.debug("olt indication", olt_ind=olt_indication, admin_state=new_admin_state, oper_state=new_oper_state,
194 connect_state=new_connect_state)
195
nick369a5062018-05-29 17:11:06 -0400196 # Propagating to the children
197
198 # Children ports
199 child_devices = self.adapter_agent.get_child_devices(self.device_id)
200 for onu_device in child_devices:
201 uni_no = platform.mk_uni_port_num(onu_device.proxy_address.channel_id, onu_device.proxy_address.onu_id)
202 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=onu_device.serial_number)
203
204 self.onu_ports_down(onu_device, uni_no, uni_name, new_oper_state)
205 # Children devices
206 self.adapter_agent.update_child_devices_state(self.device_id, oper_status=new_oper_state,
207 connect_status=ConnectStatus.UNREACHABLE,
208 admin_state=new_admin_state)
209 # Device Ports
210 device_ports = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI)
211 logical_ports_ids = [port.label for port in device_ports]
212 device_ports += self.adapter_agent.get_ports(self.device_id, Port.PON_OLT)
213
214 for port in device_ports:
215 if new_admin_state is not None:
216 port.admin_state = new_admin_state
217 if new_oper_state is not None:
218 port.oper_status = new_oper_state
219 self.adapter_agent.add_port(self.device_id, port)
220
221 # Device logical port
222 for logical_port_id in logical_ports_ids:
223 logical_port = self.adapter_agent.get_logical_port(self.logical_device_id, logical_port_id)
224 logical_port.ofp_port.state = OFPPS_LINK_DOWN
225 self.adapter_agent.update_logical_port(self.logical_device_id, logical_port)
226
227 # Device
nick47b74372018-05-25 18:22:49 -0400228 device = self.adapter_agent.get_device(self.device_id)
nick369a5062018-05-29 17:11:06 -0400229 if new_admin_state is not None:
nick47b74372018-05-25 18:22:49 -0400230 device.admin_state = new_admin_state
231 if new_oper_state is not None:
232 device.oper_status = new_oper_state
233 if new_connect_state is not None:
234 device.connect_status = new_connect_state
235
236 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000237
238 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400239 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansari2825d012018-02-22 23:57:46 +0000240 oper_state=intf_indication.oper_state)
241
242 if intf_indication.oper_state == "up":
243 oper_status = OperStatus.ACTIVE
244 else:
245 oper_status = OperStatus.DISCOVERED
246
nick47b74372018-05-25 18:22:49 -0400247 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000248 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
249
250 def intf_oper_indication(self, intf_oper_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400251 self.log.debug("Received interface oper state change indication", intf_id=intf_oper_indication.intf_id,
252 type=intf_oper_indication.type, oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000253
254 if intf_oper_indication.oper_state == "up":
255 oper_state = OperStatus.ACTIVE
256 else:
257 oper_state = OperStatus.DISCOVERED
258
259 if intf_oper_indication.type == "nni":
260
Shad Ansari0346f0d2018-04-26 06:54:09 +0000261 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000262 if intf_oper_indication.intf_id != 0:
263 return
264
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400265 # add_(logical_)port update the port if it exists
266 port_no, label = self.add_port(intf_oper_indication.intf_id, Port.ETHERNET_NNI, oper_state)
267 self.log.debug("int_oper_indication", port_no=port_no, label=label)
268 self.add_logical_port(port_no, intf_oper_indication.intf_id, oper_state)
269
Shad Ansari2825d012018-02-22 23:57:46 +0000270
271 elif intf_oper_indication.type == "pon":
272 # FIXME - handle PON oper state change
273 pass
274
275 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000276 intf_id = onu_disc_indication.intf_id
277 serial_number=onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000278
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400279 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000280
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400281 self.log.debug("onu discovery indication", intf_id=intf_id, serial_number=serial_number_str)
282
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400283 onu_device = self.adapter_agent.get_child_device(self.device_id, serial_number=serial_number_str)
284
285 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000286 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000287 try:
Shad Ansari803900a2018-05-02 06:26:00 +0000288 self.add_onu_device(intf_id,
Shad Ansari22920932018-05-17 00:33:34 +0000289 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
Shad Ansari803900a2018-05-02 06:26:00 +0000290 onu_id, serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400291 self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400292 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400293 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,serial_number=serial_number)
294 self.stub.ActivateOnu(onu)
295 except Exception as e:
296 self.log.exception('onu-activation-failed', e=e)
297
Shad Ansari2825d012018-02-22 23:57:46 +0000298 else:
nick47b74372018-05-25 18:22:49 -0400299 if onu_device.connect_status != ConnectStatus.REACHABLE:
300 onu_device.connect_status = ConnectStatus.REACHABLE
301 self.adapter_agent.update_device(onu_device)
302
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400303 onu_id = onu_device.proxy_address.onu_id
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400304 if onu_device.oper_status == OperStatus.DISCOVERED or onu_device.oper_status == OperStatus.ACTIVATING:
305 self.log.debug("ignore onu discovery indication, the onu has been discovered and should be \
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400306 activating shorlty", intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status)
307 elif onu_device.oper_status== OperStatus.ACTIVE:
308 self.log.warn("onu discovery indication whereas onu is supposed to be active",
309 intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400310 elif onu_device.oper_status == OperStatus.UNKNOWN:
311 self.log.info("onu-in-unknow-state-recovering-form-olt-reboot-activate-onu", intf_id=intf_id, onu_id=onu_id,
312 serial_number=serial_number_str)
313
314 onu_device.oper_status = OperStatus.DISCOVERED
315 self.adapter_agent.update_device(onu_device)
316
317 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id, serial_number=serial_number)
318 self.stub.ActivateOnu(onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400319 else:
320 self.log.warn('unexpected state', onu_id=onu_id, onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000321
Shad Ansari2825d012018-02-22 23:57:46 +0000322 def onu_indication(self, onu_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400323 self.log.debug("onu-indication", intf_id=onu_indication.intf_id,
324 onu_id=onu_indication.onu_id, serial_number=onu_indication.serial_number,
325 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000326
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400327 serial_number_str = self.stringify_serial_number(onu_indication.serial_number)
328
329 if serial_number_str == '000000000000':
330 self.log.debug('serial-number-was-not-provided-or-default-serial-number-provided-identifying-onu-by-onu_id')
331 #FIXME: if multiple PON ports onu_id is not a sufficient key
332 onu_device = self.adapter_agent.get_child_device(
333 self.device_id,
334 onu_id=onu_indication.onu_id)
335 else :
336 onu_device = self.adapter_agent.get_child_device(
337 self.device_id,
338 serial_number=serial_number_str)
339
340 self.log.debug('onu-device', olt_device_id=self.device_id, device=onu_device)
Shad Ansari803900a2018-05-02 06:26:00 +0000341
342 # FIXME - handle serial_number mismatch
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400343 # assert key is not None
344 # assert onu_device is not None
345 if onu_device is None:
346 self.log.warn('onu-device-is-none-invalid-message')
Shad Ansari803900a2018-05-02 06:26:00 +0000347 return
348
nick47b74372018-05-25 18:22:49 -0400349 if onu_device.connect_status != ConnectStatus.REACHABLE:
350 onu_device.connect_status = ConnectStatus.REACHABLE
351 self.adapter_agent.update_device(onu_device)
352
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400353 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400354 self.log.warn('ONU-is-on-a-different-intf-id-now',
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400355 previous_intf_id=platform.intf_id_from_pon_port_no(onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400356 current_intf_id=onu_indication.intf_id)
357 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000358
Shad Ansari2825d012018-02-22 23:57:46 +0000359
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400360 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
361 # FIXME - handle onu id mismatch
362 self.log.warn('ONU-id-mismatch', expected_onu_id=onu_device.proxy_address.onu_id,
363 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000364
Shad Ansari22920932018-05-17 00:33:34 +0000365 uni_no = platform.mk_uni_port_num(onu_indication.intf_id, onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400366 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=serial_number_str)
Shad Ansari2825d012018-02-22 23:57:46 +0000367
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400368 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000369
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400370 #Admin state
371 if onu_indication.admin_state == 'down':
372 if onu_indication.oper_state != 'down':
373 self.log.error('ONU-admin-state-down-and-oper-status-not-down', oper_state=onu_indication.oper_state)
374 onu_indication.oper_state = 'down' # Forcing the oper state change code to execute
375
376 if onu_device.admin_state != AdminState.DISABLED:
377 onu_device.admin_state = AdminState.DISABLED
378 self.adapter_agent.update(onu_device)
379 self.log.debug('putting-onu-in-disabled-state', onu_serial_number=onu_device.serial_number)
380
381 #Port and logical port update is taken care of by oper state block
382
383 elif onu_indication.admin_state == 'up':
384 if onu_device.admin_state != AdminState.ENABLED:
385 onu_device.admin_state = AdminState.ENABLED
386 self.adapter_agent.update(onu_device)
387 self.log.debug('putting-onu-in-enabled-state', onu_serial_number=onu_device.serial_number)
388
389 else:
390 self.log.warn('Invalid-or-not-implemented-admin-state', received_admin_state=onu_indication.admin_state)
391
392 self.log.debug('admin-state-dealt-with')
393
394 #Operating state
395 if onu_indication.oper_state == 'down':
396 #Move to discovered state
397 self.log.debug('onu-oper-state-is-down')
398
399 if onu_device.oper_status != OperStatus.DISCOVERED:
400 onu_device.oper_status = OperStatus.DISCOVERED
401 self.adapter_agent.update_device(onu_device)
402 #Set port oper state to Discovered
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400403
nick47b74372018-05-25 18:22:49 -0400404 self.onu_ports_down(onu_device, uni_no, uni_name, OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400405
406 elif onu_indication.oper_state == 'up':
407
408 if onu_device.oper_status != OperStatus.DISCOVERED:
409 self.log.debug("ignore onu indication", intf_id=onu_indication.intf_id,
nick47b74372018-05-25 18:22:49 -0400410 onu_id=onu_indication.onu_id, state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400411 msg_oper_state=onu_indication.oper_state)
412 return
413
414 #Device was in Discovered state, setting it to active
415
416
417
418 onu_adapter_agent = registry('adapter_loader').get_agent(onu_device.adapter)
419 if onu_adapter_agent is None:
420 self.log.error('onu_adapter_agent-could-not-be-retrieved', onu_device=onu_device)
421 return
422
423 #Prepare onu configuration
424
425 # onu initialization, base configuration (bridge setup ...)
426 def onu_initialization():
427
428 #FIXME: that's definitely cheating
429 if onu_device.adapter == 'broadcom_onu':
430 onu_adapter_agent.adapter.devices_handlers[onu_device.id].message_exchange()
431 self.log.debug('broadcom-message-exchange-started')
432
433 # tcont creation (onu)
434 tcont = TcontsConfigData()
435 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
436
437 # gem port creation
438 gem_port = GemportsConfigData()
439 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
440
441 #ports creation/update
442 def port_config():
443
444 # "v_enet" creation (olt)
445
446 #add_port update port when it exists
447 self.adapter_agent.add_port(
448 self.device_id,
449 Port(
450 port_no=uni_no,
451 label=uni_name,
452 type=Port.ETHERNET_UNI,
453 admin_state=AdminState.ENABLED,
454 oper_status=OperStatus.ACTIVE))
455
456 # v_enet creation (onu)
457
458 venet = VEnetConfig(name=uni_name)
459 venet.interface.name = uni_name
460 onu_adapter_agent.create_interface(onu_device, venet)
461
462 # ONU device status update in the datastore
463 def onu_update_oper_status():
464 onu_device.oper_status = OperStatus.ACTIVE
465 onu_device.connect_status = ConnectStatus.REACHABLE
466 self.adapter_agent.update_device(onu_device)
467
468 # FIXME : the asynchronicity has to be taken care of properly
469 onu_initialization()
470 reactor.callLater(10, onu_adapter_agent.create_tcont, device=onu_device,
471 tcont_data=tcont, traffic_descriptor_data=None)
472 reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device, gem_port)
473 reactor.callLater(12, port_config)
474 reactor.callLater(12, onu_update_oper_status)
475
476 else:
477 self.log.warn('Not-implemented-or-invalid-value-of-oper-state', oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000478
nick47b74372018-05-25 18:22:49 -0400479 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
480 # Set port oper state to Discovered
481 # add port will update port if it exists
482 self.adapter_agent.add_port(
483 self.device_id,
484 Port(
485 port_no=uni_no,
486 label=uni_name,
487 type=Port.ETHERNET_UNI,
488 admin_state=onu_device.admin_state,
489 oper_status=oper_state))
490
491 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400492 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
493 onu_port_id = None
494 for onu_port in onu_ports:
495 if onu_port.port_no == uni_no:
496 onu_port_id = onu_port.label
497 if onu_port_id is None:
498 self.log.error('matching-onu-port-label-not-found', onu_id=onu_device.id, olt_id=self.device_id,
499 onu_ports=onu_ports)
500 return
501 try:
nick369a5062018-05-29 17:11:06 -0400502 onu_logical_port = self.adapter_agent.get_logical_port(logical_device_id=self.logical_device_id,
nick47b74372018-05-25 18:22:49 -0400503 port_id=onu_port_id)
504 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
nick369a5062018-05-29 17:11:06 -0400505 self.adapter_agent.update_logical_port(logical_device_id=self.logical_device_id, port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400506 self.log.debug('cascading-oper-state-to-port-and-logical-port')
507 except KeyError as e:
508 self.log.error('matching-onu-port-label-invalid', onu_id=onu_device.id, olt_id=self.device_id,
509 onu_ports=onu_ports, onu_port_id=onu_port_id, error=e)
510
Shad Ansari2825d012018-02-22 23:57:46 +0000511 def omci_indication(self, omci_indication):
512
513 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
514 onu_id=omci_indication.onu_id)
515
Shad Ansari0efa6512018-04-28 06:42:54 +0000516 onu_device = self.adapter_agent.get_child_device(self.device_id,
517 onu_id=omci_indication.onu_id)
518
519 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
520 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000521
Shad Ansari42db7342018-04-25 21:39:46 +0000522 def packet_indication(self, pkt_indication):
523
524 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
525 gemport_id=pkt_indication.gemport_id,
526 flow_id=pkt_indication.flow_id)
527
Shad Ansari22920932018-05-17 00:33:34 +0000528 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
529 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id, onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000530
531 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000532 kw = dict(logical_device_id=self.logical_device_id,
533 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000534 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
535
nick47b74372018-05-25 18:22:49 -0400536 def olt_reachable(self):
537 device = self.adapter_agent.get_device(self.device_id)
538 device.connect_status = ConnectStatus.REACHABLE
539 self.adapter_agent.update_device(device)
540 # Not changing its child devices state, we cannot guaranty that
541
542 def heartbeat(self):
543
nick369a5062018-05-29 17:11:06 -0400544 self.channel_ready_future.result() # blocks till gRPC connection is complete
545
nick47b74372018-05-25 18:22:49 -0400546 while self.heartbeat_thread_active:
547
548 try:
549 heartbeat = self.stub.HeartbeatCheck(openolt_pb2.Empty(), timeout=GRPC_TIMEOUT)
550 except Exception as e:
551 self.heartbeat_miss += 1
552 self.log.warn('heartbeat-miss', missed_heartbeat=self.heartbeat_miss, error=e)
553 if self.heartbeat_miss == MAX_HEARTBEAT_MISS:
554 self.log.error('lost-connectivity-to-olt')
555 #TODO : send alarm/notify monitoring system
556 # Using reactor to synchronize update
557 # flagging it as unreachable and in unknow state
nick369a5062018-05-29 17:11:06 -0400558 reactor.callFromThread(self.olt_down, oper_state=OperStatus.UNKNOWN,
nick47b74372018-05-25 18:22:49 -0400559 connect_state=ConnectStatus.UNREACHABLE)
560
561 else:
562 # heartbeat received
563 if self.heartbeat_signature is None:
564 # Initialize heartbeat signature
565 self.heartbeat_signature = heartbeat.heartbeat_signature
566 self.log.debug('heartbeat-signature', device_id=self.device_id,
567 heartbeat_signature=self.heartbeat_signature)
568 # Check if signature is different
569 if self.heartbeat_signature != heartbeat.heartbeat_signature:
570 # OLT has rebooted
571 self.log.warn('OLT-was-rebooted', device_id=self.device_id)
572 #TODO: notify monitoring system
573 self.heartbeat_signature = heartbeat.heartbeat_signature
574
575 else:
576 self.log.debug('valid-heartbeat-received')
577
578 if self.heartbeat_miss > MAX_HEARTBEAT_MISS:
579 self.log.info('OLT-connection-restored')
580 #TODO : suppress alarm/notify monitoring system
581 # flagging it as reachable again
nick369a5062018-05-29 17:11:06 -0400582 reactor.callFromThread(self.olt_reachable)
nick47b74372018-05-25 18:22:49 -0400583
584 if not self.indications_thread_active:
nick369a5062018-05-29 17:11:06 -0400585 self.log.info('(re)starting-indications-thread')
nick47b74372018-05-25 18:22:49 -0400586 # reset indications thread
587 self.indications_thread = threading.Thread(target=self.process_indications)
588 self.indications_thread.setDaemon(True)
589 self.indications_thread_active = True
590 self.indications_thread.start()
591
592 self.heartbeat_miss = 0
593
594 time.sleep(HEARTBEAT_PERIOD)
595
596 self.log.debug('stopping-heartbeat-thread', device_id=self.device_id)
597
598
599
Shad Ansari42db7342018-04-25 21:39:46 +0000600 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000601 pkt = Ether(msg)
602 self.log.info('packet out', egress_port=egress_port,
603 packet=str(pkt).encode("HEX"))
604
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400605
606 # Find port type
607 egress_port_type = self.port_type(egress_port)
608
609 if egress_port_type == Port.ETHERNET_UNI:
610
611 if pkt.haslayer(Dot1Q):
612 outer_shim = pkt.getlayer(Dot1Q)
613 if isinstance(outer_shim.payload, Dot1Q):
614 # If double tag, remove the outer tag
615 payload = (
616 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
617 outer_shim.payload
618 )
619 else:
620 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000621 else:
622 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400623
624 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
625
626 self.log.info('sending-packet-to-ONU', egress_port=egress_port,
627 intf_id=platform.intf_id_from_pon_port_no(egress_port),
628 onu_id=platform.onu_id_from_port_num(egress_port),
629 packet=str(payload).encode("HEX"))
630
631 onu_pkt = openolt_pb2.OnuPacket(intf_id=platform.intf_id_from_pon_port_no(egress_port),
632 onu_id=platform.onu_id_from_port_num(egress_port), pkt=send_pkt)
633
634 self.stub.OnuPacketOut(onu_pkt)
635
636 elif egress_port_type == Port.ETHERNET_NNI:
637 self.log.info('sending-packet-to-uplink', egress_port=egress_port, packet=str(pkt).encode("HEX"))
638
639 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
640
641 uplink_pkt = openolt_pb2.UplinkPacket(intf_id=platform.intf_id_from_nni_port_num(egress_port), pkt=send_pkt)
642
643 self.stub.UplinkPacketOut(uplink_pkt)
644
Shad Ansari0346f0d2018-04-26 06:54:09 +0000645 else:
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400646 self.log.warn('Packet-out-to-this-interface-type-not-implemented', egress_port=egress_port,
647 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000648
Shad Ansari2825d012018-02-22 23:57:46 +0000649 def send_proxied_message(self, proxy_address, msg):
Shad Ansari0efa6512018-04-28 06:42:54 +0000650 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id, # intf_id
651 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000652 self.stub.OmciMsgOut(omci)
653
654 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000655 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansari0efa6512018-04-28 06:42:54 +0000656 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000657
658 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000659 proxy_address = Device.ProxyAddress(device_id=self.device_id,
660 channel_id=intf_id, onu_id=onu_id, onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000661
662 self.log.info("Adding ONU", proxy_address=proxy_address)
663
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400664 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000665
Shad Ansari0efa6512018-04-28 06:42:54 +0000666 self.adapter_agent.add_onu_device(parent_device_id=self.device_id,
667 parent_port_no=port_no, vendor_id=serial_number.vendor_id,
668 proxy_address=proxy_address, root=True,
669 serial_number=serial_number_str, admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000670
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000671 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000672 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400673 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000674 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000675 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000676 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400677 if serial_number is not None:
678 return serial_number
679 else:
680 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000681
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400682
683 def port_type(self, port_no):
684 ports = self.adapter_agent.get_ports(self.device_id)
685 for port in ports:
686 if port.port_no == port_no:
687 return port.type
688 return None
689
690
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400691 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000692 self.log.info('adding-logical-port', port_no=port_no)
693
694 label = self.port_name(port_no, Port.ETHERNET_NNI)
695
696 cap = OFPPF_1GB_FD | OFPPF_FIBER
697 curr_speed = OFPPF_1GB_FD
698 max_speed = OFPPF_1GB_FD
699
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400700 if oper_state == OperStatus.ACTIVE:
701 of_oper_state = OFPPS_LIVE
702 else:
703 of_oper_state = OFPPS_LINK_DOWN
704
Shad Ansari0efa6512018-04-28 06:42:54 +0000705 ofp = ofp_port(port_no=port_no,
706 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400707 name=label, config=0, state=of_oper_state, curr=cap,
Shad Ansari0efa6512018-04-28 06:42:54 +0000708 advertised=cap, peer=cap, curr_speed=curr_speed,
709 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000710
Shad Ansari0efa6512018-04-28 06:42:54 +0000711 logical_port = LogicalPort(id=label, ofp_port=ofp,
712 device_id=self.device_id, device_port_no=port_no,
713 root_port=True)
Shad Ansari2825d012018-02-22 23:57:46 +0000714
715 self.adapter_agent.add_logical_port(self.logical_device_id, logical_port)
716
717 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000718 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000719
Shad Ansari4a232ca2018-05-05 05:24:17 +0000720 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000721
Shad Ansari0efa6512018-04-28 06:42:54 +0000722 self.log.info('adding-port', port_no=port_no, label=label,
723 port_type=port_type)
724
725 port = Port(port_no=port_no, label=label, type=port_type,
726 admin_state=AdminState.ENABLED, oper_status=oper_status)
727
Shad Ansari2825d012018-02-22 23:57:46 +0000728 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000729
Shad Ansari2825d012018-02-22 23:57:46 +0000730 return port_no, label
731
Shad Ansari2825d012018-02-22 23:57:46 +0000732 def new_onu_id(self, intf_id):
733 onu_id = None
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400734 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000735 for i in range(1, 512):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400736 id_not_taken = True
737 for child_device in onu_devices:
738 if child_device.proxy_address.onu_id == i:
739 id_not_taken = False
740 break
741 if id_not_taken:
Shad Ansari2825d012018-02-22 23:57:46 +0000742 onu_id = i
743 break
744 return onu_id
745
746 def stringify_vendor_specific(self, vendor_specific):
747 return ''.join(str(i) for i in [
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000748 hex(ord(vendor_specific[0])>>4 & 0x0f)[2:],
749 hex(ord(vendor_specific[0]) & 0x0f)[2:],
750 hex(ord(vendor_specific[1])>>4 & 0x0f)[2:],
751 hex(ord(vendor_specific[1]) & 0x0f)[2:],
752 hex(ord(vendor_specific[2])>>4 & 0x0f)[2:],
753 hex(ord(vendor_specific[2]) & 0x0f)[2:],
754 hex(ord(vendor_specific[3])>>4 & 0x0f)[2:],
755 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000756
Shad Ansari2825d012018-02-22 23:57:46 +0000757
758 def update_flow_table(self, flows):
759 device = self.adapter_agent.get_device(self.device_id)
Shad Ansaricd2e8ff2018-05-11 20:26:22 +0000760 self.log.debug('update flow table')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000761 in_port = None
Shad Ansari2825d012018-02-22 23:57:46 +0000762
763 for flow in flows:
Shad Ansari2825d012018-02-22 23:57:46 +0000764 is_down_stream = None
Shad Ansari2dda4f32018-05-17 07:16:07 +0000765 in_port = fd.get_in_port(flow)
766 assert in_port is not None
767 # Right now there is only one NNI port. Get the NNI PORT and compare
768 # with IN_PUT port number. Need to find better way.
769 ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI)
Shad Ansari2825d012018-02-22 23:57:46 +0000770
Shad Ansari2dda4f32018-05-17 07:16:07 +0000771 for port in ports:
772 if (port.port_no == in_port):
773 self.log.info('downstream-flow')
774 is_down_stream = True
775 break
776 if is_down_stream is None:
777 is_down_stream = False
778 self.log.info('upstream-flow')
Shad Ansari2825d012018-02-22 23:57:46 +0000779
Shad Ansari2dda4f32018-05-17 07:16:07 +0000780 for flow in flows:
781 try:
782 self.flow_mgr.add_flow(flow, is_down_stream)
783 except Exception as e:
784 self.log.exception('failed-to-install-flow', e=e, flow=flow)
Shad Ansari89b09d52018-05-21 07:28:14 +0000785
786 # There has to be a better way to do this
787 def ip_hex(self, ip):
788 octets = ip.split(".")
789 hex_ip = []
790 for octet in octets:
791 octet_hex = hex(int(octet))
792 octet_hex = octet_hex.split('0x')[1]
793 octet_hex = octet_hex.rjust(2, '0')
794 hex_ip.append(octet_hex)
795 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400796
797 def stringify_serial_number(self, serial_number):
798 return ''.join([serial_number.vendor_id,
Shad Ansari5c596a52018-05-23 19:49:57 +0000799 self.stringify_vendor_specific(serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400800
801 def disable(self):
802 self.log.info('sending-deactivate-olt-message', device_id=self.device_id)
803
804 # Send grpc call
805 #self.stub.DeactivateOlt(openolt_pb2.Empty())
806
807 # Soft deactivate. Turning down hardware should remove flows, but we are not doing that presently
808
809 # Bring OLT down
810 self.olt_down(oper_state=OperStatus.UNKNOWN, admin_state=AdminState.DISABLED,
811 connect_state = ConnectStatus.UNREACHABLE)
812
813
814 def delete(self):
815 self.log.info('delete-olt', device_id=self.device_id)
816
817 # Stop the grpc communication threads
818 self.log.info('stopping-grpc-threads', device_id=self.device_id)
819 self.indications_thread_active = False
820 self.heartbeat_thread_active = False
821
822 # Close the grpc channel
823 # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id)
824 # self.channel.unsubscribe()
825
826 self.log.info('successfully-deleted-olt', device_id=self.device_id)
827
828 def reenable(self):
829 self.log.info('reenable-olt', device_id=self.device_id)
830
831 # Bring up OLT
832 self.olt_up()
833
834 # Enable all child devices
835 self.log.info('enabling-child-devices', device_id=self.device_id)
836 self.log.info('enabling-child-devices', olt_device_id=self.device_id)
837 self.adapter_agent.update_child_devices_state(parent_device_id=self.device_id,
838 admin_state=AdminState.ENABLED)
839
840 # Set all ports to enabled
841 self.log.info('enabling-all-ports', device_id=self.device_id)
842 self.adapter_agent.enable_all_ports(self.device_id)