blob: 025590135dd37cce69fdce42fdedd952f700615f [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
Shad Ansari2825d012018-02-22 23:57:46 +000019
Shad Ansari94250fc2018-07-04 06:52:11 +000020import structlog
Shad Ansari15928d12018-04-17 02:42:13 +000021from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000022from scapy.layers.l2 import Ether, Dot1Q
Shad Ansari3cd9bf22018-07-25 19:29:39 +000023from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000024
Shad Ansari2825d012018-02-22 23:57:46 +000025from voltha.protos.device_pb2 import Port, Device
26from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
27from voltha.protos.logical_device_pb2 import LogicalDevice
Shad Ansarif9d2d102018-06-13 02:15:26 +000028from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
29 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, \
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -040030 OFPC_TABLE_STATS, OFPC_FLOW_STATS, ofp_switch_features, ofp_port, \
Jonathan Hart05845412018-07-19 09:55:43 -070031 ofp_port_stats, ofp_desc
Shad Ansarif9d2d102018-06-13 02:15:26 +000032from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansari2825d012018-02-22 23:57:46 +000033from voltha.core.logical_device_agent import mac_str_to_tuple
34from voltha.registry import registry
35from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040036from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
37from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
Shad Ansari2825d012018-02-22 23:57:46 +000038
Nicolas Palpacuere761c902018-07-05 16:30:52 -040039from voltha.adapters.openolt.openolt_statistics import OpenOltStatisticsMgr
Shad Ansari94250fc2018-07-04 06:52:11 +000040import voltha.adapters.openolt.openolt_platform as platform
Shad Ansari0e7ad962018-09-28 01:42:26 +000041from voltha.adapters.openolt.openolt_flow_mgr import OpenOltFlowMgr
Shad Ansari94250fc2018-07-04 06:52:11 +000042from voltha.adapters.openolt.openolt_alarms import OpenOltAlarmMgr
Shad Ansari3cd9bf22018-07-25 19:29:39 +000043from voltha.adapters.openolt.openolt_bw import OpenOltBW
mzadig384783a2018-08-09 08:52:40 -040044from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
Shad Ansarif9d2d102018-06-13 02:15:26 +000045
46
Shad Ansari2825d012018-02-22 23:57:46 +000047class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000048 """
49 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000050
Shad Ansari94250fc2018-07-04 06:52:11 +000051 null ----> init ------> connected -----> up -----> down
52 ^ ^ | ^ | |
53 | | | | | |
54 | +-------------+ +---------+ |
55 | |
56 +-----------------------------------------+
57 """
58 # pylint: disable=too-many-instance-attributes
59 # pylint: disable=R0904
60 states = [
61 'state_null',
62 'state_init',
63 'state_connected',
64 'state_up',
65 'state_down']
66
Shad Ansari22efe832018-05-19 05:37:03 +000067 transitions = [
Shad Ansari0e7ad962018-09-28 01:42:26 +000068 {'trigger': 'go_state_init',
69 'source': ['state_null', 'state_connected', 'state_down'],
70 'dest': 'state_init',
71 'before': 'do_state_init',
72 'after': 'post_init'},
73 {'trigger': 'go_state_connected',
74 'source': 'state_init',
75 'dest': 'state_connected',
76 'before': 'do_state_connected'},
77 {'trigger': 'go_state_up',
78 'source': ['state_connected', 'state_down'],
79 'dest': 'state_up',
80 'before': 'do_state_up'},
81 {'trigger': 'go_state_down',
82 'source': ['state_up'],
83 'dest': 'state_down',
84 'before': 'do_state_down',
85 'after': 'post_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000086
Shad Ansari2825d012018-02-22 23:57:46 +000087 def __init__(self, **kwargs):
88 super(OpenoltDevice, self).__init__()
89
90 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000091 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000092 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040093 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000094 self.device_id = device.id
95 self.host_and_port = device.host_and_port
Girish Gowdru141ced82018-09-17 20:19:14 -070096 self.extra_args = device.extra_args
Shad Ansarif9d2d102018-06-13 02:15:26 +000097 self.log = structlog.get_logger(id=self.device_id,
98 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040099 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +0000100
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400101 # Device already set in the event of reconciliation
102 if not is_reconciliation:
103 # It is a new device
104 # Update device
105 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000106 device.serial_number = self.host_and_port # FIXME
Shad Ansari94250fc2018-07-04 06:52:11 +0000107 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400108 device.oper_status = OperStatus.ACTIVATING
109 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000110
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400111 # If logical device does not exist create it
Shad Ansari94250fc2018-07-04 06:52:11 +0000112 if not device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400113
114 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
115
116 # Create logical OF device
117 ld = LogicalDevice(
118 root_device_id=self.device_id,
119 switch_features=ofp_switch_features(
120 n_buffers=256, # TODO fake for now
121 n_tables=2, # TODO ditto
122 capabilities=( # TODO and ditto
123 OFPC_FLOW_STATS
124 | OFPC_TABLE_STATS
125 | OFPC_PORT_STATS
126 | OFPC_GROUP_STATS
127 )
Jonathan Hart05845412018-07-19 09:55:43 -0700128 ),
129 desc=ofp_desc(
130 serial_num=device.serial_number
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400131 )
132 )
133 ld_init = self.adapter_agent.create_logical_device(ld,
134 dpid=dpid)
135 self.logical_device_id = ld_init.id
136 else:
137 # logical device already exists
138 self.logical_device_id = device.parent_id
139 if is_reconciliation:
140 self.adapter_agent.reconcile_logical_device(
141 self.logical_device_id)
142
Shad Ansari94250fc2018-07-04 06:52:11 +0000143 # Initialize the OLT state machine
144 self.machine = Machine(model=self, states=OpenoltDevice.states,
145 transitions=OpenoltDevice.transitions,
146 send_event=True, initial='state_null')
147 self.go_state_init()
148
149 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000150 # Initialize gRPC
151 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000152 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400153
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400154 self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent,
155 self.device_id,
156 self.logical_device_id)
157 self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
158 self.bw_mgr = OpenOltBW(self.log, self.proxy)
159
160 self.log.info('openolt-device-created', device_id=self.device_id)
161
162 def post_init(self, event):
163 self.log.debug('post_init')
164
165 # We have reached init state, starting the indications thread
166
jasonhuang5f3e63b2018-07-27 01:32:48 +0800167 # Catch RuntimeError exception
168 try:
169 # Start indications thread
170 self.indications_thread_handle = threading.Thread(
171 target=self.indications_thread)
Shad Ansarif9521ad2018-09-08 10:46:43 +0000172 # Old getter/setter API for daemon; use it directly as a
jasonhuang5f3e63b2018-07-27 01:32:48 +0800173 # property instead. The Jinkins error will happon on the reason of
Shad Ansarif9521ad2018-09-08 10:46:43 +0000174 # Exception in thread Thread-1 (most likely raised # during
jasonhuang5f3e63b2018-07-27 01:32:48 +0800175 # interpreter shutdown)
176 self.indications_thread_handle.setDaemon(True)
177 self.indications_thread_handle.start()
Shad Ansarif9521ad2018-09-08 10:46:43 +0000178 except Exception as e:
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400179 self.log.exception('post_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000180
181 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400182 self.log.debug("do_state_connected")
183
Shad Ansari94250fc2018-07-04 06:52:11 +0000184 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000185
186 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400187
188 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
189 self.log.info('Device connected', device_info=device_info)
190
191 device.vendor = device_info.vendor
192 device.model = device_info.model
193 device.hardware_version = device_info.hardware_version
194 device.firmware_version = device_info.firmware_version
195
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400196 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700197 self.logical_device_id)
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400198
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400199 # TODO: check for uptime and reboot if too long (VOL-1192)
200
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400201 device.connect_status = ConnectStatus.REACHABLE
202 self.adapter_agent.update_device(device)
203
Shad Ansari94250fc2018-07-04 06:52:11 +0000204 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400205 self.log.debug("do_state_up")
206
Shad Ansari94250fc2018-07-04 06:52:11 +0000207 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000208
Shad Ansari94250fc2018-07-04 06:52:11 +0000209 # Update phys OF device
210 device.parent_id = self.logical_device_id
211 device.oper_status = OperStatus.ACTIVE
212 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400213
Shad Ansari94250fc2018-07-04 06:52:11 +0000214 def do_state_down(self, event):
215 self.log.debug("do_state_down")
216 oper_state = OperStatus.UNKNOWN
217 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400218
Shad Ansari94250fc2018-07-04 06:52:11 +0000219 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400220
Shad Ansari94250fc2018-07-04 06:52:11 +0000221 # Children ports
222 child_devices = self.adapter_agent.get_child_devices(self.device_id)
223 for onu_device in child_devices:
224 uni_no = platform.mk_uni_port_num(
225 onu_device.proxy_address.channel_id,
226 onu_device.proxy_address.onu_id)
227 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
228 serial_number=onu_device.serial_number)
Shad Ansari0e7ad962018-09-28 01:42:26 +0000229 onu_adapter_agent = \
230 registry('adapter_loader').get_agent(onu_device.adapter)
231 onu_adapter_agent.update_interface(onu_device,
232 {'oper_state': 'down'})
Shad Ansari94250fc2018-07-04 06:52:11 +0000233 self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
234 # Children devices
235 self.adapter_agent.update_child_devices_state(
236 self.device_id, oper_status=oper_state,
237 connect_status=connect_state)
238 # Device Ports
239 device_ports = self.adapter_agent.get_ports(self.device_id,
240 Port.ETHERNET_NNI)
241 logical_ports_ids = [port.label for port in device_ports]
242 device_ports += self.adapter_agent.get_ports(self.device_id,
243 Port.PON_OLT)
244
245 for port in device_ports:
246 port.oper_status = oper_state
247 self.adapter_agent.add_port(self.device_id, port)
248
249 # Device logical port
250 for logical_port_id in logical_ports_ids:
251 logical_port = self.adapter_agent.get_logical_port(
252 self.logical_device_id, logical_port_id)
253 logical_port.ofp_port.state = OFPPS_LINK_DOWN
254 self.adapter_agent.update_logical_port(self.logical_device_id,
255 logical_port)
256
257 # Device
258 device = self.adapter_agent.get_device(self.device_id)
259 device.oper_status = oper_state
260 device.connect_status = connect_state
261
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400262 reactor.callLater(2, self.adapter_agent.update_device, device)
263
264 # def post_up(self, event):
265 # self.log.debug('post-up')
266 # self.flow_mgr.reseed_flows()
267
268 def post_down(self, event):
269 self.log.debug('post_down')
270 self.flow_mgr.reset_flows()
271
Shad Ansari94250fc2018-07-04 06:52:11 +0000272 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400273 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000274 self.log.debug('connecting to olt', device_id=self.device_id)
275 self.channel_ready_future.result() # blocking call
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400276 self.log.info('connected to olt', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000277 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000278
Shad Ansari15928d12018-04-17 02:42:13 +0000279 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000280
Shad Ansari94250fc2018-07-04 06:52:11 +0000281 while True:
nick47b74372018-05-25 18:22:49 -0400282 try:
283 # get the next indication from olt
284 ind = next(self.indications)
285 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000286 self.log.warn('gRPC connection lost', error=e)
287 reactor.callFromThread(self.go_state_down)
288 reactor.callFromThread(self.go_state_init)
289 break
nick47b74372018-05-25 18:22:49 -0400290 else:
291 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000292
nick47b74372018-05-25 18:22:49 -0400293 # indication handlers run in the main event loop
294 if ind.HasField('olt_ind'):
295 reactor.callFromThread(self.olt_indication, ind.olt_ind)
296 elif ind.HasField('intf_ind'):
297 reactor.callFromThread(self.intf_indication, ind.intf_ind)
298 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000299 reactor.callFromThread(self.intf_oper_indication,
300 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400301 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000302 reactor.callFromThread(self.onu_discovery_indication,
303 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400304 elif ind.HasField('onu_ind'):
305 reactor.callFromThread(self.onu_indication, ind.onu_ind)
306 elif ind.HasField('omci_ind'):
307 reactor.callFromThread(self.omci_indication, ind.omci_ind)
308 elif ind.HasField('pkt_ind'):
309 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400310 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400311 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000312 self.stats_mgr.port_statistics_indication,
313 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400314 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400315 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000316 self.stats_mgr.flow_statistics_indication,
317 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000318 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400319 reactor.callFromThread(self.alarm_mgr.process_alarms,
320 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400321 else:
322 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400323
Shad Ansari2825d012018-02-22 23:57:46 +0000324 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000325 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000326 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000327 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000328 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000329
330 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400331 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000332 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000333
334 if intf_indication.oper_state == "up":
335 oper_status = OperStatus.ACTIVE
336 else:
337 oper_status = OperStatus.DISCOVERED
338
nick47b74372018-05-25 18:22:49 -0400339 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000340 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
341
342 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000343 self.log.debug("Received interface oper state change indication",
344 intf_id=intf_oper_indication.intf_id,
345 type=intf_oper_indication.type,
346 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000347
348 if intf_oper_indication.oper_state == "up":
349 oper_state = OperStatus.ACTIVE
350 else:
351 oper_state = OperStatus.DISCOVERED
352
353 if intf_oper_indication.type == "nni":
354
Shad Ansari0346f0d2018-04-26 06:54:09 +0000355 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000356 if intf_oper_indication.intf_id != 0:
357 return
358
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400359 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000360 port_no, label = self.add_port(intf_oper_indication.intf_id,
361 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400362 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000363 self.add_logical_port(port_no, intf_oper_indication.intf_id,
364 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000365
366 elif intf_oper_indication.type == "pon":
367 # FIXME - handle PON oper state change
368 pass
369
370 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000371 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000372 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000373
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400374 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000375
Shad Ansarif9d2d102018-06-13 02:15:26 +0000376 self.log.debug("onu discovery indication", intf_id=intf_id,
377 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400378
mzadig384783a2018-08-09 08:52:40 -0400379 # Post ONU Discover alarm 20180809_0805
380 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400381 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
382 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400383 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400384 self.log.exception("onu-discovery-alarm-error",
385 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400386 # continue for now.
387
Shad Ansarif9d2d102018-06-13 02:15:26 +0000388 onu_device = self.adapter_agent.get_child_device(
389 self.device_id,
390 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400391
392 if onu_device is None:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700393 onu_id = self.new_onu_id(intf_id)
394
Shad Ansari15928d12018-04-17 02:42:13 +0000395 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000396 self.add_onu_device(
397 intf_id,
398 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
399 onu_id, serial_number)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400400 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700401 serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400402 except Exception as e:
403 self.log.exception('onu-activation-failed', e=e)
404
Shad Ansari2825d012018-02-22 23:57:46 +0000405 else:
nick47b74372018-05-25 18:22:49 -0400406 if onu_device.connect_status != ConnectStatus.REACHABLE:
407 onu_device.connect_status = ConnectStatus.REACHABLE
408 self.adapter_agent.update_device(onu_device)
409
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400410 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000411 if onu_device.oper_status == OperStatus.DISCOVERED \
412 or onu_device.oper_status == OperStatus.ACTIVATING:
413 self.log.debug("ignore onu discovery indication, \
414 the onu has been discovered and should be \
415 activating shorlty", intf_id=intf_id,
416 onu_id=onu_id, state=onu_device.oper_status)
417 elif onu_device.oper_status == OperStatus.ACTIVE:
418 self.log.warn("onu discovery indication whereas onu is \
419 supposed to be active",
420 intf_id=intf_id, onu_id=onu_id,
421 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400422 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000423 self.log.info("onu in unknown state, recovering from olt \
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400424 reboot probably, activate onu", intf_id=intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000425 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400426
427 onu_device.oper_status = OperStatus.DISCOVERED
428 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400429 try:
430 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700431 serial_number_str)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400432 except Exception as e:
433 self.log.error('onu-activation-error',
434 serial_number=serial_number_str, error=e)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400435 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000436 self.log.warn('unexpected state', onu_id=onu_id,
437 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000438
Shad Ansari2825d012018-02-22 23:57:46 +0000439 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000440 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000441 onu_id=onu_indication.onu_id,
442 serial_number=onu_indication.serial_number,
443 oper_state=onu_indication.oper_state,
444 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400445 try:
446 serial_number_str = self.stringify_serial_number(
447 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000448 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400449 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000450
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400451 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400452 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000453 self.device_id,
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400454 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000455 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400456 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000457 self.device_id,
458 parent_port_no=platform.intf_id_to_port_no(
459 onu_indication.intf_id, Port.PON_OLT),
460 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400461
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400462 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000463 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000464 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000465 return
466
Shad Ansarif9d2d102018-06-13 02:15:26 +0000467 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
Shad Ansarif9521ad2018-09-08 10:46:43 +0000468 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400469 self.log.warn('ONU-is-on-a-different-intf-id-now',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000470 previous_intf_id=platform.intf_id_from_pon_port_no(
471 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400472 current_intf_id=onu_indication.intf_id)
473 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000474
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400475 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
476 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400477 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
478 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000479 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400480 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000481
Shad Ansarif9d2d102018-06-13 02:15:26 +0000482 uni_no = platform.mk_uni_port_num(onu_indication.intf_id,
483 onu_indication.onu_id)
484 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
485 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000486
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400487 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000488
Shad Ansarif9d2d102018-06-13 02:15:26 +0000489 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400490 if onu_indication.admin_state == 'down':
491 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000492 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
493 oper_state=onu_indication.oper_state)
494 # Forcing the oper state change code to execute
495 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400496
Shad Ansarif9d2d102018-06-13 02:15:26 +0000497 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400498
499 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400500 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400501
502 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000503 self.log.warn('Invalid-or-not-implemented-admin-state',
504 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400505
506 self.log.debug('admin-state-dealt-with')
507
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400508 onu_adapter_agent = \
509 registry('adapter_loader').get_agent(onu_device.adapter)
510 if onu_adapter_agent is None:
511 self.log.error('onu_adapter_agent-could-not-be-retrieved',
512 onu_device=onu_device)
513 return
514
Shad Ansarif9d2d102018-06-13 02:15:26 +0000515 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400516 if onu_indication.oper_state == 'down':
Nicolas Palpacuer4ea3a652018-08-22 10:33:17 -0400517
518 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
519 onu_device.connect_status = ConnectStatus.UNREACHABLE
520 self.adapter_agent.update_device(onu_device)
521
Shad Ansarif9d2d102018-06-13 02:15:26 +0000522 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400523 self.log.debug('onu-oper-state-is-down')
524
525 if onu_device.oper_status != OperStatus.DISCOVERED:
526 onu_device.oper_status = OperStatus.DISCOVERED
527 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000528 # Set port oper state to Discovered
529 self.onu_ports_down(onu_device, uni_no, uni_name,
530 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400531
Shad Ansari0e7ad962018-09-28 01:42:26 +0000532 onu_adapter_agent.update_interface(onu_device,
533 {'oper_state': 'down'})
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400534
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400535 elif onu_indication.oper_state == 'up':
536
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400537 if onu_device.connect_status != ConnectStatus.REACHABLE:
538 onu_device.connect_status = ConnectStatus.REACHABLE
539 self.adapter_agent.update_device(onu_device)
540
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400541 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000542 self.log.debug("ignore onu indication",
543 intf_id=onu_indication.intf_id,
544 onu_id=onu_indication.onu_id,
545 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400546 msg_oper_state=onu_indication.oper_state)
547 return
548
Shad Ansarif9d2d102018-06-13 02:15:26 +0000549 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400550
Shad Ansarif9d2d102018-06-13 02:15:26 +0000551 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400552
Shad Ansari0e7ad962018-09-28 01:42:26 +0000553 # tcont creation (onu)
554 tcont = TcontsConfigData()
555 tcont.alloc_id = platform.mk_alloc_id(
556 onu_indication.intf_id, onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400557
Shad Ansari0e7ad962018-09-28 01:42:26 +0000558 # gem port creation
559 gem_port = GemportsConfigData()
560 gem_port.gemport_id = platform.mk_gemport_id(
561 onu_indication.intf_id,
562 onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400563
Shad Ansari0e7ad962018-09-28 01:42:26 +0000564 gem_port.tcont_ref = str(tcont.alloc_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400565
Shad Ansari0e7ad962018-09-28 01:42:26 +0000566 self.log.info('inject-tcont-gem-data-onu-handler',
567 onu_indication=onu_indication, tcont=tcont,
568 gem_port=gem_port)
Girish Gowdru141ced82018-09-17 20:19:14 -0700569
Shad Ansari0e7ad962018-09-28 01:42:26 +0000570 onu_adapter_agent.create_tcont(onu_device, tcont,
571 traffic_descriptor_data=None)
572 onu_adapter_agent.create_gemport(onu_device, gem_port)
573 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400574
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400575 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000576 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
577 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000578
nick47b74372018-05-25 18:22:49 -0400579 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
580 # Set port oper state to Discovered
581 # add port will update port if it exists
582 self.adapter_agent.add_port(
583 self.device_id,
584 Port(
585 port_no=uni_no,
586 label=uni_name,
587 type=Port.ETHERNET_UNI,
588 admin_state=onu_device.admin_state,
589 oper_status=oper_state))
590
591 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400592 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
593 onu_port_id = None
594 for onu_port in onu_ports:
595 if onu_port.port_no == uni_no:
596 onu_port_id = onu_port.label
597 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000598 self.log.error('matching-onu-port-label-not-found',
599 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400600 onu_ports=onu_ports)
601 return
602 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000603 onu_logical_port = self.adapter_agent.get_logical_port(
604 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400605 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000606 self.adapter_agent.update_logical_port(
607 logical_device_id=self.logical_device_id,
608 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400609 self.log.debug('cascading-oper-state-to-port-and-logical-port')
610 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000611 self.log.error('matching-onu-port-label-invalid',
612 onu_id=onu_device.id, olt_id=self.device_id,
613 onu_ports=onu_ports, onu_port_id=onu_port_id,
614 error=e)
nick47b74372018-05-25 18:22:49 -0400615
Shad Ansari2825d012018-02-22 23:57:46 +0000616 def omci_indication(self, omci_indication):
617
618 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000619 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000620
Shad Ansarif9d2d102018-06-13 02:15:26 +0000621 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400622 self.device_id, onu_id=omci_indication.onu_id,
623 parent_port_no=platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700624 omci_indication.intf_id, Port.PON_OLT), )
Shad Ansari0efa6512018-04-28 06:42:54 +0000625
626 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000627 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000628
Shad Ansari42db7342018-04-25 21:39:46 +0000629 def packet_indication(self, pkt_indication):
630
Shad Ansari0ff82622018-09-30 09:32:04 +0000631 self.log.debug("packet indication",
632 intf_type=pkt_indication.intf_type,
633 intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000634 gemport_id=pkt_indication.gemport_id,
635 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000636
Shad Ansari0ff82622018-09-30 09:32:04 +0000637 if pkt_indication.intf_type == "pon":
638 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
639 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
640 onu_id)
641 elif pkt_indication.intf_type == "nni":
642 logical_port_num = platform.intf_id_to_port_no(
643 pkt_indication.intf_id,
644 Port.ETHERNET_NNI)
Shad Ansari42db7342018-04-25 21:39:46 +0000645
646 pkt = Ether(pkt_indication.pkt)
Shad Ansari0ff82622018-09-30 09:32:04 +0000647
648 self.log.debug("packet indication",
649 logical_device_id=self.logical_device_id,
650 logical_port_no=logical_port_num)
651
652 self.adapter_agent.send_packet_in(
653 logical_device_id=self.logical_device_id,
654 logical_port_no=logical_port_num,
655 packet=str(pkt))
Shad Ansari42db7342018-04-25 21:39:46 +0000656
657 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000658 pkt = Ether(msg)
Saurav Dasf87d6552018-09-26 17:05:42 -0700659 self.log.debug('packet out', egress_port=egress_port,
660 device_id=self.device_id,
661 logical_device_id=self.logical_device_id,
662 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400663
664 # Find port type
665 egress_port_type = self.port_type(egress_port)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400666 if egress_port_type == Port.ETHERNET_UNI:
667
668 if pkt.haslayer(Dot1Q):
669 outer_shim = pkt.getlayer(Dot1Q)
670 if isinstance(outer_shim.payload, Dot1Q):
671 # If double tag, remove the outer tag
672 payload = (
673 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
674 outer_shim.payload
675 )
676 else:
677 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000678 else:
679 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400680
681 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
682
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400683 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000684 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400685 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000686 onu_id=platform.onu_id_from_port_num(egress_port),
687 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400688
Shad Ansarif9d2d102018-06-13 02:15:26 +0000689 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400690 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000691 onu_id=platform.onu_id_from_port_num(egress_port),
692 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400693
694 self.stub.OnuPacketOut(onu_pkt)
695
696 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400697 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000698 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400699
700 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
701
Shad Ansarif9d2d102018-06-13 02:15:26 +0000702 uplink_pkt = openolt_pb2.UplinkPacket(
703 intf_id=platform.intf_id_from_nni_port_num(egress_port),
704 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400705
706 self.stub.UplinkPacketOut(uplink_pkt)
707
Shad Ansari0346f0d2018-04-26 06:54:09 +0000708 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000709 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
710 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400711 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000712
Shad Ansari2825d012018-02-22 23:57:46 +0000713 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9521ad2018-09-08 10:46:43 +0000714 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru141ced82018-09-17 20:19:14 -0700715 self.device_id, onu_id=proxy_address.onu_id,
Shad Ansari0e7ad962018-09-28 01:42:26 +0000716 parent_port_no=platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700717 proxy_address.channel_id, Port.PON_OLT)
718 )
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400719 if onu_device.connect_status != ConnectStatus.REACHABLE:
720 self.log.debug('ONU is not reachable, cannot send OMCI',
721 serial_number=onu_device.serial_number,
722 intf_id=onu_device.proxy_address.channel_id,
723 onu_id=onu_device.proxy_address.onu_id)
724 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000725 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
726 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000727 self.stub.OmciMsgOut(omci)
728
729 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000730 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000731 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000732
733 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000734 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000735 channel_id=intf_id, onu_id=onu_id,
736 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000737
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400738 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000739
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400740 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000741
Shad Ansarif9d2d102018-06-13 02:15:26 +0000742 self.adapter_agent.add_onu_device(
743 parent_device_id=self.device_id, parent_port_no=port_no,
744 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
745 root=True, serial_number=serial_number_str,
746 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000747
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000748 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000749 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400750 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000751 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000752 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000753 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400754 if serial_number is not None:
755 return serial_number
756 else:
757 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000758
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400759 def port_type(self, port_no):
760 ports = self.adapter_agent.get_ports(self.device_id)
761 for port in ports:
762 if port.port_no == port_no:
763 return port.type
764 return None
765
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400766 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000767 self.log.info('adding-logical-port', port_no=port_no)
768
769 label = self.port_name(port_no, Port.ETHERNET_NNI)
770
771 cap = OFPPF_1GB_FD | OFPPF_FIBER
772 curr_speed = OFPPF_1GB_FD
773 max_speed = OFPPF_1GB_FD
774
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400775 if oper_state == OperStatus.ACTIVE:
776 of_oper_state = OFPPS_LIVE
777 else:
778 of_oper_state = OFPPS_LINK_DOWN
779
Shad Ansarif9d2d102018-06-13 02:15:26 +0000780 ofp = ofp_port(
781 port_no=port_no,
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400782 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000783 name=label, config=0, state=of_oper_state, curr=cap,
784 advertised=cap, peer=cap, curr_speed=curr_speed,
785 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000786
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400787 ofp_stats = ofp_port_stats(port_no=port_no)
788
Shad Ansarif9d2d102018-06-13 02:15:26 +0000789 logical_port = LogicalPort(
790 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400791 device_port_no=port_no, root_port=True,
792 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000793
Shad Ansarif9d2d102018-06-13 02:15:26 +0000794 self.adapter_agent.add_logical_port(self.logical_device_id,
795 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000796
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400797 def _get_mac_form_port_no(self, port_no):
798 mac = ''
799 for i in range(4):
800 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
801 return '00:00' + mac
802
Shad Ansari2825d012018-02-22 23:57:46 +0000803 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000804 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000805
Shad Ansari4a232ca2018-05-05 05:24:17 +0000806 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000807
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400808 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000809 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000810
811 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000812 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000813
Shad Ansari2825d012018-02-22 23:57:46 +0000814 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000815
Shad Ansari2825d012018-02-22 23:57:46 +0000816 return port_no, label
817
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400818 def delete_logical_port(self, child_device_id):
819 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
820 self.logical_device_id))
821 for logical_port in logical_ports:
822 if logical_port.device_id == child_device_id:
823 self.log.debug('delete-logical-port',
824 onu_device_id=child_device_id,
825 logical_port=logical_port)
826 self.adapter_agent.delete_logical_port(
827 self.logical_device_id, logical_port)
828 return
Shad Ansarif9521ad2018-09-08 10:46:43 +0000829
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400830 def delete_port(self, child_serial_number):
831 ports = self.proxy.get('/devices/{}/ports'.format(
832 self.device_id))
833 for port in ports:
834 if port.label == child_serial_number:
835 self.log.debug('delete-port',
836 onu_serial_number=child_serial_number,
837 port=port)
838 self.adapter_agent.delete_port(self.device_id, port)
839 return
840
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700841 def new_onu_id(self, intf_id):
842 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
843 pon_onu_ids = [onu_device.proxy_address.onu_id
844 for onu_device in onu_devices
845 if onu_device.proxy_address.channel_id == intf_id]
846 for i in range(1, platform.MAX_ONUS_PER_PON):
847 if i not in pon_onu_ids:
848 return i
849
850 self.log.error('All available onu_ids taken on this pon',
851 intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
852 return None
853
Shad Ansari2825d012018-02-22 23:57:46 +0000854 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400855 self.log.debug('No updates here now, all is done in logical flows '
856 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000857
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400858 def update_logical_flows(self, flows_to_add, flows_to_remove,
859 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400860 if not self.is_state_up():
861 self.log.info('The OLT is not up, we cannot update flows',
862 flows_to_add=[f.id for f in flows_to_add],
863 flows_to_remove=[f.id for f in flows_to_remove])
864 return
865
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400866 try:
867 self.flow_mgr.update_children_flows(device_rules_map)
868 except Exception as e:
869 self.log.error('Error updating children flows', error=e)
Shad Ansari2825d012018-02-22 23:57:46 +0000870
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400871 self.log.debug('logical flows update', flows_to_add=flows_to_add,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000872 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000873
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400874 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400875
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400876 try:
877 self.flow_mgr.add_flow(flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400878 except Exception as e:
879 self.log.error('failed to add flow', flow=flow, e=e)
880
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400881 for flow in flows_to_remove:
882
883 try:
884 self.flow_mgr.remove_flow(flow)
885 except Exception as e:
886 self.log.error('failed to remove flow', flow=flow, e=e)
887
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400888 self.flow_mgr.repush_all_different_flows()
889
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400890 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000891 def ip_hex(self, ip):
892 octets = ip.split(".")
893 hex_ip = []
894 for octet in octets:
895 octet_hex = hex(int(octet))
896 octet_hex = octet_hex.split('0x')[1]
897 octet_hex = octet_hex.rjust(2, '0')
898 hex_ip.append(octet_hex)
899 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400900
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400901 def stringify_vendor_specific(self, vendor_specific):
902 return ''.join(str(i) for i in [
903 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
904 hex(ord(vendor_specific[0]) & 0x0f)[2:],
905 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
906 hex(ord(vendor_specific[1]) & 0x0f)[2:],
907 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
908 hex(ord(vendor_specific[2]) & 0x0f)[2:],
909 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
910 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
911
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400912 def stringify_serial_number(self, serial_number):
913 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000914 self.stringify_vendor_specific(
915 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400916
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400917 def destringify_serial_number(self, serial_number_str):
918 serial_number = openolt_pb2.SerialNumber(
919 vendor_id=serial_number_str[:4].encode('utf-8'),
920 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
921 return serial_number
922
Jonathan Davis0f917a22018-05-30 14:39:45 -0400923 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400924 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9521ad2018-09-08 10:46:43 +0000925 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400926
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400927 try:
928 # Send grpc call
929 self.stub.DisableOlt(openolt_pb2.Empty())
930 # The resulting indication will bring the OLT down
931 # self.go_state_down()
932 self.log.info('openolt device disabled')
933 except Exception as e:
934 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400935
Jonathan Davis0f917a22018-05-30 14:39:45 -0400936 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400937 self.log.info('deleting-olt', device_id=self.device_id,
938 logical_device_id=self.logical_device_id)
939
940 try:
941 # Rebooting to reset the state
942 self.reboot()
943 # Removing logical device
Saurav Dasf87d6552018-09-26 17:05:42 -0700944 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
945 self.adapter_agent.delete_logical_device(ld)
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400946 except Exception as e:
947 self.log.error('Failure to delete openolt device', error=e)
948 raise e
949 else:
950 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400951
Jonathan Davis0f917a22018-05-30 14:39:45 -0400952 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400953 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400954
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400955 try:
956 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400957
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400958 self.log.info('enabling-all-ports', device_id=self.device_id)
959 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400960 except Exception as e:
961 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400962 else:
963 self.log.info('openolt device reenabled')
964
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400965 def activate_onu(self, intf_id, onu_id, serial_number,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700966 serial_number_str):
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400967 pir = self.bw_mgr.pir(serial_number_str)
968 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000969 serial_number_str=serial_number_str,
970 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400971 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700972 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400973 self.stub.ActivateOnu(onu)
974 self.log.info('onu-activated', serial_number=serial_number_str)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400975
Jonathan Davisb45bb372018-07-19 15:05:15 -0400976 def delete_child_device(self, child_device):
977 self.log.debug('sending-deactivate-onu',
978 olt_device_id=self.device_id,
979 onu_device=child_device,
980 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400981 try:
982 self.adapter_agent.delete_child_device(self.device_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000983 child_device.id,
984 child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400985 except Exception as e:
986 self.log.error('adapter_agent error', error=e)
987 try:
988 self.delete_logical_port(child_device.id)
989 except Exception as e:
990 self.log.error('logical_port delete error', error=e)
991 try:
992 self.delete_port(child_device.serial_number)
993 except Exception as e:
994 self.log.error('port delete error', error=e)
Shad Ansarif9521ad2018-09-08 10:46:43 +0000995 serial_number = self.destringify_serial_number(
996 child_device.serial_number)
Jonathan Davisb45bb372018-07-19 15:05:15 -0400997 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
998 onu_id=child_device.proxy_address.onu_id,
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700999 serial_number=serial_number)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001000 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001001
1002 def reboot(self):
Shad Ansarif9521ad2018-09-08 10:46:43 +00001003 self.log.debug('rebooting openolt device', device_id=self.device_id)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001004 try:
1005 self.stub.Reboot(openolt_pb2.Empty())
1006 except Exception as e:
1007 self.log.error('something went wrong with the reboot', error=e)
1008 else:
1009 self.log.info('device rebooted')
1010
Nicolas Palpacuer30027f42018-09-06 15:55:54 -04001011 def trigger_statistics_collection(self):
1012 try:
1013 self.stub.CollectStatistics(openolt_pb2.Empty())
1014 except Exception as e:
1015 self.log.error('Error while triggering statistics collection',
1016 error=e)
1017 else:
1018 self.log.info('statistics requested')
Scott Bakerd3190952018-09-04 15:47:28 -07001019
1020 def simulate_alarm(self, alarm):
1021 self.alarm_mgr.simulate_alarm(alarm)