blob: 36d482f764920378e5986eb95f4f8201a9c5d5c6 [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']
Mahir Gunyel4d451502019-05-16 14:27:13 -070092 self.onus = dict() # int_id.onu_id -> OnuDevice()
William Kurkian6f436d02019-02-06 16:25:01 -050093 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
William Kurkian6f436d02019-02-06 16:25:01 -0500126 # Initialize the OLT state machine
127 self.machine = Machine(model=self, states=OpenoltDevice.states,
128 transitions=OpenoltDevice.transitions,
129 send_event=True, initial='state_null')
130 self.go_state_init()
131
William Kurkian6f436d02019-02-06 16:25:01 -0500132 def stringToMacAddr(self, uri):
133 regex = re.compile('[^a-zA-Z]')
134 uri = regex.sub('', uri)
135
136 l = len(uri)
137 if l > 6:
138 uri = uri[0:6]
139 else:
140 uri = uri + uri[0:6 - l]
141
William Kurkian6f436d02019-02-06 16:25:01 -0500142 return ":".join([hex(ord(x))[-2:] for x in uri])
143
144 def do_state_init(self, event):
145 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500146 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500147 self.channel = grpc.insecure_channel(self.host_and_port)
148 self.channel_ready_future = grpc.channel_ready_future(self.channel)
149
150 self.log.info('openolt-device-created', device_id=self.device_id)
151
152 def post_init(self, event):
153 self.log.debug('post_init')
154
155 # We have reached init state, starting the indications thread
156
157 # Catch RuntimeError exception
158 try:
159 # Start indications thread
160 self.indications_thread_handle = threading.Thread(
161 target=self.indications_thread)
162 # Old getter/setter API for daemon; use it directly as a
163 # property instead. The Jinkins error will happon on the reason of
164 # Exception in thread Thread-1 (most likely raised # during
165 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500166 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500167 self.indications_thread_handle.setDaemon(True)
168 self.indications_thread_handle.start()
169 except Exception as e:
170 self.log.exception('post_init failed', e=e)
171
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500172 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500173 def do_state_connected(self, event):
174 self.log.debug("do_state_connected")
William Kurkian27522582019-02-25 14:24:32 -0500175
William Kurkian6f436d02019-02-06 16:25:01 -0500176 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
177
William Kurkianfefd4642019-02-07 15:30:03 -0500178 delay = 1
179 while True:
180 try:
Matt Jeannerete33a7092019-03-12 21:54:14 -0400181 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
William Kurkianfefd4642019-02-07 15:30:03 -0500182 break
183 except Exception as e:
184 reraise = True
185 if delay > 120:
186 self.log.error("gRPC failure too many times")
187 else:
188 self.log.warn("gRPC failure, retry in %ds: %s"
189 % (delay, repr(e)))
190 time.sleep(delay)
191 delay += delay
192 reraise = False
193
194 if reraise:
195 raise
196
Matt Jeannerete33a7092019-03-12 21:54:14 -0400197 self.log.info('Device connected', device_info=self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500198
Matt Jeanneretaa360912019-04-22 16:23:12 -0400199 # TODO NEW CORE: logical device id is no longer available. use real device id for now
200 self.logical_device_id = self.device_id
201 dpid = self.device_info.device_id
Matt Jeannerete33a7092019-03-12 21:54:14 -0400202 serial_number = self.device_info.device_serial_number
Matt Jeanneretaa360912019-04-22 16:23:12 -0400203
204 if dpid is None: dpid = self.dpid
205 if serial_number is None: serial_number = self.serial_number
206
207 if dpid == None or dpid == '':
208 uri = self.host_and_port.split(":")[0]
209 try:
210 socket.inet_pton(socket.AF_INET, uri)
211 dpid = '00:00:' + self.ip_hex(uri)
212 except socket.error:
213 # this is not an IP
214 dpid = self.stringToMacAddr(uri)
215
216 if serial_number == None or serial_number == '':
217 serial_number = self.host_and_port
218
219 self.log.info('creating-openolt-device', dp_id=dpid, serial_number=serial_number)
220
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700221 self.device.root = True
Matt Jeanneretaa360912019-04-22 16:23:12 -0400222 self.device.serial_number = serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700223 self.device.vendor = self.device_info.vendor
224 self.device.model = self.device_info.model
225 self.device.hardware_version = self.device_info.hardware_version
226 self.device.firmware_version = self.device_info.firmware_version
William Kurkian27522582019-02-25 14:24:32 -0500227
228 # TODO: check for uptime and reboot if too long (VOL-1192)
229
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700230 self.device.connect_status = ConnectStatus.REACHABLE
Matt Jeanneretaa360912019-04-22 16:23:12 -0400231 self.device.mac_address = dpid
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700232 yield self.core_proxy.device_update(self.device)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500233
William Kurkian6f436d02019-02-06 16:25:01 -0500234 self.resource_mgr = self.resource_mgr_class(self.device_id,
235 self.host_and_port,
236 self.extra_args,
Matt Jeannerete33a7092019-03-12 21:54:14 -0400237 self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500238 self.platform = self.platform_class(self.log, self.resource_mgr)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400239 self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.adapter_proxy, self.log,
William Kurkian6f436d02019-02-06 16:25:01 -0500240 self.stub, self.device_id,
241 self.logical_device_id,
242 self.platform, self.resource_mgr)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700243
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400244 self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
William Kurkian6f436d02019-02-06 16:25:01 -0500245 self.device_id,
246 self.logical_device_id,
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700247 self.platform,
248 self.serial_number)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500249 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400250 self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)
William Kurkian27522582019-02-25 14:24:32 -0500251
252 self.connected = True
Matt Jeanneret6e315092019-02-20 10:42:57 -0500253
254 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500255 def do_state_up(self, event):
256 self.log.debug("do_state_up")
257
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400258 yield self.core_proxy.device_state_update(self.device_id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500259 connect_status=ConnectStatus.REACHABLE,
260 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500261 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500262
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500263 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500264 def do_state_down(self, event):
265 self.log.debug("do_state_down")
Arun Arora2e63b1e2019-04-03 12:15:19 +0000266 yield self.core_proxy.device_state_update(self.device_id,
267 connect_status=ConnectStatus.UNREACHABLE,
268 oper_status=OperStatus.UNKNOWN)
269 self.log.debug("done_state_down")
William Kurkian6f436d02019-02-06 16:25:01 -0500270
271 # def post_up(self, event):
272 # self.log.debug('post-up')
273 # self.flow_mgr.reseed_flows()
274
275 def post_down(self, event):
276 self.log.debug('post_down')
277 self.flow_mgr.reset_flows()
278
279 def indications_thread(self):
280 self.log.debug('starting-indications-thread')
281 self.log.debug('connecting to olt', device_id=self.device_id)
282 self.channel_ready_future.result() # blocking call
283 self.log.info('connected to olt', device_id=self.device_id)
284 self.go_state_connected()
285
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500286 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500287 # Don't continue until connected is done
288 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500289 time.sleep(0.5)
290
William Kurkian6f436d02019-02-06 16:25:01 -0500291 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
292
293 while True:
294 try:
295 # get the next indication from olt
296 ind = next(self.indications)
297 except Exception as e:
298 self.log.warn('gRPC connection lost', error=e)
299 reactor.callFromThread(self.go_state_down)
300 reactor.callFromThread(self.go_state_init)
301 break
302 else:
303 self.log.debug("rx indication", indication=ind)
304
305 # indication handlers run in the main event loop
306 if ind.HasField('olt_ind'):
307 reactor.callFromThread(self.olt_indication, ind.olt_ind)
308 elif ind.HasField('intf_ind'):
309 reactor.callFromThread(self.intf_indication, ind.intf_ind)
310 elif ind.HasField('intf_oper_ind'):
311 reactor.callFromThread(self.intf_oper_indication,
312 ind.intf_oper_ind)
313 elif ind.HasField('onu_disc_ind'):
314 reactor.callFromThread(self.onu_discovery_indication,
315 ind.onu_disc_ind)
316 elif ind.HasField('onu_ind'):
317 reactor.callFromThread(self.onu_indication, ind.onu_ind)
318 elif ind.HasField('omci_ind'):
319 reactor.callFromThread(self.omci_indication, ind.omci_ind)
320 elif ind.HasField('pkt_ind'):
321 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
322 elif ind.HasField('port_stats'):
323 reactor.callFromThread(
324 self.stats_mgr.port_statistics_indication,
325 ind.port_stats)
326 elif ind.HasField('flow_stats'):
327 reactor.callFromThread(
328 self.stats_mgr.flow_statistics_indication,
329 ind.flow_stats)
330 elif ind.HasField('alarm_ind'):
331 reactor.callFromThread(self.alarm_mgr.process_alarms,
332 ind.alarm_ind)
333 else:
334 self.log.warn('unknown indication type')
335
336 def olt_indication(self, olt_indication):
337 if olt_indication.oper_state == "up":
338 self.go_state_up()
339 elif olt_indication.oper_state == "down":
340 self.go_state_down()
341
342 def intf_indication(self, intf_indication):
343 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
344 oper_state=intf_indication.oper_state)
345
346 if intf_indication.oper_state == "up":
347 oper_status = OperStatus.ACTIVE
348 else:
349 oper_status = OperStatus.DISCOVERED
350
351 # add_port update the port if it exists
352 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
353
354 def intf_oper_indication(self, intf_oper_indication):
355 self.log.debug("Received interface oper state change indication",
356 intf_id=intf_oper_indication.intf_id,
357 type=intf_oper_indication.type,
358 oper_state=intf_oper_indication.oper_state)
359
360 if intf_oper_indication.oper_state == "up":
361 oper_state = OperStatus.ACTIVE
362 else:
363 oper_state = OperStatus.DISCOVERED
364
365 if intf_oper_indication.type == "nni":
366
367 # add_(logical_)port update the port if it exists
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500368 self.add_port(intf_oper_indication.intf_id,
369 Port.ETHERNET_NNI, oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500370
William Kurkian6f436d02019-02-06 16:25:01 -0500371 elif intf_oper_indication.type == "pon":
372 # FIXME - handle PON oper state change
373 pass
374
Matt Jeanneret6e315092019-02-20 10:42:57 -0500375 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500376 def onu_discovery_indication(self, onu_disc_indication):
377 intf_id = onu_disc_indication.intf_id
378 serial_number = onu_disc_indication.serial_number
379
380 serial_number_str = self.stringify_serial_number(serial_number)
381
382 self.log.debug("onu discovery indication", intf_id=intf_id,
383 serial_number=serial_number_str)
384
Matt Jeanneretb428b952019-03-07 05:14:17 -0500385 if serial_number_str in self.seen_discovery_indications:
386 self.log.debug("skipping-seen-onu-discovery-indication", intf_id=intf_id,
387 serial_number=serial_number_str)
388 return
389 else:
390 self.seen_discovery_indications.append(serial_number_str)
391
William Kurkian6f436d02019-02-06 16:25:01 -0500392 # Post ONU Discover alarm 20180809_0805
393 try:
394 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
395 serial_number=serial_number_str).raise_alarm()
396 except Exception as disc_alarm_error:
397 self.log.exception("onu-discovery-alarm-error",
398 errmsg=disc_alarm_error.message)
399 # continue for now.
400
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400401 onu_device = yield self.core_proxy.get_child_device(
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500402 self.device_id,
403 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500404
405 if onu_device is None:
406 try:
407 onu_id = self.resource_mgr.get_onu_id(intf_id)
408 if onu_id is None:
409 raise Exception("onu-id-unavailable")
410
411 self.add_onu_device(
412 intf_id,
413 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
414 onu_id, serial_number)
415 self.activate_onu(intf_id, onu_id, serial_number,
416 serial_number_str)
417 except Exception as e:
418 self.log.exception('onu-activation-failed', e=e)
419
420 else:
421 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400422 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500423
424 onu_id = onu_device.proxy_address.onu_id
425 if onu_device.oper_status == OperStatus.DISCOVERED \
426 or onu_device.oper_status == OperStatus.ACTIVATING:
427 self.log.debug("ignore onu discovery indication, \
428 the onu has been discovered and should be \
429 activating shorlty", intf_id=intf_id,
430 onu_id=onu_id, state=onu_device.oper_status)
431 elif onu_device.oper_status == OperStatus.ACTIVE:
432 self.log.warn("onu discovery indication whereas onu is \
433 supposed to be active",
434 intf_id=intf_id, onu_id=onu_id,
435 state=onu_device.oper_status)
436 elif onu_device.oper_status == OperStatus.UNKNOWN:
437 self.log.info("onu in unknown state, recovering from olt \
438 reboot probably, activate onu", intf_id=intf_id,
439 onu_id=onu_id, serial_number=serial_number_str)
440
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400441 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500442
William Kurkian6f436d02019-02-06 16:25:01 -0500443 try:
444 self.activate_onu(intf_id, onu_id, serial_number,
445 serial_number_str)
446 except Exception as e:
447 self.log.error('onu-activation-error',
448 serial_number=serial_number_str, error=e)
449 else:
450 self.log.warn('unexpected state', onu_id=onu_id,
451 onu_device_oper_state=onu_device.oper_status)
452
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500453 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500454 def onu_indication(self, onu_indication):
455 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
456 onu_id=onu_indication.onu_id,
457 serial_number=onu_indication.serial_number,
458 oper_state=onu_indication.oper_state,
459 admin_state=onu_indication.admin_state)
460 try:
461 serial_number_str = self.stringify_serial_number(
462 onu_indication.serial_number)
463 except Exception as e:
464 serial_number_str = None
465
466 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400467 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500468 self.device_id,
469 serial_number=serial_number_str)
470 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400471 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500472 self.device_id,
473 parent_port_no=self.platform.intf_id_to_port_no(
474 onu_indication.intf_id, Port.PON_OLT),
475 onu_id=onu_indication.onu_id)
476
477 if onu_device is None:
478 self.log.error('onu not found', intf_id=onu_indication.intf_id,
479 onu_id=onu_indication.onu_id)
480 return
Mahir Gunyel4d451502019-05-16 14:27:13 -0700481 onu_key = self.form_onu_key(onu_indication.intf_id, onu_indication.onu_id)
482 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type, serial_number_str, onu_indication.onu_id, onu_indication.intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500483 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
484 != onu_indication.intf_id:
485 self.log.warn('ONU-is-on-a-different-intf-id-now',
486 previous_intf_id=self.platform.intf_id_from_pon_port_no(
487 onu_device.parent_port_no),
488 current_intf_id=onu_indication.intf_id)
489 # FIXME - handle intf_id mismatch (ONU move?)
490
491 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
492 # FIXME - handle onu id mismatch
493 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
494 'the olt rebooted',
495 expected_onu_id=onu_device.proxy_address.onu_id,
496 received_onu_id=onu_indication.onu_id)
497
498 # Admin state
499 if onu_indication.admin_state == 'down':
500 if onu_indication.oper_state != 'down':
501 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
502 oper_state=onu_indication.oper_state)
503 # Forcing the oper state change code to execute
504 onu_indication.oper_state = 'down'
505
506 # Port and logical port update is taken care of by oper state block
507
508 elif onu_indication.admin_state == 'up':
509 pass
510
511 else:
512 self.log.warn('Invalid-or-not-implemented-admin-state',
513 received_admin_state=onu_indication.admin_state)
514
515 self.log.debug('admin-state-dealt-with')
516
William Kurkian6f436d02019-02-06 16:25:01 -0500517 # Operating state
518 if onu_indication.oper_state == 'down':
519
520 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400521 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500522
523 # Move to discovered state
524 self.log.debug('onu-oper-state-is-down')
525
526 if onu_device.oper_status != OperStatus.DISCOVERED:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400527 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500528
Matt Jeanneretb428b952019-03-07 05:14:17 -0500529 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
530
531 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
532 yield self.adapter_proxy.send_inter_adapter_message(
533 msg=onu_indication,
534 type=InterAdapterMessageType.ONU_IND_REQUEST,
535 from_adapter="openolt",
536 to_adapter=onu_device.type,
537 to_device_id=onu_device.id
538 )
William Kurkian6f436d02019-02-06 16:25:01 -0500539
540 elif onu_indication.oper_state == 'up':
541
542 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400543 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500544
545 if onu_device.oper_status != OperStatus.DISCOVERED:
546 self.log.debug("ignore onu indication",
547 intf_id=onu_indication.intf_id,
548 onu_id=onu_indication.onu_id,
549 state=onu_device.oper_status,
550 msg_oper_state=onu_indication.oper_state)
551 return
552
Matt Jeanneretb428b952019-03-07 05:14:17 -0500553 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500554
Matt Jeanneretb428b952019-03-07 05:14:17 -0500555 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
556 yield self.adapter_proxy.send_inter_adapter_message(
557 msg=onu_indication,
558 type=InterAdapterMessageType.ONU_IND_REQUEST,
559 from_adapter="openolt",
560 to_adapter=onu_device.type,
561 to_device_id=onu_device.id
562 )
William Kurkian6f436d02019-02-06 16:25:01 -0500563
564 else:
565 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
566 oper_state=onu_indication.oper_state)
William Kurkian23047b92019-05-01 11:02:35 -0400567 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500568 def onu_ports_down(self, onu_device, oper_state):
William Kurkian23047b92019-05-01 11:02:35 -0400569 pass
William Kurkian6f436d02019-02-06 16:25:01 -0500570 # Set port oper state to Discovered
571 # add port will update port if it exists
William Kurkian23047b92019-05-01 11:02:35 -0400572 # yield self.core_proxy.add_port(
William Kurkian6f436d02019-02-06 16:25:01 -0500573 # self.device_id,
574 # Port(
575 # port_no=uni_no,
576 # label=uni_name,
577 # type=Port.ETHERNET_UNI,
578 # admin_state=onu_device.admin_state,
579 # oper_status=oper_state))
580 # TODO this should be downning ports in onu adatper
581
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500582 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500583 def omci_indication(self, omci_indication):
584
585 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
586 onu_id=omci_indication.onu_id)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700587 onu_in_cache = self.onus.get(self.form_onu_key(omci_indication.intf_id, omci_indication.onu_id), default=None)
588 if onu_in_cache is None:
589 self.log.debug('omci indication for a device not in cache.', intf_id=omci_indication.intf_id,
590 onu_id=omci_indication.onu_id)
591 onu_device = yield self.core_proxy.get_child_device(
592 self.device_id, onu_id=omci_indication.onu_id,
593 parent_port_no=self.platform.intf_id_to_port_no(
594 omci_indication.intf_id, Port.PON_OLT), )
595 onu_device_type = onu_device.type
596 onu_device_id = onu_device.id
597 try:
598 serial_number_str = self.stringify_serial_number(omci_indication.serial_number)
599 except Exception as e:
600 serial_number_str = None
601 #if not exist in cache, then add to cache.
602 onu_key = self.form_onu_key(omci_indication.intf_id, omci_indication.onu_id)
603 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type, serial_number_str, omci_indication.onu_id, omci_indication.intf_id)
604 else:
605 onu_device_type = onu_in_cache.device_type
606 onu_device_id = onu_in_cache.device_id
William Kurkian6f436d02019-02-06 16:25:01 -0500607
Matt Jeanneretb428b952019-03-07 05:14:17 -0500608 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
609
610 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
611
612 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
613 yield self.adapter_proxy.send_inter_adapter_message(
614 msg=omci_msg,
615 type=InterAdapterMessageType.OMCI_REQUEST,
616 from_adapter="openolt",
Mahir Gunyel4d451502019-05-16 14:27:13 -0700617 to_adapter=onu_device_type,
618 to_device_id=onu_device_id
Matt Jeanneretb428b952019-03-07 05:14:17 -0500619 )
William Kurkian6f436d02019-02-06 16:25:01 -0500620
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500621 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500622 def packet_indication(self, pkt_indication):
623
624 self.log.debug("packet indication",
625 intf_type=pkt_indication.intf_type,
626 intf_id=pkt_indication.intf_id,
627 port_no=pkt_indication.port_no,
628 cookie=pkt_indication.cookie,
629 gemport_id=pkt_indication.gemport_id,
630 flow_id=pkt_indication.flow_id)
631
632 if pkt_indication.intf_type == "pon":
633 if pkt_indication.port_no:
Matt Jeannereta591ab82019-04-13 15:54:28 -0400634 port_num = pkt_indication.port_no
William Kurkian6f436d02019-02-06 16:25:01 -0500635 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
636 try:
637 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
638 pkt_indication.gemport_id)
639 onu_id = int(onu_id_uni_id[0])
640 uni_id = int(onu_id_uni_id[1])
641 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
642 if onu_id is None:
643 raise Exception("onu-id-none")
644 if uni_id is None:
645 raise Exception("uni-id-none")
Matt Jeannereta591ab82019-04-13 15:54:28 -0400646 port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500647 except Exception as e:
648 self.log.error("no-onu-reference-for-gem",
649 gemport_id=pkt_indication.gemport_id, e=e)
650 return
651
652
653 elif pkt_indication.intf_type == "nni":
Matt Jeannereta591ab82019-04-13 15:54:28 -0400654 port_num = self.platform.intf_id_to_port_no(
William Kurkian6f436d02019-02-06 16:25:01 -0500655 pkt_indication.intf_id,
656 Port.ETHERNET_NNI)
657
658 pkt = Ether(pkt_indication.pkt)
659
660 self.log.debug("packet indication",
Matt Jeannereta591ab82019-04-13 15:54:28 -0400661 device_id=self.device_id,
662 port_num=port_num)
William Kurkian6f436d02019-02-06 16:25:01 -0500663
Matt Jeannereta591ab82019-04-13 15:54:28 -0400664 yield self.core_proxy.send_packet_in(
665 device_id=self.device_id,
666 port=port_num,
William Kurkian6f436d02019-02-06 16:25:01 -0500667 packet=str(pkt))
668
669 def packet_out(self, egress_port, msg):
670 pkt = Ether(msg)
671 self.log.debug('packet out', egress_port=egress_port,
672 device_id=self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500673 packet=str(pkt).encode("HEX"))
674
675 # Find port type
676 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
677 if egress_port_type == Port.ETHERNET_UNI:
678
679 if pkt.haslayer(Dot1Q):
680 outer_shim = pkt.getlayer(Dot1Q)
681 if isinstance(outer_shim.payload, Dot1Q):
682 # If double tag, remove the outer tag
683 payload = (
684 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
685 outer_shim.payload
686 )
687 else:
688 payload = pkt
689 else:
690 payload = pkt
691
692 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
693
694 self.log.debug(
695 'sending-packet-to-ONU', egress_port=egress_port,
696 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
697 onu_id=self.platform.onu_id_from_port_num(egress_port),
698 uni_id=self.platform.uni_id_from_port_num(egress_port),
699 port_no=egress_port,
700 packet=str(payload).encode("HEX"))
701
702 onu_pkt = openolt_pb2.OnuPacket(
703 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
704 onu_id=self.platform.onu_id_from_port_num(egress_port),
705 port_no=egress_port,
706 pkt=send_pkt)
707
708 self.stub.OnuPacketOut(onu_pkt)
709
710 elif egress_port_type == Port.ETHERNET_NNI:
711 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
712 packet=str(pkt).encode("HEX"))
713
714 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
715
716 uplink_pkt = openolt_pb2.UplinkPacket(
717 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
718 pkt=send_pkt)
719
720 self.stub.UplinkPacketOut(uplink_pkt)
721
722 else:
723 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
724 egress_port=egress_port,
725 port_type=egress_port_type)
726
Matt Jeanneretb428b952019-03-07 05:14:17 -0500727 def process_inter_adapter_message(self, request):
728 self.log.debug('process-inter-adapter-message', msg=request)
729 try:
730 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
731 omci_msg = InterAdapterOmciMessage()
732 request.body.Unpack(omci_msg)
733 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
734
Mahir Gunyel4d451502019-05-16 14:27:13 -0700735 #onu_device_id = request.header.to_device_id
736 #onu_device = yield self.core_proxy.get_device(onu_device_id)
737 self.send_proxied_message( omci_msg)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500738
739 else:
740 self.log.error("inter-adapter-unhandled-type", request=request)
741
742 except Exception as e:
743 self.log.exception("error-processing-inter-adapter-message", e=e)
744
Mahir Gunyel4d451502019-05-16 14:27:13 -0700745 def send_proxied_message(self, omci_msg):
Matt Jeanneretb428b952019-03-07 05:14:17 -0500746
Mahir Gunyel4d451502019-05-16 14:27:13 -0700747 if omci_msg.connect_status != ConnectStatus.REACHABLE:
William Kurkian6f436d02019-02-06 16:25:01 -0500748 self.log.debug('ONU is not reachable, cannot send OMCI',
Mahir Gunyel4d451502019-05-16 14:27:13 -0700749 intf_id=omci_msg.proxy_address.channel_id,
750 onu_id=omci_msg.proxy_address.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500751 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500752
Mahir Gunyel4d451502019-05-16 14:27:13 -0700753 omci = openolt_pb2.OmciMsg(intf_id=omci_msg.proxy_address.channel_id,
754 onu_id=omci_msg.proxy_address.onu_id, pkt=str(omci_msg.message))
William Kurkian6f436d02019-02-06 16:25:01 -0500755 self.stub.OmciMsgOut(omci)
756
Mahir Gunyel4d451502019-05-16 14:27:13 -0700757 self.log.debug("omci-message-sent", intf_id=omci_msg.proxy_address.channel_id,
758 onu_id=omci_msg.proxy_address.onu_id, pkt=str(omci_msg.message))
Matt Jeanneretb428b952019-03-07 05:14:17 -0500759
Matt Jeanneret6e315092019-02-20 10:42:57 -0500760 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500761 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500762 self.log.info("adding-onu", port_no=port_no, onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500763 serial_number=serial_number)
764
William Kurkian6f436d02019-02-06 16:25:01 -0500765 serial_number_str = self.stringify_serial_number(serial_number)
766
Matt Jeanneretb428b952019-03-07 05:14:17 -0500767 # 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 -0400768 yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500769 parent_device_id=self.device_id,
770 parent_port_no=port_no,
771 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500772 channel_id=intf_id,
773 vendor_id=serial_number.vendor_id,
774 serial_number=serial_number_str,
775 onu_id=onu_id
William Kurkian6f436d02019-02-06 16:25:01 -0500776 )
777
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500778 self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
779
Matt Jeannerete33a7092019-03-12 21:54:14 -0400780 def get_ofp_device_info(self, device):
781 self.log.info('get_ofp_device_info', device_id=device.id)
782
783 mfr_desc = self.device_info.vendor
784 sw_desc = self.device_info.firmware_version
785 hw_desc = self.device_info.model
786 if self.device_info.hardware_version: hw_desc += '-' + self.device_info.hardware_version
787
788 return SwitchCapability(
789 desc=ofp_desc(
790 hw_desc=hw_desc,
791 sw_desc=sw_desc,
792 serial_num=device.serial_number
793 ),
794 switch_features=ofp_switch_features(
795 n_buffers=256, # Max packets buffered at once # TODO fake for now
796 n_tables=2, # Number of tables supported by datapath # TODO fake for now
797 capabilities=( #Bitmap of support "ofp_capabilities" # TODO fake for now
798 OFPC_FLOW_STATS
799 | OFPC_TABLE_STATS
800 | OFPC_PORT_STATS
801 | OFPC_GROUP_STATS
802 )
803 )
804 )
805
806 def get_ofp_port_info(self, device, port_no):
807 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
808 cap = OFPPF_1GB_FD | OFPPF_FIBER
809 return PortCapability(
810 port=LogicalPort(
811 ofp_port=ofp_port(
812 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
813 config=0,
814 state=OFPPS_LIVE,
815 curr=cap,
816 advertised=cap,
817 peer=cap,
818 curr_speed=OFPPF_1GB_FD,
819 max_speed=OFPPF_1GB_FD
820 ),
821 device_id=device.id,
822 device_port_no=port_no
823 )
824 )
825
William Kurkian6f436d02019-02-06 16:25:01 -0500826 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
827 if port_type is Port.ETHERNET_NNI:
828 return "nni-" + str(port_no)
829 elif port_type is Port.PON_OLT:
830 return "pon" + str(intf_id)
831 elif port_type is Port.ETHERNET_UNI:
832 assert False, 'local UNI management not supported'
833
William Kurkian6f436d02019-02-06 16:25:01 -0500834 def _get_mac_form_port_no(self, port_no):
835 mac = ''
836 for i in range(4):
837 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
838 return '00:00' + mac
839
William Kurkian92bd7122019-02-14 15:26:59 -0500840 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500841 def add_port(self, intf_id, port_type, oper_status):
842 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
843
844 label = self.port_name(port_no, port_type, intf_id)
845
846 self.log.debug('adding-port', port_no=port_no, label=label,
847 port_type=port_type)
848
849 port = Port(port_no=port_no, label=label, type=port_type,
850 admin_state=AdminState.ENABLED, oper_status=oper_status)
851
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400852 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500853
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500854 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500855 def delete_port(self, child_serial_number):
856 ports = self.proxy.get('/devices/{}/ports'.format(
857 self.device_id))
858 for port in ports:
859 if port.label == child_serial_number:
860 self.log.debug('delete-port',
861 onu_serial_number=child_serial_number,
862 port=port)
William Kurkian23047b92019-05-01 11:02:35 -0400863 yield self.core_proxy.port_removed(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500864 return
William Kurkian23047b92019-05-01 11:02:35 -0400865
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400866 def update_flow_table(self, flow_changes):
William Kurkian6f436d02019-02-06 16:25:01 -0500867
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400868 self.log.debug("update_flow_table", flow_changes=flow_changes)
869
870 flows_to_add = flow_changes.to_add.items
871 flows_to_remove = flow_changes.to_remove.items
872
William Kurkian6f436d02019-02-06 16:25:01 -0500873 if not self.is_state_up():
874 self.log.info('The OLT is not up, we cannot update flows',
875 flows_to_add=[f.id for f in flows_to_add],
876 flows_to_remove=[f.id for f in flows_to_remove])
877 return
878
Matt Jeanneretaa360912019-04-22 16:23:12 -0400879 self.log.debug('flows update', flows_to_add=flows_to_add,
William Kurkian6f436d02019-02-06 16:25:01 -0500880 flows_to_remove=flows_to_remove)
881
882 for flow in flows_to_add:
883
884 try:
885 self.flow_mgr.add_flow(flow)
886 except Exception as e:
887 self.log.error('failed to add flow', flow=flow, e=e)
888
889 for flow in flows_to_remove:
890
891 try:
892 self.flow_mgr.remove_flow(flow)
893 except Exception as e:
894 self.log.error('failed to remove flow', flow=flow, e=e)
895
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400896 # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
897 #self.flow_mgr.repush_all_different_flows()
William Kurkian23047b92019-05-01 11:02:35 -0400898
William Kurkian6f436d02019-02-06 16:25:01 -0500899 # There has to be a better way to do this
900 def ip_hex(self, ip):
901 octets = ip.split(".")
902 hex_ip = []
903 for octet in octets:
904 octet_hex = hex(int(octet))
905 octet_hex = octet_hex.split('0x')[1]
906 octet_hex = octet_hex.rjust(2, '0')
907 hex_ip.append(octet_hex)
908 return ":".join(hex_ip)
909
910 def stringify_vendor_specific(self, vendor_specific):
911 return ''.join(str(i) for i in [
912 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
913 hex(ord(vendor_specific[0]) & 0x0f)[2:],
914 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
915 hex(ord(vendor_specific[1]) & 0x0f)[2:],
916 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
917 hex(ord(vendor_specific[2]) & 0x0f)[2:],
918 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
919 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
920
921 def stringify_serial_number(self, serial_number):
922 return ''.join([serial_number.vendor_id,
923 self.stringify_vendor_specific(
924 serial_number.vendor_specific)])
925
926 def destringify_serial_number(self, serial_number_str):
927 serial_number = openolt_pb2.SerialNumber(
928 vendor_id=serial_number_str[:4].encode('utf-8'),
929 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
930 return serial_number
931
932 def disable(self):
933 self.log.debug('sending-deactivate-olt-message',
934 device_id=self.device_id)
935
936 try:
937 # Send grpc call
938 self.stub.DisableOlt(openolt_pb2.Empty())
939 # The resulting indication will bring the OLT down
940 # self.go_state_down()
941 self.log.info('openolt device disabled')
942 except Exception as e:
943 self.log.error('Failure to disable openolt device', error=e)
944
945 def delete(self):
Matt Jeanneretaa360912019-04-22 16:23:12 -0400946 self.log.info('deleting-olt', device_id=self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500947
948 # Clears up the data from the resource manager KV store
949 # for the device
950 del self.resource_mgr
951
952 try:
953 # Rebooting to reset the state
954 self.reboot()
955 # Removing logical device
William Kurkian6f436d02019-02-06 16:25:01 -0500956 except Exception as e:
957 self.log.error('Failure to delete openolt device', error=e)
958 raise e
959 else:
960 self.log.info('successfully-deleted-olt', device_id=self.device_id)
961
962 def reenable(self):
963 self.log.debug('reenabling-olt', device_id=self.device_id)
964
965 try:
966 self.stub.ReenableOlt(openolt_pb2.Empty())
967
William Kurkian6f436d02019-02-06 16:25:01 -0500968 except Exception as e:
969 self.log.error('Failure to reenable openolt device', error=e)
970 else:
971 self.log.info('openolt device reenabled')
972
973 def activate_onu(self, intf_id, onu_id, serial_number,
974 serial_number_str):
975 pir = self.bw_mgr.pir(serial_number_str)
976 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
977 serial_number_str=serial_number_str,
978 serial_number=serial_number, pir=pir)
979 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
980 serial_number=serial_number, pir=pir)
981 self.stub.ActivateOnu(onu)
982 self.log.info('onu-activated', serial_number=serial_number_str)
983
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500984 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500985 def delete_child_device(self, child_device):
986 self.log.debug('sending-deactivate-onu',
987 olt_device_id=self.device_id,
988 onu_device=child_device,
989 onu_serial_number=child_device.serial_number)
990 try:
William Kurkian23047b92019-05-01 11:02:35 -0400991 yield self.core_proxy.child_device_removed(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500992 child_device.id,
993 child_device)
994 except Exception as e:
William Kurkian23047b92019-05-01 11:02:35 -0400995 self.log.error('core_proxy error', error=e)
William Kurkian6f436d02019-02-06 16:25:01 -0500996 try:
997 self.delete_logical_port(child_device)
998 except Exception as e:
999 self.log.error('logical_port delete error', error=e)
1000 try:
1001 self.delete_port(child_device.serial_number)
1002 except Exception as e:
1003 self.log.error('port delete error', error=e)
1004 serial_number = self.destringify_serial_number(
1005 child_device.serial_number)
1006 # TODO FIXME - For each uni.
1007 # TODO FIXME - Flows are not deleted
1008 uni_id = 0 # FIXME
1009 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001010 child_device.proxy_address.channel_id,
1011 child_device.proxy_address.onu_id,
1012 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001013 )
1014 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1015 child_device.proxy_address.onu_id,
1016 uni_id)
1017 # Free any PON resources that were reserved for the ONU
1018 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1019
1020 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1021 onu_id=child_device.proxy_address.onu_id,
1022 serial_number=serial_number)
1023 self.stub.DeleteOnu(onu)
1024
1025 def reboot(self):
1026 self.log.debug('rebooting openolt device', device_id=self.device_id)
1027 try:
1028 self.stub.Reboot(openolt_pb2.Empty())
1029 except Exception as e:
1030 self.log.error('something went wrong with the reboot', error=e)
1031 else:
1032 self.log.info('device rebooted')
1033
1034 def trigger_statistics_collection(self):
1035 try:
1036 self.stub.CollectStatistics(openolt_pb2.Empty())
1037 except Exception as e:
1038 self.log.error('Error while triggering statistics collection',
1039 error=e)
1040 else:
1041 self.log.info('statistics requested')
1042
1043 def simulate_alarm(self, alarm):
1044 self.alarm_mgr.simulate_alarm(alarm)
Mahir Gunyel4d451502019-05-16 14:27:13 -07001045
1046 def form_onu_key(self, intf_id, onu_id):
1047 return str(intf_id) + "." + str(onu_id)
1048
1049
1050class OnuDevice(object):
1051 def __init__(self, device_id, device_type, serialnumber, onu_id, intf_id):
1052 self.device_id = device_id
1053 self.device_type = device_type
1054 self.serialnumber = serialnumber
1055 self.onu_id = onu_id
1056 self.intf_id = intf_id