blob: 85c5ee70b4efe4500b57b9f53d5a553324ee8e47 [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):
Scott Baker7eb0a932019-07-26 10:33:22 -0700478 self.log.debug("onu indication with retry",
479 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
490 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400491 onu_device = yield self.core_proxy.get_child_device(
Scott Baker7eb0a932019-07-26 10:33:22 -0700492 self.device_id, serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500493 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400494 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500495 self.device_id,
496 parent_port_no=self.platform.intf_id_to_port_no(
497 onu_indication.intf_id, Port.PON_OLT),
498 onu_id=onu_indication.onu_id)
499
Scott Baker7eb0a932019-07-26 10:33:22 -0700500 onu_key = self.form_onu_key(onu_indication.intf_id,
501 onu_indication.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500502 if onu_device is None:
Scott Baker7eb0a932019-07-26 10:33:22 -0700503 if serial_number_str in self.seen_discovery_indications:
504 if onu_key not in self.onu_cache:
505 # The ONU is probably getting added. Lets retry again
506 if self.indication_retries < 10:
507 self.indication_retries += 1
508 self.log.error(
509 'onu in discovery indications but not found in core. Retrying again',
510 intf_id=onu_indication.intf_id,
511 onu_id=onu_indication.onu_id)
512 reactor.callLater(3, self.onu_indication,
513 onu_indication)
514 else:
515 self.log.error('ONU not found',
516 intf_id=onu_indication.intf_id,
517 onu_id=onu_indication.onu_id)
518 else:
519 # The ONU device is in the ONU cache
520 self.log.debug('ONU in cache')
521 self.indication_retries = 0
522 onu_device = self.onu_cache[onu_key]
523 self.log.debug('ONU device found in the cache', onu_device=onu_device)
524 else:
525 self.log.error('onu not found',
526 intf_id=onu_indication.intf_id,
527 onu_id=onu_indication.onu_id)
528 if onu_device is None:
529 return
530
531 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type,
532 serial_number_str,
533 onu_indication.onu_id,
534 onu_indication.intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500535 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
536 != onu_indication.intf_id:
Scott Baker7eb0a932019-07-26 10:33:22 -0700537 self.log.warn(
538 'ONU-is-on-a-different-intf-id-now',
539 previous_intf_id=self.platform.intf_id_from_pon_port_no(
540 onu_device.parent_port_no),
541 current_intf_id=onu_indication.intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500542 # FIXME - handle intf_id mismatch (ONU move?)
543
544 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
545 # FIXME - handle onu id mismatch
Scott Baker7eb0a932019-07-26 10:33:22 -0700546 self.log.warn(
547 'ONU-id-mismatch, can happen if both voltha and '
548 'the olt rebooted',
549 expected_onu_id=onu_device.proxy_address.onu_id,
550 received_onu_id=onu_indication.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500551
552 # Admin state
553 if onu_indication.admin_state == 'down':
554 if onu_indication.oper_state != 'down':
555 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
556 oper_state=onu_indication.oper_state)
557 # Forcing the oper state change code to execute
558 onu_indication.oper_state = 'down'
559
560 # Port and logical port update is taken care of by oper state block
561
562 elif onu_indication.admin_state == 'up':
563 pass
564
565 else:
566 self.log.warn('Invalid-or-not-implemented-admin-state',
567 received_admin_state=onu_indication.admin_state)
568
569 self.log.debug('admin-state-dealt-with')
570
William Kurkian6f436d02019-02-06 16:25:01 -0500571 # Operating state
572 if onu_indication.oper_state == 'down':
573
574 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Scott Baker7eb0a932019-07-26 10:33:22 -0700575 yield self.core_proxy.device_state_update(
576 onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500577
578 # Move to discovered state
579 self.log.debug('onu-oper-state-is-down')
580
581 if onu_device.oper_status != OperStatus.DISCOVERED:
Scott Baker7eb0a932019-07-26 10:33:22 -0700582 yield self.core_proxy.device_state_update(
583 onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500584
Scott Baker7eb0a932019-07-26 10:33:22 -0700585 self.log.debug('inter-adapter-send-onu-ind',
586 onu_indication=onu_indication)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500587
588 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
589 yield self.adapter_proxy.send_inter_adapter_message(
590 msg=onu_indication,
591 type=InterAdapterMessageType.ONU_IND_REQUEST,
592 from_adapter="openolt",
593 to_adapter=onu_device.type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700594 to_device_id=onu_device.id)
William Kurkian6f436d02019-02-06 16:25:01 -0500595
596 elif onu_indication.oper_state == 'up':
597
598 if onu_device.connect_status != ConnectStatus.REACHABLE:
Scott Baker7eb0a932019-07-26 10:33:22 -0700599 yield self.core_proxy.device_state_update(
600 onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500601
602 if onu_device.oper_status != OperStatus.DISCOVERED:
Scott Baker7eb0a932019-07-26 10:33:22 -0700603 if serial_number_str not in self.seen_discovery_indications:
604 self.log.debug("Ignore ONU indication",
605 intf_id=onu_indication.intf_id,
606 onu_id=onu_indication.onu_id,
607 state=onu_device.oper_status,
608 msg_oper_state=onu_indication.oper_state)
609 return
William Kurkian6f436d02019-02-06 16:25:01 -0500610
Scott Baker7eb0a932019-07-26 10:33:22 -0700611 self.log.debug('inter-adapter-send-onu-ind',
612 onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500613
Matt Jeanneretb428b952019-03-07 05:14:17 -0500614 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
615 yield self.adapter_proxy.send_inter_adapter_message(
616 msg=onu_indication,
617 type=InterAdapterMessageType.ONU_IND_REQUEST,
618 from_adapter="openolt",
619 to_adapter=onu_device.type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700620 to_device_id=onu_device.id)
William Kurkian6f436d02019-02-06 16:25:01 -0500621
622 else:
623 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
624 oper_state=onu_indication.oper_state)
Scott Baker7eb0a932019-07-26 10:33:22 -0700625
William Kurkian23047b92019-05-01 11:02:35 -0400626 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500627 def onu_ports_down(self, onu_device, oper_state):
William Kurkian23047b92019-05-01 11:02:35 -0400628 pass
William Kurkian6f436d02019-02-06 16:25:01 -0500629 # Set port oper state to Discovered
630 # add port will update port if it exists
William Kurkian23047b92019-05-01 11:02:35 -0400631 # yield self.core_proxy.add_port(
William Kurkian6f436d02019-02-06 16:25:01 -0500632 # self.device_id,
633 # Port(
634 # port_no=uni_no,
635 # label=uni_name,
636 # type=Port.ETHERNET_UNI,
637 # admin_state=onu_device.admin_state,
638 # oper_status=oper_state))
639 # TODO this should be downning ports in onu adatper
640
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500641 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500642 def omci_indication(self, omci_indication):
643
Scott Baker7eb0a932019-07-26 10:33:22 -0700644 self.log.debug("omci indication",
645 intf_id=omci_indication.intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500646 onu_id=omci_indication.onu_id)
Scott Baker7eb0a932019-07-26 10:33:22 -0700647 onu_in_cache = self.onus.get(
648 self.form_onu_key(omci_indication.intf_id, omci_indication.onu_id),
649 None)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700650 if onu_in_cache is None:
Scott Baker7eb0a932019-07-26 10:33:22 -0700651 self.log.debug('omci indication for a device not in cache.',
652 intf_id=omci_indication.intf_id,
653 onu_id=omci_indication.onu_id)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700654 onu_device = yield self.core_proxy.get_child_device(
Scott Baker7eb0a932019-07-26 10:33:22 -0700655 self.device_id,
656 onu_id=omci_indication.onu_id,
Mahir Gunyel4d451502019-05-16 14:27:13 -0700657 parent_port_no=self.platform.intf_id_to_port_no(
Scott Baker7eb0a932019-07-26 10:33:22 -0700658 omci_indication.intf_id, Port.PON_OLT),
659 )
Mahir Gunyel4d451502019-05-16 14:27:13 -0700660 onu_device_type = onu_device.type
661 onu_device_id = onu_device.id
662 try:
Scott Baker7eb0a932019-07-26 10:33:22 -0700663 serial_number_str = self.stringify_serial_number(
664 omci_indication.serial_number)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700665 except Exception as e:
666 serial_number_str = None
Scott Baker7eb0a932019-07-26 10:33:22 -0700667
668#if not exist in cache, then add to cache.
669 onu_key = self.form_onu_key(omci_indication.intf_id,
670 omci_indication.onu_id)
671 self.onus[onu_key] = OnuDevice(onu_device.id, onu_device.type,
672 serial_number_str,
673 omci_indication.onu_id,
674 omci_indication.intf_id)
Mahir Gunyel4d451502019-05-16 14:27:13 -0700675 else:
676 onu_device_type = onu_in_cache.device_type
677 onu_device_id = onu_in_cache.device_id
William Kurkian6f436d02019-02-06 16:25:01 -0500678
Matt Jeanneretb428b952019-03-07 05:14:17 -0500679 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
680
681 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
682
683 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
684 yield self.adapter_proxy.send_inter_adapter_message(
685 msg=omci_msg,
686 type=InterAdapterMessageType.OMCI_REQUEST,
687 from_adapter="openolt",
Mahir Gunyel4d451502019-05-16 14:27:13 -0700688 to_adapter=onu_device_type,
Scott Baker7eb0a932019-07-26 10:33:22 -0700689 to_device_id=onu_device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500690
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500691 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500692 def packet_indication(self, pkt_indication):
693
694 self.log.debug("packet indication",
695 intf_type=pkt_indication.intf_type,
696 intf_id=pkt_indication.intf_id,
697 port_no=pkt_indication.port_no,
698 cookie=pkt_indication.cookie,
699 gemport_id=pkt_indication.gemport_id,
700 flow_id=pkt_indication.flow_id)
701
702 if pkt_indication.intf_type == "pon":
703 if pkt_indication.port_no:
Matt Jeannereta591ab82019-04-13 15:54:28 -0400704 port_num = pkt_indication.port_no
William Kurkian6f436d02019-02-06 16:25:01 -0500705 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
706 try:
Scott Baker7eb0a932019-07-26 10:33:22 -0700707 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(
708 pkt_indication.intf_id, pkt_indication.gemport_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500709 onu_id = int(onu_id_uni_id[0])
710 uni_id = int(onu_id_uni_id[1])
Scott Baker7eb0a932019-07-26 10:33:22 -0700711 self.log.debug("packet indication-kv",
712 onu_id=onu_id,
713 uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500714 if onu_id is None:
715 raise Exception("onu-id-none")
716 if uni_id is None:
717 raise Exception("uni-id-none")
Scott Baker7eb0a932019-07-26 10:33:22 -0700718 port_num = self.platform.mk_uni_port_num(
719 pkt_indication.intf_id, onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500720 except Exception as e:
721 self.log.error("no-onu-reference-for-gem",
Scott Baker7eb0a932019-07-26 10:33:22 -0700722 gemport_id=pkt_indication.gemport_id,
723 e=e)
William Kurkian6f436d02019-02-06 16:25:01 -0500724 return
725
William Kurkian6f436d02019-02-06 16:25:01 -0500726 elif pkt_indication.intf_type == "nni":
Matt Jeannereta591ab82019-04-13 15:54:28 -0400727 port_num = self.platform.intf_id_to_port_no(
Scott Baker7eb0a932019-07-26 10:33:22 -0700728 pkt_indication.intf_id, Port.ETHERNET_NNI)
William Kurkian6f436d02019-02-06 16:25:01 -0500729
730 pkt = Ether(pkt_indication.pkt)
731
732 self.log.debug("packet indication",
Matt Jeannereta591ab82019-04-13 15:54:28 -0400733 device_id=self.device_id,
734 port_num=port_num)
William Kurkian6f436d02019-02-06 16:25:01 -0500735
Scott Baker7eb0a932019-07-26 10:33:22 -0700736 yield self.core_proxy.send_packet_in(device_id=self.device_id,
737 port=port_num,
738 packet=str(pkt))
William Kurkian6f436d02019-02-06 16:25:01 -0500739
740 def packet_out(self, egress_port, msg):
741 pkt = Ether(msg)
Scott Baker7eb0a932019-07-26 10:33:22 -0700742 self.log.debug('packet out',
743 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500744 device_id=self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500745 packet=str(pkt).encode("HEX"))
746
747 # Find port type
748 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
749 if egress_port_type == Port.ETHERNET_UNI:
750
751 if pkt.haslayer(Dot1Q):
752 outer_shim = pkt.getlayer(Dot1Q)
753 if isinstance(outer_shim.payload, Dot1Q):
754 # If double tag, remove the outer tag
755 payload = (
Scott Baker7eb0a932019-07-26 10:33:22 -0700756 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
757 outer_shim.payload)
William Kurkian6f436d02019-02-06 16:25:01 -0500758 else:
759 payload = pkt
760 else:
761 payload = pkt
762
763 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
764
765 self.log.debug(
Scott Baker7eb0a932019-07-26 10:33:22 -0700766 'sending-packet-to-ONU',
767 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500768 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
769 onu_id=self.platform.onu_id_from_port_num(egress_port),
770 uni_id=self.platform.uni_id_from_port_num(egress_port),
771 port_no=egress_port,
772 packet=str(payload).encode("HEX"))
773
774 onu_pkt = openolt_pb2.OnuPacket(
775 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
776 onu_id=self.platform.onu_id_from_port_num(egress_port),
777 port_no=egress_port,
778 pkt=send_pkt)
779
780 self.stub.OnuPacketOut(onu_pkt)
781
782 elif egress_port_type == Port.ETHERNET_NNI:
Scott Baker7eb0a932019-07-26 10:33:22 -0700783 self.log.debug('sending-packet-to-uplink',
784 egress_port=egress_port,
William Kurkian6f436d02019-02-06 16:25:01 -0500785 packet=str(pkt).encode("HEX"))
786
787 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
788
789 uplink_pkt = openolt_pb2.UplinkPacket(
790 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
791 pkt=send_pkt)
792
793 self.stub.UplinkPacketOut(uplink_pkt)
794
795 else:
796 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
797 egress_port=egress_port,
798 port_type=egress_port_type)
799
Matt Jeanneretb428b952019-03-07 05:14:17 -0500800 def process_inter_adapter_message(self, request):
801 self.log.debug('process-inter-adapter-message', msg=request)
802 try:
803 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
804 omci_msg = InterAdapterOmciMessage()
805 request.body.Unpack(omci_msg)
806 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
807
Mahir Gunyel4d451502019-05-16 14:27:13 -0700808 #onu_device_id = request.header.to_device_id
809 #onu_device = yield self.core_proxy.get_device(onu_device_id)
Scott Baker7eb0a932019-07-26 10:33:22 -0700810 self.send_proxied_message(omci_msg)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500811
812 else:
813 self.log.error("inter-adapter-unhandled-type", request=request)
814
815 except Exception as e:
816 self.log.exception("error-processing-inter-adapter-message", e=e)
817
Mahir Gunyel4d451502019-05-16 14:27:13 -0700818 def send_proxied_message(self, omci_msg):
Matt Jeanneretb428b952019-03-07 05:14:17 -0500819
Mahir Gunyel4d451502019-05-16 14:27:13 -0700820 if omci_msg.connect_status != ConnectStatus.REACHABLE:
William Kurkian6f436d02019-02-06 16:25:01 -0500821 self.log.debug('ONU is not reachable, cannot send OMCI',
Mahir Gunyel4d451502019-05-16 14:27:13 -0700822 intf_id=omci_msg.proxy_address.channel_id,
823 onu_id=omci_msg.proxy_address.onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500824 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500825
Mahir Gunyel4d451502019-05-16 14:27:13 -0700826 omci = openolt_pb2.OmciMsg(intf_id=omci_msg.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -0700827 onu_id=omci_msg.proxy_address.onu_id,
828 pkt=str(omci_msg.message))
William Kurkian6f436d02019-02-06 16:25:01 -0500829 self.stub.OmciMsgOut(omci)
830
Scott Baker7eb0a932019-07-26 10:33:22 -0700831 self.log.debug("omci-message-sent",
832 intf_id=omci_msg.proxy_address.channel_id,
833 onu_id=omci_msg.proxy_address.onu_id,
834 pkt=str(omci_msg.message))
Matt Jeanneretb428b952019-03-07 05:14:17 -0500835
Matt Jeanneret6e315092019-02-20 10:42:57 -0500836 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500837 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -0700838 self.log.info("adding-onu",
839 port_no=port_no,
840 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500841 serial_number=serial_number)
842
William Kurkian6f436d02019-02-06 16:25:01 -0500843 serial_number_str = self.stringify_serial_number(serial_number)
844
Matt Jeanneretb428b952019-03-07 05:14:17 -0500845 # TODO NEW CORE dont hardcode child device type. find some way of determining by vendor in serial number
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400846 onu_device = yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500847 parent_device_id=self.device_id,
848 parent_port_no=port_no,
849 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500850 channel_id=intf_id,
851 vendor_id=serial_number.vendor_id,
852 serial_number=serial_number_str,
Scott Baker7eb0a932019-07-26 10:33:22 -0700853 onu_id=onu_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500854
Scott Baker7eb0a932019-07-26 10:33:22 -0700855 self.log.debug("onu-added",
856 onu_id=onu_id,
857 port_no=port_no,
858 serial_number=serial_number_str)
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500859
Scott Baker7eb0a932019-07-26 10:33:22 -0700860 yield self.core_proxy.device_state_update(
861 onu_device.id,
862 oper_status=OperStatus.DISCOVERED,
863 connect_status=ConnectStatus.REACHABLE)
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400864
Scott Baker7eb0a932019-07-26 10:33:22 -0700865 self.log.debug("set-onu-discovered",
866 onu_id=onu_id,
867 port_no=port_no,
868 serial_number=serial_number_str,
Matt Jeanneret2133c3b2019-07-03 11:34:20 -0400869 onu_device=onu_device)
870
Scott Baker7eb0a932019-07-26 10:33:22 -0700871 self.log.debug('Adding ONU device to the cache',
872 intf_id=intf_id,
873 onu_id=onu_id)
874 onu_key = self.form_onu_key(intf_id, onu_id)
875 self.onu_cache[onu_key] = onu_device
876 return
877
Matt Jeannerete33a7092019-03-12 21:54:14 -0400878 def get_ofp_device_info(self, device):
879 self.log.info('get_ofp_device_info', device_id=device.id)
880
881 mfr_desc = self.device_info.vendor
882 sw_desc = self.device_info.firmware_version
883 hw_desc = self.device_info.model
Scott Baker7eb0a932019-07-26 10:33:22 -0700884 if self.device_info.hardware_version:
885 hw_desc += '-' + self.device_info.hardware_version
Matt Jeannerete33a7092019-03-12 21:54:14 -0400886
887 return SwitchCapability(
Scott Baker7eb0a932019-07-26 10:33:22 -0700888 desc=ofp_desc(hw_desc=hw_desc,
889 sw_desc=sw_desc,
890 serial_num=device.serial_number),
Matt Jeannerete33a7092019-03-12 21:54:14 -0400891 switch_features=ofp_switch_features(
Scott Baker7eb0a932019-07-26 10:33:22 -0700892 n_buffers=
893 256, # Max packets buffered at once # TODO fake for now
894 n_tables=
895 2, # Number of tables supported by datapath # TODO fake for now
896 capabilities=
897 ( #Bitmap of support "ofp_capabilities" # TODO fake for now
898 OFPC_FLOW_STATS
899 | OFPC_TABLE_STATS
900 | OFPC_PORT_STATS
901 | OFPC_GROUP_STATS)))
Matt Jeannerete33a7092019-03-12 21:54:14 -0400902
903 def get_ofp_port_info(self, device, port_no):
Scott Baker7eb0a932019-07-26 10:33:22 -0700904 self.log.info('get_ofp_port_info',
905 port_no=port_no,
906 device_id=device.id)
Matt Jeannerete33a7092019-03-12 21:54:14 -0400907 cap = OFPPF_1GB_FD | OFPPF_FIBER
Scott Baker7eb0a932019-07-26 10:33:22 -0700908 return PortCapability(port=LogicalPort(ofp_port=ofp_port(
909 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
910 config=0,
911 state=OFPPS_LIVE,
912 curr=cap,
913 advertised=cap,
914 peer=cap,
915 curr_speed=OFPPF_1GB_FD,
916 max_speed=OFPPF_1GB_FD),
917 device_id=device.id,
918 device_port_no=port_no))
Matt Jeannerete33a7092019-03-12 21:54:14 -0400919
William Kurkian6f436d02019-02-06 16:25:01 -0500920 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
921 if port_type is Port.ETHERNET_NNI:
922 return "nni-" + str(port_no)
923 elif port_type is Port.PON_OLT:
924 return "pon" + str(intf_id)
925 elif port_type is Port.ETHERNET_UNI:
926 assert False, 'local UNI management not supported'
927
William Kurkian6f436d02019-02-06 16:25:01 -0500928 def _get_mac_form_port_no(self, port_no):
929 mac = ''
930 for i in range(4):
931 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
932 return '00:00' + mac
933
William Kurkian92bd7122019-02-14 15:26:59 -0500934 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500935 def add_port(self, intf_id, port_type, oper_status):
936 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
937
938 label = self.port_name(port_no, port_type, intf_id)
939
Scott Baker7eb0a932019-07-26 10:33:22 -0700940 self.log.debug('adding-port',
941 port_no=port_no,
942 label=label,
William Kurkian6f436d02019-02-06 16:25:01 -0500943 port_type=port_type)
944
Scott Baker7eb0a932019-07-26 10:33:22 -0700945 port = Port(port_no=port_no,
946 label=label,
947 type=port_type,
948 admin_state=AdminState.ENABLED,
949 oper_status=oper_status)
William Kurkian6f436d02019-02-06 16:25:01 -0500950
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400951 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500952
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500953 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500954 def delete_port(self, child_serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -0700955 ports = self.proxy.get('/devices/{}/ports'.format(self.device_id))
William Kurkian6f436d02019-02-06 16:25:01 -0500956 for port in ports:
957 if port.label == child_serial_number:
958 self.log.debug('delete-port',
959 onu_serial_number=child_serial_number,
960 port=port)
William Kurkian23047b92019-05-01 11:02:35 -0400961 yield self.core_proxy.port_removed(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500962 return
Scott Baker7eb0a932019-07-26 10:33:22 -0700963
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400964 def update_flow_table(self, flow_changes):
William Kurkian6f436d02019-02-06 16:25:01 -0500965
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400966 self.log.debug("update_flow_table", flow_changes=flow_changes)
967
968 flows_to_add = flow_changes.to_add.items
969 flows_to_remove = flow_changes.to_remove.items
970
William Kurkian6f436d02019-02-06 16:25:01 -0500971 if not self.is_state_up():
972 self.log.info('The OLT is not up, we cannot update flows',
973 flows_to_add=[f.id for f in flows_to_add],
974 flows_to_remove=[f.id for f in flows_to_remove])
975 return
976
Scott Baker7eb0a932019-07-26 10:33:22 -0700977 self.log.debug('flows update',
978 flows_to_add=flows_to_add,
William Kurkian6f436d02019-02-06 16:25:01 -0500979 flows_to_remove=flows_to_remove)
980
981 for flow in flows_to_add:
982
983 try:
984 self.flow_mgr.add_flow(flow)
985 except Exception as e:
986 self.log.error('failed to add flow', flow=flow, e=e)
987
988 for flow in flows_to_remove:
989
990 try:
991 self.flow_mgr.remove_flow(flow)
992 except Exception as e:
993 self.log.error('failed to remove flow', flow=flow, e=e)
994
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400995 # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
996 #self.flow_mgr.repush_all_different_flows()
Scott Baker7eb0a932019-07-26 10:33:22 -0700997
William Kurkian6f436d02019-02-06 16:25:01 -0500998 # There has to be a better way to do this
999 def ip_hex(self, ip):
1000 octets = ip.split(".")
1001 hex_ip = []
1002 for octet in octets:
1003 octet_hex = hex(int(octet))
1004 octet_hex = octet_hex.split('0x')[1]
1005 octet_hex = octet_hex.rjust(2, '0')
1006 hex_ip.append(octet_hex)
1007 return ":".join(hex_ip)
1008
1009 def stringify_vendor_specific(self, vendor_specific):
Scott Baker7eb0a932019-07-26 10:33:22 -07001010 return ''.join(
1011 str(i) for i in [
1012 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1013 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1014 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1015 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1016 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1017 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1018 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1019 hex(ord(vendor_specific[3]) & 0x0f)[2:]
1020 ])
William Kurkian6f436d02019-02-06 16:25:01 -05001021
1022 def stringify_serial_number(self, serial_number):
Scott Baker7eb0a932019-07-26 10:33:22 -07001023 return ''.join([
1024 serial_number.vendor_id,
1025 self.stringify_vendor_specific(serial_number.vendor_specific)
1026 ])
William Kurkian6f436d02019-02-06 16:25:01 -05001027
1028 def destringify_serial_number(self, serial_number_str):
1029 serial_number = openolt_pb2.SerialNumber(
1030 vendor_id=serial_number_str[:4].encode('utf-8'),
1031 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1032 return serial_number
1033
1034 def disable(self):
1035 self.log.debug('sending-deactivate-olt-message',
1036 device_id=self.device_id)
1037
1038 try:
1039 # Send grpc call
1040 self.stub.DisableOlt(openolt_pb2.Empty())
1041 # The resulting indication will bring the OLT down
1042 # self.go_state_down()
1043 self.log.info('openolt device disabled')
1044 except Exception as e:
1045 self.log.error('Failure to disable openolt device', error=e)
1046
1047 def delete(self):
Matt Jeanneretaa360912019-04-22 16:23:12 -04001048 self.log.info('deleting-olt', device_id=self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001049
1050 # Clears up the data from the resource manager KV store
1051 # for the device
1052 del self.resource_mgr
1053
1054 try:
1055 # Rebooting to reset the state
1056 self.reboot()
1057 # Removing logical device
William Kurkian6f436d02019-02-06 16:25:01 -05001058 except Exception as e:
1059 self.log.error('Failure to delete openolt device', error=e)
1060 raise e
1061 else:
1062 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1063
1064 def reenable(self):
1065 self.log.debug('reenabling-olt', device_id=self.device_id)
1066
1067 try:
1068 self.stub.ReenableOlt(openolt_pb2.Empty())
1069
William Kurkian6f436d02019-02-06 16:25:01 -05001070 except Exception as e:
1071 self.log.error('Failure to reenable openolt device', error=e)
1072 else:
1073 self.log.info('openolt device reenabled')
1074
Scott Baker7eb0a932019-07-26 10:33:22 -07001075 def activate_onu(self, intf_id, onu_id, serial_number, serial_number_str):
William Kurkian6f436d02019-02-06 16:25:01 -05001076 pir = self.bw_mgr.pir(serial_number_str)
Scott Baker7eb0a932019-07-26 10:33:22 -07001077 self.log.debug("activating-onu",
1078 intf_id=intf_id,
1079 onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001080 serial_number_str=serial_number_str,
Scott Baker7eb0a932019-07-26 10:33:22 -07001081 serial_number=serial_number,
1082 pir=pir)
1083 onu = openolt_pb2.Onu(intf_id=intf_id,
1084 onu_id=onu_id,
1085 serial_number=serial_number,
1086 pir=pir)
William Kurkian6f436d02019-02-06 16:25:01 -05001087 self.stub.ActivateOnu(onu)
1088 self.log.info('onu-activated', serial_number=serial_number_str)
1089
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001090 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001091 def delete_child_device(self, child_device):
1092 self.log.debug('sending-deactivate-onu',
1093 olt_device_id=self.device_id,
1094 onu_device=child_device,
1095 onu_serial_number=child_device.serial_number)
1096 try:
William Kurkian23047b92019-05-01 11:02:35 -04001097 yield self.core_proxy.child_device_removed(self.device_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001098 child_device.id,
1099 child_device)
William Kurkian6f436d02019-02-06 16:25:01 -05001100 except Exception as e:
William Kurkian23047b92019-05-01 11:02:35 -04001101 self.log.error('core_proxy error', error=e)
William Kurkian6f436d02019-02-06 16:25:01 -05001102 try:
1103 self.delete_logical_port(child_device)
1104 except Exception as e:
1105 self.log.error('logical_port delete error', error=e)
1106 try:
1107 self.delete_port(child_device.serial_number)
1108 except Exception as e:
1109 self.log.error('port delete error', error=e)
1110 serial_number = self.destringify_serial_number(
1111 child_device.serial_number)
1112 # TODO FIXME - For each uni.
1113 # TODO FIXME - Flows are not deleted
1114 uni_id = 0 # FIXME
1115 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001116 child_device.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001117 child_device.proxy_address.onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001118 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
Scott Baker7eb0a932019-07-26 10:33:22 -07001119 child_device.proxy_address.onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001120 # Free any PON resources that were reserved for the ONU
1121 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1122
1123 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1124 onu_id=child_device.proxy_address.onu_id,
1125 serial_number=serial_number)
1126 self.stub.DeleteOnu(onu)
1127
1128 def reboot(self):
1129 self.log.debug('rebooting openolt device', device_id=self.device_id)
1130 try:
1131 self.stub.Reboot(openolt_pb2.Empty())
1132 except Exception as e:
1133 self.log.error('something went wrong with the reboot', error=e)
1134 else:
1135 self.log.info('device rebooted')
1136
1137 def trigger_statistics_collection(self):
1138 try:
1139 self.stub.CollectStatistics(openolt_pb2.Empty())
1140 except Exception as e:
1141 self.log.error('Error while triggering statistics collection',
1142 error=e)
1143 else:
1144 self.log.info('statistics requested')
1145
1146 def simulate_alarm(self, alarm):
1147 self.alarm_mgr.simulate_alarm(alarm)
Mahir Gunyel4d451502019-05-16 14:27:13 -07001148
1149 def form_onu_key(self, intf_id, onu_id):
1150 return str(intf_id) + "." + str(onu_id)
1151
1152
1153class OnuDevice(object):
1154 def __init__(self, device_id, device_type, serialnumber, onu_id, intf_id):
1155 self.device_id = device_id
1156 self.device_type = device_type
1157 self.serialnumber = serialnumber
1158 self.onu_id = onu_id
1159 self.intf_id = intf_id