blob: 8bcf59a8717d238c3b96999c731076b1260abf64 [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#
16
Shad Ansari2825d012018-02-22 23:57:46 +000017import threading
Shad Ansari94250fc2018-07-04 06:52:11 +000018import binascii
Shad Ansari2825d012018-02-22 23:57:46 +000019import grpc
Shad Ansari2825d012018-02-22 23:57:46 +000020
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
39from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
Shad Ansari2825d012018-02-22 23:57:46 +000040import voltha.core.flow_decomposer as fd
41
Nicolas Palpacuere761c902018-07-05 16:30:52 -040042from voltha.adapters.openolt.openolt_statistics import OpenOltStatisticsMgr
Shad Ansari94250fc2018-07-04 06:52:11 +000043import voltha.adapters.openolt.openolt_platform as platform
44from voltha.adapters.openolt.openolt_flow_mgr import OpenOltFlowMgr, \
45 DEFAULT_MGMT_VLAN
46from voltha.adapters.openolt.openolt_alarms import OpenOltAlarmMgr
Shad Ansari3cd9bf22018-07-25 19:29:39 +000047from voltha.adapters.openolt.openolt_bw import OpenOltBW
mzadig384783a2018-08-09 08:52:40 -040048from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
Shad Ansarif9d2d102018-06-13 02:15:26 +000049
50
Shad Ansari2825d012018-02-22 23:57:46 +000051class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000052 """
53 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000054
Shad Ansari94250fc2018-07-04 06:52:11 +000055 null ----> init ------> connected -----> up -----> down
56 ^ ^ | ^ | |
57 | | | | | |
58 | +-------------+ +---------+ |
59 | |
60 +-----------------------------------------+
61 """
62 # pylint: disable=too-many-instance-attributes
63 # pylint: disable=R0904
64 states = [
65 'state_null',
66 'state_init',
67 'state_connected',
68 'state_up',
69 'state_down']
70
Shad Ansari22efe832018-05-19 05:37:03 +000071 transitions = [
Shad Ansari94250fc2018-07-04 06:52:11 +000072 {'trigger': 'go_state_init',
73 'source': ['state_null', 'state_connected', 'state_down'],
74 'dest': 'state_init',
75 'before': 'do_state_init'},
76 {'trigger': 'go_state_connected',
77 'source': 'state_init',
78 'dest': 'state_connected',
79 'before': 'do_state_connected'},
80 {'trigger': 'go_state_up',
81 'source': ['state_connected', 'state_down'],
82 'dest': 'state_up',
83 'before': 'do_state_up'},
84 {'trigger': 'go_state_down',
85 'source': ['state_up'],
86 'dest': 'state_down',
Nicolas Palpacuer41141352018-08-31 14:11:38 -040087 'before': 'do_state_down',
88 'after': 'post_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000089
Shad Ansari2825d012018-02-22 23:57:46 +000090 def __init__(self, **kwargs):
91 super(OpenoltDevice, self).__init__()
92
93 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000094 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000095 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040096 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000097 self.device_id = device.id
98 self.host_and_port = device.host_and_port
Shad Ansarif9d2d102018-06-13 02:15:26 +000099 self.log = structlog.get_logger(id=self.device_id,
100 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400101 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +0000102
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400103 # Device already set in the event of reconciliation
104 if not is_reconciliation:
105 # It is a new device
106 # Update device
107 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000108 device.serial_number = self.host_and_port # FIXME
Shad Ansari94250fc2018-07-04 06:52:11 +0000109 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400110 device.oper_status = OperStatus.ACTIVATING
111 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000112
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400113 # If logical device does not exist create it
Shad Ansari94250fc2018-07-04 06:52:11 +0000114 if not device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400115
116 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
117
118 # Create logical OF device
119 ld = LogicalDevice(
120 root_device_id=self.device_id,
121 switch_features=ofp_switch_features(
122 n_buffers=256, # TODO fake for now
123 n_tables=2, # TODO ditto
124 capabilities=( # TODO and ditto
125 OFPC_FLOW_STATS
126 | OFPC_TABLE_STATS
127 | OFPC_PORT_STATS
128 | OFPC_GROUP_STATS
129 )
Jonathan Hart05845412018-07-19 09:55:43 -0700130 ),
131 desc=ofp_desc(
132 serial_num=device.serial_number
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400133 )
134 )
135 ld_init = self.adapter_agent.create_logical_device(ld,
136 dpid=dpid)
137 self.logical_device_id = ld_init.id
138 else:
139 # logical device already exists
140 self.logical_device_id = device.parent_id
141 if is_reconciliation:
142 self.adapter_agent.reconcile_logical_device(
143 self.logical_device_id)
144
Shad Ansari94250fc2018-07-04 06:52:11 +0000145 # Initialize the OLT state machine
146 self.machine = Machine(model=self, states=OpenoltDevice.states,
147 transitions=OpenoltDevice.transitions,
148 send_event=True, initial='state_null')
149 self.go_state_init()
150
151 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000152 # Initialize gRPC
153 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000154 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400155
jasonhuang5f3e63b2018-07-27 01:32:48 +0800156 # Catch RuntimeError exception
157 try:
158 # Start indications thread
159 self.indications_thread_handle = threading.Thread(
160 target=self.indications_thread)
161 # Old getter/setter API for daemon; use it directly as a
162 # property instead. The Jinkins error will happon on the reason of
163 # Exception in thread Thread-1 (most likely raised # during
164 # interpreter shutdown)
165 self.indications_thread_handle.setDaemon(True)
166 self.indications_thread_handle.start()
167 except Exception as e:
168 self.log.exception('do_state_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000169 '''
170 # FIXME - Move to oper_up state without connecting to OLT?
171 if is_reconciliation:
172 # Put state machine in state up
173 reactor.callFromThread(self.go_state_up, reconciliation=True)
174 '''
175
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400176 self.log.info('openolt-device-created', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000177
178 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400179 self.log.debug("do_state_connected")
180
Shad Ansari94250fc2018-07-04 06:52:11 +0000181 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000182
183 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400184
185 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
186 self.log.info('Device connected', device_info=device_info)
187
188 device.vendor = device_info.vendor
189 device.model = device_info.model
190 device.hardware_version = device_info.hardware_version
191 device.firmware_version = device_info.firmware_version
192
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400193 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
194 self.logical_device_id)
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000195 self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent,
196 self.device_id,
mzadig7cda5ff2018-07-10 16:37:28 -0400197 self.logical_device_id)
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400198 self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000199 self.bw_mgr = OpenOltBW(self.log, self.proxy)
Shad Ansari2825d012018-02-22 23:57:46 +0000200
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400201 # TODO: use content of device_info for Resource manager (VOL-948)
202
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400203 # TODO: check for uptime and reboot if too long (VOL-1192)
204
205
206 device.connect_status = ConnectStatus.REACHABLE
207 self.adapter_agent.update_device(device)
208
Shad Ansari94250fc2018-07-04 06:52:11 +0000209 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400210 self.log.debug("do_state_up")
211
Shad Ansari94250fc2018-07-04 06:52:11 +0000212 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000213
Shad Ansari94250fc2018-07-04 06:52:11 +0000214 # Update phys OF device
215 device.parent_id = self.logical_device_id
216 device.oper_status = OperStatus.ACTIVE
217 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400218
Shad Ansari94250fc2018-07-04 06:52:11 +0000219 def do_state_down(self, event):
220 self.log.debug("do_state_down")
221 oper_state = OperStatus.UNKNOWN
222 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400223
Shad Ansari94250fc2018-07-04 06:52:11 +0000224 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400225
Shad Ansari94250fc2018-07-04 06:52:11 +0000226 # Children ports
227 child_devices = self.adapter_agent.get_child_devices(self.device_id)
228 for onu_device in child_devices:
229 uni_no = platform.mk_uni_port_num(
230 onu_device.proxy_address.channel_id,
231 onu_device.proxy_address.onu_id)
232 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
233 serial_number=onu_device.serial_number)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000234
Nicolas Palpacuer08cc8182018-08-22 10:22:19 -0400235
236 if onu_device.adapter == 'brcm_openomci_onu':
237 self.log.debug('using-brcm_openomci_onu, update_interface '
238 'down')
239 onu_adapter_agent = \
240 registry('adapter_loader').get_agent(onu_device.adapter)
241 onu_adapter_agent.update_interface(onu_device,
242 {'oper_state' :'down'})
243
Shad Ansari94250fc2018-07-04 06:52:11 +0000244 self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
245 # Children devices
246 self.adapter_agent.update_child_devices_state(
247 self.device_id, oper_status=oper_state,
248 connect_status=connect_state)
249 # Device Ports
250 device_ports = self.adapter_agent.get_ports(self.device_id,
251 Port.ETHERNET_NNI)
252 logical_ports_ids = [port.label for port in device_ports]
253 device_ports += self.adapter_agent.get_ports(self.device_id,
254 Port.PON_OLT)
255
256 for port in device_ports:
257 port.oper_status = oper_state
258 self.adapter_agent.add_port(self.device_id, port)
259
260 # Device logical port
261 for logical_port_id in logical_ports_ids:
262 logical_port = self.adapter_agent.get_logical_port(
263 self.logical_device_id, logical_port_id)
264 logical_port.ofp_port.state = OFPPS_LINK_DOWN
265 self.adapter_agent.update_logical_port(self.logical_device_id,
266 logical_port)
267
268 # Device
269 device = self.adapter_agent.get_device(self.device_id)
270 device.oper_status = oper_state
271 device.connect_status = connect_state
272
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400273 reactor.callLater(2, self.adapter_agent.update_device, device)
274
275 # def post_up(self, event):
276 # self.log.debug('post-up')
277 # self.flow_mgr.reseed_flows()
278
279 def post_down(self, event):
280 self.log.debug('post_down')
281 self.flow_mgr.reset_flows()
282
Shad Ansari94250fc2018-07-04 06:52:11 +0000283
284 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400285 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000286 self.log.debug('connecting to olt', device_id=self.device_id)
287 self.channel_ready_future.result() # blocking call
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400288 self.log.info('connected to olt', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000289 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000290
Shad Ansari15928d12018-04-17 02:42:13 +0000291 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000292
Shad Ansari94250fc2018-07-04 06:52:11 +0000293 while True:
nick47b74372018-05-25 18:22:49 -0400294 try:
295 # get the next indication from olt
296 ind = next(self.indications)
297 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000298 self.log.warn('gRPC connection lost', error=e)
299 reactor.callFromThread(self.go_state_down)
300 reactor.callFromThread(self.go_state_init)
301 break
nick47b74372018-05-25 18:22:49 -0400302 else:
303 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000304
nick47b74372018-05-25 18:22:49 -0400305 # indication handlers run in the main event loop
306 if ind.HasField('olt_ind'):
307 reactor.callFromThread(self.olt_indication, ind.olt_ind)
308 elif ind.HasField('intf_ind'):
309 reactor.callFromThread(self.intf_indication, ind.intf_ind)
310 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000311 reactor.callFromThread(self.intf_oper_indication,
312 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400313 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000314 reactor.callFromThread(self.onu_discovery_indication,
315 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400316 elif ind.HasField('onu_ind'):
317 reactor.callFromThread(self.onu_indication, ind.onu_ind)
318 elif ind.HasField('omci_ind'):
319 reactor.callFromThread(self.omci_indication, ind.omci_ind)
320 elif ind.HasField('pkt_ind'):
321 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400322 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400323 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000324 self.stats_mgr.port_statistics_indication,
325 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400326 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400327 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000328 self.stats_mgr.flow_statistics_indication,
329 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000330 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400331 reactor.callFromThread(self.alarm_mgr.process_alarms,
332 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400333 else:
334 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400335
Shad Ansari2825d012018-02-22 23:57:46 +0000336 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000337 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000338 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000339 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000340 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000341
342 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400343 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000344 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000345
346 if intf_indication.oper_state == "up":
347 oper_status = OperStatus.ACTIVE
348 else:
349 oper_status = OperStatus.DISCOVERED
350
nick47b74372018-05-25 18:22:49 -0400351 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000352 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
353
354 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000355 self.log.debug("Received interface oper state change indication",
356 intf_id=intf_oper_indication.intf_id,
357 type=intf_oper_indication.type,
358 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000359
360 if intf_oper_indication.oper_state == "up":
361 oper_state = OperStatus.ACTIVE
362 else:
363 oper_state = OperStatus.DISCOVERED
364
365 if intf_oper_indication.type == "nni":
366
Shad Ansari0346f0d2018-04-26 06:54:09 +0000367 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000368 if intf_oper_indication.intf_id != 0:
369 return
370
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400371 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000372 port_no, label = self.add_port(intf_oper_indication.intf_id,
373 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400374 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000375 self.add_logical_port(port_no, intf_oper_indication.intf_id,
376 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000377
378 elif intf_oper_indication.type == "pon":
379 # FIXME - handle PON oper state change
380 pass
381
382 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000383 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000384 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000385
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400386 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000387
Shad Ansarif9d2d102018-06-13 02:15:26 +0000388 self.log.debug("onu discovery indication", intf_id=intf_id,
389 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400390
mzadig384783a2018-08-09 08:52:40 -0400391 # Post ONU Discover alarm 20180809_0805
392 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400393 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
394 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400395 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400396 self.log.exception("onu-discovery-alarm-error",
397 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400398 # continue for now.
399
Shad Ansarif9d2d102018-06-13 02:15:26 +0000400 onu_device = self.adapter_agent.get_child_device(
401 self.device_id,
402 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400403
404 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000405 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000406 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000407 self.add_onu_device(
408 intf_id,
409 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
410 onu_id, serial_number)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400411 self.activate_onu(intf_id, onu_id, serial_number,
412 serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400413 except Exception as e:
414 self.log.exception('onu-activation-failed', e=e)
415
Shad Ansari2825d012018-02-22 23:57:46 +0000416 else:
nick47b74372018-05-25 18:22:49 -0400417 if onu_device.connect_status != ConnectStatus.REACHABLE:
418 onu_device.connect_status = ConnectStatus.REACHABLE
419 self.adapter_agent.update_device(onu_device)
420
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400421 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000422 if onu_device.oper_status == OperStatus.DISCOVERED \
423 or onu_device.oper_status == OperStatus.ACTIVATING:
424 self.log.debug("ignore onu discovery indication, \
425 the onu has been discovered and should be \
426 activating shorlty", intf_id=intf_id,
427 onu_id=onu_id, state=onu_device.oper_status)
428 elif onu_device.oper_status == OperStatus.ACTIVE:
429 self.log.warn("onu discovery indication whereas onu is \
430 supposed to be active",
431 intf_id=intf_id, onu_id=onu_id,
432 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400433 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000434 self.log.info("onu in unknown state, recovering from olt \
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400435 reboot probably, activate onu", intf_id=intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000436 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400437
438 onu_device.oper_status = OperStatus.DISCOVERED
439 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400440 try:
441 self.activate_onu(intf_id, onu_id, serial_number,
442 serial_number_str)
443 except Exception as e:
444 self.log.error('onu-activation-error',
445 serial_number=serial_number_str, error=e)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400446 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000447 self.log.warn('unexpected state', onu_id=onu_id,
448 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000449
Shad Ansari2825d012018-02-22 23:57:46 +0000450 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000451 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000452 onu_id=onu_indication.onu_id,
453 serial_number=onu_indication.serial_number,
454 oper_state=onu_indication.oper_state,
455 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400456 try:
457 serial_number_str = self.stringify_serial_number(
458 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000459 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400460 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000461
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400462 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400463 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000464 self.device_id,
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400465 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000466 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400467 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000468 self.device_id,
469 parent_port_no=platform.intf_id_to_port_no(
470 onu_indication.intf_id, Port.PON_OLT),
471 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400472
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400473 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000474 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000475 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000476 return
477
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400478
nick47b74372018-05-25 18:22:49 -0400479
Shad Ansarif9d2d102018-06-13 02:15:26 +0000480 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
481 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400482 self.log.warn('ONU-is-on-a-different-intf-id-now',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000483 previous_intf_id=platform.intf_id_from_pon_port_no(
484 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400485 current_intf_id=onu_indication.intf_id)
486 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000487
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400488 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
489 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400490 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
491 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000492 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400493 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000494
Shad Ansarif9d2d102018-06-13 02:15:26 +0000495 uni_no = platform.mk_uni_port_num(onu_indication.intf_id,
496 onu_indication.onu_id)
497 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
498 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000499
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400500 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000501
Shad Ansarif9d2d102018-06-13 02:15:26 +0000502 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400503 if onu_indication.admin_state == 'down':
504 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000505 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
506 oper_state=onu_indication.oper_state)
507 # Forcing the oper state change code to execute
508 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400509
Shad Ansarif9d2d102018-06-13 02:15:26 +0000510 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400511
512 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400513 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400514
515 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000516 self.log.warn('Invalid-or-not-implemented-admin-state',
517 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400518
519 self.log.debug('admin-state-dealt-with')
520
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400521 onu_adapter_agent = \
522 registry('adapter_loader').get_agent(onu_device.adapter)
523 if onu_adapter_agent is None:
524 self.log.error('onu_adapter_agent-could-not-be-retrieved',
525 onu_device=onu_device)
526 return
527
Shad Ansarif9d2d102018-06-13 02:15:26 +0000528 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400529 if onu_indication.oper_state == 'down':
Nicolas Palpacuer4ea3a652018-08-22 10:33:17 -0400530
531 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
532 onu_device.connect_status = ConnectStatus.UNREACHABLE
533 self.adapter_agent.update_device(onu_device)
534
Shad Ansarif9d2d102018-06-13 02:15:26 +0000535 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400536 self.log.debug('onu-oper-state-is-down')
537
538 if onu_device.oper_status != OperStatus.DISCOVERED:
539 onu_device.oper_status = OperStatus.DISCOVERED
540 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000541 # Set port oper state to Discovered
542 self.onu_ports_down(onu_device, uni_no, uni_name,
543 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400544
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400545 if onu_device.adapter == 'brcm_openomci_onu':
546 self.log.debug('using-brcm_openomci_onu')
Nicolas Palpacuer08cc8182018-08-22 10:22:19 -0400547 onu_adapter_agent.update_interface(onu_device,
548 {'oper_state':'down'})
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400549
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400550 elif onu_indication.oper_state == 'up':
551
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400552 if onu_device.connect_status != ConnectStatus.REACHABLE:
553 onu_device.connect_status = ConnectStatus.REACHABLE
554 self.adapter_agent.update_device(onu_device)
555
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400556 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000557 self.log.debug("ignore onu indication",
558 intf_id=onu_indication.intf_id,
559 onu_id=onu_indication.onu_id,
560 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400561 msg_oper_state=onu_indication.oper_state)
562 return
563
Shad Ansarif9d2d102018-06-13 02:15:26 +0000564 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400565
Shad Ansarif9d2d102018-06-13 02:15:26 +0000566 # Prepare onu configuration
Shad Ansari5df91f62018-07-25 23:59:46 +0000567 # If we are using the old/current broadcom adapter otherwise
568 # use the openomci adapter
Matt Jeannerete6a70332018-07-20 16:11:25 -0400569 if onu_device.adapter == 'broadcom_onu':
570 self.log.debug('using-broadcom_onu')
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400571
Matt Jeannerete6a70332018-07-20 16:11:25 -0400572 # onu initialization, base configuration (bridge setup ...)
573 def onu_initialization():
574
Shad Ansarif9d2d102018-06-13 02:15:26 +0000575 onu_adapter_agent.adapter.devices_handlers[onu_device.id] \
Matt Jeannerete6a70332018-07-20 16:11:25 -0400576 .message_exchange(cvid=DEFAULT_MGMT_VLAN)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400577 self.log.debug('broadcom-message-exchange-started')
578
Matt Jeannerete6a70332018-07-20 16:11:25 -0400579 # tcont creation (onu)
580 tcont = TcontsConfigData()
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400581 tcont.alloc_id = platform.mk_alloc_id(
582 onu_indication.intf_id, onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400583
Matt Jeannerete6a70332018-07-20 16:11:25 -0400584 # gem port creation
585 gem_port = GemportsConfigData()
Shad Ansari5df91f62018-07-25 23:59:46 +0000586 gem_port.gemport_id = platform.mk_gemport_id(
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400587 onu_indication.intf_id,
Shad Ansari5df91f62018-07-25 23:59:46 +0000588 onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400589
Matt Jeannerete6a70332018-07-20 16:11:25 -0400590 # ports creation/update
591 def port_config():
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400592
Matt Jeannerete6a70332018-07-20 16:11:25 -0400593 # "v_enet" creation (olt)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400594
Matt Jeannerete6a70332018-07-20 16:11:25 -0400595 # add_port update port when it exists
596 self.adapter_agent.add_port(
597 self.device_id,
598 Port(
599 port_no=uni_no,
600 label=uni_name,
601 type=Port.ETHERNET_UNI,
602 admin_state=AdminState.ENABLED,
603 oper_status=OperStatus.ACTIVE))
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400604
Matt Jeannerete6a70332018-07-20 16:11:25 -0400605 # v_enet creation (onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400606
Matt Jeannerete6a70332018-07-20 16:11:25 -0400607 venet = VEnetConfig(name=uni_name)
608 venet.interface.name = uni_name
609 onu_adapter_agent.create_interface(onu_device, venet)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400610
Matt Jeannerete6a70332018-07-20 16:11:25 -0400611 # ONU device status update in the datastore
612 def onu_update_oper_status():
613 onu_device.oper_status = OperStatus.ACTIVE
614 onu_device.connect_status = ConnectStatus.REACHABLE
615 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400616
Matt Jeannerete6a70332018-07-20 16:11:25 -0400617 # FIXME : the asynchronicity has to be taken care of properly
618 onu_initialization()
619 reactor.callLater(10, onu_adapter_agent.create_tcont,
620 device=onu_device, tcont_data=tcont,
621 traffic_descriptor_data=None)
Shad Ansari5df91f62018-07-25 23:59:46 +0000622 reactor.callLater(11, onu_adapter_agent.create_gemport,
623 onu_device, gem_port)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400624 reactor.callLater(12, port_config)
625 reactor.callLater(12, onu_update_oper_status)
626
627 elif onu_device.adapter == 'brcm_openomci_onu':
628 self.log.debug('using-brcm_openomci_onu')
629
630 # tcont creation (onu)
631 tcont = TcontsConfigData()
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400632 tcont.alloc_id = platform.mk_alloc_id(
633 onu_indication.intf_id, onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400634
635 # gem port creation
636 gem_port = GemportsConfigData()
Shad Ansari5df91f62018-07-25 23:59:46 +0000637 gem_port.gemport_id = platform.mk_gemport_id(
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400638 onu_indication.intf_id,
Shad Ansari5df91f62018-07-25 23:59:46 +0000639 onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400640 gem_port.tcont_ref = str(tcont.alloc_id)
641
Shad Ansari5df91f62018-07-25 23:59:46 +0000642 self.log.info('inject-tcont-gem-data-onu-handler',
643 onu_indication=onu_indication, tcont=tcont,
644 gem_port=gem_port)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400645
Shad Ansari5df91f62018-07-25 23:59:46 +0000646 onu_adapter_agent.create_tcont(onu_device, tcont,
647 traffic_descriptor_data=None)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400648 onu_adapter_agent.create_gemport(onu_device, gem_port)
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400649 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400650
651 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400652 self.log.error('unsupported-openolt-onu-adapter')
Matt Jeannerete6a70332018-07-20 16:11:25 -0400653
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400654 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000655 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
656 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000657
nick47b74372018-05-25 18:22:49 -0400658 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
659 # Set port oper state to Discovered
660 # add port will update port if it exists
661 self.adapter_agent.add_port(
662 self.device_id,
663 Port(
664 port_no=uni_no,
665 label=uni_name,
666 type=Port.ETHERNET_UNI,
667 admin_state=onu_device.admin_state,
668 oper_status=oper_state))
669
670 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400671 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
672 onu_port_id = None
673 for onu_port in onu_ports:
674 if onu_port.port_no == uni_no:
675 onu_port_id = onu_port.label
676 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000677 self.log.error('matching-onu-port-label-not-found',
678 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400679 onu_ports=onu_ports)
680 return
681 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000682 onu_logical_port = self.adapter_agent.get_logical_port(
683 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400684 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000685 self.adapter_agent.update_logical_port(
686 logical_device_id=self.logical_device_id,
687 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400688 self.log.debug('cascading-oper-state-to-port-and-logical-port')
689 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000690 self.log.error('matching-onu-port-label-invalid',
691 onu_id=onu_device.id, olt_id=self.device_id,
692 onu_ports=onu_ports, onu_port_id=onu_port_id,
693 error=e)
nick47b74372018-05-25 18:22:49 -0400694
Shad Ansari2825d012018-02-22 23:57:46 +0000695 def omci_indication(self, omci_indication):
696
697 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000698 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000699
Shad Ansarif9d2d102018-06-13 02:15:26 +0000700 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400701 self.device_id, onu_id=omci_indication.onu_id,
702 parent_port_no=platform.intf_id_to_port_no(
703 omci_indication.intf_id, Port.PON_OLT),)
Shad Ansari0efa6512018-04-28 06:42:54 +0000704
705 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000706 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000707
Shad Ansari42db7342018-04-25 21:39:46 +0000708 def packet_indication(self, pkt_indication):
709
710 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000711 gemport_id=pkt_indication.gemport_id,
712 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000713
Shad Ansari22920932018-05-17 00:33:34 +0000714 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000715 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
716 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000717
718 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000719 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000720 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000721 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
722
723 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000724 pkt = Ether(msg)
725 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000726 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400727
728 # Find port type
729 egress_port_type = self.port_type(egress_port)
730
731 if egress_port_type == Port.ETHERNET_UNI:
732
733 if pkt.haslayer(Dot1Q):
734 outer_shim = pkt.getlayer(Dot1Q)
735 if isinstance(outer_shim.payload, Dot1Q):
736 # If double tag, remove the outer tag
737 payload = (
738 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
739 outer_shim.payload
740 )
741 else:
742 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000743 else:
744 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400745
746 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
747
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400748 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000749 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400750 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000751 onu_id=platform.onu_id_from_port_num(egress_port),
752 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400753
Shad Ansarif9d2d102018-06-13 02:15:26 +0000754 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400755 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000756 onu_id=platform.onu_id_from_port_num(egress_port),
757 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400758
759 self.stub.OnuPacketOut(onu_pkt)
760
761 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400762 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000763 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400764
765 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
766
Shad Ansarif9d2d102018-06-13 02:15:26 +0000767 uplink_pkt = openolt_pb2.UplinkPacket(
768 intf_id=platform.intf_id_from_nni_port_num(egress_port),
769 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400770
771 self.stub.UplinkPacketOut(uplink_pkt)
772
Shad Ansari0346f0d2018-04-26 06:54:09 +0000773 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000774 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
775 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400776 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000777
Shad Ansari2825d012018-02-22 23:57:46 +0000778 def send_proxied_message(self, proxy_address, msg):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400779 onu_device = self.adapter_agent.get_child_device(self.device_id,
780 onu_id=proxy_address.onu_id,
781 parent_port_no=platform.intf_id_to_port_no(
782 proxy_address.channel_id, Port.PON_OLT))
783 if onu_device.connect_status != ConnectStatus.REACHABLE:
784 self.log.debug('ONU is not reachable, cannot send OMCI',
785 serial_number=onu_device.serial_number,
786 intf_id=onu_device.proxy_address.channel_id,
787 onu_id=onu_device.proxy_address.onu_id)
788 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000789 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
790 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000791 self.stub.OmciMsgOut(omci)
792
793 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000794 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000795 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000796
797 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000798 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000799 channel_id=intf_id, onu_id=onu_id,
800 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000801
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400802 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000803
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400804 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000805
Shad Ansarif9d2d102018-06-13 02:15:26 +0000806 self.adapter_agent.add_onu_device(
807 parent_device_id=self.device_id, parent_port_no=port_no,
808 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
809 root=True, serial_number=serial_number_str,
810 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000811
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000812 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000813 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400814 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000815 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000816 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000817 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400818 if serial_number is not None:
819 return serial_number
820 else:
821 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000822
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400823 def port_type(self, port_no):
824 ports = self.adapter_agent.get_ports(self.device_id)
825 for port in ports:
826 if port.port_no == port_no:
827 return port.type
828 return None
829
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400830 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000831 self.log.info('adding-logical-port', port_no=port_no)
832
833 label = self.port_name(port_no, Port.ETHERNET_NNI)
834
835 cap = OFPPF_1GB_FD | OFPPF_FIBER
836 curr_speed = OFPPF_1GB_FD
837 max_speed = OFPPF_1GB_FD
838
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400839 if oper_state == OperStatus.ACTIVE:
840 of_oper_state = OFPPS_LIVE
841 else:
842 of_oper_state = OFPPS_LINK_DOWN
843
Shad Ansarif9d2d102018-06-13 02:15:26 +0000844 ofp = ofp_port(
845 port_no=port_no,
846 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
847 name=label, config=0, state=of_oper_state, curr=cap,
848 advertised=cap, peer=cap, curr_speed=curr_speed,
849 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000850
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400851 ofp_stats = ofp_port_stats(port_no=port_no)
852
Shad Ansarif9d2d102018-06-13 02:15:26 +0000853 logical_port = LogicalPort(
854 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400855 device_port_no=port_no, root_port=True,
856 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000857
Shad Ansarif9d2d102018-06-13 02:15:26 +0000858 self.adapter_agent.add_logical_port(self.logical_device_id,
859 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000860
861 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000862 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000863
Shad Ansari4a232ca2018-05-05 05:24:17 +0000864 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000865
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400866 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000867 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000868
869 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000870 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000871
Shad Ansari2825d012018-02-22 23:57:46 +0000872 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000873
Shad Ansari2825d012018-02-22 23:57:46 +0000874 return port_no, label
875
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400876 def delete_logical_port(self, child_device_id):
877 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
878 self.logical_device_id))
879 for logical_port in logical_ports:
880 if logical_port.device_id == child_device_id:
881 self.log.debug('delete-logical-port',
882 onu_device_id=child_device_id,
883 logical_port=logical_port)
884 self.adapter_agent.delete_logical_port(
885 self.logical_device_id, logical_port)
886 return
887 def delete_port(self, child_serial_number):
888 ports = self.proxy.get('/devices/{}/ports'.format(
889 self.device_id))
890 for port in ports:
891 if port.label == child_serial_number:
892 self.log.debug('delete-port',
893 onu_serial_number=child_serial_number,
894 port=port)
895 self.adapter_agent.delete_port(self.device_id, port)
896 return
897
898
Shad Ansari2825d012018-02-22 23:57:46 +0000899 def new_onu_id(self, intf_id):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400900 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400901 pon_onu_ids = [onu_device.proxy_address.onu_id for onu_device in
902 onu_devices if
903 onu_device.proxy_address.channel_id == intf_id]
904 for i in range(1, platform.MAX_ONUS_PER_PON):
905 if i not in pon_onu_ids:
906 return i
907
908 self.log.error('All available onu_ids taken on this pon',
909 intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
910 return None
Shad Ansari2825d012018-02-22 23:57:46 +0000911
Shad Ansari2825d012018-02-22 23:57:46 +0000912
Shad Ansari2825d012018-02-22 23:57:46 +0000913 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400914 self.log.debug('No updates here now, all is done in logical flows '
915 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000916
Shad Ansari2825d012018-02-22 23:57:46 +0000917
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400918 def update_logical_flows(self, flows_to_add, flows_to_remove,
919 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400920 if not self.is_state_up():
921 self.log.info('The OLT is not up, we cannot update flows',
922 flows_to_add=[f.id for f in flows_to_add],
923 flows_to_remove=[f.id for f in flows_to_remove])
924 return
925
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400926 try:
927 self.flow_mgr.update_children_flows(device_rules_map)
928 except Exception as e:
929 self.log.error('Error updating children flows', error=e)
Shad Ansari2825d012018-02-22 23:57:46 +0000930
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400931 self.log.debug('logical flows update', flows_to_add=flows_to_add,
932 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000933
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400934 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400935
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400936 try:
937 self.flow_mgr.add_flow(flow)
938 except grpc.RpcError as grpc_e:
939 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
940 self.log.warn('flow already exists', e=grpc_e,
941 flow=flow)
942 else:
943 self.log.error('failed to add flow', flow=flow,
944 grpc_error=grpc_e)
945 except Exception as e:
946 self.log.error('failed to add flow', flow=flow, e=e)
947
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400948
949 for flow in flows_to_remove:
950
951 try:
952 self.flow_mgr.remove_flow(flow)
953 except Exception as e:
954 self.log.error('failed to remove flow', flow=flow, e=e)
955
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400956 self.flow_mgr.repush_all_different_flows()
957
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400958 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000959 def ip_hex(self, ip):
960 octets = ip.split(".")
961 hex_ip = []
962 for octet in octets:
963 octet_hex = hex(int(octet))
964 octet_hex = octet_hex.split('0x')[1]
965 octet_hex = octet_hex.rjust(2, '0')
966 hex_ip.append(octet_hex)
967 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400968
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400969 def stringify_vendor_specific(self, vendor_specific):
970 return ''.join(str(i) for i in [
971 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
972 hex(ord(vendor_specific[0]) & 0x0f)[2:],
973 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
974 hex(ord(vendor_specific[1]) & 0x0f)[2:],
975 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
976 hex(ord(vendor_specific[2]) & 0x0f)[2:],
977 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
978 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
979
980
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400981 def stringify_serial_number(self, serial_number):
982 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000983 self.stringify_vendor_specific(
984 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400985
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400986 def destringify_serial_number(self, serial_number_str):
987 serial_number = openolt_pb2.SerialNumber(
988 vendor_id=serial_number_str[:4].encode('utf-8'),
989 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
990 return serial_number
991
Jonathan Davis0f917a22018-05-30 14:39:45 -0400992 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400993 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000994 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400995
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400996 try:
997 # Send grpc call
998 self.stub.DisableOlt(openolt_pb2.Empty())
999 # The resulting indication will bring the OLT down
1000 # self.go_state_down()
1001 self.log.info('openolt device disabled')
1002 except Exception as e:
1003 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -04001004
Jonathan Davis0f917a22018-05-30 14:39:45 -04001005
1006 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -04001007 self.log.info('deleting-olt', device_id=self.device_id,
1008 logical_device_id=self.logical_device_id)
1009
1010 try:
1011 # Rebooting to reset the state
1012 self.reboot()
1013 # Removing logical device
1014 self.proxy.remove('/logical_devices/{}'.
1015 format(self.logical_device_id))
1016 except Exception as e:
1017 self.log.error('Failure to delete openolt device', error=e)
1018 raise e
1019 else:
1020 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -04001021
Jonathan Davis0f917a22018-05-30 14:39:45 -04001022
1023 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001024 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -04001025
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001026 try:
1027 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -04001028
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001029 self.log.info('enabling-all-ports', device_id=self.device_id)
1030 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001031 except Exception as e:
1032 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -04001033 else:
1034 self.log.info('openolt device reenabled')
1035
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001036 def activate_onu(self, intf_id, onu_id, serial_number,
1037 serial_number_str):
1038 pir = self.bw_mgr.pir(serial_number_str)
1039 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1040 serial_number_str=serial_number_str,
1041 serial_number=serial_number, pir=pir)
1042 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1043 serial_number=serial_number, pir=pir)
1044 self.stub.ActivateOnu(onu)
1045 self.log.info('onu-activated', serial_number=serial_number_str)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001046
Jonathan Davisb45bb372018-07-19 15:05:15 -04001047
Jonathan Davisb45bb372018-07-19 15:05:15 -04001048 def delete_child_device(self, child_device):
1049 self.log.debug('sending-deactivate-onu',
1050 olt_device_id=self.device_id,
1051 onu_device=child_device,
1052 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001053 try:
1054 self.adapter_agent.delete_child_device(self.device_id,
1055 child_device.id, child_device)
1056 except Exception as e:
1057 self.log.error('adapter_agent error', error=e)
1058 try:
1059 self.delete_logical_port(child_device.id)
1060 except Exception as e:
1061 self.log.error('logical_port delete error', error=e)
1062 try:
1063 self.delete_port(child_device.serial_number)
1064 except Exception as e:
1065 self.log.error('port delete error', error=e)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001066 serial_number = self.destringify_serial_number(child_device.serial_number)
Jonathan Davisb45bb372018-07-19 15:05:15 -04001067 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1068 onu_id=child_device.proxy_address.onu_id,
1069 serial_number=serial_number)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001070 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001071
1072 def reboot(self):
1073 self.log.debug('rebooting openolt device', device_id = self.device_id)
1074 try:
1075 self.stub.Reboot(openolt_pb2.Empty())
1076 except Exception as e:
1077 self.log.error('something went wrong with the reboot', error=e)
1078 else:
1079 self.log.info('device rebooted')
1080
Nicolas Palpacuer30027f42018-09-06 15:55:54 -04001081 def trigger_statistics_collection(self):
1082 try:
1083 self.stub.CollectStatistics(openolt_pb2.Empty())
1084 except Exception as e:
1085 self.log.error('Error while triggering statistics collection',
1086 error=e)
1087 else:
1088 self.log.info('statistics requested')
1089