blob: 5d8eb8e97a93327aa4098ae87aa5aca62b02e510 [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import threading
17import binascii
18import grpc
19import socket
20import re
21import structlog
William Kurkianfefd4642019-02-07 15:30:03 -050022import time
William Kurkian6f436d02019-02-06 16:25:01 -050023from twisted.internet import reactor
William Kurkian92bd7122019-02-14 15:26:59 -050024from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian6f436d02019-02-06 16:25:01 -050025from scapy.layers.l2 import Ether, Dot1Q
26from transitions import Machine
27
William Kurkian8b1690c2019-03-04 16:53:22 -050028from voltha_protos import openolt_pb2_grpc, openolt_pb2
William Kurkian6f436d02019-02-06 16:25:01 -050029
William Kurkian44cd7bb2019-02-11 16:39:12 -050030from pyvoltha.adapters.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
William Kurkian6f436d02019-02-06 16:25:01 -050031
William Kurkian44cd7bb2019-02-11 16:39:12 -050032from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
William Kurkian8b1690c2019-03-04 16:53:22 -050033from voltha_protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
William Kurkian6f436d02019-02-06 16:25:01 -050034 OFPPS_LINK_DOWN, OFPPF_1GB_FD, \
35 OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS, \
36 ofp_switch_features, ofp_port, ofp_port_stats, ofp_desc
William Kurkian44cd7bb2019-02-11 16:39:12 -050037from pyvoltha.common.utils.registry import registry
William Kurkian8b1690c2019-03-04 16:53:22 -050038from voltha_protos.common_pb2 import AdminState, OperStatus, ConnectStatus
William Kurkian8b1690c2019-03-04 16:53:22 -050039from voltha_protos.device_pb2 import Port, Device
Matt Jeannerete33a7092019-03-12 21:54:14 -040040from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
41 InterAdapterMessageType, InterAdapterOmciMessage
William Kurkian8b1690c2019-03-04 16:53:22 -050042from voltha_protos.logical_device_pb2 import LogicalDevice, LogicalPort
William Kurkian6f436d02019-02-06 16:25:01 -050043
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050044
William Kurkian6f436d02019-02-06 16:25:01 -050045class OpenoltDevice(object):
46 """
47 OpenoltDevice state machine:
48
49 null ----> init ------> connected -----> up -----> down
50 ^ ^ | ^ | |
51 | | | | | |
52 | +-------------+ +---------+ |
53 | |
54 +-----------------------------------------+
55 """
56 # pylint: disable=too-many-instance-attributes
57 # pylint: disable=R0904
58 states = [
59 'state_null',
60 'state_init',
61 'state_connected',
62 'state_up',
63 'state_down']
64
65 transitions = [
66 {'trigger': 'go_state_init',
67 'source': ['state_null', 'state_connected', 'state_down'],
68 'dest': 'state_init',
69 'before': 'do_state_init',
70 'after': 'post_init'},
71 {'trigger': 'go_state_connected',
72 'source': 'state_init',
73 'dest': 'state_connected',
74 'before': 'do_state_connected'},
75 {'trigger': 'go_state_up',
76 'source': ['state_connected', 'state_down'],
77 'dest': 'state_up',
78 'before': 'do_state_up'},
79 {'trigger': 'go_state_down',
80 'source': ['state_up'],
81 'dest': 'state_down',
82 'before': 'do_state_down',
83 'after': 'post_down'}]
84
85 def __init__(self, **kwargs):
86 super(OpenoltDevice, self).__init__()
87
Matt Jeanneretbad3d982019-03-11 16:06:10 -040088 self.core_proxy = kwargs['core_proxy']
Matt Jeanneret7906d232019-02-14 14:57:38 -050089 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050090 self.device_num = kwargs['device_num']
serkant.uluderyadcfc74d2019-03-17 23:41:42 -070091 self.device = kwargs['device']
William Kurkian6f436d02019-02-06 16:25:01 -050092
93 self.platform_class = kwargs['support_classes']['platform']
94 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
95 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
96 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
97 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
98 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
Matt Jeanneret7906d232019-02-14 14:57:38 -050099
Matt Jeanneretb428b952019-03-07 05:14:17 -0500100 self.seen_discovery_indications = []
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500101 self.stub = None
William Kurkian27522582019-02-25 14:24:32 -0500102 self.connected = False
William Kurkian6f436d02019-02-06 16:25:01 -0500103 is_reconciliation = kwargs.get('reconciliation', False)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700104 self.device_id = self.device.id
105 self.host_and_port = self.device.host_and_port
106 self.extra_args = self.device.extra_args
Matt Jeannerete33a7092019-03-12 21:54:14 -0400107 self.device_info = None
William Kurkian6f436d02019-02-06 16:25:01 -0500108 self.log = structlog.get_logger(id=self.device_id,
109 ip=self.host_and_port)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500110
William Kurkian6f436d02019-02-06 16:25:01 -0500111 self.log.info('openolt-device-init')
112
113 # default device id and device serial number. If device_info provides better results, they will be updated
114 self.dpid = kwargs.get('dp_id')
115 self.serial_number = self.host_and_port # FIXME
116
117 # Device already set in the event of reconciliation
118 if not is_reconciliation:
119 self.log.info('updating-device')
120 # It is a new device
121 # Update device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700122 self.device.root = True
123 self.device.connect_status = ConnectStatus.UNREACHABLE
124 self.device.oper_status = OperStatus.ACTIVATING
William Kurkian6f436d02019-02-06 16:25:01 -0500125
126 # If logical device does exist use it, else create one after connecting to device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700127 if self.device.parent_id:
William Kurkian6f436d02019-02-06 16:25:01 -0500128 # logical device already exists
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700129 self.logical_device_id = self.device.parent_id
William Kurkian6f436d02019-02-06 16:25:01 -0500130 if is_reconciliation:
131 self.adapter_agent.reconcile_logical_device(
132 self.logical_device_id)
133
134 # Initialize the OLT state machine
135 self.machine = Machine(model=self, states=OpenoltDevice.states,
136 transitions=OpenoltDevice.transitions,
137 send_event=True, initial='state_null')
138 self.go_state_init()
139
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500140 @inlineCallbacks
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500141 def create_logical_device(self, device_info):
William Kurkian6f436d02019-02-06 16:25:01 -0500142 dpid = device_info.device_id
143 serial_number = device_info.device_serial_number
144
145 if dpid is None: dpid = self.dpid
146 if serial_number is None: serial_number = self.serial_number
147
148 if dpid == None or dpid == '':
149 uri = self.host_and_port.split(":")[0]
150 try:
151 socket.inet_pton(socket.AF_INET, uri)
152 dpid = '00:00:' + self.ip_hex(uri)
153 except socket.error:
154 # this is not an IP
155 dpid = self.stringToMacAddr(uri)
156
157 if serial_number == None or serial_number == '':
158 serial_number = self.host_and_port
159
160 self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number)
161
162 mfr_desc = device_info.vendor
163 sw_desc = device_info.firmware_version
164 hw_desc = device_info.model
165 if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version
William Kurkian92bd7122019-02-14 15:26:59 -0500166
William Kurkian6f436d02019-02-06 16:25:01 -0500167 # Create logical OF device
168 ld = LogicalDevice(
169 root_device_id=self.device_id,
170 switch_features=ofp_switch_features(
171 n_buffers=256, # TODO fake for now
172 n_tables=2, # TODO ditto
173 capabilities=( # TODO and ditto
174 OFPC_FLOW_STATS
175 | OFPC_TABLE_STATS
176 | OFPC_PORT_STATS
177 | OFPC_GROUP_STATS
178 )
179 ),
180 desc=ofp_desc(
181 serial_num=serial_number
182 )
183 )
184 ld_init = self.adapter_agent.create_logical_device(ld,
William Kurkian92bd7122019-02-14 15:26:59 -0500185 dpid=dpid)
186
William Kurkian6f436d02019-02-06 16:25:01 -0500187 self.logical_device_id = ld_init.id
188
William Kurkian27522582019-02-25 14:24:32 -0500189 ##Moved setting serial number outside of the logical_device function
190 #device = yield self.adapter_agent.get_device(self.device_id)
191 #device.serial_number = serial_number
192 #yield self.adapter_agent.update_device(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500193
194 self.dpid = dpid
195 self.serial_number = serial_number
196
197 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
198
199 def stringToMacAddr(self, uri):
200 regex = re.compile('[^a-zA-Z]')
201 uri = regex.sub('', uri)
202
203 l = len(uri)
204 if l > 6:
205 uri = uri[0:6]
206 else:
207 uri = uri + uri[0:6 - l]
208
William Kurkian6f436d02019-02-06 16:25:01 -0500209 return ":".join([hex(ord(x))[-2:] for x in uri])
210
211 def do_state_init(self, event):
212 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500213 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500214 self.channel = grpc.insecure_channel(self.host_and_port)
215 self.channel_ready_future = grpc.channel_ready_future(self.channel)
216
217 self.log.info('openolt-device-created', device_id=self.device_id)
218
219 def post_init(self, event):
220 self.log.debug('post_init')
221
222 # We have reached init state, starting the indications thread
223
224 # Catch RuntimeError exception
225 try:
226 # Start indications thread
227 self.indications_thread_handle = threading.Thread(
228 target=self.indications_thread)
229 # Old getter/setter API for daemon; use it directly as a
230 # property instead. The Jinkins error will happon on the reason of
231 # Exception in thread Thread-1 (most likely raised # during
232 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500233 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500234 self.indications_thread_handle.setDaemon(True)
235 self.indications_thread_handle.start()
236 except Exception as e:
237 self.log.exception('post_init failed', e=e)
238
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500239 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500240 def do_state_connected(self, event):
241 self.log.debug("do_state_connected")
William Kurkian27522582019-02-25 14:24:32 -0500242
William Kurkian6f436d02019-02-06 16:25:01 -0500243 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
244
William Kurkianfefd4642019-02-07 15:30:03 -0500245 delay = 1
246 while True:
247 try:
Matt Jeannerete33a7092019-03-12 21:54:14 -0400248 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
William Kurkianfefd4642019-02-07 15:30:03 -0500249 break
250 except Exception as e:
251 reraise = True
252 if delay > 120:
253 self.log.error("gRPC failure too many times")
254 else:
255 self.log.warn("gRPC failure, retry in %ds: %s"
256 % (delay, repr(e)))
257 time.sleep(delay)
258 delay += delay
259 reraise = False
260
261 if reraise:
262 raise
263
Matt Jeannerete33a7092019-03-12 21:54:14 -0400264 self.log.info('Device connected', device_info=self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500265
Matt Jeanneret7906d232019-02-14 14:57:38 -0500266 # self.create_logical_device(device_info)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700267 self.logical_device_id = '0'
William Kurkian27522582019-02-25 14:24:32 -0500268
Matt Jeannerete33a7092019-03-12 21:54:14 -0400269 serial_number = self.device_info.device_serial_number
William Kurkian27522582019-02-25 14:24:32 -0500270 if serial_number is None:
271 serial_number = self.serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700272 self.device.serial_number = serial_number
William Kurkian27522582019-02-25 14:24:32 -0500273
274 self.serial_number = serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700275
276 self.device.root = True
277 self.device.vendor = self.device_info.vendor
278 self.device.model = self.device_info.model
279 self.device.hardware_version = self.device_info.hardware_version
280 self.device.firmware_version = self.device_info.firmware_version
William Kurkian27522582019-02-25 14:24:32 -0500281
282 # TODO: check for uptime and reboot if too long (VOL-1192)
283
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700284 self.device.connect_status = ConnectStatus.REACHABLE
285 self.device.mac_address = "AA:BB:CC:DD:EE:FF"
286 yield self.core_proxy.device_update(self.device)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500287
William Kurkian6f436d02019-02-06 16:25:01 -0500288 self.resource_mgr = self.resource_mgr_class(self.device_id,
289 self.host_and_port,
290 self.extra_args,
Matt Jeannerete33a7092019-03-12 21:54:14 -0400291 self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500292 self.platform = self.platform_class(self.log, self.resource_mgr)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400293 self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.adapter_proxy, self.log,
William Kurkian6f436d02019-02-06 16:25:01 -0500294 self.stub, self.device_id,
295 self.logical_device_id,
296 self.platform, self.resource_mgr)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700297
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400298 self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
William Kurkian6f436d02019-02-06 16:25:01 -0500299 self.device_id,
300 self.logical_device_id,
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700301 self.platform,
302 self.serial_number)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500303 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400304 self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)
William Kurkian27522582019-02-25 14:24:32 -0500305
306 self.connected = True
Matt Jeanneret6e315092019-02-20 10:42:57 -0500307
308 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500309 def do_state_up(self, event):
310 self.log.debug("do_state_up")
311
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400312 yield self.core_proxy.device_state_update(self.device_id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500313 connect_status=ConnectStatus.REACHABLE,
314 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500315 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500316
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500317 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500318 def do_state_down(self, event):
319 self.log.debug("do_state_down")
Arun Arora2e63b1e2019-04-03 12:15:19 +0000320 yield self.core_proxy.device_state_update(self.device_id,
321 connect_status=ConnectStatus.UNREACHABLE,
322 oper_status=OperStatus.UNKNOWN)
323 self.log.debug("done_state_down")
William Kurkian6f436d02019-02-06 16:25:01 -0500324
325 # def post_up(self, event):
326 # self.log.debug('post-up')
327 # self.flow_mgr.reseed_flows()
328
329 def post_down(self, event):
330 self.log.debug('post_down')
331 self.flow_mgr.reset_flows()
332
333 def indications_thread(self):
334 self.log.debug('starting-indications-thread')
335 self.log.debug('connecting to olt', device_id=self.device_id)
336 self.channel_ready_future.result() # blocking call
337 self.log.info('connected to olt', device_id=self.device_id)
338 self.go_state_connected()
339
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500340 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500341 # Don't continue until connected is done
342 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500343 time.sleep(0.5)
344
William Kurkian6f436d02019-02-06 16:25:01 -0500345 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
346
347 while True:
348 try:
349 # get the next indication from olt
350 ind = next(self.indications)
351 except Exception as e:
352 self.log.warn('gRPC connection lost', error=e)
353 reactor.callFromThread(self.go_state_down)
354 reactor.callFromThread(self.go_state_init)
355 break
356 else:
357 self.log.debug("rx indication", indication=ind)
358
359 # indication handlers run in the main event loop
360 if ind.HasField('olt_ind'):
361 reactor.callFromThread(self.olt_indication, ind.olt_ind)
362 elif ind.HasField('intf_ind'):
363 reactor.callFromThread(self.intf_indication, ind.intf_ind)
364 elif ind.HasField('intf_oper_ind'):
365 reactor.callFromThread(self.intf_oper_indication,
366 ind.intf_oper_ind)
367 elif ind.HasField('onu_disc_ind'):
368 reactor.callFromThread(self.onu_discovery_indication,
369 ind.onu_disc_ind)
370 elif ind.HasField('onu_ind'):
371 reactor.callFromThread(self.onu_indication, ind.onu_ind)
372 elif ind.HasField('omci_ind'):
373 reactor.callFromThread(self.omci_indication, ind.omci_ind)
374 elif ind.HasField('pkt_ind'):
375 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
376 elif ind.HasField('port_stats'):
377 reactor.callFromThread(
378 self.stats_mgr.port_statistics_indication,
379 ind.port_stats)
380 elif ind.HasField('flow_stats'):
381 reactor.callFromThread(
382 self.stats_mgr.flow_statistics_indication,
383 ind.flow_stats)
384 elif ind.HasField('alarm_ind'):
385 reactor.callFromThread(self.alarm_mgr.process_alarms,
386 ind.alarm_ind)
387 else:
388 self.log.warn('unknown indication type')
389
390 def olt_indication(self, olt_indication):
391 if olt_indication.oper_state == "up":
392 self.go_state_up()
393 elif olt_indication.oper_state == "down":
394 self.go_state_down()
395
396 def intf_indication(self, intf_indication):
397 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
398 oper_state=intf_indication.oper_state)
399
400 if intf_indication.oper_state == "up":
401 oper_status = OperStatus.ACTIVE
402 else:
403 oper_status = OperStatus.DISCOVERED
404
405 # add_port update the port if it exists
406 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
407
408 def intf_oper_indication(self, intf_oper_indication):
409 self.log.debug("Received interface oper state change indication",
410 intf_id=intf_oper_indication.intf_id,
411 type=intf_oper_indication.type,
412 oper_state=intf_oper_indication.oper_state)
413
414 if intf_oper_indication.oper_state == "up":
415 oper_state = OperStatus.ACTIVE
416 else:
417 oper_state = OperStatus.DISCOVERED
418
419 if intf_oper_indication.type == "nni":
420
421 # add_(logical_)port update the port if it exists
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500422 self.add_port(intf_oper_indication.intf_id,
423 Port.ETHERNET_NNI, oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500424
William Kurkian6f436d02019-02-06 16:25:01 -0500425 elif intf_oper_indication.type == "pon":
426 # FIXME - handle PON oper state change
427 pass
428
Matt Jeanneret6e315092019-02-20 10:42:57 -0500429 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500430 def onu_discovery_indication(self, onu_disc_indication):
431 intf_id = onu_disc_indication.intf_id
432 serial_number = onu_disc_indication.serial_number
433
434 serial_number_str = self.stringify_serial_number(serial_number)
435
436 self.log.debug("onu discovery indication", intf_id=intf_id,
437 serial_number=serial_number_str)
438
Matt Jeanneretb428b952019-03-07 05:14:17 -0500439 if serial_number_str in self.seen_discovery_indications:
440 self.log.debug("skipping-seen-onu-discovery-indication", intf_id=intf_id,
441 serial_number=serial_number_str)
442 return
443 else:
444 self.seen_discovery_indications.append(serial_number_str)
445
William Kurkian6f436d02019-02-06 16:25:01 -0500446 # Post ONU Discover alarm 20180809_0805
447 try:
448 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
449 serial_number=serial_number_str).raise_alarm()
450 except Exception as disc_alarm_error:
451 self.log.exception("onu-discovery-alarm-error",
452 errmsg=disc_alarm_error.message)
453 # continue for now.
454
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400455 onu_device = yield self.core_proxy.get_child_device(
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500456 self.device_id,
457 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500458
459 if onu_device is None:
460 try:
461 onu_id = self.resource_mgr.get_onu_id(intf_id)
462 if onu_id is None:
463 raise Exception("onu-id-unavailable")
464
465 self.add_onu_device(
466 intf_id,
467 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
468 onu_id, serial_number)
469 self.activate_onu(intf_id, onu_id, serial_number,
470 serial_number_str)
471 except Exception as e:
472 self.log.exception('onu-activation-failed', e=e)
473
474 else:
475 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400476 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500477
478 onu_id = onu_device.proxy_address.onu_id
479 if onu_device.oper_status == OperStatus.DISCOVERED \
480 or onu_device.oper_status == OperStatus.ACTIVATING:
481 self.log.debug("ignore onu discovery indication, \
482 the onu has been discovered and should be \
483 activating shorlty", intf_id=intf_id,
484 onu_id=onu_id, state=onu_device.oper_status)
485 elif onu_device.oper_status == OperStatus.ACTIVE:
486 self.log.warn("onu discovery indication whereas onu is \
487 supposed to be active",
488 intf_id=intf_id, onu_id=onu_id,
489 state=onu_device.oper_status)
490 elif onu_device.oper_status == OperStatus.UNKNOWN:
491 self.log.info("onu in unknown state, recovering from olt \
492 reboot probably, activate onu", intf_id=intf_id,
493 onu_id=onu_id, serial_number=serial_number_str)
494
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400495 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500496
William Kurkian6f436d02019-02-06 16:25:01 -0500497 try:
498 self.activate_onu(intf_id, onu_id, serial_number,
499 serial_number_str)
500 except Exception as e:
501 self.log.error('onu-activation-error',
502 serial_number=serial_number_str, error=e)
503 else:
504 self.log.warn('unexpected state', onu_id=onu_id,
505 onu_device_oper_state=onu_device.oper_status)
506
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500507 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500508 def onu_indication(self, onu_indication):
509 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
510 onu_id=onu_indication.onu_id,
511 serial_number=onu_indication.serial_number,
512 oper_state=onu_indication.oper_state,
513 admin_state=onu_indication.admin_state)
514 try:
515 serial_number_str = self.stringify_serial_number(
516 onu_indication.serial_number)
517 except Exception as e:
518 serial_number_str = None
519
520 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400521 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500522 self.device_id,
523 serial_number=serial_number_str)
524 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400525 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500526 self.device_id,
527 parent_port_no=self.platform.intf_id_to_port_no(
528 onu_indication.intf_id, Port.PON_OLT),
529 onu_id=onu_indication.onu_id)
530
531 if onu_device is None:
532 self.log.error('onu not found', intf_id=onu_indication.intf_id,
533 onu_id=onu_indication.onu_id)
534 return
535
536 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
537 != onu_indication.intf_id:
538 self.log.warn('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)
542 # 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
546 self.log.warn('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)
550
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:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400574 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500575
576 # Move to discovered state
577 self.log.debug('onu-oper-state-is-down')
578
579 if onu_device.oper_status != OperStatus.DISCOVERED:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400580 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500581
Matt Jeanneretb428b952019-03-07 05:14:17 -0500582 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
583
584 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
585 yield self.adapter_proxy.send_inter_adapter_message(
586 msg=onu_indication,
587 type=InterAdapterMessageType.ONU_IND_REQUEST,
588 from_adapter="openolt",
589 to_adapter=onu_device.type,
590 to_device_id=onu_device.id
591 )
William Kurkian6f436d02019-02-06 16:25:01 -0500592
593 elif onu_indication.oper_state == 'up':
594
595 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400596 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500597
598 if onu_device.oper_status != OperStatus.DISCOVERED:
599 self.log.debug("ignore onu indication",
600 intf_id=onu_indication.intf_id,
601 onu_id=onu_indication.onu_id,
602 state=onu_device.oper_status,
603 msg_oper_state=onu_indication.oper_state)
604 return
605
Matt Jeanneretb428b952019-03-07 05:14:17 -0500606 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500607
Matt Jeanneretb428b952019-03-07 05:14:17 -0500608 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
609 yield self.adapter_proxy.send_inter_adapter_message(
610 msg=onu_indication,
611 type=InterAdapterMessageType.ONU_IND_REQUEST,
612 from_adapter="openolt",
613 to_adapter=onu_device.type,
614 to_device_id=onu_device.id
615 )
William Kurkian6f436d02019-02-06 16:25:01 -0500616
617 else:
618 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
619 oper_state=onu_indication.oper_state)
620
621 def onu_ports_down(self, onu_device, oper_state):
622 # Set port oper state to Discovered
623 # add port will update port if it exists
624 # self.adapter_agent.add_port(
625 # self.device_id,
626 # Port(
627 # port_no=uni_no,
628 # label=uni_name,
629 # type=Port.ETHERNET_UNI,
630 # admin_state=onu_device.admin_state,
631 # oper_status=oper_state))
632 # TODO this should be downning ports in onu adatper
633
634 # Disable logical port
635 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
636 for onu_port in onu_ports:
637 self.log.debug('onu-ports-down', onu_port=onu_port)
638 onu_port_id = onu_port.label
639 try:
640 onu_logical_port = self.adapter_agent.get_logical_port(
641 logical_device_id=self.logical_device_id, port_id=onu_port_id)
642 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
643 self.adapter_agent.update_logical_port(
644 logical_device_id=self.logical_device_id,
645 port=onu_logical_port)
646 self.log.debug('cascading-oper-state-to-port-and-logical-port')
647 except KeyError as e:
648 self.log.error('matching-onu-port-label-invalid',
649 onu_id=onu_device.id, olt_id=self.device_id,
650 onu_ports=onu_ports, onu_port_id=onu_port_id,
651 error=e)
652
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500653 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500654 def omci_indication(self, omci_indication):
655
656 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
657 onu_id=omci_indication.onu_id)
658
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400659 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500660 self.device_id, onu_id=omci_indication.onu_id,
661 parent_port_no=self.platform.intf_id_to_port_no(
662 omci_indication.intf_id, Port.PON_OLT), )
663
Matt Jeanneretb428b952019-03-07 05:14:17 -0500664 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
665
666 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
667
668 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
669 yield self.adapter_proxy.send_inter_adapter_message(
670 msg=omci_msg,
671 type=InterAdapterMessageType.OMCI_REQUEST,
672 from_adapter="openolt",
673 to_adapter=onu_device.type,
674 to_device_id=onu_device.id
675 )
William Kurkian6f436d02019-02-06 16:25:01 -0500676
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500677 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500678 def packet_indication(self, pkt_indication):
679
680 self.log.debug("packet indication",
681 intf_type=pkt_indication.intf_type,
682 intf_id=pkt_indication.intf_id,
683 port_no=pkt_indication.port_no,
684 cookie=pkt_indication.cookie,
685 gemport_id=pkt_indication.gemport_id,
686 flow_id=pkt_indication.flow_id)
687
688 if pkt_indication.intf_type == "pon":
689 if pkt_indication.port_no:
Matt Jeannereta591ab82019-04-13 15:54:28 -0400690 port_num = pkt_indication.port_no
William Kurkian6f436d02019-02-06 16:25:01 -0500691 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
692 try:
693 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
694 pkt_indication.gemport_id)
695 onu_id = int(onu_id_uni_id[0])
696 uni_id = int(onu_id_uni_id[1])
697 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
698 if onu_id is None:
699 raise Exception("onu-id-none")
700 if uni_id is None:
701 raise Exception("uni-id-none")
Matt Jeannereta591ab82019-04-13 15:54:28 -0400702 port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500703 except Exception as e:
704 self.log.error("no-onu-reference-for-gem",
705 gemport_id=pkt_indication.gemport_id, e=e)
706 return
707
708
709 elif pkt_indication.intf_type == "nni":
Matt Jeannereta591ab82019-04-13 15:54:28 -0400710 port_num = self.platform.intf_id_to_port_no(
William Kurkian6f436d02019-02-06 16:25:01 -0500711 pkt_indication.intf_id,
712 Port.ETHERNET_NNI)
713
714 pkt = Ether(pkt_indication.pkt)
715
716 self.log.debug("packet indication",
Matt Jeannereta591ab82019-04-13 15:54:28 -0400717 device_id=self.device_id,
718 port_num=port_num)
William Kurkian6f436d02019-02-06 16:25:01 -0500719
Matt Jeannereta591ab82019-04-13 15:54:28 -0400720 yield self.core_proxy.send_packet_in(
721 device_id=self.device_id,
722 port=port_num,
William Kurkian6f436d02019-02-06 16:25:01 -0500723 packet=str(pkt))
724
725 def packet_out(self, egress_port, msg):
726 pkt = Ether(msg)
727 self.log.debug('packet out', egress_port=egress_port,
728 device_id=self.device_id,
729 logical_device_id=self.logical_device_id,
730 packet=str(pkt).encode("HEX"))
731
732 # Find port type
733 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
734 if egress_port_type == Port.ETHERNET_UNI:
735
736 if pkt.haslayer(Dot1Q):
737 outer_shim = pkt.getlayer(Dot1Q)
738 if isinstance(outer_shim.payload, Dot1Q):
739 # If double tag, remove the outer tag
740 payload = (
741 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
742 outer_shim.payload
743 )
744 else:
745 payload = pkt
746 else:
747 payload = pkt
748
749 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
750
751 self.log.debug(
752 'sending-packet-to-ONU', egress_port=egress_port,
753 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
754 onu_id=self.platform.onu_id_from_port_num(egress_port),
755 uni_id=self.platform.uni_id_from_port_num(egress_port),
756 port_no=egress_port,
757 packet=str(payload).encode("HEX"))
758
759 onu_pkt = openolt_pb2.OnuPacket(
760 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
761 onu_id=self.platform.onu_id_from_port_num(egress_port),
762 port_no=egress_port,
763 pkt=send_pkt)
764
765 self.stub.OnuPacketOut(onu_pkt)
766
767 elif egress_port_type == Port.ETHERNET_NNI:
768 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
769 packet=str(pkt).encode("HEX"))
770
771 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
772
773 uplink_pkt = openolt_pb2.UplinkPacket(
774 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
775 pkt=send_pkt)
776
777 self.stub.UplinkPacketOut(uplink_pkt)
778
779 else:
780 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
781 egress_port=egress_port,
782 port_type=egress_port_type)
783
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500784 @inlineCallbacks
Matt Jeanneretb428b952019-03-07 05:14:17 -0500785 def process_inter_adapter_message(self, request):
786 self.log.debug('process-inter-adapter-message', msg=request)
787 try:
788 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
789 omci_msg = InterAdapterOmciMessage()
790 request.body.Unpack(omci_msg)
791 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
792
793 onu_device_id = request.header.to_device_id
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400794 onu_device = yield self.core_proxy.get_device(onu_device_id)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500795 self.send_proxied_message(onu_device, omci_msg.message)
796
797 else:
798 self.log.error("inter-adapter-unhandled-type", request=request)
799
800 except Exception as e:
801 self.log.exception("error-processing-inter-adapter-message", e=e)
802
803 def send_proxied_message(self, onu_device, msg):
804
William Kurkian6f436d02019-02-06 16:25:01 -0500805 if onu_device.connect_status != ConnectStatus.REACHABLE:
806 self.log.debug('ONU is not reachable, cannot send OMCI',
807 serial_number=onu_device.serial_number,
808 intf_id=onu_device.proxy_address.channel_id,
809 onu_id=onu_device.proxy_address.onu_id)
810 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500811
812 omci = openolt_pb2.OmciMsg(intf_id=onu_device.proxy_address.channel_id,
813 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
William Kurkian6f436d02019-02-06 16:25:01 -0500814 self.stub.OmciMsgOut(omci)
815
Matt Jeanneretb428b952019-03-07 05:14:17 -0500816 self.log.debug("omci-message-sent", intf_id=onu_device.proxy_address.channel_id,
817 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
818
Matt Jeanneret6e315092019-02-20 10:42:57 -0500819 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500820 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500821 self.log.info("adding-onu", port_no=port_no, onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500822 serial_number=serial_number)
823
William Kurkian6f436d02019-02-06 16:25:01 -0500824 serial_number_str = self.stringify_serial_number(serial_number)
825
Matt Jeanneretb428b952019-03-07 05:14:17 -0500826 # TODO NEW CORE dont hardcode child device type. find some way of determining by vendor in serial number
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400827 yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500828 parent_device_id=self.device_id,
829 parent_port_no=port_no,
830 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500831 channel_id=intf_id,
832 vendor_id=serial_number.vendor_id,
833 serial_number=serial_number_str,
834 onu_id=onu_id
William Kurkian6f436d02019-02-06 16:25:01 -0500835 )
836
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500837 self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
838
Matt Jeannerete33a7092019-03-12 21:54:14 -0400839 def get_ofp_device_info(self, device):
840 self.log.info('get_ofp_device_info', device_id=device.id)
841
842 mfr_desc = self.device_info.vendor
843 sw_desc = self.device_info.firmware_version
844 hw_desc = self.device_info.model
845 if self.device_info.hardware_version: hw_desc += '-' + self.device_info.hardware_version
846
847 return SwitchCapability(
848 desc=ofp_desc(
849 hw_desc=hw_desc,
850 sw_desc=sw_desc,
851 serial_num=device.serial_number
852 ),
853 switch_features=ofp_switch_features(
854 n_buffers=256, # Max packets buffered at once # TODO fake for now
855 n_tables=2, # Number of tables supported by datapath # TODO fake for now
856 capabilities=( #Bitmap of support "ofp_capabilities" # TODO fake for now
857 OFPC_FLOW_STATS
858 | OFPC_TABLE_STATS
859 | OFPC_PORT_STATS
860 | OFPC_GROUP_STATS
861 )
862 )
863 )
864
865 def get_ofp_port_info(self, device, port_no):
866 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
867 cap = OFPPF_1GB_FD | OFPPF_FIBER
868 return PortCapability(
869 port=LogicalPort(
870 ofp_port=ofp_port(
871 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
872 config=0,
873 state=OFPPS_LIVE,
874 curr=cap,
875 advertised=cap,
876 peer=cap,
877 curr_speed=OFPPF_1GB_FD,
878 max_speed=OFPPF_1GB_FD
879 ),
880 device_id=device.id,
881 device_port_no=port_no
882 )
883 )
884
William Kurkian6f436d02019-02-06 16:25:01 -0500885 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
886 if port_type is Port.ETHERNET_NNI:
887 return "nni-" + str(port_no)
888 elif port_type is Port.PON_OLT:
889 return "pon" + str(intf_id)
890 elif port_type is Port.ETHERNET_UNI:
891 assert False, 'local UNI management not supported'
892
893 def add_logical_port(self, port_no, intf_id, oper_state):
894 self.log.info('adding-logical-port', port_no=port_no)
895
896 label = self.port_name(port_no, Port.ETHERNET_NNI)
897
898 cap = OFPPF_1GB_FD | OFPPF_FIBER
899 curr_speed = OFPPF_1GB_FD
900 max_speed = OFPPF_1GB_FD
901
902 if oper_state == OperStatus.ACTIVE:
903 of_oper_state = OFPPS_LIVE
904 else:
905 of_oper_state = OFPPS_LINK_DOWN
906
907 ofp = ofp_port(
908 port_no=port_no,
909 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
910 name=label, config=0, state=of_oper_state, curr=cap,
911 advertised=cap, peer=cap, curr_speed=curr_speed,
912 max_speed=max_speed)
913
914 ofp_stats = ofp_port_stats(port_no=port_no)
915
916 logical_port = LogicalPort(
917 id=label, ofp_port=ofp, device_id=self.device_id,
918 device_port_no=port_no, root_port=True,
919 ofp_port_stats=ofp_stats)
920
921 self.adapter_agent.add_logical_port(self.logical_device_id,
922 logical_port)
923
924 def _get_mac_form_port_no(self, port_no):
925 mac = ''
926 for i in range(4):
927 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
928 return '00:00' + mac
929
William Kurkian92bd7122019-02-14 15:26:59 -0500930 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500931 def add_port(self, intf_id, port_type, oper_status):
932 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
933
934 label = self.port_name(port_no, port_type, intf_id)
935
936 self.log.debug('adding-port', port_no=port_no, label=label,
937 port_type=port_type)
938
939 port = Port(port_no=port_no, label=label, type=port_type,
940 admin_state=AdminState.ENABLED, oper_status=oper_status)
941
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400942 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500943
944 def delete_logical_port(self, child_device):
945 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
946 self.logical_device_id))
947 for logical_port in logical_ports:
948 if logical_port.device_id == child_device.id:
949 self.log.debug('delete-logical-port',
950 onu_device_id=child_device.id,
951 logical_port=logical_port)
952 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
953 child_device, logical_port)
954 self.adapter_agent.delete_logical_port(
955 self.logical_device_id, logical_port)
956 return
957
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500958 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500959 def delete_port(self, child_serial_number):
960 ports = self.proxy.get('/devices/{}/ports'.format(
961 self.device_id))
962 for port in ports:
963 if port.label == child_serial_number:
964 self.log.debug('delete-port',
965 onu_serial_number=child_serial_number,
966 port=port)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500967 yield self.adapter_agent.delete_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500968 return
969
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400970 def update_flow_table(self, flow_changes):
William Kurkian6f436d02019-02-06 16:25:01 -0500971
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400972 self.log.debug("update_flow_table", flow_changes=flow_changes)
973
974 flows_to_add = flow_changes.to_add.items
975 flows_to_remove = flow_changes.to_remove.items
976
William Kurkian6f436d02019-02-06 16:25:01 -0500977 if not self.is_state_up():
978 self.log.info('The OLT is not up, we cannot update flows',
979 flows_to_add=[f.id for f in flows_to_add],
980 flows_to_remove=[f.id for f in flows_to_remove])
981 return
982
William Kurkian6f436d02019-02-06 16:25:01 -0500983 self.log.debug('logical flows update', flows_to_add=flows_to_add,
984 flows_to_remove=flows_to_remove)
985
986 for flow in flows_to_add:
987
988 try:
989 self.flow_mgr.add_flow(flow)
990 except Exception as e:
991 self.log.error('failed to add flow', flow=flow, e=e)
992
993 for flow in flows_to_remove:
994
995 try:
996 self.flow_mgr.remove_flow(flow)
997 except Exception as e:
998 self.log.error('failed to remove flow', flow=flow, e=e)
999
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001000 # TODO NEW CORE: Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
1001 #self.flow_mgr.repush_all_different_flows()
William Kurkian6f436d02019-02-06 16:25:01 -05001002
1003 # There has to be a better way to do this
1004 def ip_hex(self, ip):
1005 octets = ip.split(".")
1006 hex_ip = []
1007 for octet in octets:
1008 octet_hex = hex(int(octet))
1009 octet_hex = octet_hex.split('0x')[1]
1010 octet_hex = octet_hex.rjust(2, '0')
1011 hex_ip.append(octet_hex)
1012 return ":".join(hex_ip)
1013
1014 def stringify_vendor_specific(self, vendor_specific):
1015 return ''.join(str(i) for i in [
1016 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1017 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1018 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1019 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1020 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1021 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1022 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1023 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
1024
1025 def stringify_serial_number(self, serial_number):
1026 return ''.join([serial_number.vendor_id,
1027 self.stringify_vendor_specific(
1028 serial_number.vendor_specific)])
1029
1030 def destringify_serial_number(self, serial_number_str):
1031 serial_number = openolt_pb2.SerialNumber(
1032 vendor_id=serial_number_str[:4].encode('utf-8'),
1033 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1034 return serial_number
1035
1036 def disable(self):
1037 self.log.debug('sending-deactivate-olt-message',
1038 device_id=self.device_id)
1039
1040 try:
1041 # Send grpc call
1042 self.stub.DisableOlt(openolt_pb2.Empty())
1043 # The resulting indication will bring the OLT down
1044 # self.go_state_down()
1045 self.log.info('openolt device disabled')
1046 except Exception as e:
1047 self.log.error('Failure to disable openolt device', error=e)
1048
1049 def delete(self):
1050 self.log.info('deleting-olt', device_id=self.device_id,
1051 logical_device_id=self.logical_device_id)
1052
1053 # Clears up the data from the resource manager KV store
1054 # for the device
1055 del self.resource_mgr
1056
1057 try:
1058 # Rebooting to reset the state
1059 self.reboot()
1060 # Removing logical device
William Kurkian6f436d02019-02-06 16:25:01 -05001061 except Exception as e:
1062 self.log.error('Failure to delete openolt device', error=e)
1063 raise e
1064 else:
1065 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1066
1067 def reenable(self):
1068 self.log.debug('reenabling-olt', device_id=self.device_id)
1069
1070 try:
1071 self.stub.ReenableOlt(openolt_pb2.Empty())
1072
William Kurkian6f436d02019-02-06 16:25:01 -05001073 except Exception as e:
1074 self.log.error('Failure to reenable openolt device', error=e)
1075 else:
1076 self.log.info('openolt device reenabled')
1077
1078 def activate_onu(self, intf_id, onu_id, serial_number,
1079 serial_number_str):
1080 pir = self.bw_mgr.pir(serial_number_str)
1081 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1082 serial_number_str=serial_number_str,
1083 serial_number=serial_number, pir=pir)
1084 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1085 serial_number=serial_number, pir=pir)
1086 self.stub.ActivateOnu(onu)
1087 self.log.info('onu-activated', serial_number=serial_number_str)
1088
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001089 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001090 def delete_child_device(self, child_device):
1091 self.log.debug('sending-deactivate-onu',
1092 olt_device_id=self.device_id,
1093 onu_device=child_device,
1094 onu_serial_number=child_device.serial_number)
1095 try:
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001096 yield self.adapter_agent.delete_child_device(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001097 child_device.id,
1098 child_device)
1099 except Exception as e:
1100 self.log.error('adapter_agent error', error=e)
1101 try:
1102 self.delete_logical_port(child_device)
1103 except Exception as e:
1104 self.log.error('logical_port delete error', error=e)
1105 try:
1106 self.delete_port(child_device.serial_number)
1107 except Exception as e:
1108 self.log.error('port delete error', error=e)
1109 serial_number = self.destringify_serial_number(
1110 child_device.serial_number)
1111 # TODO FIXME - For each uni.
1112 # TODO FIXME - Flows are not deleted
1113 uni_id = 0 # FIXME
1114 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001115 child_device.proxy_address.channel_id,
1116 child_device.proxy_address.onu_id,
1117 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001118 )
1119 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1120 child_device.proxy_address.onu_id,
1121 uni_id)
1122 # Free any PON resources that were reserved for the ONU
1123 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1124
1125 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1126 onu_id=child_device.proxy_address.onu_id,
1127 serial_number=serial_number)
1128 self.stub.DeleteOnu(onu)
1129
1130 def reboot(self):
1131 self.log.debug('rebooting openolt device', device_id=self.device_id)
1132 try:
1133 self.stub.Reboot(openolt_pb2.Empty())
1134 except Exception as e:
1135 self.log.error('something went wrong with the reboot', error=e)
1136 else:
1137 self.log.info('device rebooted')
1138
1139 def trigger_statistics_collection(self):
1140 try:
1141 self.stub.CollectStatistics(openolt_pb2.Empty())
1142 except Exception as e:
1143 self.log.error('Error while triggering statistics collection',
1144 error=e)
1145 else:
1146 self.log.info('statistics requested')
1147
1148 def simulate_alarm(self, alarm):
1149 self.alarm_mgr.simulate_alarm(alarm)