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