blob: 3785598d57713190c33a2599dd7cee93775fc739 [file] [log] [blame]
Shad Ansari2825d012018-02-22 23:57:46 +00001#
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#
Shad Ansari2825d012018-02-22 23:57:46 +000016import threading
Shad Ansari94250fc2018-07-04 06:52:11 +000017import binascii
Shad Ansari2825d012018-02-22 23:57:46 +000018import grpc
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -080019import socket
20import re
Shad Ansari94250fc2018-07-04 06:52:11 +000021import structlog
Shad Ansari15928d12018-04-17 02:42:13 +000022from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000023from scapy.layers.l2 import Ether, Dot1Q
Shad Ansari3cd9bf22018-07-25 19:29:39 +000024from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000025
Shad Ansari2825d012018-02-22 23:57:46 +000026from voltha.protos.device_pb2 import Port, Device
27from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
28from voltha.protos.logical_device_pb2 import LogicalDevice
Shad Ansarif9d2d102018-06-13 02:15:26 +000029from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
30 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, \
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -040031 OFPC_TABLE_STATS, OFPC_FLOW_STATS, ofp_switch_features, ofp_port, \
Jonathan Hart05845412018-07-19 09:55:43 -070032 ofp_port_stats, ofp_desc
Shad Ansarif9d2d102018-06-13 02:15:26 +000033from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.core.logical_device_agent import mac_str_to_tuple
35from voltha.registry import registry
36from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040037from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
38from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
Shad Ansari2825d012018-02-22 23:57:46 +000039
mzadig384783a2018-08-09 08:52:40 -040040from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
Shad Ansarif9d2d102018-06-13 02:15:26 +000041
42
Shad Ansari2825d012018-02-22 23:57:46 +000043class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000044 """
45 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000046
Shad Ansari94250fc2018-07-04 06:52:11 +000047 null ----> init ------> connected -----> up -----> down
48 ^ ^ | ^ | |
49 | | | | | |
50 | +-------------+ +---------+ |
51 | |
52 +-----------------------------------------+
53 """
54 # pylint: disable=too-many-instance-attributes
55 # pylint: disable=R0904
56 states = [
57 'state_null',
58 'state_init',
59 'state_connected',
60 'state_up',
61 'state_down']
62
Shad Ansari22efe832018-05-19 05:37:03 +000063 transitions = [
Shad Ansari0e7ad962018-09-28 01:42:26 +000064 {'trigger': 'go_state_init',
65 'source': ['state_null', 'state_connected', 'state_down'],
66 'dest': 'state_init',
67 'before': 'do_state_init',
68 'after': 'post_init'},
69 {'trigger': 'go_state_connected',
70 'source': 'state_init',
71 'dest': 'state_connected',
72 'before': 'do_state_connected'},
73 {'trigger': 'go_state_up',
74 'source': ['state_connected', 'state_down'],
75 'dest': 'state_up',
76 'before': 'do_state_up'},
77 {'trigger': 'go_state_down',
78 'source': ['state_up'],
79 'dest': 'state_down',
80 'before': 'do_state_down',
81 'after': 'post_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000082
Shad Ansari2825d012018-02-22 23:57:46 +000083 def __init__(self, **kwargs):
84 super(OpenoltDevice, self).__init__()
85
86 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000087 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000088 device = kwargs['device']
Shad Ansaricd20a6d2018-10-02 14:36:33 +000089
90 self.platform_class = kwargs['support_classes']['platform']
Girish Gowdru1e77ea02018-09-24 09:10:35 -070091 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
Shad Ansaricd20a6d2018-10-02 14:36:33 +000092 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
93 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
94 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
95 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
96
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040097 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000098 self.device_id = device.id
99 self.host_and_port = device.host_and_port
Girish Gowdru141ced82018-09-17 20:19:14 -0700100 self.extra_args = device.extra_args
Shad Ansarif9d2d102018-06-13 02:15:26 +0000101 self.log = structlog.get_logger(id=self.device_id,
102 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400103 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +0000104
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -0800105 self.log.info('openolt-device-init')
106
Craig Lutgen109d4072018-12-11 17:01:16 -0600107 # default device id and device serial number. If device_info provides better results, they will be updated
108 self.dpid = kwargs.get('dp_id')
109 self.serial_number = self.host_and_port # FIXME
110
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400111 # Device already set in the event of reconciliation
112 if not is_reconciliation:
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -0800113 self.log.info('updating-device')
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400114 # It is a new device
115 # Update device
116 device.root = True
Shad Ansari94250fc2018-07-04 06:52:11 +0000117 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400118 device.oper_status = OperStatus.ACTIVATING
119 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000120
Craig Lutgen109d4072018-12-11 17:01:16 -0600121 # If logical device does exist use it, else create one after connecting to device
122 if device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400123 # logical device already exists
124 self.logical_device_id = device.parent_id
125 if is_reconciliation:
126 self.adapter_agent.reconcile_logical_device(
127 self.logical_device_id)
128
Shad Ansari94250fc2018-07-04 06:52:11 +0000129 # Initialize the OLT state machine
130 self.machine = Machine(model=self, states=OpenoltDevice.states,
131 transitions=OpenoltDevice.transitions,
132 send_event=True, initial='state_null')
133 self.go_state_init()
134
Craig Lutgen109d4072018-12-11 17:01:16 -0600135 def create_logical_device(self, device_info):
136 dpid = device_info.device_id
137 serial_number = device_info.device_serial_number
138
139 if dpid is None: dpid = self.dpid
140 if serial_number is None: serial_number = self.serial_number
141
142 if dpid == None:
143 uri = self.host_and_port.split(":")[0]
144 try:
145 socket.inet_pton(socket.AF_INET, uri)
146 dpid = '00:00:' + self.ip_hex(uri)
147 except socket.error:
148 # this is not an IP
149 dpid = self.stringToMacAddr(uri)
150
151 self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number)
152
153 mfr_desc = device_info.vendor
154 sw_desc = device_info.firmware_version
155 hw_desc = device_info.model
156 if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version
157
158 # Create logical OF device
159 ld = LogicalDevice(
160 root_device_id=self.device_id,
161 switch_features=ofp_switch_features(
162 n_buffers=256, # TODO fake for now
163 n_tables=2, # TODO ditto
164 capabilities=( # TODO and ditto
165 OFPC_FLOW_STATS
166 | OFPC_TABLE_STATS
167 | OFPC_PORT_STATS
168 | OFPC_GROUP_STATS
169 )
170 ),
171 desc=ofp_desc(
172 mfr_desc=mfr_desc,
173 hw_desc=hw_desc,
174 sw_desc=sw_desc,
175 serial_num=serial_number
176 )
177 )
178 ld_init = self.adapter_agent.create_logical_device(ld,
179 dpid=dpid)
180
181 self.logical_device_id = ld_init.id
182
183 device = self.adapter_agent.get_device(self.device_id)
184 device.serial_number = serial_number
185 self.adapter_agent.update_device(device)
186
187 self.dpid = dpid
188 self.serial_number = serial_number
189
190 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
191
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -0800192 def stringToMacAddr(self, uri):
193 regex = re.compile('[^a-zA-Z]')
194 uri = regex.sub('', uri)
195
196 l = len(uri)
197 if l > 6:
198 uri = uri[0:6]
199 else:
200 uri = uri + uri[0:6 - l]
201
202 print uri
203
204 return ":".join([hex(ord(x))[-2:] for x in uri])
205
Shad Ansari94250fc2018-07-04 06:52:11 +0000206 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000207 # Initialize gRPC
208 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000209 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400210
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400211 self.log.info('openolt-device-created', device_id=self.device_id)
212
213 def post_init(self, event):
214 self.log.debug('post_init')
215
216 # We have reached init state, starting the indications thread
217
jasonhuang5f3e63b2018-07-27 01:32:48 +0800218 # Catch RuntimeError exception
219 try:
220 # Start indications thread
221 self.indications_thread_handle = threading.Thread(
222 target=self.indications_thread)
Shad Ansarif9521ad2018-09-08 10:46:43 +0000223 # Old getter/setter API for daemon; use it directly as a
jasonhuang5f3e63b2018-07-27 01:32:48 +0800224 # property instead. The Jinkins error will happon on the reason of
Shad Ansarif9521ad2018-09-08 10:46:43 +0000225 # Exception in thread Thread-1 (most likely raised # during
jasonhuang5f3e63b2018-07-27 01:32:48 +0800226 # interpreter shutdown)
227 self.indications_thread_handle.setDaemon(True)
228 self.indications_thread_handle.start()
Shad Ansarif9521ad2018-09-08 10:46:43 +0000229 except Exception as e:
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400230 self.log.exception('post_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000231
232 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400233 self.log.debug("do_state_connected")
234
Shad Ansari94250fc2018-07-04 06:52:11 +0000235 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000236
237 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400238
239 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
240 self.log.info('Device connected', device_info=device_info)
241
Craig Lutgen109d4072018-12-11 17:01:16 -0600242 self.create_logical_device(device_info)
243
244 device.serial_number = self.serial_number
245
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700246 self.resource_mgr = self.resource_mgr_class(self.device_id,
Shad Ansari78de2be2018-10-12 22:13:54 +0000247 self.host_and_port,
248 self.extra_args,
249 device_info)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000250 self.platform = self.platform_class(self.log, self.resource_mgr)
Girish Gowdruab836e92018-10-25 01:17:57 -0700251 self.flow_mgr = self.flow_mgr_class(self.adapter_agent, self.log,
252 self.stub, self.device_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000253 self.logical_device_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700254 self.platform, self.resource_mgr)
255
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000256 self.alarm_mgr = self.alarm_mgr_class(self.log, self.adapter_agent,
257 self.device_id,
258 self.logical_device_id,
259 self.platform)
260 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
261 self.bw_mgr = self.bw_mgr_class(self.log, self.proxy)
262
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400263 device.vendor = device_info.vendor
264 device.model = device_info.model
265 device.hardware_version = device_info.hardware_version
266 device.firmware_version = device_info.firmware_version
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700267
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700268 # TODO: check for uptime and reboot if too long (VOL-1192)
269
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400270 device.connect_status = ConnectStatus.REACHABLE
271 self.adapter_agent.update_device(device)
272
Shad Ansari94250fc2018-07-04 06:52:11 +0000273 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400274 self.log.debug("do_state_up")
275
Shad Ansari94250fc2018-07-04 06:52:11 +0000276 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000277
Shad Ansari94250fc2018-07-04 06:52:11 +0000278 # Update phys OF device
279 device.parent_id = self.logical_device_id
280 device.oper_status = OperStatus.ACTIVE
281 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400282
Shad Ansari94250fc2018-07-04 06:52:11 +0000283 def do_state_down(self, event):
284 self.log.debug("do_state_down")
285 oper_state = OperStatus.UNKNOWN
286 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400287
Shad Ansari94250fc2018-07-04 06:52:11 +0000288 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400289
Shad Ansari94250fc2018-07-04 06:52:11 +0000290 # Children ports
291 child_devices = self.adapter_agent.get_child_devices(self.device_id)
292 for onu_device in child_devices:
Shad Ansari0e7ad962018-09-28 01:42:26 +0000293 onu_adapter_agent = \
294 registry('adapter_loader').get_agent(onu_device.adapter)
295 onu_adapter_agent.update_interface(onu_device,
296 {'oper_state': 'down'})
Craig Lutgenabd9c842018-11-15 23:58:27 +0000297 self.onu_ports_down(onu_device, oper_state)
298
Shad Ansari94250fc2018-07-04 06:52:11 +0000299 # Children devices
300 self.adapter_agent.update_child_devices_state(
301 self.device_id, oper_status=oper_state,
302 connect_status=connect_state)
303 # Device Ports
304 device_ports = self.adapter_agent.get_ports(self.device_id,
305 Port.ETHERNET_NNI)
306 logical_ports_ids = [port.label for port in device_ports]
307 device_ports += self.adapter_agent.get_ports(self.device_id,
308 Port.PON_OLT)
309
310 for port in device_ports:
311 port.oper_status = oper_state
312 self.adapter_agent.add_port(self.device_id, port)
313
314 # Device logical port
315 for logical_port_id in logical_ports_ids:
316 logical_port = self.adapter_agent.get_logical_port(
317 self.logical_device_id, logical_port_id)
318 logical_port.ofp_port.state = OFPPS_LINK_DOWN
319 self.adapter_agent.update_logical_port(self.logical_device_id,
320 logical_port)
321
322 # Device
323 device = self.adapter_agent.get_device(self.device_id)
324 device.oper_status = oper_state
325 device.connect_status = connect_state
326
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400327 reactor.callLater(2, self.adapter_agent.update_device, device)
328
329 # def post_up(self, event):
330 # self.log.debug('post-up')
331 # self.flow_mgr.reseed_flows()
332
333 def post_down(self, event):
334 self.log.debug('post_down')
335 self.flow_mgr.reset_flows()
336
Shad Ansari94250fc2018-07-04 06:52:11 +0000337 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400338 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000339 self.log.debug('connecting to olt', device_id=self.device_id)
340 self.channel_ready_future.result() # blocking call
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400341 self.log.info('connected to olt', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000342 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000343
Shad Ansari15928d12018-04-17 02:42:13 +0000344 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000345
Shad Ansari94250fc2018-07-04 06:52:11 +0000346 while True:
nick47b74372018-05-25 18:22:49 -0400347 try:
348 # get the next indication from olt
349 ind = next(self.indications)
350 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000351 self.log.warn('gRPC connection lost', error=e)
352 reactor.callFromThread(self.go_state_down)
353 reactor.callFromThread(self.go_state_init)
354 break
nick47b74372018-05-25 18:22:49 -0400355 else:
356 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000357
nick47b74372018-05-25 18:22:49 -0400358 # indication handlers run in the main event loop
359 if ind.HasField('olt_ind'):
360 reactor.callFromThread(self.olt_indication, ind.olt_ind)
361 elif ind.HasField('intf_ind'):
362 reactor.callFromThread(self.intf_indication, ind.intf_ind)
363 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000364 reactor.callFromThread(self.intf_oper_indication,
365 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400366 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000367 reactor.callFromThread(self.onu_discovery_indication,
368 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400369 elif ind.HasField('onu_ind'):
370 reactor.callFromThread(self.onu_indication, ind.onu_ind)
371 elif ind.HasField('omci_ind'):
372 reactor.callFromThread(self.omci_indication, ind.omci_ind)
373 elif ind.HasField('pkt_ind'):
374 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400375 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400376 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700377 self.stats_mgr.port_statistics_indication,
378 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400379 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400380 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700381 self.stats_mgr.flow_statistics_indication,
382 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000383 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400384 reactor.callFromThread(self.alarm_mgr.process_alarms,
385 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400386 else:
387 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400388
Shad Ansari2825d012018-02-22 23:57:46 +0000389 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000390 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000391 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000392 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000393 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000394
395 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400396 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000397 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000398
399 if intf_indication.oper_state == "up":
400 oper_status = OperStatus.ACTIVE
401 else:
402 oper_status = OperStatus.DISCOVERED
403
nick47b74372018-05-25 18:22:49 -0400404 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000405 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
406
407 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000408 self.log.debug("Received interface oper state change indication",
409 intf_id=intf_oper_indication.intf_id,
410 type=intf_oper_indication.type,
411 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000412
413 if intf_oper_indication.oper_state == "up":
414 oper_state = OperStatus.ACTIVE
415 else:
416 oper_state = OperStatus.DISCOVERED
417
418 if intf_oper_indication.type == "nni":
419
Shad Ansari0346f0d2018-04-26 06:54:09 +0000420 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000421 if intf_oper_indication.intf_id != 0:
422 return
423
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400424 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000425 port_no, label = self.add_port(intf_oper_indication.intf_id,
426 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400427 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000428 self.add_logical_port(port_no, intf_oper_indication.intf_id,
429 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000430
431 elif intf_oper_indication.type == "pon":
432 # FIXME - handle PON oper state change
433 pass
434
435 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000436 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000437 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000438
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400439 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000440
Shad Ansarif9d2d102018-06-13 02:15:26 +0000441 self.log.debug("onu discovery indication", intf_id=intf_id,
442 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400443
mzadig384783a2018-08-09 08:52:40 -0400444 # Post ONU Discover alarm 20180809_0805
445 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400446 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
447 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400448 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400449 self.log.exception("onu-discovery-alarm-error",
450 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400451 # continue for now.
452
Shad Ansarif9d2d102018-06-13 02:15:26 +0000453 onu_device = self.adapter_agent.get_child_device(
454 self.device_id,
455 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400456
457 if onu_device is None:
Shad Ansari15928d12018-04-17 02:42:13 +0000458 try:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700459 onu_id = self.resource_mgr.get_onu_id(intf_id)
460 if onu_id is None:
461 raise Exception("onu-id-unavailable")
462
Shad Ansarif9d2d102018-06-13 02:15:26 +0000463 self.add_onu_device(
464 intf_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000465 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000466 onu_id, serial_number)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400467 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -0700468 serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400469 except Exception as e:
470 self.log.exception('onu-activation-failed', e=e)
471
Shad Ansari2825d012018-02-22 23:57:46 +0000472 else:
nick47b74372018-05-25 18:22:49 -0400473 if onu_device.connect_status != ConnectStatus.REACHABLE:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700474 onu_device.connect_status = ConnectStatus.REACHABLE
475 self.adapter_agent.update_device(onu_device)
nick47b74372018-05-25 18:22:49 -0400476
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400477 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000478 if onu_device.oper_status == OperStatus.DISCOVERED \
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700479 or onu_device.oper_status == OperStatus.ACTIVATING:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000480 self.log.debug("ignore onu discovery indication, \
481 the onu has been discovered and should be \
482 activating shorlty", intf_id=intf_id,
483 onu_id=onu_id, state=onu_device.oper_status)
484 elif onu_device.oper_status == OperStatus.ACTIVE:
485 self.log.warn("onu discovery indication whereas onu is \
486 supposed to be active",
487 intf_id=intf_id, onu_id=onu_id,
488 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400489 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000490 self.log.info("onu in unknown state, recovering from olt \
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400491 reboot probably, activate onu", intf_id=intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000492 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400493
494 onu_device.oper_status = OperStatus.DISCOVERED
495 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400496 try:
497 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -0700498 serial_number_str)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400499 except Exception as e:
500 self.log.error('onu-activation-error',
501 serial_number=serial_number_str, error=e)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400502 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000503 self.log.warn('unexpected state', onu_id=onu_id,
504 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000505
Shad Ansari2825d012018-02-22 23:57:46 +0000506 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000507 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000508 onu_id=onu_indication.onu_id,
509 serial_number=onu_indication.serial_number,
510 oper_state=onu_indication.oper_state,
511 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400512 try:
513 serial_number_str = self.stringify_serial_number(
514 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000515 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400516 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000517
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400518 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400519 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700520 self.device_id,
521 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000522 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400523 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700524 self.device_id,
525 parent_port_no=self.platform.intf_id_to_port_no(
526 onu_indication.intf_id, Port.PON_OLT),
527 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400528
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400529 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000530 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000531 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000532 return
533
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000534 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
Shad Ansarif9521ad2018-09-08 10:46:43 +0000535 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400536 self.log.warn('ONU-is-on-a-different-intf-id-now',
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900537 previous_intf_id=self.platform.intf_id_from_pon_port_no(
538 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400539 current_intf_id=onu_indication.intf_id)
540 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000541
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400542 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
543 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400544 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
545 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000546 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400547 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000548
Shad Ansarif9d2d102018-06-13 02:15:26 +0000549 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400550 if onu_indication.admin_state == 'down':
551 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000552 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
553 oper_state=onu_indication.oper_state)
554 # Forcing the oper state change code to execute
555 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400556
Shad Ansarif9d2d102018-06-13 02:15:26 +0000557 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400558
559 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400560 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400561
562 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000563 self.log.warn('Invalid-or-not-implemented-admin-state',
564 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400565
566 self.log.debug('admin-state-dealt-with')
567
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400568 onu_adapter_agent = \
569 registry('adapter_loader').get_agent(onu_device.adapter)
570 if onu_adapter_agent is None:
571 self.log.error('onu_adapter_agent-could-not-be-retrieved',
572 onu_device=onu_device)
573 return
574
Shad Ansarif9d2d102018-06-13 02:15:26 +0000575 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400576 if onu_indication.oper_state == 'down':
Nicolas Palpacuer4ea3a652018-08-22 10:33:17 -0400577
578 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
579 onu_device.connect_status = ConnectStatus.UNREACHABLE
580 self.adapter_agent.update_device(onu_device)
581
Shad Ansarif9d2d102018-06-13 02:15:26 +0000582 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400583 self.log.debug('onu-oper-state-is-down')
584
585 if onu_device.oper_status != OperStatus.DISCOVERED:
586 onu_device.oper_status = OperStatus.DISCOVERED
587 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000588 # Set port oper state to Discovered
Craig Lutgenabd9c842018-11-15 23:58:27 +0000589 self.onu_ports_down(onu_device, OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400590
Shad Ansari0e7ad962018-09-28 01:42:26 +0000591 onu_adapter_agent.update_interface(onu_device,
592 {'oper_state': 'down'})
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400593
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400594 elif onu_indication.oper_state == 'up':
595
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400596 if onu_device.connect_status != ConnectStatus.REACHABLE:
597 onu_device.connect_status = ConnectStatus.REACHABLE
598 self.adapter_agent.update_device(onu_device)
599
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400600 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000601 self.log.debug("ignore onu indication",
602 intf_id=onu_indication.intf_id,
603 onu_id=onu_indication.onu_id,
604 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400605 msg_oper_state=onu_indication.oper_state)
606 return
607
Shad Ansarif9d2d102018-06-13 02:15:26 +0000608 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400609
Shad Ansarif9d2d102018-06-13 02:15:26 +0000610 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400611
Shad Ansari0e7ad962018-09-28 01:42:26 +0000612 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400613
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400614 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000615 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
616 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000617
Craig Lutgenabd9c842018-11-15 23:58:27 +0000618 def onu_ports_down(self, onu_device, oper_state):
nick47b74372018-05-25 18:22:49 -0400619 # Set port oper state to Discovered
620 # add port will update port if it exists
Craig Lutgenabd9c842018-11-15 23:58:27 +0000621 # self.adapter_agent.add_port(
622 # self.device_id,
623 # Port(
624 # port_no=uni_no,
625 # label=uni_name,
626 # type=Port.ETHERNET_UNI,
627 # admin_state=onu_device.admin_state,
628 # oper_status=oper_state))
629 # TODO this should be downning ports in onu adatper
nick47b74372018-05-25 18:22:49 -0400630
631 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400632 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
nick47b74372018-05-25 18:22:49 -0400633 for onu_port in onu_ports:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000634 self.log.debug('onu-ports-down', onu_port=onu_port)
635 onu_port_id = onu_port.label
636 try:
637 onu_logical_port = self.adapter_agent.get_logical_port(
638 logical_device_id=self.logical_device_id, port_id=onu_port_id)
639 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
640 self.adapter_agent.update_logical_port(
641 logical_device_id=self.logical_device_id,
642 port=onu_logical_port)
643 self.log.debug('cascading-oper-state-to-port-and-logical-port')
644 except KeyError as e:
645 self.log.error('matching-onu-port-label-invalid',
646 onu_id=onu_device.id, olt_id=self.device_id,
647 onu_ports=onu_ports, onu_port_id=onu_port_id,
648 error=e)
nick47b74372018-05-25 18:22:49 -0400649
Shad Ansari2825d012018-02-22 23:57:46 +0000650 def omci_indication(self, omci_indication):
651
652 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000653 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000654
Shad Ansarif9d2d102018-06-13 02:15:26 +0000655 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400656 self.device_id, onu_id=omci_indication.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000657 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700658 omci_indication.intf_id, Port.PON_OLT), )
Shad Ansari0efa6512018-04-28 06:42:54 +0000659
660 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000661 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000662
Shad Ansari42db7342018-04-25 21:39:46 +0000663 def packet_indication(self, pkt_indication):
664
Shad Ansari0ff82622018-09-30 09:32:04 +0000665 self.log.debug("packet indication",
666 intf_type=pkt_indication.intf_type,
667 intf_id=pkt_indication.intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000668 port_no=pkt_indication.port_no,
669 cookie=pkt_indication.cookie,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000670 gemport_id=pkt_indication.gemport_id,
671 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000672
Shad Ansari0ff82622018-09-30 09:32:04 +0000673 if pkt_indication.intf_type == "pon":
Craig Lutgenabd9c842018-11-15 23:58:27 +0000674 if pkt_indication.port_no:
675 logical_port_num = pkt_indication.port_no
676 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
677 try:
678 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
679 pkt_indication.gemport_id)
680 onu_id = int(onu_id_uni_id[0])
681 uni_id = int(onu_id_uni_id[1])
682 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
683 if onu_id is None:
684 raise Exception("onu-id-none")
685 if uni_id is None:
686 raise Exception("uni-id-none")
687 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
688 except Exception as e:
689 self.log.error("no-onu-reference-for-gem",
690 gemport_id=pkt_indication.gemport_id, e=e)
691 return
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700692
Craig Lutgenabd9c842018-11-15 23:58:27 +0000693
Shad Ansari0ff82622018-09-30 09:32:04 +0000694 elif pkt_indication.intf_type == "nni":
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000695 logical_port_num = self.platform.intf_id_to_port_no(
Shad Ansari0ff82622018-09-30 09:32:04 +0000696 pkt_indication.intf_id,
697 Port.ETHERNET_NNI)
Shad Ansari42db7342018-04-25 21:39:46 +0000698
699 pkt = Ether(pkt_indication.pkt)
Shad Ansari0ff82622018-09-30 09:32:04 +0000700
701 self.log.debug("packet indication",
702 logical_device_id=self.logical_device_id,
703 logical_port_no=logical_port_num)
704
705 self.adapter_agent.send_packet_in(
706 logical_device_id=self.logical_device_id,
707 logical_port_no=logical_port_num,
708 packet=str(pkt))
Shad Ansari42db7342018-04-25 21:39:46 +0000709
710 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000711 pkt = Ether(msg)
Saurav Dasf87d6552018-09-26 17:05:42 -0700712 self.log.debug('packet out', egress_port=egress_port,
713 device_id=self.device_id,
714 logical_device_id=self.logical_device_id,
715 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400716
717 # Find port type
Craig Lutgenabd9c842018-11-15 23:58:27 +0000718 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400719 if egress_port_type == Port.ETHERNET_UNI:
720
721 if pkt.haslayer(Dot1Q):
722 outer_shim = pkt.getlayer(Dot1Q)
723 if isinstance(outer_shim.payload, Dot1Q):
724 # If double tag, remove the outer tag
725 payload = (
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700726 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
727 outer_shim.payload
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400728 )
729 else:
730 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000731 else:
732 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400733
734 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
735
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400736 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000737 'sending-packet-to-ONU', egress_port=egress_port,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000738 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
739 onu_id=self.platform.onu_id_from_port_num(egress_port),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000740 uni_id=self.platform.uni_id_from_port_num(egress_port),
741 port_no=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000742 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400743
Shad Ansarif9d2d102018-06-13 02:15:26 +0000744 onu_pkt = openolt_pb2.OnuPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000745 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
746 onu_id=self.platform.onu_id_from_port_num(egress_port),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000747 port_no=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000748 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400749
750 self.stub.OnuPacketOut(onu_pkt)
751
752 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400753 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000754 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400755
756 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
757
Shad Ansarif9d2d102018-06-13 02:15:26 +0000758 uplink_pkt = openolt_pb2.UplinkPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000759 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000760 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400761
762 self.stub.UplinkPacketOut(uplink_pkt)
763
Shad Ansari0346f0d2018-04-26 06:54:09 +0000764 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000765 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
766 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400767 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000768
Shad Ansari2825d012018-02-22 23:57:46 +0000769 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9521ad2018-09-08 10:46:43 +0000770 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru141ced82018-09-17 20:19:14 -0700771 self.device_id, onu_id=proxy_address.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000772 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700773 proxy_address.channel_id, Port.PON_OLT)
774 )
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400775 if onu_device.connect_status != ConnectStatus.REACHABLE:
776 self.log.debug('ONU is not reachable, cannot send OMCI',
777 serial_number=onu_device.serial_number,
778 intf_id=onu_device.proxy_address.channel_id,
779 onu_id=onu_device.proxy_address.onu_id)
780 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000781 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
782 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000783 self.stub.OmciMsgOut(omci)
784
785 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000786 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000787 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000788
789 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000790 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000791 channel_id=intf_id, onu_id=onu_id,
792 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000793
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400794 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000795
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400796 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000797
Shad Ansarif9d2d102018-06-13 02:15:26 +0000798 self.adapter_agent.add_onu_device(
799 parent_device_id=self.device_id, parent_port_no=port_no,
800 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
801 root=True, serial_number=serial_number_str,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000802 admin_state=AdminState.ENABLED#, **{'vlan':4091} # magic still maps to brcm_openomci_onu.pon_port.BRDCM_DEFAULT_VLAN
803 )
Shad Ansari2825d012018-02-22 23:57:46 +0000804
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000805 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000806 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400807 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000808 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000809 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000810 elif port_type is Port.ETHERNET_UNI:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000811 assert False, 'local UNI management not supported'
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400812
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400813 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000814 self.log.info('adding-logical-port', port_no=port_no)
815
816 label = self.port_name(port_no, Port.ETHERNET_NNI)
817
818 cap = OFPPF_1GB_FD | OFPPF_FIBER
819 curr_speed = OFPPF_1GB_FD
820 max_speed = OFPPF_1GB_FD
821
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400822 if oper_state == OperStatus.ACTIVE:
823 of_oper_state = OFPPS_LIVE
824 else:
825 of_oper_state = OFPPS_LINK_DOWN
826
Shad Ansarif9d2d102018-06-13 02:15:26 +0000827 ofp = ofp_port(
828 port_no=port_no,
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400829 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000830 name=label, config=0, state=of_oper_state, curr=cap,
831 advertised=cap, peer=cap, curr_speed=curr_speed,
832 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000833
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400834 ofp_stats = ofp_port_stats(port_no=port_no)
835
Shad Ansarif9d2d102018-06-13 02:15:26 +0000836 logical_port = LogicalPort(
837 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400838 device_port_no=port_no, root_port=True,
839 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000840
Shad Ansarif9d2d102018-06-13 02:15:26 +0000841 self.adapter_agent.add_logical_port(self.logical_device_id,
842 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000843
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400844 def _get_mac_form_port_no(self, port_no):
845 mac = ''
846 for i in range(4):
847 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
848 return '00:00' + mac
849
Shad Ansari2825d012018-02-22 23:57:46 +0000850 def add_port(self, intf_id, port_type, oper_status):
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000851 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000852
Shad Ansari4a232ca2018-05-05 05:24:17 +0000853 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000854
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400855 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000856 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000857
858 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000859 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000860
Shad Ansari2825d012018-02-22 23:57:46 +0000861 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000862
Shad Ansari2825d012018-02-22 23:57:46 +0000863 return port_no, label
864
Girish Gowdruab836e92018-10-25 01:17:57 -0700865 def delete_logical_port(self, child_device):
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400866 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
867 self.logical_device_id))
868 for logical_port in logical_ports:
Girish Gowdruab836e92018-10-25 01:17:57 -0700869 if logical_port.device_id == child_device.id:
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400870 self.log.debug('delete-logical-port',
Girish Gowdruab836e92018-10-25 01:17:57 -0700871 onu_device_id=child_device.id,
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400872 logical_port=logical_port)
Girish Gowdruab836e92018-10-25 01:17:57 -0700873 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
874 child_device, logical_port)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400875 self.adapter_agent.delete_logical_port(
876 self.logical_device_id, logical_port)
877 return
Shad Ansarif9521ad2018-09-08 10:46:43 +0000878
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400879 def delete_port(self, child_serial_number):
880 ports = self.proxy.get('/devices/{}/ports'.format(
881 self.device_id))
882 for port in ports:
883 if port.label == child_serial_number:
884 self.log.debug('delete-port',
885 onu_serial_number=child_serial_number,
886 port=port)
887 self.adapter_agent.delete_port(self.device_id, port)
888 return
889
Shad Ansari2825d012018-02-22 23:57:46 +0000890 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400891 self.log.debug('No updates here now, all is done in logical flows '
892 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000893
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400894 def update_logical_flows(self, flows_to_add, flows_to_remove,
895 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400896 if not self.is_state_up():
897 self.log.info('The OLT is not up, we cannot update flows',
898 flows_to_add=[f.id for f in flows_to_add],
899 flows_to_remove=[f.id for f in flows_to_remove])
900 return
901
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400902 try:
903 self.flow_mgr.update_children_flows(device_rules_map)
904 except Exception as e:
905 self.log.error('Error updating children flows', error=e)
Shad Ansari2825d012018-02-22 23:57:46 +0000906
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400907 self.log.debug('logical flows update', flows_to_add=flows_to_add,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000908 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000909
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400910 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400911
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400912 try:
913 self.flow_mgr.add_flow(flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400914 except Exception as e:
915 self.log.error('failed to add flow', flow=flow, e=e)
916
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400917 for flow in flows_to_remove:
918
919 try:
920 self.flow_mgr.remove_flow(flow)
921 except Exception as e:
922 self.log.error('failed to remove flow', flow=flow, e=e)
923
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400924 self.flow_mgr.repush_all_different_flows()
925
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400926 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000927 def ip_hex(self, ip):
928 octets = ip.split(".")
929 hex_ip = []
930 for octet in octets:
931 octet_hex = hex(int(octet))
932 octet_hex = octet_hex.split('0x')[1]
933 octet_hex = octet_hex.rjust(2, '0')
934 hex_ip.append(octet_hex)
935 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400936
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400937 def stringify_vendor_specific(self, vendor_specific):
938 return ''.join(str(i) for i in [
939 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
940 hex(ord(vendor_specific[0]) & 0x0f)[2:],
941 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
942 hex(ord(vendor_specific[1]) & 0x0f)[2:],
943 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
944 hex(ord(vendor_specific[2]) & 0x0f)[2:],
945 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
946 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
947
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400948 def stringify_serial_number(self, serial_number):
949 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000950 self.stringify_vendor_specific(
951 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400952
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400953 def destringify_serial_number(self, serial_number_str):
954 serial_number = openolt_pb2.SerialNumber(
955 vendor_id=serial_number_str[:4].encode('utf-8'),
956 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
957 return serial_number
958
Jonathan Davis0f917a22018-05-30 14:39:45 -0400959 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400960 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9521ad2018-09-08 10:46:43 +0000961 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400962
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400963 try:
964 # Send grpc call
965 self.stub.DisableOlt(openolt_pb2.Empty())
966 # The resulting indication will bring the OLT down
967 # self.go_state_down()
968 self.log.info('openolt device disabled')
969 except Exception as e:
970 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400971
Jonathan Davis0f917a22018-05-30 14:39:45 -0400972 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400973 self.log.info('deleting-olt', device_id=self.device_id,
974 logical_device_id=self.logical_device_id)
975
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700976 # Clears up the data from the resource manager KV store
977 # for the device
978 del self.resource_mgr
979
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400980 try:
981 # Rebooting to reset the state
982 self.reboot()
983 # Removing logical device
Saurav Dasf87d6552018-09-26 17:05:42 -0700984 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
985 self.adapter_agent.delete_logical_device(ld)
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400986 except Exception as e:
987 self.log.error('Failure to delete openolt device', error=e)
988 raise e
989 else:
990 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400991
Jonathan Davis0f917a22018-05-30 14:39:45 -0400992 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400993 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400994
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400995 try:
996 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400997
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400998 self.log.info('enabling-all-ports', device_id=self.device_id)
999 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001000 except Exception as e:
1001 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -04001002 else:
1003 self.log.info('openolt device reenabled')
1004
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001005 def activate_onu(self, intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -07001006 serial_number_str):
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001007 pir = self.bw_mgr.pir(serial_number_str)
1008 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +00001009 serial_number_str=serial_number_str,
Girish Gowdruab836e92018-10-25 01:17:57 -07001010 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001011 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Girish Gowdruab836e92018-10-25 01:17:57 -07001012 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001013 self.stub.ActivateOnu(onu)
1014 self.log.info('onu-activated', serial_number=serial_number_str)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001015
Jonathan Davisb45bb372018-07-19 15:05:15 -04001016 def delete_child_device(self, child_device):
1017 self.log.debug('sending-deactivate-onu',
1018 olt_device_id=self.device_id,
1019 onu_device=child_device,
1020 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001021 try:
1022 self.adapter_agent.delete_child_device(self.device_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +00001023 child_device.id,
1024 child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001025 except Exception as e:
1026 self.log.error('adapter_agent error', error=e)
1027 try:
Girish Gowdruab836e92018-10-25 01:17:57 -07001028 self.delete_logical_port(child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001029 except Exception as e:
1030 self.log.error('logical_port delete error', error=e)
1031 try:
1032 self.delete_port(child_device.serial_number)
1033 except Exception as e:
1034 self.log.error('port delete error', error=e)
Shad Ansarif9521ad2018-09-08 10:46:43 +00001035 serial_number = self.destringify_serial_number(
1036 child_device.serial_number)
Craig Lutgenabd9c842018-11-15 23:58:27 +00001037 # TODO FIXME - For each uni.
1038 # TODO FIXME - Flows are not deleted
1039 uni_id = 0 # FIXME
Girish Gowdrub761bc12018-11-29 02:22:18 -08001040 self.flow_mgr.delete_tech_profile_instance(
1041 child_device.proxy_address.channel_id,
1042 child_device.proxy_address.onu_id,
1043 uni_id
1044 )
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001045 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +00001046 child_device.proxy_address.onu_id,
1047 uni_id)
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001048 # Free any PON resources that were reserved for the ONU
1049 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1050
Jonathan Davisb45bb372018-07-19 15:05:15 -04001051 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1052 onu_id=child_device.proxy_address.onu_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -08001053 serial_number=serial_number)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001054 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001055
1056 def reboot(self):
Shad Ansarif9521ad2018-09-08 10:46:43 +00001057 self.log.debug('rebooting openolt device', device_id=self.device_id)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001058 try:
1059 self.stub.Reboot(openolt_pb2.Empty())
1060 except Exception as e:
1061 self.log.error('something went wrong with the reboot', error=e)
1062 else:
1063 self.log.info('device rebooted')
1064
Nicolas Palpacuer30027f42018-09-06 15:55:54 -04001065 def trigger_statistics_collection(self):
1066 try:
1067 self.stub.CollectStatistics(openolt_pb2.Empty())
1068 except Exception as e:
1069 self.log.error('Error while triggering statistics collection',
1070 error=e)
1071 else:
1072 self.log.info('statistics requested')
Scott Bakerd3190952018-09-04 15:47:28 -07001073
1074 def simulate_alarm(self, alarm):
1075 self.alarm_mgr.simulate_alarm(alarm)