blob: 4734c50add2dac59f85bcb9b8a353d516883c12b [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 = [
Scott Baker7eb0a932019-07-26 10:33:22 -070059 'state_null', 'state_init', 'state_connected', 'state_up', 'state_down'
60 ]
William Kurkian6f436d02019-02-06 16:25:01 -050061
Scott Baker7eb0a932019-07-26 10:33:22 -070062 transitions = [{
63 'trigger': 'go_state_init',
64 'source': ['state_null', 'state_connected', 'state_down'],
65 'dest': 'state_init',
66 'before': 'do_state_init',
67 'after': 'post_init'
68 }, {
69 'trigger': 'go_state_connected',
70 'source': 'state_init',
71 'dest': 'state_connected',
72 'before': 'do_state_connected'
73 }, {
74 'trigger': 'go_state_up',
75 'source': ['state_connected', 'state_down'],
76 'dest': 'state_up',
77 'before': 'do_state_up'
78 }, {
79 'trigger': 'go_state_down',
80 'source': ['state_up'],
81 'dest': 'state_down',
82 'before': 'do_state_down',
83 'after': 'post_down'
84 }]
William Kurkian6f436d02019-02-06 16:25:01 -050085
86 def __init__(self, **kwargs):
87 super(OpenoltDevice, self).__init__()
88
Matt Jeanneretbad3d982019-03-11 16:06:10 -040089 self.core_proxy = kwargs['core_proxy']
Matt Jeanneret7906d232019-02-14 14:57:38 -050090 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050091 self.device_num = kwargs['device_num']
serkant.uluderyadcfc74d2019-03-17 23:41:42 -070092 self.device = kwargs['device']
William Kurkiandbdc1342019-06-07 16:43:15 -040093 self.onus = dict() # int_id.onu_id -> OnuDevice()
William Kurkian6f436d02019-02-06 16:25:01 -050094 self.platform_class = kwargs['support_classes']['platform']
95 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
96 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
97 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
98 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
99 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
Matt Jeanneret7906d232019-02-14 14:57:38 -0500100
Matt Jeanneretb428b952019-03-07 05:14:17 -0500101 self.seen_discovery_indications = []
Scott Baker7eb0a932019-07-26 10:33:22 -0700102 self.onu_cache = dict()
103 self.indication_retries = 0
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500104 self.stub = None
William Kurkian27522582019-02-25 14:24:32 -0500105 self.connected = False
William Kurkian6f436d02019-02-06 16:25:01 -0500106 is_reconciliation = kwargs.get('reconciliation', False)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700107 self.device_id = self.device.id
108 self.host_and_port = self.device.host_and_port
109 self.extra_args = self.device.extra_args
Matt Jeannerete33a7092019-03-12 21:54:14 -0400110 self.device_info = None
William Kurkian6f436d02019-02-06 16:25:01 -0500111 self.log = structlog.get_logger(id=self.device_id,
112 ip=self.host_and_port)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500113
William Kurkian6f436d02019-02-06 16:25:01 -0500114 self.log.info('openolt-device-init')
115
116 # default device id and device serial number. If device_info provides better results, they will be updated
117 self.dpid = kwargs.get('dp_id')
118 self.serial_number = self.host_and_port # FIXME
119
120 # Device already set in the event of reconciliation
121 if not is_reconciliation:
122 self.log.info('updating-device')
123 # It is a new device
124 # Update device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700125 self.device.root = True
126 self.device.connect_status = ConnectStatus.UNREACHABLE
127 self.device.oper_status = OperStatus.ACTIVATING
William Kurkian6f436d02019-02-06 16:25:01 -0500128
William Kurkian6f436d02019-02-06 16:25:01 -0500129 # Initialize the OLT state machine
Scott Baker7eb0a932019-07-26 10:33:22 -0700130 self.machine = Machine(model=self,
131 states=OpenoltDevice.states,
William Kurkian6f436d02019-02-06 16:25:01 -0500132 transitions=OpenoltDevice.transitions,
Scott Baker7eb0a932019-07-26 10:33:22 -0700133 send_event=True,
134 initial='state_null')
William Kurkian6f436d02019-02-06 16:25:01 -0500135 self.go_state_init()
136
William Kurkian6f436d02019-02-06 16:25:01 -0500137 def stringToMacAddr(self, uri):
138 regex = re.compile('[^a-zA-Z]')
139 uri = regex.sub('', uri)
140
141 l = len(uri)
142 if l > 6:
143 uri = uri[0:6]
144 else:
145 uri = uri + uri[0:6 - l]
146
William Kurkian6f436d02019-02-06 16:25:01 -0500147 return ":".join([hex(ord(x))[-2:] for x in uri])
148
149 def do_state_init(self, event):
150 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500151 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500152 self.channel = grpc.insecure_channel(self.host_and_port)
153 self.channel_ready_future = grpc.channel_ready_future(self.channel)
154
155 self.log.info('openolt-device-created', device_id=self.device_id)
156
157 def post_init(self, event):
158 self.log.debug('post_init')
159
160 # We have reached init state, starting the indications thread
161
162 # Catch RuntimeError exception
163 try:
164 # Start indications thread
165 self.indications_thread_handle = threading.Thread(
166 target=self.indications_thread)
167 # Old getter/setter API for daemon; use it directly as a
168 # property instead. The Jinkins error will happon on the reason of
169 # Exception in thread Thread-1 (most likely raised # during
170 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500171 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500172 self.indications_thread_handle.setDaemon(True)
173 self.indications_thread_handle.start()
174 except Exception as e:
175 self.log.exception('post_init failed', e=e)
176
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500177 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500178 def do_state_connected(self, event):
179 self.log.debug("do_state_connected")
Scott Baker7eb0a932019-07-26 10:33:22 -0700180
William Kurkian6f436d02019-02-06 16:25:01 -0500181 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
182
William Kurkianfefd4642019-02-07 15:30:03 -0500183 delay = 1
184 while True:
185 try:
Matt Jeannerete33a7092019-03-12 21:54:14 -0400186 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
William Kurkianfefd4642019-02-07 15:30:03 -0500187 break
188 except Exception as e:
189 reraise = True
190 if delay > 120:
191 self.log.error("gRPC failure too many times")
192 else:
Scott Baker7eb0a932019-07-26 10:33:22 -0700193 self.log.warn("gRPC failure, retry in %ds: %s" %
194 (delay, repr(e)))
William Kurkianfefd4642019-02-07 15:30:03 -0500195 time.sleep(delay)
196 delay += delay
197 reraise = False
198
199 if reraise:
200 raise
201
Matt Jeannerete33a7092019-03-12 21:54:14 -0400202 self.log.info('Device connected', device_info=self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500203
Matt Jeanneretaa360912019-04-22 16:23:12 -0400204 # TODO NEW CORE: logical device id is no longer available. use real device id for now
205 self.logical_device_id = self.device_id
206 dpid = self.device_info.device_id
Matt Jeannerete33a7092019-03-12 21:54:14 -0400207 serial_number = self.device_info.device_serial_number
Matt Jeanneretaa360912019-04-22 16:23:12 -0400208
209 if dpid is None: dpid = self.dpid
210 if serial_number is None: serial_number = self.serial_number
211
212 if dpid == None or dpid == '':
213 uri = self.host_and_port.split(":")[0]
214 try:
215 socket.inet_pton(socket.AF_INET, uri)
216 dpid = '00:00:' + self.ip_hex(uri)
217 except socket.error:
218 # this is not an IP
219 dpid = self.stringToMacAddr(uri)
220
221 if serial_number == None or serial_number == '':
222 serial_number = self.host_and_port
223
Scott Baker7eb0a932019-07-26 10:33:22 -0700224 self.log.info('creating-openolt-device',
225 dp_id=dpid,
226 serial_number=serial_number)
Matt Jeanneretaa360912019-04-22 16:23:12 -0400227
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700228 self.device.root = True
Matt Jeanneretaa360912019-04-22 16:23:12 -0400229 self.device.serial_number = serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700230 self.device.vendor = self.device_info.vendor
231 self.device.model = self.device_info.model
232 self.device.hardware_version = self.device_info.hardware_version
233 self.device.firmware_version = self.device_info.firmware_version
William Kurkian27522582019-02-25 14:24:32 -0500234
235 # TODO: check for uptime and reboot if too long (VOL-1192)
236
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700237 self.device.connect_status = ConnectStatus.REACHABLE
Matt Jeanneretaa360912019-04-22 16:23:12 -0400238 self.device.mac_address = dpid
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700239 yield self.core_proxy.device_update(self.device)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500240
William Kurkian6f436d02019-02-06 16:25:01 -0500241 self.resource_mgr = self.resource_mgr_class(self.device_id,
242 self.host_and_port,
243 self.extra_args,
Matt Jeannerete33a7092019-03-12 21:54:14 -0400244 self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500245 self.platform = self.platform_class(self.log, self.resource_mgr)
Scott Baker7eb0a932019-07-26 10:33:22 -0700246 self.flow_mgr = self.flow_mgr_class(self.core_proxy,
247 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)
Scott Baker7eb0a932019-07-26 10:33:22 -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)
Scott Baker7eb0a932019-07-26 10:33:22 -0700259
William Kurkian27522582019-02-25 14:24:32 -0500260 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
Scott Baker7eb0a932019-07-26 10:33:22 -0700266 yield self.core_proxy.device_state_update(
267 self.device_id,
268 connect_status=ConnectStatus.REACHABLE,
269 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500270 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500271
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500272 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500273 def do_state_down(self, event):
274 self.log.debug("do_state_down")
Scott Baker7eb0a932019-07-26 10:33:22 -0700275 yield self.core_proxy.device_state_update(
276 self.device_id,
277 connect_status=ConnectStatus.UNREACHABLE,
278 oper_status=OperStatus.UNKNOWN)
Arun Arora2e63b1e2019-04-03 12:15:19 +0000279 self.log.debug("done_state_down")
William Kurkian6f436d02019-02-06 16:25:01 -0500280
281 # def post_up(self, event):
282 # self.log.debug('post-up')
283 # self.flow_mgr.reseed_flows()
284
285 def post_down(self, event):
286 self.log.debug('post_down')
287 self.flow_mgr.reset_flows()
288
289 def indications_thread(self):
290 self.log.debug('starting-indications-thread')
291 self.log.debug('connecting to olt', device_id=self.device_id)
292 self.channel_ready_future.result() # blocking call
293 self.log.info('connected to olt', device_id=self.device_id)
294 self.go_state_connected()
295
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500296 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500297 # Don't continue until connected is done
298 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500299 time.sleep(0.5)
300
William Kurkian6f436d02019-02-06 16:25:01 -0500301 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
302
303 while True:
304 try:
305 # get the next indication from olt
306 ind = next(self.indications)
307 except Exception as e:
308 self.log.warn('gRPC connection lost', error=e)
309 reactor.callFromThread(self.go_state_down)
310 reactor.callFromThread(self.go_state_init)
311 break
312 else:
313 self.log.debug("rx indication", indication=ind)
314
315 # indication handlers run in the main event loop
316 if ind.HasField('olt_ind'):
317 reactor.callFromThread(self.olt_indication, ind.olt_ind)
318 elif ind.HasField('intf_ind'):
319 reactor.callFromThread(self.intf_indication, ind.intf_ind)
320 elif ind.HasField('intf_oper_ind'):
321 reactor.callFromThread(self.intf_oper_indication,
322 ind.intf_oper_ind)
323 elif ind.HasField('onu_disc_ind'):
324 reactor.callFromThread(self.onu_discovery_indication,
325 ind.onu_disc_ind)
326 elif ind.HasField('onu_ind'):
327 reactor.callFromThread(self.onu_indication, ind.onu_ind)
328 elif ind.HasField('omci_ind'):
329 reactor.callFromThread(self.omci_indication, ind.omci_ind)
330 elif ind.HasField('pkt_ind'):
331 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
332 elif ind.HasField('port_stats'):
333 reactor.callFromThread(
334 self.stats_mgr.port_statistics_indication,
335 ind.port_stats)
336 elif ind.HasField('flow_stats'):
337 reactor.callFromThread(
338 self.stats_mgr.flow_statistics_indication,
339 ind.flow_stats)
340 elif ind.HasField('alarm_ind'):
341 reactor.callFromThread(self.alarm_mgr.process_alarms,
342 ind.alarm_ind)
343 else:
344 self.log.warn('unknown indication type')
345
346 def olt_indication(self, olt_indication):
347 if olt_indication.oper_state == "up":
348 self.go_state_up()
349 elif olt_indication.oper_state == "down":
350 self.go_state_down()
351
352 def intf_indication(self, intf_indication):
Scott Baker7eb0a932019-07-26 10:33:22 -0700353 self.log.debug("intf indication",
354 intf_id=intf_indication.intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500355 oper_state=intf_indication.oper_state)
356
357 if intf_indication.oper_state == "up":
358 oper_status = OperStatus.ACTIVE
359 else:
360 oper_status = OperStatus.DISCOVERED
361
362 # add_port update the port if it exists
363 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
364
365 def intf_oper_indication(self, intf_oper_indication):
366 self.log.debug("Received interface oper state change indication",
367 intf_id=intf_oper_indication.intf_id,
368 type=intf_oper_indication.type,
369 oper_state=intf_oper_indication.oper_state)
370
371 if intf_oper_indication.oper_state == "up":
372 oper_state = OperStatus.ACTIVE
373 else:
374 oper_state = OperStatus.DISCOVERED
375
376 if intf_oper_indication.type == "nni":
377
378 # add_(logical_)port update the port if it exists
Scott Baker7eb0a932019-07-26 10:33:22 -0700379 self.add_port(intf_oper_indication.intf_id, Port.ETHERNET_NNI,
380 oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500381
William Kurkian6f436d02019-02-06 16:25:01 -0500382 elif intf_oper_indication.type == "pon":
383 # FIXME - handle PON oper state change
384 pass
385
Matt Jeanneret6e315092019-02-20 10:42:57 -0500386 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500387 def onu_discovery_indication(self, onu_disc_indication):
388 intf_id = onu_disc_indication.intf_id
389 serial_number = onu_disc_indication.serial_number
390
391 serial_number_str = self.stringify_serial_number(serial_number)
392
Scott Baker7eb0a932019-07-26 10:33:22 -0700393 self.log.debug("onu discovery indication",
394 intf_id=intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500395 serial_number=serial_number_str)
396
Matt Jeanneretb428b952019-03-07 05:14:17 -0500397 if serial_number_str in self.seen_discovery_indications:
Scott Baker7eb0a932019-07-26 10:33:22 -0700398 self.log.debug("skipping-seen-onu-discovery-indication",
399 intf_id=intf_id,
Matt Jeanneretb428b952019-03-07 05:14:17 -0500400 serial_number=serial_number_str)
401 return
402 else:
403 self.seen_discovery_indications.append(serial_number_str)
404
Scott Baker7eb0a932019-07-26 10:33:22 -0700405 self.indication_retries = 0
William Kurkian6f436d02019-02-06 16:25:01 -0500406 # Post ONU Discover alarm 20180809_0805
407 try:
Scott Baker7eb0a932019-07-26 10:33:22 -0700408 OnuDiscoveryAlarm(self.alarm_mgr.alarms,
409 pon_id=intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500410 serial_number=serial_number_str).raise_alarm()
411 except Exception as disc_alarm_error:
412 self.log.exception("onu-discovery-alarm-error",
413 errmsg=disc_alarm_error.message)
414 # continue for now.
415
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400416 onu_device = yield self.core_proxy.get_child_device(
Scott Baker7eb0a932019-07-26 10:33:22 -0700417 self.device_id, serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500418
419 if onu_device is None:
420 try:
421 onu_id = self.resource_mgr.get_onu_id(intf_id)
422 if onu_id is None:
423 raise Exception("onu-id-unavailable")
424
425 self.add_onu_device(
426 intf_id,
427 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
428 onu_id, serial_number)
429 self.activate_onu(intf_id, onu_id, serial_number,
430 serial_number_str)
431 except Exception as e:
432 self.log.exception('onu-activation-failed', e=e)
433
434 else:
435 if onu_device.connect_status != ConnectStatus.REACHABLE:
Scott Baker7eb0a932019-07-26 10:33:22 -0700436 yield self.core_proxy.device_state_update(
437 onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500438
439 onu_id = onu_device.proxy_address.onu_id
440 if onu_device.oper_status == OperStatus.DISCOVERED \
441 or onu_device.oper_status == OperStatus.ACTIVATING:
442 self.log.debug("ignore onu discovery indication, \
443 the onu has been discovered and should be \
Scott Baker7eb0a932019-07-26 10:33:22 -0700444 activating shorlty",
445 intf_id=intf_id,
446 onu_id=onu_id,
447 state=onu_device.oper_status)
William Kurkian6f436d02019-02-06 16:25:01 -0500448 elif onu_device.oper_status == OperStatus.ACTIVE:
449 self.log.warn("onu discovery indication whereas onu is \
450 supposed to be active",
Scott Baker7eb0a932019-07-26 10:33:22 -0700451 intf_id=intf_id,
452 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500453 state=onu_device.oper_status)
454 elif onu_device.oper_status == OperStatus.UNKNOWN:
455 self.log.info("onu in unknown state, recovering from olt \
Scott Baker7eb0a932019-07-26 10:33:22 -0700456 reboot probably, activate onu",
457 intf_id=intf_id,
458 onu_id=onu_id,
459 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500460
Scott Baker7eb0a932019-07-26 10:33:22 -0700461 yield self.core_proxy.device_state_update(
462 onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500463
William Kurkian6f436d02019-02-06 16:25:01 -0500464 try:
465 self.activate_onu(intf_id, onu_id, serial_number,
466 serial_number_str)
467 except Exception as e:
468 self.log.error('onu-activation-error',
Scott Baker7eb0a932019-07-26 10:33:22 -0700469 serial_number=serial_number_str,
470 error=e)
William Kurkian6f436d02019-02-06 16:25:01 -0500471 else:
Scott Baker7eb0a932019-07-26 10:33:22 -0700472 self.log.warn('unexpected state',
473 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500474 onu_device_oper_state=onu_device.oper_status)
475
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500476 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500477 def onu_indication(self, onu_indication):
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400478 self.log.debug("onu indication",
Scott Baker7eb0a932019-07-26 10:33:22 -0700479 intf_id=onu_indication.intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500480 onu_id=onu_indication.onu_id,
481 serial_number=onu_indication.serial_number,
482 oper_state=onu_indication.oper_state,
483 admin_state=onu_indication.admin_state)
484 try:
485 serial_number_str = self.stringify_serial_number(
486 onu_indication.serial_number)
487 except Exception as e:
488 serial_number_str = None
489
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400490 onu_device = None
491 found_in_cache = False
Scott Baker7eb0a932019-07-26 10:33:22 -0700492 onu_key = self.form_onu_key(onu_indication.intf_id,
493 onu_indication.onu_id)
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400494
495 if onu_key in self.onu_cache:
496 onu_cache_device = self.onu_cache[onu_key]
497 found_in_cache = True
498 self.log.debug('lookup-updated-device', serial_number=serial_number_str)
499 # If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper.
500 onu_device = yield self.core_proxy.get_device(onu_cache_device.id)
501 else:
502 if serial_number_str is not None:
503 self.log.debug('lookup-device-by-serial-number', serial_number=serial_number_str)
504 onu_device = yield self.core_proxy.get_child_device(
505 self.device_id, serial_number=serial_number_str)
Scott Baker7eb0a932019-07-26 10:33:22 -0700506 else:
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400507 self.log.debug('lookup-device-by-parent-port', intf_id=onu_indication.intf_id, onu_id=onu_indication.onu_id)
508 onu_device = yield self.core_proxy.get_child_device(
509 self.device_id,
510 parent_port_no=self.platform.intf_id_to_port_no(
511 onu_indication.intf_id, Port.PON_OLT),
512 onu_id=onu_indication.onu_id)
513
514 if onu_device is None:
515 self.log.warn('onu not found in cache nor core, trying again',
516 intf_id=onu_indication.intf_id,
517 onu_id=onu_indication.onu_id)
518 if self.indication_retries < 10:
519 self.indication_retries += 1
520 reactor.callLater(3, self.onu_indication, onu_indication)
521 else:
522 self.log.error('onu not found in cache nor core, giving up',
Scott Baker7eb0a932019-07-26 10:33:22 -0700523 intf_id=onu_indication.intf_id,
524 onu_id=onu_indication.onu_id)
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400525 return
526
527 self.log.debug('onu-device-found', onu_device=onu_device)
Scott Baker7eb0a932019-07-26 10:33:22 -0700528
529 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type,
530 serial_number_str,
531 onu_indication.onu_id,
532 onu_indication.intf_id)
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400533
William Kurkian6f436d02019-02-06 16:25:01 -0500534 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
535 != onu_indication.intf_id:
Scott Baker7eb0a932019-07-26 10:33:22 -0700536 self.log.warn(
537 'ONU-is-on-a-different-intf-id-now',
538 previous_intf_id=self.platform.intf_id_from_pon_port_no(
539 onu_device.parent_port_no),
540 current_intf_id=onu_indication.intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500541 # FIXME - handle intf_id mismatch (ONU move?)
542
543 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
544 # FIXME - handle onu id mismatch
Scott Baker7eb0a932019-07-26 10:33:22 -0700545 self.log.warn(
546 'ONU-id-mismatch, can happen if both voltha and '
547 'the olt rebooted',
548 expected_onu_id=onu_device.proxy_address.onu_id,
549 received_onu_id=onu_indication.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500550
551 # Admin state
552 if onu_indication.admin_state == 'down':
553 if onu_indication.oper_state != 'down':
554 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
555 oper_state=onu_indication.oper_state)
556 # Forcing the oper state change code to execute
557 onu_indication.oper_state = 'down'
558
559 # Port and logical port update is taken care of by oper state block
560
561 elif onu_indication.admin_state == 'up':
562 pass
563
564 else:
565 self.log.warn('Invalid-or-not-implemented-admin-state',
566 received_admin_state=onu_indication.admin_state)
567
568 self.log.debug('admin-state-dealt-with')
569
William Kurkian6f436d02019-02-06 16:25:01 -0500570 # Operating state
571 if onu_indication.oper_state == 'down':
572
573 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Scott Baker7eb0a932019-07-26 10:33:22 -0700574 yield self.core_proxy.device_state_update(
575 onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500576
577 # Move to discovered state
578 self.log.debug('onu-oper-state-is-down')
579
580 if onu_device.oper_status != OperStatus.DISCOVERED:
Scott Baker7eb0a932019-07-26 10:33:22 -0700581 yield self.core_proxy.device_state_update(
582 onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500583
Scott Baker7eb0a932019-07-26 10:33:22 -0700584 self.log.debug('inter-adapter-send-onu-ind',
585 onu_indication=onu_indication)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500586
587 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
588 yield self.adapter_proxy.send_inter_adapter_message(
589 msg=onu_indication,
590 type=InterAdapterMessageType.ONU_IND_REQUEST,
591 from_adapter="openolt",
592 to_adapter=onu_device.type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700593 to_device_id=onu_device.id)
William Kurkian6f436d02019-02-06 16:25:01 -0500594
595 elif onu_indication.oper_state == 'up':
596
597 if onu_device.connect_status != ConnectStatus.REACHABLE:
Scott Baker7eb0a932019-07-26 10:33:22 -0700598 yield self.core_proxy.device_state_update(
599 onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500600
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400601 if not found_in_cache and onu_device.oper_status != OperStatus.DISCOVERED:
Scott Baker7eb0a932019-07-26 10:33:22 -0700602 if serial_number_str not in self.seen_discovery_indications:
603 self.log.debug("Ignore ONU indication",
604 intf_id=onu_indication.intf_id,
605 onu_id=onu_indication.onu_id,
606 state=onu_device.oper_status,
607 msg_oper_state=onu_indication.oper_state)
608 return
William Kurkian6f436d02019-02-06 16:25:01 -0500609
Scott Baker7eb0a932019-07-26 10:33:22 -0700610 self.log.debug('inter-adapter-send-onu-ind',
611 onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500612
Matt Jeanneretb428b952019-03-07 05:14:17 -0500613 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
614 yield self.adapter_proxy.send_inter_adapter_message(
615 msg=onu_indication,
616 type=InterAdapterMessageType.ONU_IND_REQUEST,
617 from_adapter="openolt",
618 to_adapter=onu_device.type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700619 to_device_id=onu_device.id)
William Kurkian6f436d02019-02-06 16:25:01 -0500620
621 else:
622 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
623 oper_state=onu_indication.oper_state)
Scott Baker7eb0a932019-07-26 10:33:22 -0700624
William Kurkian23047b92019-05-01 11:02:35 -0400625 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500626 def onu_ports_down(self, onu_device, oper_state):
William Kurkian23047b92019-05-01 11:02:35 -0400627 pass
William Kurkian6f436d02019-02-06 16:25:01 -0500628 # Set port oper state to Discovered
629 # add port will update port if it exists
William Kurkian23047b92019-05-01 11:02:35 -0400630 # yield self.core_proxy.add_port(
William Kurkian6f436d02019-02-06 16:25:01 -0500631 # self.device_id,
632 # Port(
633 # port_no=uni_no,
634 # label=uni_name,
635 # type=Port.ETHERNET_UNI,
636 # admin_state=onu_device.admin_state,
637 # oper_status=oper_state))
638 # TODO this should be downning ports in onu adatper
639
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500640 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500641 def omci_indication(self, omci_indication):
642
Scott Baker7eb0a932019-07-26 10:33:22 -0700643 self.log.debug("omci indication",
644 intf_id=omci_indication.intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500645 onu_id=omci_indication.onu_id)
Scott Baker7eb0a932019-07-26 10:33:22 -0700646 onu_in_cache = self.onus.get(
647 self.form_onu_key(omci_indication.intf_id, omci_indication.onu_id),
648 None)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700649 if onu_in_cache is None:
Scott Baker7eb0a932019-07-26 10:33:22 -0700650 self.log.debug('omci indication for a device not in cache.',
651 intf_id=omci_indication.intf_id,
652 onu_id=omci_indication.onu_id)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700653 onu_device = yield self.core_proxy.get_child_device(
Scott Baker7eb0a932019-07-26 10:33:22 -0700654 self.device_id,
655 onu_id=omci_indication.onu_id,
Mahir Gunyel4d451502019-05-16 14:27:13 -0700656 parent_port_no=self.platform.intf_id_to_port_no(
Scott Baker7eb0a932019-07-26 10:33:22 -0700657 omci_indication.intf_id, Port.PON_OLT),
658 )
Mahir Gunyel4d451502019-05-16 14:27:13 -0700659 onu_device_type = onu_device.type
660 onu_device_id = onu_device.id
661 try:
Scott Baker7eb0a932019-07-26 10:33:22 -0700662 serial_number_str = self.stringify_serial_number(
663 omci_indication.serial_number)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700664 except Exception as e:
665 serial_number_str = None
Scott Baker7eb0a932019-07-26 10:33:22 -0700666
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400667 #if not exist in cache, then add to cache.
Scott Baker7eb0a932019-07-26 10:33:22 -0700668 onu_key = self.form_onu_key(omci_indication.intf_id,
669 omci_indication.onu_id)
670 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type,
671 serial_number_str,
672 omci_indication.onu_id,
673 omci_indication.intf_id)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700674 else:
675 onu_device_type = onu_in_cache.device_type
676 onu_device_id = onu_in_cache.device_id
William Kurkian6f436d02019-02-06 16:25:01 -0500677
Matt Jeanneretb428b952019-03-07 05:14:17 -0500678 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
679
680 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
681
682 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
683 yield self.adapter_proxy.send_inter_adapter_message(
684 msg=omci_msg,
685 type=InterAdapterMessageType.OMCI_REQUEST,
686 from_adapter="openolt",
Mahir Gunyel4d451502019-05-16 14:27:13 -0700687 to_adapter=onu_device_type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700688 to_device_id=onu_device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500689
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500690 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500691 def packet_indication(self, pkt_indication):
692
693 self.log.debug("packet indication",
694 intf_type=pkt_indication.intf_type,
695 intf_id=pkt_indication.intf_id,
696 port_no=pkt_indication.port_no,
697 cookie=pkt_indication.cookie,
698 gemport_id=pkt_indication.gemport_id,
699 flow_id=pkt_indication.flow_id)
700
701 if pkt_indication.intf_type == "pon":
702 if pkt_indication.port_no:
Matt Jeannereta591ab82019-04-13 15:54:28 -0400703 port_num = pkt_indication.port_no
William Kurkian6f436d02019-02-06 16:25:01 -0500704 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
705 try:
Scott Baker7eb0a932019-07-26 10:33:22 -0700706 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(
707 pkt_indication.intf_id, pkt_indication.gemport_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500708 onu_id = int(onu_id_uni_id[0])
709 uni_id = int(onu_id_uni_id[1])
Scott Baker7eb0a932019-07-26 10:33:22 -0700710 self.log.debug("packet indication-kv",
711 onu_id=onu_id,
712 uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500713 if onu_id is None:
714 raise Exception("onu-id-none")
715 if uni_id is None:
716 raise Exception("uni-id-none")
Scott Baker7eb0a932019-07-26 10:33:22 -0700717 port_num = self.platform.mk_uni_port_num(
718 pkt_indication.intf_id, onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500719 except Exception as e:
720 self.log.error("no-onu-reference-for-gem",
Scott Baker7eb0a932019-07-26 10:33:22 -0700721 gemport_id=pkt_indication.gemport_id,
722 e=e)
William Kurkian6f436d02019-02-06 16:25:01 -0500723 return
724
William Kurkian6f436d02019-02-06 16:25:01 -0500725 elif pkt_indication.intf_type == "nni":
Matt Jeannereta591ab82019-04-13 15:54:28 -0400726 port_num = self.platform.intf_id_to_port_no(
Scott Baker7eb0a932019-07-26 10:33:22 -0700727 pkt_indication.intf_id, Port.ETHERNET_NNI)
William Kurkian6f436d02019-02-06 16:25:01 -0500728
729 pkt = Ether(pkt_indication.pkt)
730
731 self.log.debug("packet indication",
Matt Jeannereta591ab82019-04-13 15:54:28 -0400732 device_id=self.device_id,
733 port_num=port_num)
William Kurkian6f436d02019-02-06 16:25:01 -0500734
Scott Baker7eb0a932019-07-26 10:33:22 -0700735 yield self.core_proxy.send_packet_in(device_id=self.device_id,
736 port=port_num,
737 packet=str(pkt))
William Kurkian6f436d02019-02-06 16:25:01 -0500738
739 def packet_out(self, egress_port, msg):
740 pkt = Ether(msg)
Scott Baker7eb0a932019-07-26 10:33:22 -0700741 self.log.debug('packet out',
742 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500743 device_id=self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500744 packet=str(pkt).encode("HEX"))
745
746 # Find port type
747 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
748 if egress_port_type == Port.ETHERNET_UNI:
749
750 if pkt.haslayer(Dot1Q):
751 outer_shim = pkt.getlayer(Dot1Q)
752 if isinstance(outer_shim.payload, Dot1Q):
753 # If double tag, remove the outer tag
754 payload = (
Scott Baker7eb0a932019-07-26 10:33:22 -0700755 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
756 outer_shim.payload)
William Kurkian6f436d02019-02-06 16:25:01 -0500757 else:
758 payload = pkt
759 else:
760 payload = pkt
761
762 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
763
764 self.log.debug(
Scott Baker7eb0a932019-07-26 10:33:22 -0700765 'sending-packet-to-ONU',
766 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500767 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
768 onu_id=self.platform.onu_id_from_port_num(egress_port),
769 uni_id=self.platform.uni_id_from_port_num(egress_port),
770 port_no=egress_port,
771 packet=str(payload).encode("HEX"))
772
773 onu_pkt = openolt_pb2.OnuPacket(
774 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
775 onu_id=self.platform.onu_id_from_port_num(egress_port),
776 port_no=egress_port,
777 pkt=send_pkt)
778
779 self.stub.OnuPacketOut(onu_pkt)
780
781 elif egress_port_type == Port.ETHERNET_NNI:
Scott Baker7eb0a932019-07-26 10:33:22 -0700782 self.log.debug('sending-packet-to-uplink',
783 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500784 packet=str(pkt).encode("HEX"))
785
786 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
787
788 uplink_pkt = openolt_pb2.UplinkPacket(
789 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
790 pkt=send_pkt)
791
792 self.stub.UplinkPacketOut(uplink_pkt)
793
794 else:
795 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
796 egress_port=egress_port,
797 port_type=egress_port_type)
798
Matt Jeanneretb428b952019-03-07 05:14:17 -0500799 def process_inter_adapter_message(self, request):
800 self.log.debug('process-inter-adapter-message', msg=request)
801 try:
802 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
803 omci_msg = InterAdapterOmciMessage()
804 request.body.Unpack(omci_msg)
805 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
806
Mahir Gunyel4d451502019-05-16 14:27:13 -0700807 #onu_device_id = request.header.to_device_id
808 #onu_device = yield self.core_proxy.get_device(onu_device_id)
Scott Baker7eb0a932019-07-26 10:33:22 -0700809 self.send_proxied_message(omci_msg)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500810
811 else:
812 self.log.error("inter-adapter-unhandled-type", request=request)
813
814 except Exception as e:
815 self.log.exception("error-processing-inter-adapter-message", e=e)
816
Mahir Gunyel4d451502019-05-16 14:27:13 -0700817 def send_proxied_message(self, omci_msg):
Matt Jeanneretb428b952019-03-07 05:14:17 -0500818
Mahir Gunyel4d451502019-05-16 14:27:13 -0700819 if omci_msg.connect_status != ConnectStatus.REACHABLE:
William Kurkian6f436d02019-02-06 16:25:01 -0500820 self.log.debug('ONU is not reachable, cannot send OMCI',
Mahir Gunyel4d451502019-05-16 14:27:13 -0700821 intf_id=omci_msg.proxy_address.channel_id,
822 onu_id=omci_msg.proxy_address.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500823 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500824
Mahir Gunyel4d451502019-05-16 14:27:13 -0700825 omci = openolt_pb2.OmciMsg(intf_id=omci_msg.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -0700826 onu_id=omci_msg.proxy_address.onu_id,
827 pkt=str(omci_msg.message))
William Kurkian6f436d02019-02-06 16:25:01 -0500828 self.stub.OmciMsgOut(omci)
829
Scott Baker7eb0a932019-07-26 10:33:22 -0700830 self.log.debug("omci-message-sent",
831 intf_id=omci_msg.proxy_address.channel_id,
832 onu_id=omci_msg.proxy_address.onu_id,
833 pkt=str(omci_msg.message))
Matt Jeanneretb428b952019-03-07 05:14:17 -0500834
Matt Jeanneret6e315092019-02-20 10:42:57 -0500835 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500836 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -0700837 self.log.info("adding-onu",
838 port_no=port_no,
839 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500840 serial_number=serial_number)
841
William Kurkian6f436d02019-02-06 16:25:01 -0500842 serial_number_str = self.stringify_serial_number(serial_number)
843
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400844 onu_device = yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500845 parent_device_id=self.device_id,
846 parent_port_no=port_no,
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400847 child_device_type='',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500848 channel_id=intf_id,
849 vendor_id=serial_number.vendor_id,
850 serial_number=serial_number_str,
Scott Baker7eb0a932019-07-26 10:33:22 -0700851 onu_id=onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500852
Scott Baker7eb0a932019-07-26 10:33:22 -0700853 self.log.debug("onu-added",
854 onu_id=onu_id,
855 port_no=port_no,
856 serial_number=serial_number_str)
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500857
Matt Jeanneret8fa5c302019-08-09 11:28:58 -0400858 self.log.debug('Adding ONU device to the cache',
859 intf_id=intf_id,
860 onu_id=onu_id)
861 onu_key = self.form_onu_key(intf_id, onu_id)
862 self.onu_cache[onu_key] = onu_device
863
Scott Baker7eb0a932019-07-26 10:33:22 -0700864 yield self.core_proxy.device_state_update(
865 onu_device.id,
866 oper_status=OperStatus.DISCOVERED,
867 connect_status=ConnectStatus.REACHABLE)
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400868
Scott Baker7eb0a932019-07-26 10:33:22 -0700869 self.log.debug("set-onu-discovered",
870 onu_id=onu_id,
871 port_no=port_no,
872 serial_number=serial_number_str,
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400873 onu_device=onu_device)
Scott Baker7eb0a932019-07-26 10:33:22 -0700874 return
875
Matt Jeannerete33a7092019-03-12 21:54:14 -0400876 def get_ofp_device_info(self, device):
877 self.log.info('get_ofp_device_info', device_id=device.id)
878
879 mfr_desc = self.device_info.vendor
880 sw_desc = self.device_info.firmware_version
881 hw_desc = self.device_info.model
Scott Baker7eb0a932019-07-26 10:33:22 -0700882 if self.device_info.hardware_version:
883 hw_desc += '-' + self.device_info.hardware_version
Matt Jeannerete33a7092019-03-12 21:54:14 -0400884
885 return SwitchCapability(
Scott Baker7eb0a932019-07-26 10:33:22 -0700886 desc=ofp_desc(hw_desc=hw_desc,
887 sw_desc=sw_desc,
888 serial_num=device.serial_number),
Matt Jeannerete33a7092019-03-12 21:54:14 -0400889 switch_features=ofp_switch_features(
Scott Baker7eb0a932019-07-26 10:33:22 -0700890 n_buffers=
891 256, # Max packets buffered at once # TODO fake for now
892 n_tables=
893 2, # Number of tables supported by datapath # TODO fake for now
894 capabilities=
895 ( #Bitmap of support "ofp_capabilities" # TODO fake for now
896 OFPC_FLOW_STATS
897 | OFPC_TABLE_STATS
898 | OFPC_PORT_STATS
899 | OFPC_GROUP_STATS)))
Matt Jeannerete33a7092019-03-12 21:54:14 -0400900
901 def get_ofp_port_info(self, device, port_no):
Scott Baker7eb0a932019-07-26 10:33:22 -0700902 self.log.info('get_ofp_port_info',
903 port_no=port_no,
904 device_id=device.id)
Matt Jeannerete33a7092019-03-12 21:54:14 -0400905 cap = OFPPF_1GB_FD | OFPPF_FIBER
Scott Baker7eb0a932019-07-26 10:33:22 -0700906 return PortCapability(port=LogicalPort(ofp_port=ofp_port(
907 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
908 config=0,
909 state=OFPPS_LIVE,
910 curr=cap,
911 advertised=cap,
912 peer=cap,
913 curr_speed=OFPPF_1GB_FD,
914 max_speed=OFPPF_1GB_FD),
915 device_id=device.id,
916 device_port_no=port_no))
Matt Jeannerete33a7092019-03-12 21:54:14 -0400917
William Kurkian6f436d02019-02-06 16:25:01 -0500918 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
919 if port_type is Port.ETHERNET_NNI:
920 return "nni-" + str(port_no)
921 elif port_type is Port.PON_OLT:
922 return "pon" + str(intf_id)
923 elif port_type is Port.ETHERNET_UNI:
924 assert False, 'local UNI management not supported'
925
William Kurkian6f436d02019-02-06 16:25:01 -0500926 def _get_mac_form_port_no(self, port_no):
927 mac = ''
928 for i in range(4):
929 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
930 return '00:00' + mac
931
William Kurkian92bd7122019-02-14 15:26:59 -0500932 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500933 def add_port(self, intf_id, port_type, oper_status):
934 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
935
936 label = self.port_name(port_no, port_type, intf_id)
937
Scott Baker7eb0a932019-07-26 10:33:22 -0700938 self.log.debug('adding-port',
939 port_no=port_no,
940 label=label,
William Kurkian6f436d02019-02-06 16:25:01 -0500941 port_type=port_type)
942
Scott Baker7eb0a932019-07-26 10:33:22 -0700943 port = Port(port_no=port_no,
944 label=label,
945 type=port_type,
946 admin_state=AdminState.ENABLED,
947 oper_status=oper_status)
William Kurkian6f436d02019-02-06 16:25:01 -0500948
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400949 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500950
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500951 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500952 def delete_port(self, child_serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -0700953 ports = self.proxy.get('/devices/{}/ports'.format(self.device_id))
William Kurkian6f436d02019-02-06 16:25:01 -0500954 for port in ports:
955 if port.label == child_serial_number:
956 self.log.debug('delete-port',
957 onu_serial_number=child_serial_number,
958 port=port)
William Kurkian23047b92019-05-01 11:02:35 -0400959 yield self.core_proxy.port_removed(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500960 return
Scott Baker7eb0a932019-07-26 10:33:22 -0700961
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400962 def update_flow_table(self, flow_changes):
William Kurkian6f436d02019-02-06 16:25:01 -0500963
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400964 self.log.debug("update_flow_table", flow_changes=flow_changes)
965
966 flows_to_add = flow_changes.to_add.items
967 flows_to_remove = flow_changes.to_remove.items
968
William Kurkian6f436d02019-02-06 16:25:01 -0500969 if not self.is_state_up():
970 self.log.info('The OLT is not up, we cannot update flows',
971 flows_to_add=[f.id for f in flows_to_add],
972 flows_to_remove=[f.id for f in flows_to_remove])
973 return
974
Scott Baker7eb0a932019-07-26 10:33:22 -0700975 self.log.debug('flows update',
976 flows_to_add=flows_to_add,
William Kurkian6f436d02019-02-06 16:25:01 -0500977 flows_to_remove=flows_to_remove)
978
979 for flow in flows_to_add:
980
981 try:
982 self.flow_mgr.add_flow(flow)
983 except Exception as e:
984 self.log.error('failed to add flow', flow=flow, e=e)
985
986 for flow in flows_to_remove:
987
988 try:
989 self.flow_mgr.remove_flow(flow)
990 except Exception as e:
991 self.log.error('failed to remove flow', flow=flow, e=e)
992
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400993 # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
994 #self.flow_mgr.repush_all_different_flows()
Scott Baker7eb0a932019-07-26 10:33:22 -0700995
William Kurkian6f436d02019-02-06 16:25:01 -0500996 # There has to be a better way to do this
997 def ip_hex(self, ip):
998 octets = ip.split(".")
999 hex_ip = []
1000 for octet in octets:
1001 octet_hex = hex(int(octet))
1002 octet_hex = octet_hex.split('0x')[1]
1003 octet_hex = octet_hex.rjust(2, '0')
1004 hex_ip.append(octet_hex)
1005 return ":".join(hex_ip)
1006
1007 def stringify_vendor_specific(self, vendor_specific):
Scott Baker7eb0a932019-07-26 10:33:22 -07001008 return ''.join(
1009 str(i) for i in [
1010 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1011 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1012 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1013 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1014 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1015 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1016 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1017 hex(ord(vendor_specific[3]) & 0x0f)[2:]
1018 ])
William Kurkian6f436d02019-02-06 16:25:01 -05001019
1020 def stringify_serial_number(self, serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -07001021 return ''.join([
1022 serial_number.vendor_id,
1023 self.stringify_vendor_specific(serial_number.vendor_specific)
1024 ])
William Kurkian6f436d02019-02-06 16:25:01 -05001025
1026 def destringify_serial_number(self, serial_number_str):
1027 serial_number = openolt_pb2.SerialNumber(
1028 vendor_id=serial_number_str[:4].encode('utf-8'),
1029 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1030 return serial_number
1031
1032 def disable(self):
1033 self.log.debug('sending-deactivate-olt-message',
1034 device_id=self.device_id)
1035
1036 try:
1037 # Send grpc call
1038 self.stub.DisableOlt(openolt_pb2.Empty())
1039 # The resulting indication will bring the OLT down
1040 # self.go_state_down()
1041 self.log.info('openolt device disabled')
1042 except Exception as e:
1043 self.log.error('Failure to disable openolt device', error=e)
1044
1045 def delete(self):
Matt Jeanneretaa360912019-04-22 16:23:12 -04001046 self.log.info('deleting-olt', device_id=self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001047
1048 # Clears up the data from the resource manager KV store
1049 # for the device
1050 del self.resource_mgr
1051
1052 try:
1053 # Rebooting to reset the state
1054 self.reboot()
1055 # Removing logical device
William Kurkian6f436d02019-02-06 16:25:01 -05001056 except Exception as e:
1057 self.log.error('Failure to delete openolt device', error=e)
1058 raise e
1059 else:
1060 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1061
1062 def reenable(self):
1063 self.log.debug('reenabling-olt', device_id=self.device_id)
1064
1065 try:
1066 self.stub.ReenableOlt(openolt_pb2.Empty())
1067
William Kurkian6f436d02019-02-06 16:25:01 -05001068 except Exception as e:
1069 self.log.error('Failure to reenable openolt device', error=e)
1070 else:
1071 self.log.info('openolt device reenabled')
1072
Scott Baker7eb0a932019-07-26 10:33:22 -07001073 def activate_onu(self, intf_id, onu_id, serial_number, serial_number_str):
William Kurkian6f436d02019-02-06 16:25:01 -05001074 pir = self.bw_mgr.pir(serial_number_str)
Scott Baker7eb0a932019-07-26 10:33:22 -07001075 self.log.debug("activating-onu",
1076 intf_id=intf_id,
1077 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001078 serial_number_str=serial_number_str,
Scott Baker7eb0a932019-07-26 10:33:22 -07001079 serial_number=serial_number,
1080 pir=pir)
1081 onu = openolt_pb2.Onu(intf_id=intf_id,
1082 onu_id=onu_id,
1083 serial_number=serial_number,
1084 pir=pir)
William Kurkian6f436d02019-02-06 16:25:01 -05001085 self.stub.ActivateOnu(onu)
1086 self.log.info('onu-activated', serial_number=serial_number_str)
1087
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001088 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001089 def delete_child_device(self, child_device):
1090 self.log.debug('sending-deactivate-onu',
1091 olt_device_id=self.device_id,
1092 onu_device=child_device,
1093 onu_serial_number=child_device.serial_number)
1094 try:
William Kurkian23047b92019-05-01 11:02:35 -04001095 yield self.core_proxy.child_device_removed(self.device_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001096 child_device.id,
1097 child_device)
William Kurkian6f436d02019-02-06 16:25:01 -05001098 except Exception as e:
William Kurkian23047b92019-05-01 11:02:35 -04001099 self.log.error('core_proxy error', error=e)
William Kurkian6f436d02019-02-06 16:25:01 -05001100 try:
1101 self.delete_logical_port(child_device)
1102 except Exception as e:
1103 self.log.error('logical_port delete error', error=e)
1104 try:
1105 self.delete_port(child_device.serial_number)
1106 except Exception as e:
1107 self.log.error('port delete error', error=e)
1108 serial_number = self.destringify_serial_number(
1109 child_device.serial_number)
1110 # TODO FIXME - For each uni.
1111 # TODO FIXME - Flows are not deleted
1112 uni_id = 0 # FIXME
1113 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001114 child_device.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001115 child_device.proxy_address.onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001116 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001117 child_device.proxy_address.onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001118 # Free any PON resources that were reserved for the ONU
1119 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1120
1121 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1122 onu_id=child_device.proxy_address.onu_id,
1123 serial_number=serial_number)
1124 self.stub.DeleteOnu(onu)
1125
1126 def reboot(self):
1127 self.log.debug('rebooting openolt device', device_id=self.device_id)
1128 try:
1129 self.stub.Reboot(openolt_pb2.Empty())
1130 except Exception as e:
1131 self.log.error('something went wrong with the reboot', error=e)
1132 else:
1133 self.log.info('device rebooted')
1134
1135 def trigger_statistics_collection(self):
1136 try:
1137 self.stub.CollectStatistics(openolt_pb2.Empty())
1138 except Exception as e:
1139 self.log.error('Error while triggering statistics collection',
1140 error=e)
1141 else:
1142 self.log.info('statistics requested')
1143
1144 def simulate_alarm(self, alarm):
1145 self.alarm_mgr.simulate_alarm(alarm)
Mahir Gunyel4d451502019-05-16 14:27:13 -07001146
1147 def form_onu_key(self, intf_id, onu_id):
1148 return str(intf_id) + "." + str(onu_id)
1149
1150
1151class OnuDevice(object):
1152 def __init__(self, device_id, device_type, serialnumber, onu_id, intf_id):
1153 self.device_id = device_id
1154 self.device_type = device_type
1155 self.serialnumber = serialnumber
1156 self.onu_id = onu_id
1157 self.intf_id = intf_id