blob: 92a35215f9e722364189357e2388d67a94b005d9 [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 Jeannerete33a7092019-03-12 21:54:14 -040040from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
41 InterAdapterMessageType, InterAdapterOmciMessage
William Kurkian8b1690c2019-03-04 16:53:22 -050042from voltha_protos.logical_device_pb2 import LogicalDevice, LogicalPort
William Kurkian6f436d02019-02-06 16:25:01 -050043
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050044
William Kurkian6f436d02019-02-06 16:25:01 -050045class 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 Jeanneretbad3d982019-03-11 16:06:10 -040088 self.core_proxy = kwargs['core_proxy']
Matt Jeanneret7906d232019-02-14 14:57:38 -050089 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050090 self.device_num = kwargs['device_num']
serkant.uluderyadcfc74d2019-03-17 23:41:42 -070091 self.device = kwargs['device']
William Kurkian6f436d02019-02-06 16:25:01 -050092
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
Matt Jeanneretb428b952019-03-07 05:14:17 -0500100 self.seen_discovery_indications = []
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500101 self.stub = None
William Kurkian27522582019-02-25 14:24:32 -0500102 self.connected = False
William Kurkian6f436d02019-02-06 16:25:01 -0500103 is_reconciliation = kwargs.get('reconciliation', False)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700104 self.device_id = self.device.id
105 self.host_and_port = self.device.host_and_port
106 self.extra_args = self.device.extra_args
Matt Jeannerete33a7092019-03-12 21:54:14 -0400107 self.device_info = None
William Kurkian6f436d02019-02-06 16:25:01 -0500108 self.log = structlog.get_logger(id=self.device_id,
109 ip=self.host_and_port)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500110
William Kurkian6f436d02019-02-06 16:25:01 -0500111 self.log.info('openolt-device-init')
112
113 # default device id and device serial number. If device_info provides better results, they will be updated
114 self.dpid = kwargs.get('dp_id')
115 self.serial_number = self.host_and_port # FIXME
116
117 # Device already set in the event of reconciliation
118 if not is_reconciliation:
119 self.log.info('updating-device')
120 # It is a new device
121 # Update device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700122 self.device.root = True
123 self.device.connect_status = ConnectStatus.UNREACHABLE
124 self.device.oper_status = OperStatus.ACTIVATING
William Kurkian6f436d02019-02-06 16:25:01 -0500125
126 # If logical device does exist use it, else create one after connecting to device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700127 if self.device.parent_id:
William Kurkian6f436d02019-02-06 16:25:01 -0500128 # logical device already exists
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700129 self.logical_device_id = self.device.parent_id
William Kurkian6f436d02019-02-06 16:25:01 -0500130 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
William Kurkian6f436d02019-02-06 16:25:01 -0500140 def stringToMacAddr(self, uri):
141 regex = re.compile('[^a-zA-Z]')
142 uri = regex.sub('', uri)
143
144 l = len(uri)
145 if l > 6:
146 uri = uri[0:6]
147 else:
148 uri = uri + uri[0:6 - l]
149
William Kurkian6f436d02019-02-06 16:25:01 -0500150 return ":".join([hex(ord(x))[-2:] for x in uri])
151
152 def do_state_init(self, event):
153 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500154 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500155 self.channel = grpc.insecure_channel(self.host_and_port)
156 self.channel_ready_future = grpc.channel_ready_future(self.channel)
157
158 self.log.info('openolt-device-created', device_id=self.device_id)
159
160 def post_init(self, event):
161 self.log.debug('post_init')
162
163 # We have reached init state, starting the indications thread
164
165 # Catch RuntimeError exception
166 try:
167 # Start indications thread
168 self.indications_thread_handle = threading.Thread(
169 target=self.indications_thread)
170 # Old getter/setter API for daemon; use it directly as a
171 # property instead. The Jinkins error will happon on the reason of
172 # Exception in thread Thread-1 (most likely raised # during
173 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500174 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500175 self.indications_thread_handle.setDaemon(True)
176 self.indications_thread_handle.start()
177 except Exception as e:
178 self.log.exception('post_init failed', e=e)
179
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500180 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500181 def do_state_connected(self, event):
182 self.log.debug("do_state_connected")
William Kurkian27522582019-02-25 14:24:32 -0500183
William Kurkian6f436d02019-02-06 16:25:01 -0500184 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
185
William Kurkianfefd4642019-02-07 15:30:03 -0500186 delay = 1
187 while True:
188 try:
Matt Jeannerete33a7092019-03-12 21:54:14 -0400189 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
William Kurkianfefd4642019-02-07 15:30:03 -0500190 break
191 except Exception as e:
192 reraise = True
193 if delay > 120:
194 self.log.error("gRPC failure too many times")
195 else:
196 self.log.warn("gRPC failure, retry in %ds: %s"
197 % (delay, repr(e)))
198 time.sleep(delay)
199 delay += delay
200 reraise = False
201
202 if reraise:
203 raise
204
Matt Jeannerete33a7092019-03-12 21:54:14 -0400205 self.log.info('Device connected', device_info=self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500206
Matt Jeanneretaa360912019-04-22 16:23:12 -0400207 # TODO NEW CORE: logical device id is no longer available. use real device id for now
208 self.logical_device_id = self.device_id
209 dpid = self.device_info.device_id
Matt Jeannerete33a7092019-03-12 21:54:14 -0400210 serial_number = self.device_info.device_serial_number
Matt Jeanneretaa360912019-04-22 16:23:12 -0400211
212 if dpid is None: dpid = self.dpid
213 if serial_number is None: serial_number = self.serial_number
214
215 if dpid == None or dpid == '':
216 uri = self.host_and_port.split(":")[0]
217 try:
218 socket.inet_pton(socket.AF_INET, uri)
219 dpid = '00:00:' + self.ip_hex(uri)
220 except socket.error:
221 # this is not an IP
222 dpid = self.stringToMacAddr(uri)
223
224 if serial_number == None or serial_number == '':
225 serial_number = self.host_and_port
226
227 self.log.info('creating-openolt-device', dp_id=dpid, serial_number=serial_number)
228
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700229 self.device.root = True
Matt Jeanneretaa360912019-04-22 16:23:12 -0400230 self.device.serial_number = serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700231 self.device.vendor = self.device_info.vendor
232 self.device.model = self.device_info.model
233 self.device.hardware_version = self.device_info.hardware_version
234 self.device.firmware_version = self.device_info.firmware_version
William Kurkian27522582019-02-25 14:24:32 -0500235
236 # TODO: check for uptime and reboot if too long (VOL-1192)
237
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700238 self.device.connect_status = ConnectStatus.REACHABLE
Matt Jeanneretaa360912019-04-22 16:23:12 -0400239 self.device.mac_address = dpid
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700240 yield self.core_proxy.device_update(self.device)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500241
William Kurkian6f436d02019-02-06 16:25:01 -0500242 self.resource_mgr = self.resource_mgr_class(self.device_id,
243 self.host_and_port,
244 self.extra_args,
Matt Jeannerete33a7092019-03-12 21:54:14 -0400245 self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500246 self.platform = self.platform_class(self.log, self.resource_mgr)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400247 self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.adapter_proxy, self.log,
William Kurkian6f436d02019-02-06 16:25:01 -0500248 self.stub, self.device_id,
249 self.logical_device_id,
250 self.platform, self.resource_mgr)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700251
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400252 self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
William Kurkian6f436d02019-02-06 16:25:01 -0500253 self.device_id,
254 self.logical_device_id,
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700255 self.platform,
256 self.serial_number)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500257 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400258 self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)
William Kurkian27522582019-02-25 14:24:32 -0500259
260 self.connected = True
Matt Jeanneret6e315092019-02-20 10:42:57 -0500261
262 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500263 def do_state_up(self, event):
264 self.log.debug("do_state_up")
265
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400266 yield self.core_proxy.device_state_update(self.device_id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500267 connect_status=ConnectStatus.REACHABLE,
268 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500269 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500270
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500271 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500272 def do_state_down(self, event):
273 self.log.debug("do_state_down")
Arun Arora2e63b1e2019-04-03 12:15:19 +0000274 yield self.core_proxy.device_state_update(self.device_id,
275 connect_status=ConnectStatus.UNREACHABLE,
276 oper_status=OperStatus.UNKNOWN)
277 self.log.debug("done_state_down")
William Kurkian6f436d02019-02-06 16:25:01 -0500278
279 # def post_up(self, event):
280 # self.log.debug('post-up')
281 # self.flow_mgr.reseed_flows()
282
283 def post_down(self, event):
284 self.log.debug('post_down')
285 self.flow_mgr.reset_flows()
286
287 def indications_thread(self):
288 self.log.debug('starting-indications-thread')
289 self.log.debug('connecting to olt', device_id=self.device_id)
290 self.channel_ready_future.result() # blocking call
291 self.log.info('connected to olt', device_id=self.device_id)
292 self.go_state_connected()
293
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500294 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500295 # Don't continue until connected is done
296 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500297 time.sleep(0.5)
298
William Kurkian6f436d02019-02-06 16:25:01 -0500299 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
300
301 while True:
302 try:
303 # get the next indication from olt
304 ind = next(self.indications)
305 except Exception as e:
306 self.log.warn('gRPC connection lost', error=e)
307 reactor.callFromThread(self.go_state_down)
308 reactor.callFromThread(self.go_state_init)
309 break
310 else:
311 self.log.debug("rx indication", indication=ind)
312
313 # indication handlers run in the main event loop
314 if ind.HasField('olt_ind'):
315 reactor.callFromThread(self.olt_indication, ind.olt_ind)
316 elif ind.HasField('intf_ind'):
317 reactor.callFromThread(self.intf_indication, ind.intf_ind)
318 elif ind.HasField('intf_oper_ind'):
319 reactor.callFromThread(self.intf_oper_indication,
320 ind.intf_oper_ind)
321 elif ind.HasField('onu_disc_ind'):
322 reactor.callFromThread(self.onu_discovery_indication,
323 ind.onu_disc_ind)
324 elif ind.HasField('onu_ind'):
325 reactor.callFromThread(self.onu_indication, ind.onu_ind)
326 elif ind.HasField('omci_ind'):
327 reactor.callFromThread(self.omci_indication, ind.omci_ind)
328 elif ind.HasField('pkt_ind'):
329 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
330 elif ind.HasField('port_stats'):
331 reactor.callFromThread(
332 self.stats_mgr.port_statistics_indication,
333 ind.port_stats)
334 elif ind.HasField('flow_stats'):
335 reactor.callFromThread(
336 self.stats_mgr.flow_statistics_indication,
337 ind.flow_stats)
338 elif ind.HasField('alarm_ind'):
339 reactor.callFromThread(self.alarm_mgr.process_alarms,
340 ind.alarm_ind)
341 else:
342 self.log.warn('unknown indication type')
343
344 def olt_indication(self, olt_indication):
345 if olt_indication.oper_state == "up":
346 self.go_state_up()
347 elif olt_indication.oper_state == "down":
348 self.go_state_down()
349
350 def intf_indication(self, intf_indication):
351 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
352 oper_state=intf_indication.oper_state)
353
354 if intf_indication.oper_state == "up":
355 oper_status = OperStatus.ACTIVE
356 else:
357 oper_status = OperStatus.DISCOVERED
358
359 # add_port update the port if it exists
360 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
361
362 def intf_oper_indication(self, intf_oper_indication):
363 self.log.debug("Received interface oper state change indication",
364 intf_id=intf_oper_indication.intf_id,
365 type=intf_oper_indication.type,
366 oper_state=intf_oper_indication.oper_state)
367
368 if intf_oper_indication.oper_state == "up":
369 oper_state = OperStatus.ACTIVE
370 else:
371 oper_state = OperStatus.DISCOVERED
372
373 if intf_oper_indication.type == "nni":
374
375 # add_(logical_)port update the port if it exists
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500376 self.add_port(intf_oper_indication.intf_id,
377 Port.ETHERNET_NNI, oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500378
William Kurkian6f436d02019-02-06 16:25:01 -0500379 elif intf_oper_indication.type == "pon":
380 # FIXME - handle PON oper state change
381 pass
382
Matt Jeanneret6e315092019-02-20 10:42:57 -0500383 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500384 def onu_discovery_indication(self, onu_disc_indication):
385 intf_id = onu_disc_indication.intf_id
386 serial_number = onu_disc_indication.serial_number
387
388 serial_number_str = self.stringify_serial_number(serial_number)
389
390 self.log.debug("onu discovery indication", intf_id=intf_id,
391 serial_number=serial_number_str)
392
Matt Jeanneretb428b952019-03-07 05:14:17 -0500393 if serial_number_str in self.seen_discovery_indications:
394 self.log.debug("skipping-seen-onu-discovery-indication", intf_id=intf_id,
395 serial_number=serial_number_str)
396 return
397 else:
398 self.seen_discovery_indications.append(serial_number_str)
399
William Kurkian6f436d02019-02-06 16:25:01 -0500400 # Post ONU Discover alarm 20180809_0805
401 try:
402 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
403 serial_number=serial_number_str).raise_alarm()
404 except Exception as disc_alarm_error:
405 self.log.exception("onu-discovery-alarm-error",
406 errmsg=disc_alarm_error.message)
407 # continue for now.
408
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400409 onu_device = yield self.core_proxy.get_child_device(
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500410 self.device_id,
411 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500412
413 if onu_device is None:
414 try:
415 onu_id = self.resource_mgr.get_onu_id(intf_id)
416 if onu_id is None:
417 raise Exception("onu-id-unavailable")
418
419 self.add_onu_device(
420 intf_id,
421 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
422 onu_id, serial_number)
423 self.activate_onu(intf_id, onu_id, serial_number,
424 serial_number_str)
425 except Exception as e:
426 self.log.exception('onu-activation-failed', e=e)
427
428 else:
429 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400430 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500431
432 onu_id = onu_device.proxy_address.onu_id
433 if onu_device.oper_status == OperStatus.DISCOVERED \
434 or onu_device.oper_status == OperStatus.ACTIVATING:
435 self.log.debug("ignore onu discovery indication, \
436 the onu has been discovered and should be \
437 activating shorlty", intf_id=intf_id,
438 onu_id=onu_id, state=onu_device.oper_status)
439 elif onu_device.oper_status == OperStatus.ACTIVE:
440 self.log.warn("onu discovery indication whereas onu is \
441 supposed to be active",
442 intf_id=intf_id, onu_id=onu_id,
443 state=onu_device.oper_status)
444 elif onu_device.oper_status == OperStatus.UNKNOWN:
445 self.log.info("onu in unknown state, recovering from olt \
446 reboot probably, activate onu", intf_id=intf_id,
447 onu_id=onu_id, serial_number=serial_number_str)
448
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400449 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500450
William Kurkian6f436d02019-02-06 16:25:01 -0500451 try:
452 self.activate_onu(intf_id, onu_id, serial_number,
453 serial_number_str)
454 except Exception as e:
455 self.log.error('onu-activation-error',
456 serial_number=serial_number_str, error=e)
457 else:
458 self.log.warn('unexpected state', onu_id=onu_id,
459 onu_device_oper_state=onu_device.oper_status)
460
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500461 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500462 def onu_indication(self, onu_indication):
463 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
464 onu_id=onu_indication.onu_id,
465 serial_number=onu_indication.serial_number,
466 oper_state=onu_indication.oper_state,
467 admin_state=onu_indication.admin_state)
468 try:
469 serial_number_str = self.stringify_serial_number(
470 onu_indication.serial_number)
471 except Exception as e:
472 serial_number_str = None
473
474 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400475 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500476 self.device_id,
477 serial_number=serial_number_str)
478 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400479 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500480 self.device_id,
481 parent_port_no=self.platform.intf_id_to_port_no(
482 onu_indication.intf_id, Port.PON_OLT),
483 onu_id=onu_indication.onu_id)
484
485 if onu_device is None:
486 self.log.error('onu not found', intf_id=onu_indication.intf_id,
487 onu_id=onu_indication.onu_id)
488 return
489
490 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
491 != onu_indication.intf_id:
492 self.log.warn('ONU-is-on-a-different-intf-id-now',
493 previous_intf_id=self.platform.intf_id_from_pon_port_no(
494 onu_device.parent_port_no),
495 current_intf_id=onu_indication.intf_id)
496 # FIXME - handle intf_id mismatch (ONU move?)
497
498 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
499 # FIXME - handle onu id mismatch
500 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
501 'the olt rebooted',
502 expected_onu_id=onu_device.proxy_address.onu_id,
503 received_onu_id=onu_indication.onu_id)
504
505 # Admin state
506 if onu_indication.admin_state == 'down':
507 if onu_indication.oper_state != 'down':
508 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
509 oper_state=onu_indication.oper_state)
510 # Forcing the oper state change code to execute
511 onu_indication.oper_state = 'down'
512
513 # Port and logical port update is taken care of by oper state block
514
515 elif onu_indication.admin_state == 'up':
516 pass
517
518 else:
519 self.log.warn('Invalid-or-not-implemented-admin-state',
520 received_admin_state=onu_indication.admin_state)
521
522 self.log.debug('admin-state-dealt-with')
523
William Kurkian6f436d02019-02-06 16:25:01 -0500524 # Operating state
525 if onu_indication.oper_state == 'down':
526
527 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400528 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500529
530 # Move to discovered state
531 self.log.debug('onu-oper-state-is-down')
532
533 if onu_device.oper_status != OperStatus.DISCOVERED:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400534 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500535
Matt Jeanneretb428b952019-03-07 05:14:17 -0500536 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
537
538 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
539 yield self.adapter_proxy.send_inter_adapter_message(
540 msg=onu_indication,
541 type=InterAdapterMessageType.ONU_IND_REQUEST,
542 from_adapter="openolt",
543 to_adapter=onu_device.type,
544 to_device_id=onu_device.id
545 )
William Kurkian6f436d02019-02-06 16:25:01 -0500546
547 elif onu_indication.oper_state == 'up':
548
549 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400550 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500551
552 if onu_device.oper_status != OperStatus.DISCOVERED:
553 self.log.debug("ignore onu indication",
554 intf_id=onu_indication.intf_id,
555 onu_id=onu_indication.onu_id,
556 state=onu_device.oper_status,
557 msg_oper_state=onu_indication.oper_state)
558 return
559
Matt Jeanneretb428b952019-03-07 05:14:17 -0500560 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500561
Matt Jeanneretb428b952019-03-07 05:14:17 -0500562 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
563 yield self.adapter_proxy.send_inter_adapter_message(
564 msg=onu_indication,
565 type=InterAdapterMessageType.ONU_IND_REQUEST,
566 from_adapter="openolt",
567 to_adapter=onu_device.type,
568 to_device_id=onu_device.id
569 )
William Kurkian6f436d02019-02-06 16:25:01 -0500570
571 else:
572 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
573 oper_state=onu_indication.oper_state)
574
575 def onu_ports_down(self, onu_device, oper_state):
576 # Set port oper state to Discovered
577 # add port will update port if it exists
578 # self.adapter_agent.add_port(
579 # self.device_id,
580 # Port(
581 # port_no=uni_no,
582 # label=uni_name,
583 # type=Port.ETHERNET_UNI,
584 # admin_state=onu_device.admin_state,
585 # oper_status=oper_state))
586 # TODO this should be downning ports in onu adatper
587
588 # Disable logical port
589 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
590 for onu_port in onu_ports:
591 self.log.debug('onu-ports-down', onu_port=onu_port)
592 onu_port_id = onu_port.label
593 try:
594 onu_logical_port = self.adapter_agent.get_logical_port(
595 logical_device_id=self.logical_device_id, port_id=onu_port_id)
596 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
597 self.adapter_agent.update_logical_port(
598 logical_device_id=self.logical_device_id,
599 port=onu_logical_port)
600 self.log.debug('cascading-oper-state-to-port-and-logical-port')
601 except KeyError as e:
602 self.log.error('matching-onu-port-label-invalid',
603 onu_id=onu_device.id, olt_id=self.device_id,
604 onu_ports=onu_ports, onu_port_id=onu_port_id,
605 error=e)
606
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500607 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500608 def omci_indication(self, omci_indication):
609
610 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
611 onu_id=omci_indication.onu_id)
612
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400613 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500614 self.device_id, onu_id=omci_indication.onu_id,
615 parent_port_no=self.platform.intf_id_to_port_no(
616 omci_indication.intf_id, Port.PON_OLT), )
617
Matt Jeanneretb428b952019-03-07 05:14:17 -0500618 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
619
620 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
621
622 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
623 yield self.adapter_proxy.send_inter_adapter_message(
624 msg=omci_msg,
625 type=InterAdapterMessageType.OMCI_REQUEST,
626 from_adapter="openolt",
627 to_adapter=onu_device.type,
628 to_device_id=onu_device.id
629 )
William Kurkian6f436d02019-02-06 16:25:01 -0500630
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500631 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500632 def packet_indication(self, pkt_indication):
633
634 self.log.debug("packet indication",
635 intf_type=pkt_indication.intf_type,
636 intf_id=pkt_indication.intf_id,
637 port_no=pkt_indication.port_no,
638 cookie=pkt_indication.cookie,
639 gemport_id=pkt_indication.gemport_id,
640 flow_id=pkt_indication.flow_id)
641
642 if pkt_indication.intf_type == "pon":
643 if pkt_indication.port_no:
Matt Jeannereta591ab82019-04-13 15:54:28 -0400644 port_num = pkt_indication.port_no
William Kurkian6f436d02019-02-06 16:25:01 -0500645 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
646 try:
647 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
648 pkt_indication.gemport_id)
649 onu_id = int(onu_id_uni_id[0])
650 uni_id = int(onu_id_uni_id[1])
651 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
652 if onu_id is None:
653 raise Exception("onu-id-none")
654 if uni_id is None:
655 raise Exception("uni-id-none")
Matt Jeannereta591ab82019-04-13 15:54:28 -0400656 port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500657 except Exception as e:
658 self.log.error("no-onu-reference-for-gem",
659 gemport_id=pkt_indication.gemport_id, e=e)
660 return
661
662
663 elif pkt_indication.intf_type == "nni":
Matt Jeannereta591ab82019-04-13 15:54:28 -0400664 port_num = self.platform.intf_id_to_port_no(
William Kurkian6f436d02019-02-06 16:25:01 -0500665 pkt_indication.intf_id,
666 Port.ETHERNET_NNI)
667
668 pkt = Ether(pkt_indication.pkt)
669
670 self.log.debug("packet indication",
Matt Jeannereta591ab82019-04-13 15:54:28 -0400671 device_id=self.device_id,
672 port_num=port_num)
William Kurkian6f436d02019-02-06 16:25:01 -0500673
Matt Jeannereta591ab82019-04-13 15:54:28 -0400674 yield self.core_proxy.send_packet_in(
675 device_id=self.device_id,
676 port=port_num,
William Kurkian6f436d02019-02-06 16:25:01 -0500677 packet=str(pkt))
678
679 def packet_out(self, egress_port, msg):
680 pkt = Ether(msg)
681 self.log.debug('packet out', egress_port=egress_port,
682 device_id=self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500683 packet=str(pkt).encode("HEX"))
684
685 # Find port type
686 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
687 if egress_port_type == Port.ETHERNET_UNI:
688
689 if pkt.haslayer(Dot1Q):
690 outer_shim = pkt.getlayer(Dot1Q)
691 if isinstance(outer_shim.payload, Dot1Q):
692 # If double tag, remove the outer tag
693 payload = (
694 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
695 outer_shim.payload
696 )
697 else:
698 payload = pkt
699 else:
700 payload = pkt
701
702 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
703
704 self.log.debug(
705 'sending-packet-to-ONU', egress_port=egress_port,
706 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
707 onu_id=self.platform.onu_id_from_port_num(egress_port),
708 uni_id=self.platform.uni_id_from_port_num(egress_port),
709 port_no=egress_port,
710 packet=str(payload).encode("HEX"))
711
712 onu_pkt = openolt_pb2.OnuPacket(
713 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
714 onu_id=self.platform.onu_id_from_port_num(egress_port),
715 port_no=egress_port,
716 pkt=send_pkt)
717
718 self.stub.OnuPacketOut(onu_pkt)
719
720 elif egress_port_type == Port.ETHERNET_NNI:
721 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
722 packet=str(pkt).encode("HEX"))
723
724 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
725
726 uplink_pkt = openolt_pb2.UplinkPacket(
727 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
728 pkt=send_pkt)
729
730 self.stub.UplinkPacketOut(uplink_pkt)
731
732 else:
733 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
734 egress_port=egress_port,
735 port_type=egress_port_type)
736
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500737 @inlineCallbacks
Matt Jeanneretb428b952019-03-07 05:14:17 -0500738 def process_inter_adapter_message(self, request):
739 self.log.debug('process-inter-adapter-message', msg=request)
740 try:
741 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
742 omci_msg = InterAdapterOmciMessage()
743 request.body.Unpack(omci_msg)
744 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
745
746 onu_device_id = request.header.to_device_id
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400747 onu_device = yield self.core_proxy.get_device(onu_device_id)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500748 self.send_proxied_message(onu_device, omci_msg.message)
749
750 else:
751 self.log.error("inter-adapter-unhandled-type", request=request)
752
753 except Exception as e:
754 self.log.exception("error-processing-inter-adapter-message", e=e)
755
756 def send_proxied_message(self, onu_device, msg):
757
William Kurkian6f436d02019-02-06 16:25:01 -0500758 if onu_device.connect_status != ConnectStatus.REACHABLE:
759 self.log.debug('ONU is not reachable, cannot send OMCI',
760 serial_number=onu_device.serial_number,
761 intf_id=onu_device.proxy_address.channel_id,
762 onu_id=onu_device.proxy_address.onu_id)
763 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500764
765 omci = openolt_pb2.OmciMsg(intf_id=onu_device.proxy_address.channel_id,
766 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
William Kurkian6f436d02019-02-06 16:25:01 -0500767 self.stub.OmciMsgOut(omci)
768
Matt Jeanneretb428b952019-03-07 05:14:17 -0500769 self.log.debug("omci-message-sent", intf_id=onu_device.proxy_address.channel_id,
770 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
771
Matt Jeanneret6e315092019-02-20 10:42:57 -0500772 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500773 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500774 self.log.info("adding-onu", port_no=port_no, onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500775 serial_number=serial_number)
776
William Kurkian6f436d02019-02-06 16:25:01 -0500777 serial_number_str = self.stringify_serial_number(serial_number)
778
Matt Jeanneretb428b952019-03-07 05:14:17 -0500779 # 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 -0400780 yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500781 parent_device_id=self.device_id,
782 parent_port_no=port_no,
783 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500784 channel_id=intf_id,
785 vendor_id=serial_number.vendor_id,
786 serial_number=serial_number_str,
787 onu_id=onu_id
William Kurkian6f436d02019-02-06 16:25:01 -0500788 )
789
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500790 self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
791
Matt Jeannerete33a7092019-03-12 21:54:14 -0400792 def get_ofp_device_info(self, device):
793 self.log.info('get_ofp_device_info', device_id=device.id)
794
795 mfr_desc = self.device_info.vendor
796 sw_desc = self.device_info.firmware_version
797 hw_desc = self.device_info.model
798 if self.device_info.hardware_version: hw_desc += '-' + self.device_info.hardware_version
799
800 return SwitchCapability(
801 desc=ofp_desc(
802 hw_desc=hw_desc,
803 sw_desc=sw_desc,
804 serial_num=device.serial_number
805 ),
806 switch_features=ofp_switch_features(
807 n_buffers=256, # Max packets buffered at once # TODO fake for now
808 n_tables=2, # Number of tables supported by datapath # TODO fake for now
809 capabilities=( #Bitmap of support "ofp_capabilities" # TODO fake for now
810 OFPC_FLOW_STATS
811 | OFPC_TABLE_STATS
812 | OFPC_PORT_STATS
813 | OFPC_GROUP_STATS
814 )
815 )
816 )
817
818 def get_ofp_port_info(self, device, port_no):
819 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
820 cap = OFPPF_1GB_FD | OFPPF_FIBER
821 return PortCapability(
822 port=LogicalPort(
823 ofp_port=ofp_port(
824 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
825 config=0,
826 state=OFPPS_LIVE,
827 curr=cap,
828 advertised=cap,
829 peer=cap,
830 curr_speed=OFPPF_1GB_FD,
831 max_speed=OFPPF_1GB_FD
832 ),
833 device_id=device.id,
834 device_port_no=port_no
835 )
836 )
837
William Kurkian6f436d02019-02-06 16:25:01 -0500838 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
839 if port_type is Port.ETHERNET_NNI:
840 return "nni-" + str(port_no)
841 elif port_type is Port.PON_OLT:
842 return "pon" + str(intf_id)
843 elif port_type is Port.ETHERNET_UNI:
844 assert False, 'local UNI management not supported'
845
William Kurkian6f436d02019-02-06 16:25:01 -0500846 def _get_mac_form_port_no(self, port_no):
847 mac = ''
848 for i in range(4):
849 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
850 return '00:00' + mac
851
William Kurkian92bd7122019-02-14 15:26:59 -0500852 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500853 def add_port(self, intf_id, port_type, oper_status):
854 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
855
856 label = self.port_name(port_no, port_type, intf_id)
857
858 self.log.debug('adding-port', port_no=port_no, label=label,
859 port_type=port_type)
860
861 port = Port(port_no=port_no, label=label, type=port_type,
862 admin_state=AdminState.ENABLED, oper_status=oper_status)
863
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400864 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500865
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500866 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500867 def delete_port(self, child_serial_number):
868 ports = self.proxy.get('/devices/{}/ports'.format(
869 self.device_id))
870 for port in ports:
871 if port.label == child_serial_number:
872 self.log.debug('delete-port',
873 onu_serial_number=child_serial_number,
874 port=port)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500875 yield self.adapter_agent.delete_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500876 return
877
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400878 def update_flow_table(self, flow_changes):
William Kurkian6f436d02019-02-06 16:25:01 -0500879
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400880 self.log.debug("update_flow_table", flow_changes=flow_changes)
881
882 flows_to_add = flow_changes.to_add.items
883 flows_to_remove = flow_changes.to_remove.items
884
William Kurkian6f436d02019-02-06 16:25:01 -0500885 if not self.is_state_up():
886 self.log.info('The OLT is not up, we cannot update flows',
887 flows_to_add=[f.id for f in flows_to_add],
888 flows_to_remove=[f.id for f in flows_to_remove])
889 return
890
Matt Jeanneretaa360912019-04-22 16:23:12 -0400891 self.log.debug('flows update', flows_to_add=flows_to_add,
William Kurkian6f436d02019-02-06 16:25:01 -0500892 flows_to_remove=flows_to_remove)
893
894 for flow in flows_to_add:
895
896 try:
897 self.flow_mgr.add_flow(flow)
898 except Exception as e:
899 self.log.error('failed to add flow', flow=flow, e=e)
900
901 for flow in flows_to_remove:
902
903 try:
904 self.flow_mgr.remove_flow(flow)
905 except Exception as e:
906 self.log.error('failed to remove flow', flow=flow, e=e)
907
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400908 # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
909 #self.flow_mgr.repush_all_different_flows()
William Kurkian6f436d02019-02-06 16:25:01 -0500910
911 # There has to be a better way to do this
912 def ip_hex(self, ip):
913 octets = ip.split(".")
914 hex_ip = []
915 for octet in octets:
916 octet_hex = hex(int(octet))
917 octet_hex = octet_hex.split('0x')[1]
918 octet_hex = octet_hex.rjust(2, '0')
919 hex_ip.append(octet_hex)
920 return ":".join(hex_ip)
921
922 def stringify_vendor_specific(self, vendor_specific):
923 return ''.join(str(i) for i in [
924 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
925 hex(ord(vendor_specific[0]) & 0x0f)[2:],
926 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
927 hex(ord(vendor_specific[1]) & 0x0f)[2:],
928 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
929 hex(ord(vendor_specific[2]) & 0x0f)[2:],
930 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
931 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
932
933 def stringify_serial_number(self, serial_number):
934 return ''.join([serial_number.vendor_id,
935 self.stringify_vendor_specific(
936 serial_number.vendor_specific)])
937
938 def destringify_serial_number(self, serial_number_str):
939 serial_number = openolt_pb2.SerialNumber(
940 vendor_id=serial_number_str[:4].encode('utf-8'),
941 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
942 return serial_number
943
944 def disable(self):
945 self.log.debug('sending-deactivate-olt-message',
946 device_id=self.device_id)
947
948 try:
949 # Send grpc call
950 self.stub.DisableOlt(openolt_pb2.Empty())
951 # The resulting indication will bring the OLT down
952 # self.go_state_down()
953 self.log.info('openolt device disabled')
954 except Exception as e:
955 self.log.error('Failure to disable openolt device', error=e)
956
957 def delete(self):
Matt Jeanneretaa360912019-04-22 16:23:12 -0400958 self.log.info('deleting-olt', device_id=self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500959
960 # Clears up the data from the resource manager KV store
961 # for the device
962 del self.resource_mgr
963
964 try:
965 # Rebooting to reset the state
966 self.reboot()
967 # Removing logical device
William Kurkian6f436d02019-02-06 16:25:01 -0500968 except Exception as e:
969 self.log.error('Failure to delete openolt device', error=e)
970 raise e
971 else:
972 self.log.info('successfully-deleted-olt', device_id=self.device_id)
973
974 def reenable(self):
975 self.log.debug('reenabling-olt', device_id=self.device_id)
976
977 try:
978 self.stub.ReenableOlt(openolt_pb2.Empty())
979
William Kurkian6f436d02019-02-06 16:25:01 -0500980 except Exception as e:
981 self.log.error('Failure to reenable openolt device', error=e)
982 else:
983 self.log.info('openolt device reenabled')
984
985 def activate_onu(self, intf_id, onu_id, serial_number,
986 serial_number_str):
987 pir = self.bw_mgr.pir(serial_number_str)
988 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
989 serial_number_str=serial_number_str,
990 serial_number=serial_number, pir=pir)
991 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
992 serial_number=serial_number, pir=pir)
993 self.stub.ActivateOnu(onu)
994 self.log.info('onu-activated', serial_number=serial_number_str)
995
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500996 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500997 def delete_child_device(self, child_device):
998 self.log.debug('sending-deactivate-onu',
999 olt_device_id=self.device_id,
1000 onu_device=child_device,
1001 onu_serial_number=child_device.serial_number)
1002 try:
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001003 yield self.adapter_agent.delete_child_device(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001004 child_device.id,
1005 child_device)
1006 except Exception as e:
1007 self.log.error('adapter_agent error', error=e)
1008 try:
1009 self.delete_logical_port(child_device)
1010 except Exception as e:
1011 self.log.error('logical_port delete error', error=e)
1012 try:
1013 self.delete_port(child_device.serial_number)
1014 except Exception as e:
1015 self.log.error('port delete error', error=e)
1016 serial_number = self.destringify_serial_number(
1017 child_device.serial_number)
1018 # TODO FIXME - For each uni.
1019 # TODO FIXME - Flows are not deleted
1020 uni_id = 0 # FIXME
1021 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001022 child_device.proxy_address.channel_id,
1023 child_device.proxy_address.onu_id,
1024 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001025 )
1026 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1027 child_device.proxy_address.onu_id,
1028 uni_id)
1029 # Free any PON resources that were reserved for the ONU
1030 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1031
1032 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1033 onu_id=child_device.proxy_address.onu_id,
1034 serial_number=serial_number)
1035 self.stub.DeleteOnu(onu)
1036
1037 def reboot(self):
1038 self.log.debug('rebooting openolt device', device_id=self.device_id)
1039 try:
1040 self.stub.Reboot(openolt_pb2.Empty())
1041 except Exception as e:
1042 self.log.error('something went wrong with the reboot', error=e)
1043 else:
1044 self.log.info('device rebooted')
1045
1046 def trigger_statistics_collection(self):
1047 try:
1048 self.stub.CollectStatistics(openolt_pb2.Empty())
1049 except Exception as e:
1050 self.log.error('Error while triggering statistics collection',
1051 error=e)
1052 else:
1053 self.log.info('statistics requested')
1054
1055 def simulate_alarm(self, alarm):
1056 self.alarm_mgr.simulate_alarm(alarm)