blob: 366b90035738f4998ae357b4082137a75caacfe8 [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',
87 'before': 'do_state_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000088
Shad Ansari2825d012018-02-22 23:57:46 +000089 def __init__(self, **kwargs):
90 super(OpenoltDevice, self).__init__()
91
92 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000093 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000094 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040095 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000096 self.device_id = device.id
97 self.host_and_port = device.host_and_port
Shad Ansarif9d2d102018-06-13 02:15:26 +000098 self.log = structlog.get_logger(id=self.device_id,
99 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400100 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +0000101
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400102 # Device already set in the event of reconciliation
103 if not is_reconciliation:
104 # It is a new device
105 # Update device
106 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000107 device.serial_number = self.host_and_port # FIXME
Shad Ansari94250fc2018-07-04 06:52:11 +0000108 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400109 device.oper_status = OperStatus.ACTIVATING
110 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000111
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400112 # If logical device does not exist create it
Shad Ansari94250fc2018-07-04 06:52:11 +0000113 if not device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400114
115 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
116
117 # Create logical OF device
118 ld = LogicalDevice(
119 root_device_id=self.device_id,
120 switch_features=ofp_switch_features(
121 n_buffers=256, # TODO fake for now
122 n_tables=2, # TODO ditto
123 capabilities=( # TODO and ditto
124 OFPC_FLOW_STATS
125 | OFPC_TABLE_STATS
126 | OFPC_PORT_STATS
127 | OFPC_GROUP_STATS
128 )
Jonathan Hart05845412018-07-19 09:55:43 -0700129 ),
130 desc=ofp_desc(
131 serial_num=device.serial_number
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400132 )
133 )
134 ld_init = self.adapter_agent.create_logical_device(ld,
135 dpid=dpid)
136 self.logical_device_id = ld_init.id
137 else:
138 # logical device already exists
139 self.logical_device_id = device.parent_id
140 if is_reconciliation:
141 self.adapter_agent.reconcile_logical_device(
142 self.logical_device_id)
143
Shad Ansari94250fc2018-07-04 06:52:11 +0000144 # Initialize the OLT state machine
145 self.machine = Machine(model=self, states=OpenoltDevice.states,
146 transitions=OpenoltDevice.transitions,
147 send_event=True, initial='state_null')
148 self.go_state_init()
149
150 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000151 # Initialize gRPC
152 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000153 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400154
jasonhuang5f3e63b2018-07-27 01:32:48 +0800155 # Catch RuntimeError exception
156 try:
157 # Start indications thread
158 self.indications_thread_handle = threading.Thread(
159 target=self.indications_thread)
160 # Old getter/setter API for daemon; use it directly as a
161 # property instead. The Jinkins error will happon on the reason of
162 # Exception in thread Thread-1 (most likely raised # during
163 # interpreter shutdown)
164 self.indications_thread_handle.setDaemon(True)
165 self.indications_thread_handle.start()
166 except Exception as e:
167 self.log.exception('do_state_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000168 '''
169 # FIXME - Move to oper_up state without connecting to OLT?
170 if is_reconciliation:
171 # Put state machine in state up
172 reactor.callFromThread(self.go_state_up, reconciliation=True)
173 '''
174
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400175 self.log.info('openolt-device-created', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000176
177 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400178 self.log.debug("do_state_connected")
179
Shad Ansari94250fc2018-07-04 06:52:11 +0000180 device = self.adapter_agent.get_device(self.device_id)
181 device.connect_status = ConnectStatus.REACHABLE
182 self.adapter_agent.update_device(device)
183
184 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400185 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
186 self.logical_device_id)
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000187 self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent,
188 self.device_id,
mzadig7cda5ff2018-07-10 16:37:28 -0400189 self.logical_device_id)
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400190 self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000191 self.bw_mgr = OpenOltBW(self.log, self.proxy)
Shad Ansari2825d012018-02-22 23:57:46 +0000192
Shad Ansari94250fc2018-07-04 06:52:11 +0000193 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400194 self.log.debug("do_state_up")
195
Shad Ansari94250fc2018-07-04 06:52:11 +0000196 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000197
Shad Ansari94250fc2018-07-04 06:52:11 +0000198 # Update phys OF device
199 device.parent_id = self.logical_device_id
200 device.oper_status = OperStatus.ACTIVE
201 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400202
Shad Ansari94250fc2018-07-04 06:52:11 +0000203 def do_state_down(self, event):
204 self.log.debug("do_state_down")
205 oper_state = OperStatus.UNKNOWN
206 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400207
Shad Ansari94250fc2018-07-04 06:52:11 +0000208 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400209
Shad Ansari94250fc2018-07-04 06:52:11 +0000210 # Children ports
211 child_devices = self.adapter_agent.get_child_devices(self.device_id)
212 for onu_device in child_devices:
213 uni_no = platform.mk_uni_port_num(
214 onu_device.proxy_address.channel_id,
215 onu_device.proxy_address.onu_id)
216 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
217 serial_number=onu_device.serial_number)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000218
Shad Ansari94250fc2018-07-04 06:52:11 +0000219 self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
220 # Children devices
221 self.adapter_agent.update_child_devices_state(
222 self.device_id, oper_status=oper_state,
223 connect_status=connect_state)
224 # Device Ports
225 device_ports = self.adapter_agent.get_ports(self.device_id,
226 Port.ETHERNET_NNI)
227 logical_ports_ids = [port.label for port in device_ports]
228 device_ports += self.adapter_agent.get_ports(self.device_id,
229 Port.PON_OLT)
230
231 for port in device_ports:
232 port.oper_status = oper_state
233 self.adapter_agent.add_port(self.device_id, port)
234
235 # Device logical port
236 for logical_port_id in logical_ports_ids:
237 logical_port = self.adapter_agent.get_logical_port(
238 self.logical_device_id, logical_port_id)
239 logical_port.ofp_port.state = OFPPS_LINK_DOWN
240 self.adapter_agent.update_logical_port(self.logical_device_id,
241 logical_port)
242
243 # Device
244 device = self.adapter_agent.get_device(self.device_id)
245 device.oper_status = oper_state
246 device.connect_status = connect_state
247
248 self.adapter_agent.update_device(device)
249
250 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400251 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000252 self.log.debug('connecting to olt', device_id=self.device_id)
253 self.channel_ready_future.result() # blocking call
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400254 self.log.info('connected to olt', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000255 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000256
Shad Ansari15928d12018-04-17 02:42:13 +0000257 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000258
Shad Ansari94250fc2018-07-04 06:52:11 +0000259 while True:
nick47b74372018-05-25 18:22:49 -0400260 try:
261 # get the next indication from olt
262 ind = next(self.indications)
263 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000264 self.log.warn('gRPC connection lost', error=e)
265 reactor.callFromThread(self.go_state_down)
266 reactor.callFromThread(self.go_state_init)
267 break
nick47b74372018-05-25 18:22:49 -0400268 else:
269 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000270
nick47b74372018-05-25 18:22:49 -0400271 # indication handlers run in the main event loop
272 if ind.HasField('olt_ind'):
273 reactor.callFromThread(self.olt_indication, ind.olt_ind)
274 elif ind.HasField('intf_ind'):
275 reactor.callFromThread(self.intf_indication, ind.intf_ind)
276 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000277 reactor.callFromThread(self.intf_oper_indication,
278 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400279 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000280 reactor.callFromThread(self.onu_discovery_indication,
281 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400282 elif ind.HasField('onu_ind'):
283 reactor.callFromThread(self.onu_indication, ind.onu_ind)
284 elif ind.HasField('omci_ind'):
285 reactor.callFromThread(self.omci_indication, ind.omci_ind)
286 elif ind.HasField('pkt_ind'):
287 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400288 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400289 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000290 self.stats_mgr.port_statistics_indication,
291 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400292 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400293 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000294 self.stats_mgr.flow_statistics_indication,
295 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000296 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400297 reactor.callFromThread(self.alarm_mgr.process_alarms,
298 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400299 else:
300 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400301
Shad Ansari2825d012018-02-22 23:57:46 +0000302 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000303 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000304 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000305 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000306 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000307
308 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400309 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000310 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000311
312 if intf_indication.oper_state == "up":
313 oper_status = OperStatus.ACTIVE
314 else:
315 oper_status = OperStatus.DISCOVERED
316
nick47b74372018-05-25 18:22:49 -0400317 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000318 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
319
320 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000321 self.log.debug("Received interface oper state change indication",
322 intf_id=intf_oper_indication.intf_id,
323 type=intf_oper_indication.type,
324 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000325
326 if intf_oper_indication.oper_state == "up":
327 oper_state = OperStatus.ACTIVE
328 else:
329 oper_state = OperStatus.DISCOVERED
330
331 if intf_oper_indication.type == "nni":
332
Shad Ansari0346f0d2018-04-26 06:54:09 +0000333 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000334 if intf_oper_indication.intf_id != 0:
335 return
336
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400337 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000338 port_no, label = self.add_port(intf_oper_indication.intf_id,
339 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400340 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000341 self.add_logical_port(port_no, intf_oper_indication.intf_id,
342 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000343
344 elif intf_oper_indication.type == "pon":
345 # FIXME - handle PON oper state change
346 pass
347
348 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000349 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000350 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000351
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400352 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000353
Shad Ansarif9d2d102018-06-13 02:15:26 +0000354 self.log.debug("onu discovery indication", intf_id=intf_id,
355 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400356
mzadig384783a2018-08-09 08:52:40 -0400357 # Post ONU Discover alarm 20180809_0805
358 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400359 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
360 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400361 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400362 self.log.exception("onu-discovery-alarm-error",
363 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400364 # continue for now.
365
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000366 pir = self.bw_mgr.pir(serial_number_str)
367 self.log.debug("peak information rate", serial_number=serial_number,
368 pir=pir)
369
Shad Ansarif9d2d102018-06-13 02:15:26 +0000370 onu_device = self.adapter_agent.get_child_device(
371 self.device_id,
372 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400373
374 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000375 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000376 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000377 self.add_onu_device(
378 intf_id,
379 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
380 onu_id, serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400381 self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000382 serial_number=serial_number_str)
383 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000384 serial_number=serial_number, pir=pir)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400385 self.stub.ActivateOnu(onu)
386 except Exception as e:
387 self.log.exception('onu-activation-failed', e=e)
388
Shad Ansari2825d012018-02-22 23:57:46 +0000389 else:
nick47b74372018-05-25 18:22:49 -0400390 if onu_device.connect_status != ConnectStatus.REACHABLE:
391 onu_device.connect_status = ConnectStatus.REACHABLE
392 self.adapter_agent.update_device(onu_device)
393
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400394 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000395 if onu_device.oper_status == OperStatus.DISCOVERED \
396 or onu_device.oper_status == OperStatus.ACTIVATING:
397 self.log.debug("ignore onu discovery indication, \
398 the onu has been discovered and should be \
399 activating shorlty", intf_id=intf_id,
400 onu_id=onu_id, state=onu_device.oper_status)
401 elif onu_device.oper_status == OperStatus.ACTIVE:
402 self.log.warn("onu discovery indication whereas onu is \
403 supposed to be active",
404 intf_id=intf_id, onu_id=onu_id,
405 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400406 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000407 self.log.info("onu in unknown state, recovering from olt \
408 reboot, activate onu", intf_id=intf_id,
409 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400410
411 onu_device.oper_status = OperStatus.DISCOVERED
412 self.adapter_agent.update_device(onu_device)
413
Shad Ansarif9d2d102018-06-13 02:15:26 +0000414 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000415 serial_number=serial_number, pir=pir)
nick47b74372018-05-25 18:22:49 -0400416 self.stub.ActivateOnu(onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400417 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000418 self.log.warn('unexpected state', onu_id=onu_id,
419 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000420
Shad Ansari2825d012018-02-22 23:57:46 +0000421 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000422 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000423 onu_id=onu_indication.onu_id,
424 serial_number=onu_indication.serial_number,
425 oper_state=onu_indication.oper_state,
426 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400427 try:
428 serial_number_str = self.stringify_serial_number(
429 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000430 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400431 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000432
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400433 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400434 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000435 self.device_id,
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400436 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000437 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400438 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000439 self.device_id,
440 parent_port_no=platform.intf_id_to_port_no(
441 onu_indication.intf_id, Port.PON_OLT),
442 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400443
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400444 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000445 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000446 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000447 return
448
nick47b74372018-05-25 18:22:49 -0400449 if onu_device.connect_status != ConnectStatus.REACHABLE:
450 onu_device.connect_status = ConnectStatus.REACHABLE
451 self.adapter_agent.update_device(onu_device)
452
Shad Ansarif9d2d102018-06-13 02:15:26 +0000453 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
454 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400455 self.log.warn('ONU-is-on-a-different-intf-id-now',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000456 previous_intf_id=platform.intf_id_from_pon_port_no(
457 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400458 current_intf_id=onu_indication.intf_id)
459 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000460
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400461 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
462 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400463 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
464 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000465 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400466 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000467
Shad Ansarif9d2d102018-06-13 02:15:26 +0000468 uni_no = platform.mk_uni_port_num(onu_indication.intf_id,
469 onu_indication.onu_id)
470 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
471 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000472
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400473 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000474
Shad Ansarif9d2d102018-06-13 02:15:26 +0000475 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400476 if onu_indication.admin_state == 'down':
477 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000478 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
479 oper_state=onu_indication.oper_state)
480 # Forcing the oper state change code to execute
481 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400482
Shad Ansarif9d2d102018-06-13 02:15:26 +0000483 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400484
485 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400486 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400487
488 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000489 self.log.warn('Invalid-or-not-implemented-admin-state',
490 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400491
492 self.log.debug('admin-state-dealt-with')
493
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400494 onu_adapter_agent = \
495 registry('adapter_loader').get_agent(onu_device.adapter)
496 if onu_adapter_agent is None:
497 self.log.error('onu_adapter_agent-could-not-be-retrieved',
498 onu_device=onu_device)
499 return
500
Shad Ansarif9d2d102018-06-13 02:15:26 +0000501 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400502 if onu_indication.oper_state == 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000503 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400504 self.log.debug('onu-oper-state-is-down')
505
506 if onu_device.oper_status != OperStatus.DISCOVERED:
507 onu_device.oper_status = OperStatus.DISCOVERED
508 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000509 # Set port oper state to Discovered
510 self.onu_ports_down(onu_device, uni_no, uni_name,
511 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400512
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400513 if onu_device.adapter == 'brcm_openomci_onu':
514 self.log.debug('using-brcm_openomci_onu')
515 onu_adapter_agent.update_interface(onu_device, onu_indication)
516
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400517 elif onu_indication.oper_state == 'up':
518
519 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000520 self.log.debug("ignore onu indication",
521 intf_id=onu_indication.intf_id,
522 onu_id=onu_indication.onu_id,
523 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400524 msg_oper_state=onu_indication.oper_state)
525 return
526
Shad Ansarif9d2d102018-06-13 02:15:26 +0000527 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400528
Shad Ansarif9d2d102018-06-13 02:15:26 +0000529 # Prepare onu configuration
Shad Ansari5df91f62018-07-25 23:59:46 +0000530 # If we are using the old/current broadcom adapter otherwise
531 # use the openomci adapter
Matt Jeannerete6a70332018-07-20 16:11:25 -0400532 if onu_device.adapter == 'broadcom_onu':
533 self.log.debug('using-broadcom_onu')
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400534
Matt Jeannerete6a70332018-07-20 16:11:25 -0400535 # onu initialization, base configuration (bridge setup ...)
536 def onu_initialization():
537
Shad Ansarif9d2d102018-06-13 02:15:26 +0000538 onu_adapter_agent.adapter.devices_handlers[onu_device.id] \
Matt Jeannerete6a70332018-07-20 16:11:25 -0400539 .message_exchange(cvid=DEFAULT_MGMT_VLAN)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400540 self.log.debug('broadcom-message-exchange-started')
541
Matt Jeannerete6a70332018-07-20 16:11:25 -0400542 # tcont creation (onu)
543 tcont = TcontsConfigData()
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400544 tcont.alloc_id = platform.mk_alloc_id(
545 onu_indication.intf_id, onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400546
Matt Jeannerete6a70332018-07-20 16:11:25 -0400547 # gem port creation
548 gem_port = GemportsConfigData()
Shad Ansari5df91f62018-07-25 23:59:46 +0000549 gem_port.gemport_id = platform.mk_gemport_id(
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400550 onu_indication.intf_id,
Shad Ansari5df91f62018-07-25 23:59:46 +0000551 onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400552
Matt Jeannerete6a70332018-07-20 16:11:25 -0400553 # ports creation/update
554 def port_config():
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400555
Matt Jeannerete6a70332018-07-20 16:11:25 -0400556 # "v_enet" creation (olt)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400557
Matt Jeannerete6a70332018-07-20 16:11:25 -0400558 # add_port update port when it exists
559 self.adapter_agent.add_port(
560 self.device_id,
561 Port(
562 port_no=uni_no,
563 label=uni_name,
564 type=Port.ETHERNET_UNI,
565 admin_state=AdminState.ENABLED,
566 oper_status=OperStatus.ACTIVE))
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400567
Matt Jeannerete6a70332018-07-20 16:11:25 -0400568 # v_enet creation (onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400569
Matt Jeannerete6a70332018-07-20 16:11:25 -0400570 venet = VEnetConfig(name=uni_name)
571 venet.interface.name = uni_name
572 onu_adapter_agent.create_interface(onu_device, venet)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400573
Matt Jeannerete6a70332018-07-20 16:11:25 -0400574 # ONU device status update in the datastore
575 def onu_update_oper_status():
576 onu_device.oper_status = OperStatus.ACTIVE
577 onu_device.connect_status = ConnectStatus.REACHABLE
578 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400579
Matt Jeannerete6a70332018-07-20 16:11:25 -0400580 # FIXME : the asynchronicity has to be taken care of properly
581 onu_initialization()
582 reactor.callLater(10, onu_adapter_agent.create_tcont,
583 device=onu_device, tcont_data=tcont,
584 traffic_descriptor_data=None)
Shad Ansari5df91f62018-07-25 23:59:46 +0000585 reactor.callLater(11, onu_adapter_agent.create_gemport,
586 onu_device, gem_port)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400587 reactor.callLater(12, port_config)
588 reactor.callLater(12, onu_update_oper_status)
589
590 elif onu_device.adapter == 'brcm_openomci_onu':
591 self.log.debug('using-brcm_openomci_onu')
592
593 # tcont creation (onu)
594 tcont = TcontsConfigData()
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400595 tcont.alloc_id = platform.mk_alloc_id(
596 onu_indication.intf_id, onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400597
598 # gem port creation
599 gem_port = GemportsConfigData()
Shad Ansari5df91f62018-07-25 23:59:46 +0000600 gem_port.gemport_id = platform.mk_gemport_id(
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400601 onu_indication.intf_id,
Shad Ansari5df91f62018-07-25 23:59:46 +0000602 onu_indication.onu_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400603 gem_port.tcont_ref = str(tcont.alloc_id)
604
Shad Ansari5df91f62018-07-25 23:59:46 +0000605 self.log.info('inject-tcont-gem-data-onu-handler',
606 onu_indication=onu_indication, tcont=tcont,
607 gem_port=gem_port)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400608
Shad Ansari5df91f62018-07-25 23:59:46 +0000609 onu_adapter_agent.create_tcont(onu_device, tcont,
610 traffic_descriptor_data=None)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400611 onu_adapter_agent.create_gemport(onu_device, gem_port)
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400612 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400613
614 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400615 self.log.error('unsupported-openolt-onu-adapter')
Matt Jeannerete6a70332018-07-20 16:11:25 -0400616
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400617 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000618 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
619 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000620
nick47b74372018-05-25 18:22:49 -0400621 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
622 # Set port oper state to Discovered
623 # add port will update port if it exists
624 self.adapter_agent.add_port(
625 self.device_id,
626 Port(
627 port_no=uni_no,
628 label=uni_name,
629 type=Port.ETHERNET_UNI,
630 admin_state=onu_device.admin_state,
631 oper_status=oper_state))
632
633 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400634 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
635 onu_port_id = None
636 for onu_port in onu_ports:
637 if onu_port.port_no == uni_no:
638 onu_port_id = onu_port.label
639 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000640 self.log.error('matching-onu-port-label-not-found',
641 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400642 onu_ports=onu_ports)
643 return
644 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000645 onu_logical_port = self.adapter_agent.get_logical_port(
646 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400647 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000648 self.adapter_agent.update_logical_port(
649 logical_device_id=self.logical_device_id,
650 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400651 self.log.debug('cascading-oper-state-to-port-and-logical-port')
652 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000653 self.log.error('matching-onu-port-label-invalid',
654 onu_id=onu_device.id, olt_id=self.device_id,
655 onu_ports=onu_ports, onu_port_id=onu_port_id,
656 error=e)
nick47b74372018-05-25 18:22:49 -0400657
Shad Ansari2825d012018-02-22 23:57:46 +0000658 def omci_indication(self, omci_indication):
659
660 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000661 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000662
Shad Ansarif9d2d102018-06-13 02:15:26 +0000663 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400664 self.device_id, onu_id=omci_indication.onu_id,
665 parent_port_no=platform.intf_id_to_port_no(
666 omci_indication.intf_id, Port.PON_OLT),)
Shad Ansari0efa6512018-04-28 06:42:54 +0000667
668 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000669 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000670
Shad Ansari42db7342018-04-25 21:39:46 +0000671 def packet_indication(self, pkt_indication):
672
673 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000674 gemport_id=pkt_indication.gemport_id,
675 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000676
Shad Ansari22920932018-05-17 00:33:34 +0000677 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000678 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
679 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000680
681 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000682 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000683 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000684 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
685
686 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000687 pkt = Ether(msg)
688 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000689 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400690
691 # Find port type
692 egress_port_type = self.port_type(egress_port)
693
694 if egress_port_type == Port.ETHERNET_UNI:
695
696 if pkt.haslayer(Dot1Q):
697 outer_shim = pkt.getlayer(Dot1Q)
698 if isinstance(outer_shim.payload, Dot1Q):
699 # If double tag, remove the outer tag
700 payload = (
701 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
702 outer_shim.payload
703 )
704 else:
705 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000706 else:
707 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400708
709 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
710
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400711 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000712 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400713 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000714 onu_id=platform.onu_id_from_port_num(egress_port),
715 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400716
Shad Ansarif9d2d102018-06-13 02:15:26 +0000717 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400718 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000719 onu_id=platform.onu_id_from_port_num(egress_port),
720 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400721
722 self.stub.OnuPacketOut(onu_pkt)
723
724 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400725 self.log.debug('sending-packet-to-uplink', 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 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
729
Shad Ansarif9d2d102018-06-13 02:15:26 +0000730 uplink_pkt = openolt_pb2.UplinkPacket(
731 intf_id=platform.intf_id_from_nni_port_num(egress_port),
732 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400733
734 self.stub.UplinkPacketOut(uplink_pkt)
735
Shad Ansari0346f0d2018-04-26 06:54:09 +0000736 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000737 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
738 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400739 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000740
Shad Ansari2825d012018-02-22 23:57:46 +0000741 def send_proxied_message(self, proxy_address, msg):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400742 onu_device = self.adapter_agent.get_child_device(self.device_id,
743 onu_id=proxy_address.onu_id,
744 parent_port_no=platform.intf_id_to_port_no(
745 proxy_address.channel_id, Port.PON_OLT))
746 if onu_device.connect_status != ConnectStatus.REACHABLE:
747 self.log.debug('ONU is not reachable, cannot send OMCI',
748 serial_number=onu_device.serial_number,
749 intf_id=onu_device.proxy_address.channel_id,
750 onu_id=onu_device.proxy_address.onu_id)
751 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000752 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
753 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000754 self.stub.OmciMsgOut(omci)
755
756 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000757 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000758 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000759
760 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000761 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000762 channel_id=intf_id, onu_id=onu_id,
763 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000764
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400765 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000766
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400767 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000768
Shad Ansarif9d2d102018-06-13 02:15:26 +0000769 self.adapter_agent.add_onu_device(
770 parent_device_id=self.device_id, parent_port_no=port_no,
771 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
772 root=True, serial_number=serial_number_str,
773 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000774
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000775 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000776 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400777 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000778 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000779 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000780 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400781 if serial_number is not None:
782 return serial_number
783 else:
784 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000785
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400786 def port_type(self, port_no):
787 ports = self.adapter_agent.get_ports(self.device_id)
788 for port in ports:
789 if port.port_no == port_no:
790 return port.type
791 return None
792
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400793 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000794 self.log.info('adding-logical-port', port_no=port_no)
795
796 label = self.port_name(port_no, Port.ETHERNET_NNI)
797
798 cap = OFPPF_1GB_FD | OFPPF_FIBER
799 curr_speed = OFPPF_1GB_FD
800 max_speed = OFPPF_1GB_FD
801
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400802 if oper_state == OperStatus.ACTIVE:
803 of_oper_state = OFPPS_LIVE
804 else:
805 of_oper_state = OFPPS_LINK_DOWN
806
Shad Ansarif9d2d102018-06-13 02:15:26 +0000807 ofp = ofp_port(
808 port_no=port_no,
809 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
810 name=label, config=0, state=of_oper_state, curr=cap,
811 advertised=cap, peer=cap, curr_speed=curr_speed,
812 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000813
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400814 ofp_stats = ofp_port_stats(port_no=port_no)
815
Shad Ansarif9d2d102018-06-13 02:15:26 +0000816 logical_port = LogicalPort(
817 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400818 device_port_no=port_no, root_port=True,
819 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000820
Shad Ansarif9d2d102018-06-13 02:15:26 +0000821 self.adapter_agent.add_logical_port(self.logical_device_id,
822 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000823
824 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000825 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000826
Shad Ansari4a232ca2018-05-05 05:24:17 +0000827 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000828
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400829 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000830 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000831
832 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000833 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000834
Shad Ansari2825d012018-02-22 23:57:46 +0000835 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000836
Shad Ansari2825d012018-02-22 23:57:46 +0000837 return port_no, label
838
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400839 def delete_logical_port(self, child_device_id):
840 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
841 self.logical_device_id))
842 for logical_port in logical_ports:
843 if logical_port.device_id == child_device_id:
844 self.log.debug('delete-logical-port',
845 onu_device_id=child_device_id,
846 logical_port=logical_port)
847 self.adapter_agent.delete_logical_port(
848 self.logical_device_id, logical_port)
849 return
850 def delete_port(self, child_serial_number):
851 ports = self.proxy.get('/devices/{}/ports'.format(
852 self.device_id))
853 for port in ports:
854 if port.label == child_serial_number:
855 self.log.debug('delete-port',
856 onu_serial_number=child_serial_number,
857 port=port)
858 self.adapter_agent.delete_port(self.device_id, port)
859 return
860
861
Shad Ansari2825d012018-02-22 23:57:46 +0000862 def new_onu_id(self, intf_id):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400863 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400864 pon_onu_ids = [onu_device.proxy_address.onu_id for onu_device in
865 onu_devices if
866 onu_device.proxy_address.channel_id == intf_id]
867 for i in range(1, platform.MAX_ONUS_PER_PON):
868 if i not in pon_onu_ids:
869 return i
870
871 self.log.error('All available onu_ids taken on this pon',
872 intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
873 return None
Shad Ansari2825d012018-02-22 23:57:46 +0000874
875 def stringify_vendor_specific(self, vendor_specific):
876 return ''.join(str(i) for i in [
Shad Ansarif9d2d102018-06-13 02:15:26 +0000877 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000878 hex(ord(vendor_specific[0]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000879 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000880 hex(ord(vendor_specific[1]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000881 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000882 hex(ord(vendor_specific[2]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000883 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000884 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000885
Shad Ansari2825d012018-02-22 23:57:46 +0000886 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400887 self.log.debug('No updates here now, all is done in logical flows '
888 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000889
Shad Ansari2825d012018-02-22 23:57:46 +0000890
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400891 def update_logical_flows(self, flows_to_add, flows_to_remove,
892 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400893 if not self.is_state_up():
894 self.log.info('The OLT is not up, we cannot update flows',
895 flows_to_add=[f.id for f in flows_to_add],
896 flows_to_remove=[f.id for f in flows_to_remove])
897 return
898
Shad Ansari2825d012018-02-22 23:57:46 +0000899
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400900 self.log.debug('logical flows update', flows_to_add=flows_to_add,
901 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000902
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400903 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400904
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400905 try:
906 self.flow_mgr.add_flow(flow)
907 except grpc.RpcError as grpc_e:
908 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
909 self.log.warn('flow already exists', e=grpc_e,
910 flow=flow)
911 else:
912 self.log.error('failed to add flow', flow=flow,
913 grpc_error=grpc_e)
914 except Exception as e:
915 self.log.error('failed to add flow', flow=flow, e=e)
916
917 self.flow_mgr.update_children_flows(device_rules_map)
918
919 for flow in flows_to_remove:
920
921 try:
922 self.flow_mgr.remove_flow(flow)
923 except Exception as e:
924 self.log.error('failed to remove flow', flow=flow, e=e)
925
926 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000927 def ip_hex(self, ip):
928 octets = ip.split(".")
929 hex_ip = []
930 for octet in octets:
931 octet_hex = hex(int(octet))
932 octet_hex = octet_hex.split('0x')[1]
933 octet_hex = octet_hex.rjust(2, '0')
934 hex_ip.append(octet_hex)
935 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400936
937 def stringify_serial_number(self, serial_number):
938 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000939 self.stringify_vendor_specific(
940 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400941
942 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400943 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000944 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400945
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400946 try:
947 # Send grpc call
948 self.stub.DisableOlt(openolt_pb2.Empty())
949 # The resulting indication will bring the OLT down
950 # self.go_state_down()
951 self.log.info('openolt device disabled')
952 except Exception as e:
953 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400954
Jonathan Davis0f917a22018-05-30 14:39:45 -0400955
956 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400957 self.log.info('deleting-olt', device_id=self.device_id,
958 logical_device_id=self.logical_device_id)
959
960 try:
961 # Rebooting to reset the state
962 self.reboot()
963 # Removing logical device
964 self.proxy.remove('/logical_devices/{}'.
965 format(self.logical_device_id))
966 except Exception as e:
967 self.log.error('Failure to delete openolt device', error=e)
968 raise e
969 else:
970 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400971
Jonathan Davis0f917a22018-05-30 14:39:45 -0400972
973 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400974 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400975
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400976 try:
977 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400978
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400979 self.log.info('enabling-all-ports', device_id=self.device_id)
980 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400981 except Exception as e:
982 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400983 else:
984 self.log.info('openolt device reenabled')
985
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400986
Jonathan Davisb45bb372018-07-19 15:05:15 -0400987
988 def disable_child_device(self, child_device):
989 self.log.debug('sending-disable-onu',
990 olt_device_id=self.device_id,
991 onu_device=child_device,
992 onu_serial_number=child_device.serial_number)
993 vendor_id = child_device.vendor_id.encode('hex')
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000994 vendor_specific = child_device.serial_number.replace(
995 child_device.vendor_id, '').encode('hex')
996 serial_number = openolt_pb2.SerialNumber(
997 vendor_id=vendor_id, vendor_specific=vendor_specific)
Jonathan Davisb45bb372018-07-19 15:05:15 -0400998 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
999 onu_id=child_device.proxy_address.onu_id,
1000 serial_number=serial_number)
1001 self.stub.DeactivateOnu(onu)
1002
1003 def delete_child_device(self, child_device):
1004 self.log.debug('sending-deactivate-onu',
1005 olt_device_id=self.device_id,
1006 onu_device=child_device,
1007 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001008 try:
1009 self.adapter_agent.delete_child_device(self.device_id,
1010 child_device.id, child_device)
1011 except Exception as e:
1012 self.log.error('adapter_agent error', error=e)
1013 try:
1014 self.delete_logical_port(child_device.id)
1015 except Exception as e:
1016 self.log.error('logical_port delete error', error=e)
1017 try:
1018 self.delete_port(child_device.serial_number)
1019 except Exception as e:
1020 self.log.error('port delete error', error=e)
Jonathan Davisb45bb372018-07-19 15:05:15 -04001021 vendor_id = child_device.vendor_id.encode('hex')
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001022 vendor_specific = child_device.serial_number.replace(
1023 child_device.vendor_id, '').encode('hex')
1024 serial_number = openolt_pb2.SerialNumber(
1025 vendor_id=vendor_id, vendor_specific=vendor_specific)
Jonathan Davisb45bb372018-07-19 15:05:15 -04001026 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1027 onu_id=child_device.proxy_address.onu_id,
1028 serial_number=serial_number)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001029 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001030
1031 def reboot(self):
1032 self.log.debug('rebooting openolt device', device_id = self.device_id)
1033 try:
1034 self.stub.Reboot(openolt_pb2.Empty())
1035 except Exception as e:
1036 self.log.error('something went wrong with the reboot', error=e)
1037 else:
1038 self.log.info('device rebooted')
1039