blob: 23949072f0eae0826fb27181ff412d4d79a3e2ab [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 Jeanneretbad3d982019-03-11 16:06:10 -0400293 self.flow_mgr = self.flow_mgr_class(self.core_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:
690 logical_port_num = pkt_indication.port_no
691 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")
702 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
703 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":
710 logical_port_num = self.platform.intf_id_to_port_no(
711 pkt_indication.intf_id,
712 Port.ETHERNET_NNI)
713
714 pkt = Ether(pkt_indication.pkt)
715
716 self.log.debug("packet indication",
717 logical_device_id=self.logical_device_id,
718 logical_port_no=logical_port_num)
719
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500720 yield self.adapter_agent.send_packet_in(
William Kurkian6f436d02019-02-06 16:25:01 -0500721 logical_device_id=self.logical_device_id,
722 logical_port_no=logical_port_num,
723 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
970 def update_flow_table(self, flows):
971 self.log.debug('No updates here now, all is done in logical flows '
972 'update')
973
974 def update_logical_flows(self, flows_to_add, flows_to_remove,
975 device_rules_map):
976 if not self.is_state_up():
977 self.log.info('The OLT is not up, we cannot update flows',
978 flows_to_add=[f.id for f in flows_to_add],
979 flows_to_remove=[f.id for f in flows_to_remove])
980 return
981
982 try:
983 self.flow_mgr.update_children_flows(device_rules_map)
984 except Exception as e:
985 self.log.error('Error updating children flows', error=e)
986
987 self.log.debug('logical flows update', flows_to_add=flows_to_add,
988 flows_to_remove=flows_to_remove)
989
990 for flow in flows_to_add:
991
992 try:
993 self.flow_mgr.add_flow(flow)
994 except Exception as e:
995 self.log.error('failed to add flow', flow=flow, e=e)
996
997 for flow in flows_to_remove:
998
999 try:
1000 self.flow_mgr.remove_flow(flow)
1001 except Exception as e:
1002 self.log.error('failed to remove flow', flow=flow, e=e)
1003
1004 self.flow_mgr.repush_all_different_flows()
1005
1006 # There has to be a better way to do this
1007 def ip_hex(self, ip):
1008 octets = ip.split(".")
1009 hex_ip = []
1010 for octet in octets:
1011 octet_hex = hex(int(octet))
1012 octet_hex = octet_hex.split('0x')[1]
1013 octet_hex = octet_hex.rjust(2, '0')
1014 hex_ip.append(octet_hex)
1015 return ":".join(hex_ip)
1016
1017 def stringify_vendor_specific(self, vendor_specific):
1018 return ''.join(str(i) for i in [
1019 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1020 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1021 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1022 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1023 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1024 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1025 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1026 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
1027
1028 def stringify_serial_number(self, serial_number):
1029 return ''.join([serial_number.vendor_id,
1030 self.stringify_vendor_specific(
1031 serial_number.vendor_specific)])
1032
1033 def destringify_serial_number(self, serial_number_str):
1034 serial_number = openolt_pb2.SerialNumber(
1035 vendor_id=serial_number_str[:4].encode('utf-8'),
1036 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1037 return serial_number
1038
1039 def disable(self):
1040 self.log.debug('sending-deactivate-olt-message',
1041 device_id=self.device_id)
1042
1043 try:
1044 # Send grpc call
1045 self.stub.DisableOlt(openolt_pb2.Empty())
1046 # The resulting indication will bring the OLT down
1047 # self.go_state_down()
1048 self.log.info('openolt device disabled')
1049 except Exception as e:
1050 self.log.error('Failure to disable openolt device', error=e)
1051
1052 def delete(self):
1053 self.log.info('deleting-olt', device_id=self.device_id,
1054 logical_device_id=self.logical_device_id)
1055
1056 # Clears up the data from the resource manager KV store
1057 # for the device
1058 del self.resource_mgr
1059
1060 try:
1061 # Rebooting to reset the state
1062 self.reboot()
1063 # Removing logical device
1064 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
1065 self.adapter_agent.delete_logical_device(ld)
1066 except Exception as e:
1067 self.log.error('Failure to delete openolt device', error=e)
1068 raise e
1069 else:
1070 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1071
1072 def reenable(self):
1073 self.log.debug('reenabling-olt', device_id=self.device_id)
1074
1075 try:
1076 self.stub.ReenableOlt(openolt_pb2.Empty())
1077
William Kurkian6f436d02019-02-06 16:25:01 -05001078 except Exception as e:
1079 self.log.error('Failure to reenable openolt device', error=e)
1080 else:
1081 self.log.info('openolt device reenabled')
1082
1083 def activate_onu(self, intf_id, onu_id, serial_number,
1084 serial_number_str):
1085 pir = self.bw_mgr.pir(serial_number_str)
1086 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1087 serial_number_str=serial_number_str,
1088 serial_number=serial_number, pir=pir)
1089 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1090 serial_number=serial_number, pir=pir)
1091 self.stub.ActivateOnu(onu)
1092 self.log.info('onu-activated', serial_number=serial_number_str)
1093
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001094 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001095 def delete_child_device(self, child_device):
1096 self.log.debug('sending-deactivate-onu',
1097 olt_device_id=self.device_id,
1098 onu_device=child_device,
1099 onu_serial_number=child_device.serial_number)
1100 try:
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001101 yield self.adapter_agent.delete_child_device(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001102 child_device.id,
1103 child_device)
1104 except Exception as e:
1105 self.log.error('adapter_agent error', error=e)
1106 try:
1107 self.delete_logical_port(child_device)
1108 except Exception as e:
1109 self.log.error('logical_port delete error', error=e)
1110 try:
1111 self.delete_port(child_device.serial_number)
1112 except Exception as e:
1113 self.log.error('port delete error', error=e)
1114 serial_number = self.destringify_serial_number(
1115 child_device.serial_number)
1116 # TODO FIXME - For each uni.
1117 # TODO FIXME - Flows are not deleted
1118 uni_id = 0 # FIXME
1119 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001120 child_device.proxy_address.channel_id,
1121 child_device.proxy_address.onu_id,
1122 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001123 )
1124 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1125 child_device.proxy_address.onu_id,
1126 uni_id)
1127 # Free any PON resources that were reserved for the ONU
1128 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1129
1130 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1131 onu_id=child_device.proxy_address.onu_id,
1132 serial_number=serial_number)
1133 self.stub.DeleteOnu(onu)
1134
1135 def reboot(self):
1136 self.log.debug('rebooting openolt device', device_id=self.device_id)
1137 try:
1138 self.stub.Reboot(openolt_pb2.Empty())
1139 except Exception as e:
1140 self.log.error('something went wrong with the reboot', error=e)
1141 else:
1142 self.log.info('device rebooted')
1143
1144 def trigger_statistics_collection(self):
1145 try:
1146 self.stub.CollectStatistics(openolt_pb2.Empty())
1147 except Exception as e:
1148 self.log.error('Error while triggering statistics collection',
1149 error=e)
1150 else:
1151 self.log.info('statistics requested')
1152
1153 def simulate_alarm(self, alarm):
1154 self.alarm_mgr.simulate_alarm(alarm)