blob: 0ebf04fcc2e05cccf99a21cbdcdb7ad44f5f14d0 [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
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#
16import threading
17import binascii
18import grpc
19import socket
20import re
21import structlog
William Kurkianfefd4642019-02-07 15:30:03 -050022import time
William Kurkian6f436d02019-02-06 16:25:01 -050023from twisted.internet import reactor
William Kurkian92bd7122019-02-14 15:26:59 -050024from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian6f436d02019-02-06 16:25:01 -050025from scapy.layers.l2 import Ether, Dot1Q
26from transitions import Machine
27
William Kurkian44cd7bb2019-02-11 16:39:12 -050028from pyvoltha.protos import openolt_pb2_grpc, openolt_pb2
William Kurkian6f436d02019-02-06 16:25:01 -050029
William Kurkian44cd7bb2019-02-11 16:39:12 -050030from pyvoltha.adapters.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
William Kurkian6f436d02019-02-06 16:25:01 -050031
William Kurkian44cd7bb2019-02-11 16:39:12 -050032from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
33from pyvoltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
William Kurkian6f436d02019-02-06 16:25:01 -050034 OFPPS_LINK_DOWN, OFPPF_1GB_FD, \
35 OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS, \
36 ofp_switch_features, ofp_port, ofp_port_stats, ofp_desc
William Kurkian44cd7bb2019-02-11 16:39:12 -050037from pyvoltha.common.utils.registry import registry
38from pyvoltha.protos import third_party
39from pyvoltha.protos.common_pb2 import AdminState, OperStatus, ConnectStatus
40from pyvoltha.protos.common_pb2 import LogLevel
41from pyvoltha.protos.device_pb2 import Port, Device
William Kurkian6f436d02019-02-06 16:25:01 -050042
William Kurkian44cd7bb2019-02-11 16:39:12 -050043from pyvoltha.protos.logical_device_pb2 import LogicalDevice, LogicalPort
William Kurkian6f436d02019-02-06 16:25:01 -050044
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050045
William Kurkian6f436d02019-02-06 16:25:01 -050046class OpenoltDevice(object):
47 """
48 OpenoltDevice state machine:
49
50 null ----> init ------> connected -----> up -----> down
51 ^ ^ | ^ | |
52 | | | | | |
53 | +-------------+ +---------+ |
54 | |
55 +-----------------------------------------+
56 """
57 # pylint: disable=too-many-instance-attributes
58 # pylint: disable=R0904
59 states = [
60 'state_null',
61 'state_init',
62 'state_connected',
63 'state_up',
64 'state_down']
65
66 transitions = [
67 {'trigger': 'go_state_init',
68 'source': ['state_null', 'state_connected', 'state_down'],
69 'dest': 'state_init',
70 'before': 'do_state_init',
71 'after': 'post_init'},
72 {'trigger': 'go_state_connected',
73 'source': 'state_init',
74 'dest': 'state_connected',
75 'before': 'do_state_connected'},
76 {'trigger': 'go_state_up',
77 'source': ['state_connected', 'state_down'],
78 'dest': 'state_up',
79 'before': 'do_state_up'},
80 {'trigger': 'go_state_down',
81 'source': ['state_up'],
82 'dest': 'state_down',
83 'before': 'do_state_down',
84 'after': 'post_down'}]
85
86 def __init__(self, **kwargs):
87 super(OpenoltDevice, self).__init__()
88
Matt Jeanneret7906d232019-02-14 14:57:38 -050089 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050090 self.adapter_agent = kwargs['adapter_agent']
91 self.device_num = kwargs['device_num']
92 device = kwargs['device']
93
94 self.platform_class = kwargs['support_classes']['platform']
95 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
96 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
97 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
98 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
99 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
Matt Jeanneret7906d232019-02-14 14:57:38 -0500100
William Kurkian6f436d02019-02-06 16:25:01 -0500101 is_reconciliation = kwargs.get('reconciliation', False)
102 self.device_id = device.id
103 self.host_and_port = device.host_and_port
104 self.extra_args = device.extra_args
105 self.log = structlog.get_logger(id=self.device_id,
106 ip=self.host_and_port)
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500107 # self.proxy = registry('core').get_proxy('/')
William Kurkian6f436d02019-02-06 16:25:01 -0500108
109 self.log.info('openolt-device-init')
110
111 # default device id and device serial number. If device_info provides better results, they will be updated
112 self.dpid = kwargs.get('dp_id')
113 self.serial_number = self.host_and_port # FIXME
114
115 # Device already set in the event of reconciliation
116 if not is_reconciliation:
117 self.log.info('updating-device')
118 # It is a new device
119 # Update device
120 device.root = True
121 device.connect_status = ConnectStatus.UNREACHABLE
122 device.oper_status = OperStatus.ACTIVATING
William Kurkianfefd4642019-02-07 15:30:03 -0500123 self.adapter_agent.device_update(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500124
125 # If logical device does exist use it, else create one after connecting to device
126 if device.parent_id:
127 # logical device already exists
128 self.logical_device_id = device.parent_id
129 if is_reconciliation:
130 self.adapter_agent.reconcile_logical_device(
131 self.logical_device_id)
132
133 # Initialize the OLT state machine
134 self.machine = Machine(model=self, states=OpenoltDevice.states,
135 transitions=OpenoltDevice.transitions,
136 send_event=True, initial='state_null')
137 self.go_state_init()
138
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500139 def create_logical_device(self, device_info):
William Kurkian6f436d02019-02-06 16:25:01 -0500140 dpid = device_info.device_id
141 serial_number = device_info.device_serial_number
142
143 if dpid is None: dpid = self.dpid
144 if serial_number is None: serial_number = self.serial_number
145
146 if dpid == None or dpid == '':
147 uri = self.host_and_port.split(":")[0]
148 try:
149 socket.inet_pton(socket.AF_INET, uri)
150 dpid = '00:00:' + self.ip_hex(uri)
151 except socket.error:
152 # this is not an IP
153 dpid = self.stringToMacAddr(uri)
154
155 if serial_number == None or serial_number == '':
156 serial_number = self.host_and_port
157
158 self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number)
159
160 mfr_desc = device_info.vendor
161 sw_desc = device_info.firmware_version
162 hw_desc = device_info.model
163 if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version
William Kurkian92bd7122019-02-14 15:26:59 -0500164
William Kurkian6f436d02019-02-06 16:25:01 -0500165 # Create logical OF device
166 ld = LogicalDevice(
167 root_device_id=self.device_id,
168 switch_features=ofp_switch_features(
169 n_buffers=256, # TODO fake for now
170 n_tables=2, # TODO ditto
171 capabilities=( # TODO and ditto
172 OFPC_FLOW_STATS
173 | OFPC_TABLE_STATS
174 | OFPC_PORT_STATS
175 | OFPC_GROUP_STATS
176 )
177 ),
178 desc=ofp_desc(
179 serial_num=serial_number
180 )
181 )
182 ld_init = self.adapter_agent.create_logical_device(ld,
William Kurkian92bd7122019-02-14 15:26:59 -0500183 dpid=dpid)
184
William Kurkian6f436d02019-02-06 16:25:01 -0500185 self.logical_device_id = ld_init.id
186
187 device = self.adapter_agent.get_device(self.device_id)
188 device.serial_number = serial_number
William Kurkian92bd7122019-02-14 15:26:59 -0500189 self.adapter_agent.update_device(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500190
191 self.dpid = dpid
192 self.serial_number = serial_number
193
194 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
195
196 def stringToMacAddr(self, uri):
197 regex = re.compile('[^a-zA-Z]')
198 uri = regex.sub('', uri)
199
200 l = len(uri)
201 if l > 6:
202 uri = uri[0:6]
203 else:
204 uri = uri + uri[0:6 - l]
205
William Kurkian6f436d02019-02-06 16:25:01 -0500206 return ":".join([hex(ord(x))[-2:] for x in uri])
207
208 def do_state_init(self, event):
209 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500210 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500211 self.channel = grpc.insecure_channel(self.host_and_port)
212 self.channel_ready_future = grpc.channel_ready_future(self.channel)
213
214 self.log.info('openolt-device-created', device_id=self.device_id)
215
216 def post_init(self, event):
217 self.log.debug('post_init')
218
219 # We have reached init state, starting the indications thread
220
221 # Catch RuntimeError exception
222 try:
223 # Start indications thread
224 self.indications_thread_handle = threading.Thread(
225 target=self.indications_thread)
226 # Old getter/setter API for daemon; use it directly as a
227 # property instead. The Jinkins error will happon on the reason of
228 # Exception in thread Thread-1 (most likely raised # during
229 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500230 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500231 self.indications_thread_handle.setDaemon(True)
232 self.indications_thread_handle.start()
233 except Exception as e:
234 self.log.exception('post_init failed', e=e)
235
236 def do_state_connected(self, event):
237 self.log.debug("do_state_connected")
238
239 device = self.adapter_agent.get_device(self.device_id)
240
241 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
242
William Kurkianfefd4642019-02-07 15:30:03 -0500243 delay = 1
244 while True:
245 try:
246 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
247 break
248 except Exception as e:
249 reraise = True
250 if delay > 120:
251 self.log.error("gRPC failure too many times")
252 else:
253 self.log.warn("gRPC failure, retry in %ds: %s"
254 % (delay, repr(e)))
255 time.sleep(delay)
256 delay += delay
257 reraise = False
258
259 if reraise:
260 raise
261
William Kurkian6f436d02019-02-06 16:25:01 -0500262 self.log.info('Device connected', device_info=device_info)
263
Matt Jeanneret7906d232019-02-14 14:57:38 -0500264 # self.create_logical_device(device_info)
265 self.logical_device_id = 0
William Kurkian6f436d02019-02-06 16:25:01 -0500266 device.serial_number = self.serial_number
Matt Jeanneret7906d232019-02-14 14:57:38 -0500267
William Kurkian6f436d02019-02-06 16:25:01 -0500268 self.resource_mgr = self.resource_mgr_class(self.device_id,
269 self.host_and_port,
270 self.extra_args,
271 device_info)
272 self.platform = self.platform_class(self.log, self.resource_mgr)
273 self.flow_mgr = self.flow_mgr_class(self.adapter_agent, self.log,
274 self.stub, self.device_id,
275 self.logical_device_id,
276 self.platform, self.resource_mgr)
277
278 self.alarm_mgr = self.alarm_mgr_class(self.log, self.adapter_agent,
279 self.device_id,
280 self.logical_device_id,
281 self.platform)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500282 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
William Kurkian92bd7122019-02-14 15:26:59 -0500283 self.bw_mgr = self.bw_mgr_class(self.log, self.adapter_agent)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500284
William Kurkian6f436d02019-02-06 16:25:01 -0500285 device.vendor = device_info.vendor
286 device.model = device_info.model
287 device.hardware_version = device_info.hardware_version
288 device.firmware_version = device_info.firmware_version
289
290 # TODO: check for uptime and reboot if too long (VOL-1192)
291
292 device.connect_status = ConnectStatus.REACHABLE
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500293 device.mac_address = "AA:BB:CC:DD:EE:FF"
William Kurkianfefd4642019-02-07 15:30:03 -0500294 self.adapter_agent.device_update(device)
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500295
William Kurkian6f436d02019-02-06 16:25:01 -0500296 def do_state_up(self, event):
297 self.log.debug("do_state_up")
298
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500299 self.adapter_agent.device_state_update(self.device_id,
300 connect_status=ConnectStatus.REACHABLE,
301 oper_status=OperStatus.ACTIVE)
William Kurkian6f436d02019-02-06 16:25:01 -0500302
303 def do_state_down(self, event):
304 self.log.debug("do_state_down")
305 oper_state = OperStatus.UNKNOWN
306 connect_state = ConnectStatus.UNREACHABLE
307
308 # Propagating to the children
309
310 # Children ports
311 child_devices = self.adapter_agent.get_child_devices(self.device_id)
312 for onu_device in child_devices:
313 onu_adapter_agent = \
314 registry('adapter_loader').get_agent(onu_device.adapter)
315 onu_adapter_agent.update_interface(onu_device,
316 {'oper_state': 'down'})
317 self.onu_ports_down(onu_device, oper_state)
318
319 # Children devices
320 self.adapter_agent.update_child_devices_state(
321 self.device_id, oper_status=oper_state,
322 connect_status=connect_state)
323 # Device Ports
324 device_ports = self.adapter_agent.get_ports(self.device_id,
325 Port.ETHERNET_NNI)
326 logical_ports_ids = [port.label for port in device_ports]
327 device_ports += self.adapter_agent.get_ports(self.device_id,
328 Port.PON_OLT)
329
330 for port in device_ports:
331 port.oper_status = oper_state
332 self.adapter_agent.add_port(self.device_id, port)
333
334 # Device logical port
335 for logical_port_id in logical_ports_ids:
336 logical_port = self.adapter_agent.get_logical_port(
337 self.logical_device_id, logical_port_id)
338 logical_port.ofp_port.state = OFPPS_LINK_DOWN
339 self.adapter_agent.update_logical_port(self.logical_device_id,
340 logical_port)
341
342 # Device
343 device = self.adapter_agent.get_device(self.device_id)
344 device.oper_status = oper_state
345 device.connect_status = connect_state
346
William Kurkianfefd4642019-02-07 15:30:03 -0500347 reactor.callLater(2, self.adapter_agent.device_update, device)
William Kurkian6f436d02019-02-06 16:25:01 -0500348
349 # def post_up(self, event):
350 # self.log.debug('post-up')
351 # self.flow_mgr.reseed_flows()
352
353 def post_down(self, event):
354 self.log.debug('post_down')
355 self.flow_mgr.reset_flows()
356
357 def indications_thread(self):
358 self.log.debug('starting-indications-thread')
359 self.log.debug('connecting to olt', device_id=self.device_id)
360 self.channel_ready_future.result() # blocking call
361 self.log.info('connected to olt', device_id=self.device_id)
362 self.go_state_connected()
363
364 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
365
366 while True:
367 try:
368 # get the next indication from olt
369 ind = next(self.indications)
370 except Exception as e:
371 self.log.warn('gRPC connection lost', error=e)
372 reactor.callFromThread(self.go_state_down)
373 reactor.callFromThread(self.go_state_init)
374 break
375 else:
376 self.log.debug("rx indication", indication=ind)
377
378 # indication handlers run in the main event loop
379 if ind.HasField('olt_ind'):
380 reactor.callFromThread(self.olt_indication, ind.olt_ind)
381 elif ind.HasField('intf_ind'):
382 reactor.callFromThread(self.intf_indication, ind.intf_ind)
383 elif ind.HasField('intf_oper_ind'):
384 reactor.callFromThread(self.intf_oper_indication,
385 ind.intf_oper_ind)
386 elif ind.HasField('onu_disc_ind'):
387 reactor.callFromThread(self.onu_discovery_indication,
388 ind.onu_disc_ind)
389 elif ind.HasField('onu_ind'):
390 reactor.callFromThread(self.onu_indication, ind.onu_ind)
391 elif ind.HasField('omci_ind'):
392 reactor.callFromThread(self.omci_indication, ind.omci_ind)
393 elif ind.HasField('pkt_ind'):
394 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
395 elif ind.HasField('port_stats'):
396 reactor.callFromThread(
397 self.stats_mgr.port_statistics_indication,
398 ind.port_stats)
399 elif ind.HasField('flow_stats'):
400 reactor.callFromThread(
401 self.stats_mgr.flow_statistics_indication,
402 ind.flow_stats)
403 elif ind.HasField('alarm_ind'):
404 reactor.callFromThread(self.alarm_mgr.process_alarms,
405 ind.alarm_ind)
406 else:
407 self.log.warn('unknown indication type')
408
409 def olt_indication(self, olt_indication):
410 if olt_indication.oper_state == "up":
411 self.go_state_up()
412 elif olt_indication.oper_state == "down":
413 self.go_state_down()
414
415 def intf_indication(self, intf_indication):
416 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
417 oper_state=intf_indication.oper_state)
418
419 if intf_indication.oper_state == "up":
420 oper_status = OperStatus.ACTIVE
421 else:
422 oper_status = OperStatus.DISCOVERED
423
424 # add_port update the port if it exists
425 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
426
427 def intf_oper_indication(self, intf_oper_indication):
428 self.log.debug("Received interface oper state change indication",
429 intf_id=intf_oper_indication.intf_id,
430 type=intf_oper_indication.type,
431 oper_state=intf_oper_indication.oper_state)
432
433 if intf_oper_indication.oper_state == "up":
434 oper_state = OperStatus.ACTIVE
435 else:
436 oper_state = OperStatus.DISCOVERED
437
438 if intf_oper_indication.type == "nni":
439
440 # add_(logical_)port update the port if it exists
441 port_no, label = self.add_port(intf_oper_indication.intf_id,
442 Port.ETHERNET_NNI, oper_state)
443 self.log.debug("int_oper_indication", port_no=port_no, label=label)
444 self.add_logical_port(port_no, intf_oper_indication.intf_id,
445 oper_state)
446
447 elif intf_oper_indication.type == "pon":
448 # FIXME - handle PON oper state change
449 pass
450
451 def onu_discovery_indication(self, onu_disc_indication):
452 intf_id = onu_disc_indication.intf_id
453 serial_number = onu_disc_indication.serial_number
454
455 serial_number_str = self.stringify_serial_number(serial_number)
456
457 self.log.debug("onu discovery indication", intf_id=intf_id,
458 serial_number=serial_number_str)
459
460 # Post ONU Discover alarm 20180809_0805
461 try:
462 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
463 serial_number=serial_number_str).raise_alarm()
464 except Exception as disc_alarm_error:
465 self.log.exception("onu-discovery-alarm-error",
466 errmsg=disc_alarm_error.message)
467 # continue for now.
468
469 onu_device = self.adapter_agent.get_child_device(
470 self.device_id,
471 serial_number=serial_number_str)
472
473 if onu_device is None:
474 try:
475 onu_id = self.resource_mgr.get_onu_id(intf_id)
476 if onu_id is None:
477 raise Exception("onu-id-unavailable")
478
479 self.add_onu_device(
480 intf_id,
481 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
482 onu_id, serial_number)
483 self.activate_onu(intf_id, onu_id, serial_number,
484 serial_number_str)
485 except Exception as e:
486 self.log.exception('onu-activation-failed', e=e)
487
488 else:
489 if onu_device.connect_status != ConnectStatus.REACHABLE:
490 onu_device.connect_status = ConnectStatus.REACHABLE
William Kurkianfefd4642019-02-07 15:30:03 -0500491 self.adapter_agent.device_update(onu_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500492
493 onu_id = onu_device.proxy_address.onu_id
494 if onu_device.oper_status == OperStatus.DISCOVERED \
495 or onu_device.oper_status == OperStatus.ACTIVATING:
496 self.log.debug("ignore onu discovery indication, \
497 the onu has been discovered and should be \
498 activating shorlty", intf_id=intf_id,
499 onu_id=onu_id, state=onu_device.oper_status)
500 elif onu_device.oper_status == OperStatus.ACTIVE:
501 self.log.warn("onu discovery indication whereas onu is \
502 supposed to be active",
503 intf_id=intf_id, onu_id=onu_id,
504 state=onu_device.oper_status)
505 elif onu_device.oper_status == OperStatus.UNKNOWN:
506 self.log.info("onu in unknown state, recovering from olt \
507 reboot probably, activate onu", intf_id=intf_id,
508 onu_id=onu_id, serial_number=serial_number_str)
509
510 onu_device.oper_status = OperStatus.DISCOVERED
William Kurkianfefd4642019-02-07 15:30:03 -0500511 self.adapter_agent.device_update(onu_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500512 try:
513 self.activate_onu(intf_id, onu_id, serial_number,
514 serial_number_str)
515 except Exception as e:
516 self.log.error('onu-activation-error',
517 serial_number=serial_number_str, error=e)
518 else:
519 self.log.warn('unexpected state', onu_id=onu_id,
520 onu_device_oper_state=onu_device.oper_status)
521
522 def onu_indication(self, onu_indication):
523 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
524 onu_id=onu_indication.onu_id,
525 serial_number=onu_indication.serial_number,
526 oper_state=onu_indication.oper_state,
527 admin_state=onu_indication.admin_state)
528 try:
529 serial_number_str = self.stringify_serial_number(
530 onu_indication.serial_number)
531 except Exception as e:
532 serial_number_str = None
533
534 if serial_number_str is not None:
535 onu_device = self.adapter_agent.get_child_device(
536 self.device_id,
537 serial_number=serial_number_str)
538 else:
539 onu_device = self.adapter_agent.get_child_device(
540 self.device_id,
541 parent_port_no=self.platform.intf_id_to_port_no(
542 onu_indication.intf_id, Port.PON_OLT),
543 onu_id=onu_indication.onu_id)
544
545 if onu_device is None:
546 self.log.error('onu not found', intf_id=onu_indication.intf_id,
547 onu_id=onu_indication.onu_id)
548 return
549
550 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
551 != onu_indication.intf_id:
552 self.log.warn('ONU-is-on-a-different-intf-id-now',
553 previous_intf_id=self.platform.intf_id_from_pon_port_no(
554 onu_device.parent_port_no),
555 current_intf_id=onu_indication.intf_id)
556 # FIXME - handle intf_id mismatch (ONU move?)
557
558 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
559 # FIXME - handle onu id mismatch
560 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
561 'the olt rebooted',
562 expected_onu_id=onu_device.proxy_address.onu_id,
563 received_onu_id=onu_indication.onu_id)
564
565 # Admin state
566 if onu_indication.admin_state == 'down':
567 if onu_indication.oper_state != 'down':
568 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
569 oper_state=onu_indication.oper_state)
570 # Forcing the oper state change code to execute
571 onu_indication.oper_state = 'down'
572
573 # Port and logical port update is taken care of by oper state block
574
575 elif onu_indication.admin_state == 'up':
576 pass
577
578 else:
579 self.log.warn('Invalid-or-not-implemented-admin-state',
580 received_admin_state=onu_indication.admin_state)
581
582 self.log.debug('admin-state-dealt-with')
583
584 onu_adapter_agent = \
585 registry('adapter_loader').get_agent(onu_device.adapter)
586 if onu_adapter_agent is None:
587 self.log.error('onu_adapter_agent-could-not-be-retrieved',
588 onu_device=onu_device)
589 return
590
591 # Operating state
592 if onu_indication.oper_state == 'down':
593
594 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
595 onu_device.connect_status = ConnectStatus.UNREACHABLE
William Kurkianfefd4642019-02-07 15:30:03 -0500596 self.adapter_agent.device_update(onu_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500597
598 # Move to discovered state
599 self.log.debug('onu-oper-state-is-down')
600
601 if onu_device.oper_status != OperStatus.DISCOVERED:
602 onu_device.oper_status = OperStatus.DISCOVERED
William Kurkianfefd4642019-02-07 15:30:03 -0500603 self.adapter_agent.device_update(onu_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500604 # Set port oper state to Discovered
605 self.onu_ports_down(onu_device, OperStatus.DISCOVERED)
606
607 onu_adapter_agent.update_interface(onu_device,
608 {'oper_state': 'down'})
609
610 elif onu_indication.oper_state == 'up':
611
612 if onu_device.connect_status != ConnectStatus.REACHABLE:
613 onu_device.connect_status = ConnectStatus.REACHABLE
William Kurkianfefd4642019-02-07 15:30:03 -0500614 self.adapter_agent.device_update(onu_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500615
616 if onu_device.oper_status != OperStatus.DISCOVERED:
617 self.log.debug("ignore onu indication",
618 intf_id=onu_indication.intf_id,
619 onu_id=onu_indication.onu_id,
620 state=onu_device.oper_status,
621 msg_oper_state=onu_indication.oper_state)
622 return
623
624 # Device was in Discovered state, setting it to active
625
626 # Prepare onu configuration
627
628 onu_adapter_agent.create_interface(onu_device, onu_indication)
629
630 else:
631 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
632 oper_state=onu_indication.oper_state)
633
634 def onu_ports_down(self, onu_device, oper_state):
635 # Set port oper state to Discovered
636 # add port will update port if it exists
637 # self.adapter_agent.add_port(
638 # self.device_id,
639 # Port(
640 # port_no=uni_no,
641 # label=uni_name,
642 # type=Port.ETHERNET_UNI,
643 # admin_state=onu_device.admin_state,
644 # oper_status=oper_state))
645 # TODO this should be downning ports in onu adatper
646
647 # Disable logical port
648 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
649 for onu_port in onu_ports:
650 self.log.debug('onu-ports-down', onu_port=onu_port)
651 onu_port_id = onu_port.label
652 try:
653 onu_logical_port = self.adapter_agent.get_logical_port(
654 logical_device_id=self.logical_device_id, port_id=onu_port_id)
655 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
656 self.adapter_agent.update_logical_port(
657 logical_device_id=self.logical_device_id,
658 port=onu_logical_port)
659 self.log.debug('cascading-oper-state-to-port-and-logical-port')
660 except KeyError as e:
661 self.log.error('matching-onu-port-label-invalid',
662 onu_id=onu_device.id, olt_id=self.device_id,
663 onu_ports=onu_ports, onu_port_id=onu_port_id,
664 error=e)
665
666 def omci_indication(self, omci_indication):
667
668 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
669 onu_id=omci_indication.onu_id)
670
671 onu_device = self.adapter_agent.get_child_device(
672 self.device_id, onu_id=omci_indication.onu_id,
673 parent_port_no=self.platform.intf_id_to_port_no(
674 omci_indication.intf_id, Port.PON_OLT), )
675
676 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
677 omci_indication.pkt)
678
679 def packet_indication(self, pkt_indication):
680
681 self.log.debug("packet indication",
682 intf_type=pkt_indication.intf_type,
683 intf_id=pkt_indication.intf_id,
684 port_no=pkt_indication.port_no,
685 cookie=pkt_indication.cookie,
686 gemport_id=pkt_indication.gemport_id,
687 flow_id=pkt_indication.flow_id)
688
689 if pkt_indication.intf_type == "pon":
690 if pkt_indication.port_no:
691 logical_port_num = pkt_indication.port_no
692 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
693 try:
694 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
695 pkt_indication.gemport_id)
696 onu_id = int(onu_id_uni_id[0])
697 uni_id = int(onu_id_uni_id[1])
698 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
699 if onu_id is None:
700 raise Exception("onu-id-none")
701 if uni_id is None:
702 raise Exception("uni-id-none")
703 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
704 except Exception as e:
705 self.log.error("no-onu-reference-for-gem",
706 gemport_id=pkt_indication.gemport_id, e=e)
707 return
708
709
710 elif pkt_indication.intf_type == "nni":
711 logical_port_num = self.platform.intf_id_to_port_no(
712 pkt_indication.intf_id,
713 Port.ETHERNET_NNI)
714
715 pkt = Ether(pkt_indication.pkt)
716
717 self.log.debug("packet indication",
718 logical_device_id=self.logical_device_id,
719 logical_port_no=logical_port_num)
720
721 self.adapter_agent.send_packet_in(
722 logical_device_id=self.logical_device_id,
723 logical_port_no=logical_port_num,
724 packet=str(pkt))
725
726 def packet_out(self, egress_port, msg):
727 pkt = Ether(msg)
728 self.log.debug('packet out', egress_port=egress_port,
729 device_id=self.device_id,
730 logical_device_id=self.logical_device_id,
731 packet=str(pkt).encode("HEX"))
732
733 # Find port type
734 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
735 if egress_port_type == Port.ETHERNET_UNI:
736
737 if pkt.haslayer(Dot1Q):
738 outer_shim = pkt.getlayer(Dot1Q)
739 if isinstance(outer_shim.payload, Dot1Q):
740 # If double tag, remove the outer tag
741 payload = (
742 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
743 outer_shim.payload
744 )
745 else:
746 payload = pkt
747 else:
748 payload = pkt
749
750 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
751
752 self.log.debug(
753 'sending-packet-to-ONU', egress_port=egress_port,
754 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
755 onu_id=self.platform.onu_id_from_port_num(egress_port),
756 uni_id=self.platform.uni_id_from_port_num(egress_port),
757 port_no=egress_port,
758 packet=str(payload).encode("HEX"))
759
760 onu_pkt = openolt_pb2.OnuPacket(
761 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
762 onu_id=self.platform.onu_id_from_port_num(egress_port),
763 port_no=egress_port,
764 pkt=send_pkt)
765
766 self.stub.OnuPacketOut(onu_pkt)
767
768 elif egress_port_type == Port.ETHERNET_NNI:
769 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
770 packet=str(pkt).encode("HEX"))
771
772 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
773
774 uplink_pkt = openolt_pb2.UplinkPacket(
775 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
776 pkt=send_pkt)
777
778 self.stub.UplinkPacketOut(uplink_pkt)
779
780 else:
781 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
782 egress_port=egress_port,
783 port_type=egress_port_type)
784
785 def send_proxied_message(self, proxy_address, msg):
786 onu_device = self.adapter_agent.get_child_device(
787 self.device_id, onu_id=proxy_address.onu_id,
788 parent_port_no=self.platform.intf_id_to_port_no(
789 proxy_address.channel_id, Port.PON_OLT)
790 )
791 if onu_device.connect_status != ConnectStatus.REACHABLE:
792 self.log.debug('ONU is not reachable, cannot send OMCI',
793 serial_number=onu_device.serial_number,
794 intf_id=onu_device.proxy_address.channel_id,
795 onu_id=onu_device.proxy_address.onu_id)
796 return
797 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
798 onu_id=proxy_address.onu_id, pkt=str(msg))
799 self.stub.OmciMsgOut(omci)
800
801 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
802 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
803 serial_number=serial_number)
804
805 # NOTE - channel_id of onu is set to intf_id
806 proxy_address = Device.ProxyAddress(device_id=self.device_id,
807 channel_id=intf_id, onu_id=onu_id,
808 onu_session_id=onu_id)
809
810 self.log.debug("Adding ONU", proxy_address=proxy_address)
811
812 serial_number_str = self.stringify_serial_number(serial_number)
813
814 self.adapter_agent.add_onu_device(
815 parent_device_id=self.device_id, parent_port_no=port_no,
816 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
817 root=True, serial_number=serial_number_str,
Matt Jeanneret7906d232019-02-14 14:57:38 -0500818 admin_state=AdminState.ENABLED
819 # , **{'vlan':4091} # magic still maps to brcm_openomci_onu.pon_port.BRDCM_DEFAULT_VLAN
William Kurkian6f436d02019-02-06 16:25:01 -0500820 )
821
822 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
823 if port_type is Port.ETHERNET_NNI:
824 return "nni-" + str(port_no)
825 elif port_type is Port.PON_OLT:
826 return "pon" + str(intf_id)
827 elif port_type is Port.ETHERNET_UNI:
828 assert False, 'local UNI management not supported'
829
830 def add_logical_port(self, port_no, intf_id, oper_state):
831 self.log.info('adding-logical-port', port_no=port_no)
832
833 label = self.port_name(port_no, Port.ETHERNET_NNI)
834
835 cap = OFPPF_1GB_FD | OFPPF_FIBER
836 curr_speed = OFPPF_1GB_FD
837 max_speed = OFPPF_1GB_FD
838
839 if oper_state == OperStatus.ACTIVE:
840 of_oper_state = OFPPS_LIVE
841 else:
842 of_oper_state = OFPPS_LINK_DOWN
843
844 ofp = ofp_port(
845 port_no=port_no,
846 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
847 name=label, config=0, state=of_oper_state, curr=cap,
848 advertised=cap, peer=cap, curr_speed=curr_speed,
849 max_speed=max_speed)
850
851 ofp_stats = ofp_port_stats(port_no=port_no)
852
853 logical_port = LogicalPort(
854 id=label, ofp_port=ofp, device_id=self.device_id,
855 device_port_no=port_no, root_port=True,
856 ofp_port_stats=ofp_stats)
857
858 self.adapter_agent.add_logical_port(self.logical_device_id,
859 logical_port)
860
861 def _get_mac_form_port_no(self, port_no):
862 mac = ''
863 for i in range(4):
864 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
865 return '00:00' + mac
866
William Kurkian92bd7122019-02-14 15:26:59 -0500867 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500868 def add_port(self, intf_id, port_type, oper_status):
869 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
870
871 label = self.port_name(port_no, port_type, intf_id)
872
873 self.log.debug('adding-port', port_no=port_no, label=label,
874 port_type=port_type)
875
876 port = Port(port_no=port_no, label=label, type=port_type,
877 admin_state=AdminState.ENABLED, oper_status=oper_status)
878
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500879 yield self.adapter_agent.port_created(self.device_id, port)
880 # self.adapter_agent.add_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500881
William Kurkian92bd7122019-02-14 15:26:59 -0500882 returnValue(port_no, label)
William Kurkian6f436d02019-02-06 16:25:01 -0500883
884 def delete_logical_port(self, child_device):
885 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
886 self.logical_device_id))
887 for logical_port in logical_ports:
888 if logical_port.device_id == child_device.id:
889 self.log.debug('delete-logical-port',
890 onu_device_id=child_device.id,
891 logical_port=logical_port)
892 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
893 child_device, logical_port)
894 self.adapter_agent.delete_logical_port(
895 self.logical_device_id, logical_port)
896 return
897
898 def delete_port(self, child_serial_number):
899 ports = self.proxy.get('/devices/{}/ports'.format(
900 self.device_id))
901 for port in ports:
902 if port.label == child_serial_number:
903 self.log.debug('delete-port',
904 onu_serial_number=child_serial_number,
905 port=port)
906 self.adapter_agent.delete_port(self.device_id, port)
907 return
908
909 def update_flow_table(self, flows):
910 self.log.debug('No updates here now, all is done in logical flows '
911 'update')
912
913 def update_logical_flows(self, flows_to_add, flows_to_remove,
914 device_rules_map):
915 if not self.is_state_up():
916 self.log.info('The OLT is not up, we cannot update flows',
917 flows_to_add=[f.id for f in flows_to_add],
918 flows_to_remove=[f.id for f in flows_to_remove])
919 return
920
921 try:
922 self.flow_mgr.update_children_flows(device_rules_map)
923 except Exception as e:
924 self.log.error('Error updating children flows', error=e)
925
926 self.log.debug('logical flows update', flows_to_add=flows_to_add,
927 flows_to_remove=flows_to_remove)
928
929 for flow in flows_to_add:
930
931 try:
932 self.flow_mgr.add_flow(flow)
933 except Exception as e:
934 self.log.error('failed to add flow', flow=flow, e=e)
935
936 for flow in flows_to_remove:
937
938 try:
939 self.flow_mgr.remove_flow(flow)
940 except Exception as e:
941 self.log.error('failed to remove flow', flow=flow, e=e)
942
943 self.flow_mgr.repush_all_different_flows()
944
945 # There has to be a better way to do this
946 def ip_hex(self, ip):
947 octets = ip.split(".")
948 hex_ip = []
949 for octet in octets:
950 octet_hex = hex(int(octet))
951 octet_hex = octet_hex.split('0x')[1]
952 octet_hex = octet_hex.rjust(2, '0')
953 hex_ip.append(octet_hex)
954 return ":".join(hex_ip)
955
956 def stringify_vendor_specific(self, vendor_specific):
957 return ''.join(str(i) for i in [
958 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
959 hex(ord(vendor_specific[0]) & 0x0f)[2:],
960 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
961 hex(ord(vendor_specific[1]) & 0x0f)[2:],
962 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
963 hex(ord(vendor_specific[2]) & 0x0f)[2:],
964 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
965 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
966
967 def stringify_serial_number(self, serial_number):
968 return ''.join([serial_number.vendor_id,
969 self.stringify_vendor_specific(
970 serial_number.vendor_specific)])
971
972 def destringify_serial_number(self, serial_number_str):
973 serial_number = openolt_pb2.SerialNumber(
974 vendor_id=serial_number_str[:4].encode('utf-8'),
975 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
976 return serial_number
977
978 def disable(self):
979 self.log.debug('sending-deactivate-olt-message',
980 device_id=self.device_id)
981
982 try:
983 # Send grpc call
984 self.stub.DisableOlt(openolt_pb2.Empty())
985 # The resulting indication will bring the OLT down
986 # self.go_state_down()
987 self.log.info('openolt device disabled')
988 except Exception as e:
989 self.log.error('Failure to disable openolt device', error=e)
990
991 def delete(self):
992 self.log.info('deleting-olt', device_id=self.device_id,
993 logical_device_id=self.logical_device_id)
994
995 # Clears up the data from the resource manager KV store
996 # for the device
997 del self.resource_mgr
998
999 try:
1000 # Rebooting to reset the state
1001 self.reboot()
1002 # Removing logical device
1003 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
1004 self.adapter_agent.delete_logical_device(ld)
1005 except Exception as e:
1006 self.log.error('Failure to delete openolt device', error=e)
1007 raise e
1008 else:
1009 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1010
1011 def reenable(self):
1012 self.log.debug('reenabling-olt', device_id=self.device_id)
1013
1014 try:
1015 self.stub.ReenableOlt(openolt_pb2.Empty())
1016
William Kurkian6f436d02019-02-06 16:25:01 -05001017 except Exception as e:
1018 self.log.error('Failure to reenable openolt device', error=e)
1019 else:
1020 self.log.info('openolt device reenabled')
1021
1022 def activate_onu(self, intf_id, onu_id, serial_number,
1023 serial_number_str):
1024 pir = self.bw_mgr.pir(serial_number_str)
1025 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1026 serial_number_str=serial_number_str,
1027 serial_number=serial_number, pir=pir)
1028 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1029 serial_number=serial_number, pir=pir)
1030 self.stub.ActivateOnu(onu)
1031 self.log.info('onu-activated', serial_number=serial_number_str)
1032
1033 def delete_child_device(self, child_device):
1034 self.log.debug('sending-deactivate-onu',
1035 olt_device_id=self.device_id,
1036 onu_device=child_device,
1037 onu_serial_number=child_device.serial_number)
1038 try:
1039 self.adapter_agent.delete_child_device(self.device_id,
1040 child_device.id,
1041 child_device)
1042 except Exception as e:
1043 self.log.error('adapter_agent error', error=e)
1044 try:
1045 self.delete_logical_port(child_device)
1046 except Exception as e:
1047 self.log.error('logical_port delete error', error=e)
1048 try:
1049 self.delete_port(child_device.serial_number)
1050 except Exception as e:
1051 self.log.error('port delete error', error=e)
1052 serial_number = self.destringify_serial_number(
1053 child_device.serial_number)
1054 # TODO FIXME - For each uni.
1055 # TODO FIXME - Flows are not deleted
1056 uni_id = 0 # FIXME
1057 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001058 child_device.proxy_address.channel_id,
1059 child_device.proxy_address.onu_id,
1060 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001061 )
1062 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1063 child_device.proxy_address.onu_id,
1064 uni_id)
1065 # Free any PON resources that were reserved for the ONU
1066 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1067
1068 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1069 onu_id=child_device.proxy_address.onu_id,
1070 serial_number=serial_number)
1071 self.stub.DeleteOnu(onu)
1072
1073 def reboot(self):
1074 self.log.debug('rebooting openolt device', device_id=self.device_id)
1075 try:
1076 self.stub.Reboot(openolt_pb2.Empty())
1077 except Exception as e:
1078 self.log.error('something went wrong with the reboot', error=e)
1079 else:
1080 self.log.info('device rebooted')
1081
1082 def trigger_statistics_collection(self):
1083 try:
1084 self.stub.CollectStatistics(openolt_pb2.Empty())
1085 except Exception as e:
1086 self.log.error('Error while triggering statistics collection',
1087 error=e)
1088 else:
1089 self.log.info('statistics requested')
1090
1091 def simulate_alarm(self, alarm):
1092 self.alarm_mgr.simulate_alarm(alarm)