blob: 4cba020b39a1a34826300421a78df5dc87d5e8c1 [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 Kurkian8b1690c2019-03-04 16:53:22 -050028from voltha_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
William Kurkian8b1690c2019-03-04 16:53:22 -050033from voltha_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
William Kurkian8b1690c2019-03-04 16:53:22 -050038from voltha_protos.common_pb2 import AdminState, OperStatus, ConnectStatus
William Kurkian8b1690c2019-03-04 16:53:22 -050039from voltha_protos.device_pb2 import Port, Device
Matt Jeanneretb428b952019-03-07 05:14:17 -050040from voltha_protos.inter_container_pb2 import InterAdapterMessageType, InterAdapterOmciMessage
William Kurkian8b1690c2019-03-04 16:53:22 -050041from voltha_protos.logical_device_pb2 import LogicalDevice, LogicalPort
William Kurkian6f436d02019-02-06 16:25:01 -050042
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050043
William Kurkian6f436d02019-02-06 16:25:01 -050044class OpenoltDevice(object):
45 """
46 OpenoltDevice state machine:
47
48 null ----> init ------> connected -----> up -----> down
49 ^ ^ | ^ | |
50 | | | | | |
51 | +-------------+ +---------+ |
52 | |
53 +-----------------------------------------+
54 """
55 # pylint: disable=too-many-instance-attributes
56 # pylint: disable=R0904
57 states = [
58 'state_null',
59 'state_init',
60 'state_connected',
61 'state_up',
62 'state_down']
63
64 transitions = [
65 {'trigger': 'go_state_init',
66 'source': ['state_null', 'state_connected', 'state_down'],
67 'dest': 'state_init',
68 'before': 'do_state_init',
69 'after': 'post_init'},
70 {'trigger': 'go_state_connected',
71 'source': 'state_init',
72 'dest': 'state_connected',
73 'before': 'do_state_connected'},
74 {'trigger': 'go_state_up',
75 'source': ['state_connected', 'state_down'],
76 'dest': 'state_up',
77 'before': 'do_state_up'},
78 {'trigger': 'go_state_down',
79 'source': ['state_up'],
80 'dest': 'state_down',
81 'before': 'do_state_down',
82 'after': 'post_down'}]
83
84 def __init__(self, **kwargs):
85 super(OpenoltDevice, self).__init__()
86
Matt Jeanneretbad3d982019-03-11 16:06:10 -040087 self.core_proxy = kwargs['core_proxy']
Matt Jeanneret7906d232019-02-14 14:57:38 -050088 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050089 self.device_num = kwargs['device_num']
90 device = kwargs['device']
91
92 self.platform_class = kwargs['support_classes']['platform']
93 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
94 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
95 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
96 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
97 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
Matt Jeanneret7906d232019-02-14 14:57:38 -050098
Matt Jeanneretb428b952019-03-07 05:14:17 -050099 self.seen_discovery_indications = []
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500100 self.stub = None
William Kurkian27522582019-02-25 14:24:32 -0500101 self.connected = False
William Kurkian6f436d02019-02-06 16:25:01 -0500102 is_reconciliation = kwargs.get('reconciliation', False)
103 self.device_id = device.id
104 self.host_and_port = device.host_and_port
105 self.extra_args = device.extra_args
106 self.log = structlog.get_logger(id=self.device_id,
107 ip=self.host_and_port)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500108
William Kurkian6f436d02019-02-06 16:25:01 -0500109 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
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500123 # TODO NEW CORE. need to move this, cant have a constructor be a generator (yield)
124 #self.adapter_agent.device_update(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500125
126 # If logical device does exist use it, else create one after connecting to device
127 if device.parent_id:
128 # logical device already exists
129 self.logical_device_id = device.parent_id
130 if is_reconciliation:
131 self.adapter_agent.reconcile_logical_device(
132 self.logical_device_id)
133
134 # Initialize the OLT state machine
135 self.machine = Machine(model=self, states=OpenoltDevice.states,
136 transitions=OpenoltDevice.transitions,
137 send_event=True, initial='state_null')
138 self.go_state_init()
139
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500140 @inlineCallbacks
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500141 def create_logical_device(self, device_info):
William Kurkian6f436d02019-02-06 16:25:01 -0500142 dpid = device_info.device_id
143 serial_number = device_info.device_serial_number
144
145 if dpid is None: dpid = self.dpid
146 if serial_number is None: serial_number = self.serial_number
147
148 if dpid == None or dpid == '':
149 uri = self.host_and_port.split(":")[0]
150 try:
151 socket.inet_pton(socket.AF_INET, uri)
152 dpid = '00:00:' + self.ip_hex(uri)
153 except socket.error:
154 # this is not an IP
155 dpid = self.stringToMacAddr(uri)
156
157 if serial_number == None or serial_number == '':
158 serial_number = self.host_and_port
159
160 self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number)
161
162 mfr_desc = device_info.vendor
163 sw_desc = device_info.firmware_version
164 hw_desc = device_info.model
165 if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version
William Kurkian92bd7122019-02-14 15:26:59 -0500166
William Kurkian6f436d02019-02-06 16:25:01 -0500167 # Create logical OF device
168 ld = LogicalDevice(
169 root_device_id=self.device_id,
170 switch_features=ofp_switch_features(
171 n_buffers=256, # TODO fake for now
172 n_tables=2, # TODO ditto
173 capabilities=( # TODO and ditto
174 OFPC_FLOW_STATS
175 | OFPC_TABLE_STATS
176 | OFPC_PORT_STATS
177 | OFPC_GROUP_STATS
178 )
179 ),
180 desc=ofp_desc(
181 serial_num=serial_number
182 )
183 )
184 ld_init = self.adapter_agent.create_logical_device(ld,
William Kurkian92bd7122019-02-14 15:26:59 -0500185 dpid=dpid)
186
William Kurkian6f436d02019-02-06 16:25:01 -0500187 self.logical_device_id = ld_init.id
188
William Kurkian27522582019-02-25 14:24:32 -0500189 ##Moved setting serial number outside of the logical_device function
190 #device = yield self.adapter_agent.get_device(self.device_id)
191 #device.serial_number = serial_number
192 #yield self.adapter_agent.update_device(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500193
194 self.dpid = dpid
195 self.serial_number = serial_number
196
197 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
198
199 def stringToMacAddr(self, uri):
200 regex = re.compile('[^a-zA-Z]')
201 uri = regex.sub('', uri)
202
203 l = len(uri)
204 if l > 6:
205 uri = uri[0:6]
206 else:
207 uri = uri + uri[0:6 - l]
208
William Kurkian6f436d02019-02-06 16:25:01 -0500209 return ":".join([hex(ord(x))[-2:] for x in uri])
210
211 def do_state_init(self, event):
212 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500213 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500214 self.channel = grpc.insecure_channel(self.host_and_port)
215 self.channel_ready_future = grpc.channel_ready_future(self.channel)
216
217 self.log.info('openolt-device-created', device_id=self.device_id)
218
219 def post_init(self, event):
220 self.log.debug('post_init')
221
222 # We have reached init state, starting the indications thread
223
224 # Catch RuntimeError exception
225 try:
226 # Start indications thread
227 self.indications_thread_handle = threading.Thread(
228 target=self.indications_thread)
229 # Old getter/setter API for daemon; use it directly as a
230 # property instead. The Jinkins error will happon on the reason of
231 # Exception in thread Thread-1 (most likely raised # during
232 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500233 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500234 self.indications_thread_handle.setDaemon(True)
235 self.indications_thread_handle.start()
236 except Exception as e:
237 self.log.exception('post_init failed', e=e)
238
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500239 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500240 def do_state_connected(self, event):
241 self.log.debug("do_state_connected")
William Kurkian27522582019-02-25 14:24:32 -0500242
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400243 device = yield self.core_proxy.get_device(self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500244
William Kurkian27522582019-02-25 14:24:32 -0500245
William Kurkian6f436d02019-02-06 16:25:01 -0500246 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
247
William Kurkianfefd4642019-02-07 15:30:03 -0500248 delay = 1
249 while True:
250 try:
251 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
252 break
253 except Exception as e:
254 reraise = True
255 if delay > 120:
256 self.log.error("gRPC failure too many times")
257 else:
258 self.log.warn("gRPC failure, retry in %ds: %s"
259 % (delay, repr(e)))
260 time.sleep(delay)
261 delay += delay
262 reraise = False
263
264 if reraise:
265 raise
266
William Kurkian6f436d02019-02-06 16:25:01 -0500267 self.log.info('Device connected', device_info=device_info)
268
Matt Jeanneret7906d232019-02-14 14:57:38 -0500269 # self.create_logical_device(device_info)
270 self.logical_device_id = 0
William Kurkian27522582019-02-25 14:24:32 -0500271
272 serial_number = device_info.device_serial_number
273 if serial_number is None:
274 serial_number = self.serial_number
275 device.serial_number = serial_number
276
277 self.serial_number = serial_number
278
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500279 device.root = True
William Kurkian27522582019-02-25 14:24:32 -0500280 device.vendor = device_info.vendor
281 device.model = device_info.model
282 device.hardware_version = device_info.hardware_version
283 device.firmware_version = device_info.firmware_version
284
285 # TODO: check for uptime and reboot if too long (VOL-1192)
286
287 device.connect_status = ConnectStatus.REACHABLE
288 device.mac_address = "AA:BB:CC:DD:EE:FF"
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400289 yield self.core_proxy.device_update(device)
William Kurkian27522582019-02-25 14:24:32 -0500290
Matt Jeanneret7906d232019-02-14 14:57:38 -0500291
William Kurkian6f436d02019-02-06 16:25:01 -0500292 self.resource_mgr = self.resource_mgr_class(self.device_id,
293 self.host_and_port,
294 self.extra_args,
295 device_info)
296 self.platform = self.platform_class(self.log, self.resource_mgr)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400297 self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.log,
William Kurkian6f436d02019-02-06 16:25:01 -0500298 self.stub, self.device_id,
299 self.logical_device_id,
300 self.platform, self.resource_mgr)
301
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400302 self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
William Kurkian6f436d02019-02-06 16:25:01 -0500303 self.device_id,
304 self.logical_device_id,
305 self.platform)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500306 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400307 self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)
William Kurkian27522582019-02-25 14:24:32 -0500308
309 self.connected = True
Matt Jeanneret6e315092019-02-20 10:42:57 -0500310
311 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500312 def do_state_up(self, event):
313 self.log.debug("do_state_up")
314
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400315 yield self.core_proxy.device_state_update(self.device_id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500316 connect_status=ConnectStatus.REACHABLE,
317 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500318 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500319
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500320 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500321 def do_state_down(self, event):
322 self.log.debug("do_state_down")
323 oper_state = OperStatus.UNKNOWN
324 connect_state = ConnectStatus.UNREACHABLE
325
326 # Propagating to the children
327
328 # Children ports
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500329 child_devices = yield self.adapter_agent.get_child_devices(self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500330 for onu_device in child_devices:
331 onu_adapter_agent = \
332 registry('adapter_loader').get_agent(onu_device.adapter)
333 onu_adapter_agent.update_interface(onu_device,
334 {'oper_state': 'down'})
335 self.onu_ports_down(onu_device, oper_state)
336
337 # Children devices
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500338 yield self.adapter_agent.update_child_devices_state(
William Kurkian6f436d02019-02-06 16:25:01 -0500339 self.device_id, oper_status=oper_state,
340 connect_status=connect_state)
341 # Device Ports
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500342 device_ports = yield self.adapter_agent.get_ports(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500343 Port.ETHERNET_NNI)
344 logical_ports_ids = [port.label for port in device_ports]
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500345 device_ports += yield self.adapter_agent.get_ports(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500346 Port.PON_OLT)
347
348 for port in device_ports:
349 port.oper_status = oper_state
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500350 yield self.adapter_agent.add_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500351
352 # Device logical port
353 for logical_port_id in logical_ports_ids:
354 logical_port = self.adapter_agent.get_logical_port(
355 self.logical_device_id, logical_port_id)
356 logical_port.ofp_port.state = OFPPS_LINK_DOWN
357 self.adapter_agent.update_logical_port(self.logical_device_id,
358 logical_port)
359
360 # Device
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500361 device = yield self.adapter_agent.get_device(self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500362 device.oper_status = oper_state
363 device.connect_status = connect_state
364
William Kurkianfefd4642019-02-07 15:30:03 -0500365 reactor.callLater(2, self.adapter_agent.device_update, device)
William Kurkian6f436d02019-02-06 16:25:01 -0500366
367 # def post_up(self, event):
368 # self.log.debug('post-up')
369 # self.flow_mgr.reseed_flows()
370
371 def post_down(self, event):
372 self.log.debug('post_down')
373 self.flow_mgr.reset_flows()
374
375 def indications_thread(self):
376 self.log.debug('starting-indications-thread')
377 self.log.debug('connecting to olt', device_id=self.device_id)
378 self.channel_ready_future.result() # blocking call
379 self.log.info('connected to olt', device_id=self.device_id)
380 self.go_state_connected()
381
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500382 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500383 # Don't continue until connected is done
384 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500385 time.sleep(0.5)
386
William Kurkian6f436d02019-02-06 16:25:01 -0500387 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
388
389 while True:
390 try:
391 # get the next indication from olt
392 ind = next(self.indications)
393 except Exception as e:
394 self.log.warn('gRPC connection lost', error=e)
395 reactor.callFromThread(self.go_state_down)
396 reactor.callFromThread(self.go_state_init)
397 break
398 else:
399 self.log.debug("rx indication", indication=ind)
400
401 # indication handlers run in the main event loop
402 if ind.HasField('olt_ind'):
403 reactor.callFromThread(self.olt_indication, ind.olt_ind)
404 elif ind.HasField('intf_ind'):
405 reactor.callFromThread(self.intf_indication, ind.intf_ind)
406 elif ind.HasField('intf_oper_ind'):
407 reactor.callFromThread(self.intf_oper_indication,
408 ind.intf_oper_ind)
409 elif ind.HasField('onu_disc_ind'):
410 reactor.callFromThread(self.onu_discovery_indication,
411 ind.onu_disc_ind)
412 elif ind.HasField('onu_ind'):
413 reactor.callFromThread(self.onu_indication, ind.onu_ind)
414 elif ind.HasField('omci_ind'):
415 reactor.callFromThread(self.omci_indication, ind.omci_ind)
416 elif ind.HasField('pkt_ind'):
417 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
418 elif ind.HasField('port_stats'):
419 reactor.callFromThread(
420 self.stats_mgr.port_statistics_indication,
421 ind.port_stats)
422 elif ind.HasField('flow_stats'):
423 reactor.callFromThread(
424 self.stats_mgr.flow_statistics_indication,
425 ind.flow_stats)
426 elif ind.HasField('alarm_ind'):
427 reactor.callFromThread(self.alarm_mgr.process_alarms,
428 ind.alarm_ind)
429 else:
430 self.log.warn('unknown indication type')
431
432 def olt_indication(self, olt_indication):
433 if olt_indication.oper_state == "up":
434 self.go_state_up()
435 elif olt_indication.oper_state == "down":
436 self.go_state_down()
437
438 def intf_indication(self, intf_indication):
439 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
440 oper_state=intf_indication.oper_state)
441
442 if intf_indication.oper_state == "up":
443 oper_status = OperStatus.ACTIVE
444 else:
445 oper_status = OperStatus.DISCOVERED
446
447 # add_port update the port if it exists
448 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
449
450 def intf_oper_indication(self, intf_oper_indication):
451 self.log.debug("Received interface oper state change indication",
452 intf_id=intf_oper_indication.intf_id,
453 type=intf_oper_indication.type,
454 oper_state=intf_oper_indication.oper_state)
455
456 if intf_oper_indication.oper_state == "up":
457 oper_state = OperStatus.ACTIVE
458 else:
459 oper_state = OperStatus.DISCOVERED
460
461 if intf_oper_indication.type == "nni":
462
463 # add_(logical_)port update the port if it exists
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500464 self.add_port(intf_oper_indication.intf_id,
465 Port.ETHERNET_NNI, oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500466
William Kurkian6f436d02019-02-06 16:25:01 -0500467 elif intf_oper_indication.type == "pon":
468 # FIXME - handle PON oper state change
469 pass
470
Matt Jeanneret6e315092019-02-20 10:42:57 -0500471 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500472 def onu_discovery_indication(self, onu_disc_indication):
473 intf_id = onu_disc_indication.intf_id
474 serial_number = onu_disc_indication.serial_number
475
476 serial_number_str = self.stringify_serial_number(serial_number)
477
478 self.log.debug("onu discovery indication", intf_id=intf_id,
479 serial_number=serial_number_str)
480
Matt Jeanneretb428b952019-03-07 05:14:17 -0500481 if serial_number_str in self.seen_discovery_indications:
482 self.log.debug("skipping-seen-onu-discovery-indication", intf_id=intf_id,
483 serial_number=serial_number_str)
484 return
485 else:
486 self.seen_discovery_indications.append(serial_number_str)
487
William Kurkian6f436d02019-02-06 16:25:01 -0500488 # Post ONU Discover alarm 20180809_0805
489 try:
490 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
491 serial_number=serial_number_str).raise_alarm()
492 except Exception as disc_alarm_error:
493 self.log.exception("onu-discovery-alarm-error",
494 errmsg=disc_alarm_error.message)
495 # continue for now.
496
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400497 onu_device = yield self.core_proxy.get_child_device(
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500498 self.device_id,
499 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500500
501 if onu_device is None:
502 try:
503 onu_id = self.resource_mgr.get_onu_id(intf_id)
504 if onu_id is None:
505 raise Exception("onu-id-unavailable")
506
507 self.add_onu_device(
508 intf_id,
509 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
510 onu_id, serial_number)
511 self.activate_onu(intf_id, onu_id, serial_number,
512 serial_number_str)
513 except Exception as e:
514 self.log.exception('onu-activation-failed', e=e)
515
516 else:
517 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400518 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500519
520 onu_id = onu_device.proxy_address.onu_id
521 if onu_device.oper_status == OperStatus.DISCOVERED \
522 or onu_device.oper_status == OperStatus.ACTIVATING:
523 self.log.debug("ignore onu discovery indication, \
524 the onu has been discovered and should be \
525 activating shorlty", intf_id=intf_id,
526 onu_id=onu_id, state=onu_device.oper_status)
527 elif onu_device.oper_status == OperStatus.ACTIVE:
528 self.log.warn("onu discovery indication whereas onu is \
529 supposed to be active",
530 intf_id=intf_id, onu_id=onu_id,
531 state=onu_device.oper_status)
532 elif onu_device.oper_status == OperStatus.UNKNOWN:
533 self.log.info("onu in unknown state, recovering from olt \
534 reboot probably, activate onu", intf_id=intf_id,
535 onu_id=onu_id, serial_number=serial_number_str)
536
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400537 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500538
William Kurkian6f436d02019-02-06 16:25:01 -0500539 try:
540 self.activate_onu(intf_id, onu_id, serial_number,
541 serial_number_str)
542 except Exception as e:
543 self.log.error('onu-activation-error',
544 serial_number=serial_number_str, error=e)
545 else:
546 self.log.warn('unexpected state', onu_id=onu_id,
547 onu_device_oper_state=onu_device.oper_status)
548
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500549 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500550 def onu_indication(self, onu_indication):
551 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
552 onu_id=onu_indication.onu_id,
553 serial_number=onu_indication.serial_number,
554 oper_state=onu_indication.oper_state,
555 admin_state=onu_indication.admin_state)
556 try:
557 serial_number_str = self.stringify_serial_number(
558 onu_indication.serial_number)
559 except Exception as e:
560 serial_number_str = None
561
562 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400563 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500564 self.device_id,
565 serial_number=serial_number_str)
566 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400567 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500568 self.device_id,
569 parent_port_no=self.platform.intf_id_to_port_no(
570 onu_indication.intf_id, Port.PON_OLT),
571 onu_id=onu_indication.onu_id)
572
573 if onu_device is None:
574 self.log.error('onu not found', intf_id=onu_indication.intf_id,
575 onu_id=onu_indication.onu_id)
576 return
577
578 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
579 != onu_indication.intf_id:
580 self.log.warn('ONU-is-on-a-different-intf-id-now',
581 previous_intf_id=self.platform.intf_id_from_pon_port_no(
582 onu_device.parent_port_no),
583 current_intf_id=onu_indication.intf_id)
584 # FIXME - handle intf_id mismatch (ONU move?)
585
586 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
587 # FIXME - handle onu id mismatch
588 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
589 'the olt rebooted',
590 expected_onu_id=onu_device.proxy_address.onu_id,
591 received_onu_id=onu_indication.onu_id)
592
593 # Admin state
594 if onu_indication.admin_state == 'down':
595 if onu_indication.oper_state != 'down':
596 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
597 oper_state=onu_indication.oper_state)
598 # Forcing the oper state change code to execute
599 onu_indication.oper_state = 'down'
600
601 # Port and logical port update is taken care of by oper state block
602
603 elif onu_indication.admin_state == 'up':
604 pass
605
606 else:
607 self.log.warn('Invalid-or-not-implemented-admin-state',
608 received_admin_state=onu_indication.admin_state)
609
610 self.log.debug('admin-state-dealt-with')
611
William Kurkian6f436d02019-02-06 16:25:01 -0500612 # Operating state
613 if onu_indication.oper_state == 'down':
614
615 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400616 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500617
618 # Move to discovered state
619 self.log.debug('onu-oper-state-is-down')
620
621 if onu_device.oper_status != OperStatus.DISCOVERED:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400622 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500623
Matt Jeanneretb428b952019-03-07 05:14:17 -0500624 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
625
626 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
627 yield self.adapter_proxy.send_inter_adapter_message(
628 msg=onu_indication,
629 type=InterAdapterMessageType.ONU_IND_REQUEST,
630 from_adapter="openolt",
631 to_adapter=onu_device.type,
632 to_device_id=onu_device.id
633 )
William Kurkian6f436d02019-02-06 16:25:01 -0500634
635 elif onu_indication.oper_state == 'up':
636
637 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400638 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500639
640 if onu_device.oper_status != OperStatus.DISCOVERED:
641 self.log.debug("ignore onu indication",
642 intf_id=onu_indication.intf_id,
643 onu_id=onu_indication.onu_id,
644 state=onu_device.oper_status,
645 msg_oper_state=onu_indication.oper_state)
646 return
647
Matt Jeanneretb428b952019-03-07 05:14:17 -0500648 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500649
Matt Jeanneretb428b952019-03-07 05:14:17 -0500650 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
651 yield self.adapter_proxy.send_inter_adapter_message(
652 msg=onu_indication,
653 type=InterAdapterMessageType.ONU_IND_REQUEST,
654 from_adapter="openolt",
655 to_adapter=onu_device.type,
656 to_device_id=onu_device.id
657 )
William Kurkian6f436d02019-02-06 16:25:01 -0500658
659 else:
660 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
661 oper_state=onu_indication.oper_state)
662
663 def onu_ports_down(self, onu_device, oper_state):
664 # Set port oper state to Discovered
665 # add port will update port if it exists
666 # self.adapter_agent.add_port(
667 # self.device_id,
668 # Port(
669 # port_no=uni_no,
670 # label=uni_name,
671 # type=Port.ETHERNET_UNI,
672 # admin_state=onu_device.admin_state,
673 # oper_status=oper_state))
674 # TODO this should be downning ports in onu adatper
675
676 # Disable logical port
677 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
678 for onu_port in onu_ports:
679 self.log.debug('onu-ports-down', onu_port=onu_port)
680 onu_port_id = onu_port.label
681 try:
682 onu_logical_port = self.adapter_agent.get_logical_port(
683 logical_device_id=self.logical_device_id, port_id=onu_port_id)
684 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
685 self.adapter_agent.update_logical_port(
686 logical_device_id=self.logical_device_id,
687 port=onu_logical_port)
688 self.log.debug('cascading-oper-state-to-port-and-logical-port')
689 except KeyError as e:
690 self.log.error('matching-onu-port-label-invalid',
691 onu_id=onu_device.id, olt_id=self.device_id,
692 onu_ports=onu_ports, onu_port_id=onu_port_id,
693 error=e)
694
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500695 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500696 def omci_indication(self, omci_indication):
697
698 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
699 onu_id=omci_indication.onu_id)
700
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400701 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500702 self.device_id, onu_id=omci_indication.onu_id,
703 parent_port_no=self.platform.intf_id_to_port_no(
704 omci_indication.intf_id, Port.PON_OLT), )
705
Matt Jeanneretb428b952019-03-07 05:14:17 -0500706 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
707
708 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
709
710 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
711 yield self.adapter_proxy.send_inter_adapter_message(
712 msg=omci_msg,
713 type=InterAdapterMessageType.OMCI_REQUEST,
714 from_adapter="openolt",
715 to_adapter=onu_device.type,
716 to_device_id=onu_device.id
717 )
William Kurkian6f436d02019-02-06 16:25:01 -0500718
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500719 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500720 def packet_indication(self, pkt_indication):
721
722 self.log.debug("packet indication",
723 intf_type=pkt_indication.intf_type,
724 intf_id=pkt_indication.intf_id,
725 port_no=pkt_indication.port_no,
726 cookie=pkt_indication.cookie,
727 gemport_id=pkt_indication.gemport_id,
728 flow_id=pkt_indication.flow_id)
729
730 if pkt_indication.intf_type == "pon":
731 if pkt_indication.port_no:
732 logical_port_num = pkt_indication.port_no
733 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
734 try:
735 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
736 pkt_indication.gemport_id)
737 onu_id = int(onu_id_uni_id[0])
738 uni_id = int(onu_id_uni_id[1])
739 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
740 if onu_id is None:
741 raise Exception("onu-id-none")
742 if uni_id is None:
743 raise Exception("uni-id-none")
744 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
745 except Exception as e:
746 self.log.error("no-onu-reference-for-gem",
747 gemport_id=pkt_indication.gemport_id, e=e)
748 return
749
750
751 elif pkt_indication.intf_type == "nni":
752 logical_port_num = self.platform.intf_id_to_port_no(
753 pkt_indication.intf_id,
754 Port.ETHERNET_NNI)
755
756 pkt = Ether(pkt_indication.pkt)
757
758 self.log.debug("packet indication",
759 logical_device_id=self.logical_device_id,
760 logical_port_no=logical_port_num)
761
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500762 yield self.adapter_agent.send_packet_in(
William Kurkian6f436d02019-02-06 16:25:01 -0500763 logical_device_id=self.logical_device_id,
764 logical_port_no=logical_port_num,
765 packet=str(pkt))
766
767 def packet_out(self, egress_port, msg):
768 pkt = Ether(msg)
769 self.log.debug('packet out', egress_port=egress_port,
770 device_id=self.device_id,
771 logical_device_id=self.logical_device_id,
772 packet=str(pkt).encode("HEX"))
773
774 # Find port type
775 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
776 if egress_port_type == Port.ETHERNET_UNI:
777
778 if pkt.haslayer(Dot1Q):
779 outer_shim = pkt.getlayer(Dot1Q)
780 if isinstance(outer_shim.payload, Dot1Q):
781 # If double tag, remove the outer tag
782 payload = (
783 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
784 outer_shim.payload
785 )
786 else:
787 payload = pkt
788 else:
789 payload = pkt
790
791 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
792
793 self.log.debug(
794 'sending-packet-to-ONU', egress_port=egress_port,
795 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
796 onu_id=self.platform.onu_id_from_port_num(egress_port),
797 uni_id=self.platform.uni_id_from_port_num(egress_port),
798 port_no=egress_port,
799 packet=str(payload).encode("HEX"))
800
801 onu_pkt = openolt_pb2.OnuPacket(
802 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
803 onu_id=self.platform.onu_id_from_port_num(egress_port),
804 port_no=egress_port,
805 pkt=send_pkt)
806
807 self.stub.OnuPacketOut(onu_pkt)
808
809 elif egress_port_type == Port.ETHERNET_NNI:
810 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
811 packet=str(pkt).encode("HEX"))
812
813 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
814
815 uplink_pkt = openolt_pb2.UplinkPacket(
816 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
817 pkt=send_pkt)
818
819 self.stub.UplinkPacketOut(uplink_pkt)
820
821 else:
822 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
823 egress_port=egress_port,
824 port_type=egress_port_type)
825
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500826 @inlineCallbacks
Matt Jeanneretb428b952019-03-07 05:14:17 -0500827 def process_inter_adapter_message(self, request):
828 self.log.debug('process-inter-adapter-message', msg=request)
829 try:
830 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
831 omci_msg = InterAdapterOmciMessage()
832 request.body.Unpack(omci_msg)
833 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
834
835 onu_device_id = request.header.to_device_id
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400836 onu_device = yield self.core_proxy.get_device(onu_device_id)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500837 self.send_proxied_message(onu_device, omci_msg.message)
838
839 else:
840 self.log.error("inter-adapter-unhandled-type", request=request)
841
842 except Exception as e:
843 self.log.exception("error-processing-inter-adapter-message", e=e)
844
845 def send_proxied_message(self, onu_device, msg):
846
William Kurkian6f436d02019-02-06 16:25:01 -0500847 if onu_device.connect_status != ConnectStatus.REACHABLE:
848 self.log.debug('ONU is not reachable, cannot send OMCI',
849 serial_number=onu_device.serial_number,
850 intf_id=onu_device.proxy_address.channel_id,
851 onu_id=onu_device.proxy_address.onu_id)
852 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500853
854 omci = openolt_pb2.OmciMsg(intf_id=onu_device.proxy_address.channel_id,
855 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
William Kurkian6f436d02019-02-06 16:25:01 -0500856 self.stub.OmciMsgOut(omci)
857
Matt Jeanneretb428b952019-03-07 05:14:17 -0500858 self.log.debug("omci-message-sent", intf_id=onu_device.proxy_address.channel_id,
859 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
860
Matt Jeanneret6e315092019-02-20 10:42:57 -0500861 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500862 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500863 self.log.info("adding-onu", port_no=port_no, onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500864 serial_number=serial_number)
865
William Kurkian6f436d02019-02-06 16:25:01 -0500866 serial_number_str = self.stringify_serial_number(serial_number)
867
Matt Jeanneretb428b952019-03-07 05:14:17 -0500868 # TODO NEW CORE dont hardcode child device type. find some way of determining by vendor in serial number
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400869 yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500870 parent_device_id=self.device_id,
871 parent_port_no=port_no,
872 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500873 channel_id=intf_id,
874 vendor_id=serial_number.vendor_id,
875 serial_number=serial_number_str,
876 onu_id=onu_id
William Kurkian6f436d02019-02-06 16:25:01 -0500877 )
878
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500879 self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
880
William Kurkian6f436d02019-02-06 16:25:01 -0500881 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
882 if port_type is Port.ETHERNET_NNI:
883 return "nni-" + str(port_no)
884 elif port_type is Port.PON_OLT:
885 return "pon" + str(intf_id)
886 elif port_type is Port.ETHERNET_UNI:
887 assert False, 'local UNI management not supported'
888
889 def add_logical_port(self, port_no, intf_id, oper_state):
890 self.log.info('adding-logical-port', port_no=port_no)
891
892 label = self.port_name(port_no, Port.ETHERNET_NNI)
893
894 cap = OFPPF_1GB_FD | OFPPF_FIBER
895 curr_speed = OFPPF_1GB_FD
896 max_speed = OFPPF_1GB_FD
897
898 if oper_state == OperStatus.ACTIVE:
899 of_oper_state = OFPPS_LIVE
900 else:
901 of_oper_state = OFPPS_LINK_DOWN
902
903 ofp = ofp_port(
904 port_no=port_no,
905 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
906 name=label, config=0, state=of_oper_state, curr=cap,
907 advertised=cap, peer=cap, curr_speed=curr_speed,
908 max_speed=max_speed)
909
910 ofp_stats = ofp_port_stats(port_no=port_no)
911
912 logical_port = LogicalPort(
913 id=label, ofp_port=ofp, device_id=self.device_id,
914 device_port_no=port_no, root_port=True,
915 ofp_port_stats=ofp_stats)
916
917 self.adapter_agent.add_logical_port(self.logical_device_id,
918 logical_port)
919
920 def _get_mac_form_port_no(self, port_no):
921 mac = ''
922 for i in range(4):
923 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
924 return '00:00' + mac
925
William Kurkian92bd7122019-02-14 15:26:59 -0500926 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500927 def add_port(self, intf_id, port_type, oper_status):
928 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
929
930 label = self.port_name(port_no, port_type, intf_id)
931
932 self.log.debug('adding-port', port_no=port_no, label=label,
933 port_type=port_type)
934
935 port = Port(port_no=port_no, label=label, type=port_type,
936 admin_state=AdminState.ENABLED, oper_status=oper_status)
937
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400938 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500939
940 def delete_logical_port(self, child_device):
941 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
942 self.logical_device_id))
943 for logical_port in logical_ports:
944 if logical_port.device_id == child_device.id:
945 self.log.debug('delete-logical-port',
946 onu_device_id=child_device.id,
947 logical_port=logical_port)
948 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
949 child_device, logical_port)
950 self.adapter_agent.delete_logical_port(
951 self.logical_device_id, logical_port)
952 return
953
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500954 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500955 def delete_port(self, child_serial_number):
956 ports = self.proxy.get('/devices/{}/ports'.format(
957 self.device_id))
958 for port in ports:
959 if port.label == child_serial_number:
960 self.log.debug('delete-port',
961 onu_serial_number=child_serial_number,
962 port=port)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500963 yield self.adapter_agent.delete_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500964 return
965
966 def update_flow_table(self, flows):
967 self.log.debug('No updates here now, all is done in logical flows '
968 'update')
969
970 def update_logical_flows(self, flows_to_add, flows_to_remove,
971 device_rules_map):
972 if not self.is_state_up():
973 self.log.info('The OLT is not up, we cannot update flows',
974 flows_to_add=[f.id for f in flows_to_add],
975 flows_to_remove=[f.id for f in flows_to_remove])
976 return
977
978 try:
979 self.flow_mgr.update_children_flows(device_rules_map)
980 except Exception as e:
981 self.log.error('Error updating children flows', error=e)
982
983 self.log.debug('logical flows update', flows_to_add=flows_to_add,
984 flows_to_remove=flows_to_remove)
985
986 for flow in flows_to_add:
987
988 try:
989 self.flow_mgr.add_flow(flow)
990 except Exception as e:
991 self.log.error('failed to add flow', flow=flow, e=e)
992
993 for flow in flows_to_remove:
994
995 try:
996 self.flow_mgr.remove_flow(flow)
997 except Exception as e:
998 self.log.error('failed to remove flow', flow=flow, e=e)
999
1000 self.flow_mgr.repush_all_different_flows()
1001
1002 # There has to be a better way to do this
1003 def ip_hex(self, ip):
1004 octets = ip.split(".")
1005 hex_ip = []
1006 for octet in octets:
1007 octet_hex = hex(int(octet))
1008 octet_hex = octet_hex.split('0x')[1]
1009 octet_hex = octet_hex.rjust(2, '0')
1010 hex_ip.append(octet_hex)
1011 return ":".join(hex_ip)
1012
1013 def stringify_vendor_specific(self, vendor_specific):
1014 return ''.join(str(i) for i in [
1015 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1016 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1017 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1018 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1019 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1020 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1021 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1022 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
1023
1024 def stringify_serial_number(self, serial_number):
1025 return ''.join([serial_number.vendor_id,
1026 self.stringify_vendor_specific(
1027 serial_number.vendor_specific)])
1028
1029 def destringify_serial_number(self, serial_number_str):
1030 serial_number = openolt_pb2.SerialNumber(
1031 vendor_id=serial_number_str[:4].encode('utf-8'),
1032 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1033 return serial_number
1034
1035 def disable(self):
1036 self.log.debug('sending-deactivate-olt-message',
1037 device_id=self.device_id)
1038
1039 try:
1040 # Send grpc call
1041 self.stub.DisableOlt(openolt_pb2.Empty())
1042 # The resulting indication will bring the OLT down
1043 # self.go_state_down()
1044 self.log.info('openolt device disabled')
1045 except Exception as e:
1046 self.log.error('Failure to disable openolt device', error=e)
1047
1048 def delete(self):
1049 self.log.info('deleting-olt', device_id=self.device_id,
1050 logical_device_id=self.logical_device_id)
1051
1052 # Clears up the data from the resource manager KV store
1053 # for the device
1054 del self.resource_mgr
1055
1056 try:
1057 # Rebooting to reset the state
1058 self.reboot()
1059 # Removing logical device
1060 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
1061 self.adapter_agent.delete_logical_device(ld)
1062 except Exception as e:
1063 self.log.error('Failure to delete openolt device', error=e)
1064 raise e
1065 else:
1066 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1067
1068 def reenable(self):
1069 self.log.debug('reenabling-olt', device_id=self.device_id)
1070
1071 try:
1072 self.stub.ReenableOlt(openolt_pb2.Empty())
1073
William Kurkian6f436d02019-02-06 16:25:01 -05001074 except Exception as e:
1075 self.log.error('Failure to reenable openolt device', error=e)
1076 else:
1077 self.log.info('openolt device reenabled')
1078
1079 def activate_onu(self, intf_id, onu_id, serial_number,
1080 serial_number_str):
1081 pir = self.bw_mgr.pir(serial_number_str)
1082 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1083 serial_number_str=serial_number_str,
1084 serial_number=serial_number, pir=pir)
1085 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1086 serial_number=serial_number, pir=pir)
1087 self.stub.ActivateOnu(onu)
1088 self.log.info('onu-activated', serial_number=serial_number_str)
1089
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001090 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001091 def delete_child_device(self, child_device):
1092 self.log.debug('sending-deactivate-onu',
1093 olt_device_id=self.device_id,
1094 onu_device=child_device,
1095 onu_serial_number=child_device.serial_number)
1096 try:
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001097 yield self.adapter_agent.delete_child_device(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001098 child_device.id,
1099 child_device)
1100 except Exception as e:
1101 self.log.error('adapter_agent error', error=e)
1102 try:
1103 self.delete_logical_port(child_device)
1104 except Exception as e:
1105 self.log.error('logical_port delete error', error=e)
1106 try:
1107 self.delete_port(child_device.serial_number)
1108 except Exception as e:
1109 self.log.error('port delete error', error=e)
1110 serial_number = self.destringify_serial_number(
1111 child_device.serial_number)
1112 # TODO FIXME - For each uni.
1113 # TODO FIXME - Flows are not deleted
1114 uni_id = 0 # FIXME
1115 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001116 child_device.proxy_address.channel_id,
1117 child_device.proxy_address.onu_id,
1118 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001119 )
1120 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1121 child_device.proxy_address.onu_id,
1122 uni_id)
1123 # Free any PON resources that were reserved for the ONU
1124 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1125
1126 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1127 onu_id=child_device.proxy_address.onu_id,
1128 serial_number=serial_number)
1129 self.stub.DeleteOnu(onu)
1130
1131 def reboot(self):
1132 self.log.debug('rebooting openolt device', device_id=self.device_id)
1133 try:
1134 self.stub.Reboot(openolt_pb2.Empty())
1135 except Exception as e:
1136 self.log.error('something went wrong with the reboot', error=e)
1137 else:
1138 self.log.info('device rebooted')
1139
1140 def trigger_statistics_collection(self):
1141 try:
1142 self.stub.CollectStatistics(openolt_pb2.Empty())
1143 except Exception as e:
1144 self.log.error('Error while triggering statistics collection',
1145 error=e)
1146 else:
1147 self.log.info('statistics requested')
1148
1149 def simulate_alarm(self, alarm):
1150 self.alarm_mgr.simulate_alarm(alarm)