blob: 25beae4a689d217338bbef4d648c32234af0ff14 [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(
664 self.device_id, onu_id=omci_indication.onu_id)
Shad Ansari0efa6512018-04-28 06:42:54 +0000665
666 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000667 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000668
Shad Ansari42db7342018-04-25 21:39:46 +0000669 def packet_indication(self, pkt_indication):
670
671 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000672 gemport_id=pkt_indication.gemport_id,
673 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000674
Shad Ansari22920932018-05-17 00:33:34 +0000675 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000676 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
677 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000678
679 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000680 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000681 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000682 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
683
684 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000685 pkt = Ether(msg)
686 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000687 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400688
689 # Find port type
690 egress_port_type = self.port_type(egress_port)
691
692 if egress_port_type == Port.ETHERNET_UNI:
693
694 if pkt.haslayer(Dot1Q):
695 outer_shim = pkt.getlayer(Dot1Q)
696 if isinstance(outer_shim.payload, Dot1Q):
697 # If double tag, remove the outer tag
698 payload = (
699 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
700 outer_shim.payload
701 )
702 else:
703 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000704 else:
705 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400706
707 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
708
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400709 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000710 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400711 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000712 onu_id=platform.onu_id_from_port_num(egress_port),
713 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400714
Shad Ansarif9d2d102018-06-13 02:15:26 +0000715 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400716 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000717 onu_id=platform.onu_id_from_port_num(egress_port),
718 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400719
720 self.stub.OnuPacketOut(onu_pkt)
721
722 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400723 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000724 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400725
726 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
727
Shad Ansarif9d2d102018-06-13 02:15:26 +0000728 uplink_pkt = openolt_pb2.UplinkPacket(
729 intf_id=platform.intf_id_from_nni_port_num(egress_port),
730 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400731
732 self.stub.UplinkPacketOut(uplink_pkt)
733
Shad Ansari0346f0d2018-04-26 06:54:09 +0000734 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000735 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
736 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400737 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000738
Shad Ansari2825d012018-02-22 23:57:46 +0000739 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000740 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
741 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000742 self.stub.OmciMsgOut(omci)
743
744 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000745 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000746 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000747
748 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000749 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000750 channel_id=intf_id, onu_id=onu_id,
751 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000752
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400753 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000754
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400755 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000756
Shad Ansarif9d2d102018-06-13 02:15:26 +0000757 self.adapter_agent.add_onu_device(
758 parent_device_id=self.device_id, parent_port_no=port_no,
759 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
760 root=True, serial_number=serial_number_str,
761 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000762
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000763 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000764 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400765 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000766 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000767 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000768 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400769 if serial_number is not None:
770 return serial_number
771 else:
772 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000773
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400774 def port_type(self, port_no):
775 ports = self.adapter_agent.get_ports(self.device_id)
776 for port in ports:
777 if port.port_no == port_no:
778 return port.type
779 return None
780
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400781 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000782 self.log.info('adding-logical-port', port_no=port_no)
783
784 label = self.port_name(port_no, Port.ETHERNET_NNI)
785
786 cap = OFPPF_1GB_FD | OFPPF_FIBER
787 curr_speed = OFPPF_1GB_FD
788 max_speed = OFPPF_1GB_FD
789
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400790 if oper_state == OperStatus.ACTIVE:
791 of_oper_state = OFPPS_LIVE
792 else:
793 of_oper_state = OFPPS_LINK_DOWN
794
Shad Ansarif9d2d102018-06-13 02:15:26 +0000795 ofp = ofp_port(
796 port_no=port_no,
797 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
798 name=label, config=0, state=of_oper_state, curr=cap,
799 advertised=cap, peer=cap, curr_speed=curr_speed,
800 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000801
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400802 ofp_stats = ofp_port_stats(port_no=port_no)
803
Shad Ansarif9d2d102018-06-13 02:15:26 +0000804 logical_port = LogicalPort(
805 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400806 device_port_no=port_no, root_port=True,
807 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000808
Shad Ansarif9d2d102018-06-13 02:15:26 +0000809 self.adapter_agent.add_logical_port(self.logical_device_id,
810 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000811
812 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000813 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000814
Shad Ansari4a232ca2018-05-05 05:24:17 +0000815 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000816
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400817 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000818 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000819
820 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000821 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000822
Shad Ansari2825d012018-02-22 23:57:46 +0000823 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000824
Shad Ansari2825d012018-02-22 23:57:46 +0000825 return port_no, label
826
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400827 def delete_logical_port(self, child_device_id):
828 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
829 self.logical_device_id))
830 for logical_port in logical_ports:
831 if logical_port.device_id == child_device_id:
832 self.log.debug('delete-logical-port',
833 onu_device_id=child_device_id,
834 logical_port=logical_port)
835 self.adapter_agent.delete_logical_port(
836 self.logical_device_id, logical_port)
837 return
838 def delete_port(self, child_serial_number):
839 ports = self.proxy.get('/devices/{}/ports'.format(
840 self.device_id))
841 for port in ports:
842 if port.label == child_serial_number:
843 self.log.debug('delete-port',
844 onu_serial_number=child_serial_number,
845 port=port)
846 self.adapter_agent.delete_port(self.device_id, port)
847 return
848
849
Shad Ansari2825d012018-02-22 23:57:46 +0000850 def new_onu_id(self, intf_id):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400851 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400852 pon_onu_ids = [onu_device.proxy_address.onu_id for onu_device in
853 onu_devices if
854 onu_device.proxy_address.channel_id == intf_id]
855 for i in range(1, platform.MAX_ONUS_PER_PON):
856 if i not in pon_onu_ids:
857 return i
858
859 self.log.error('All available onu_ids taken on this pon',
860 intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
861 return None
Shad Ansari2825d012018-02-22 23:57:46 +0000862
863 def stringify_vendor_specific(self, vendor_specific):
864 return ''.join(str(i) for i in [
Shad Ansarif9d2d102018-06-13 02:15:26 +0000865 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000866 hex(ord(vendor_specific[0]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000867 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000868 hex(ord(vendor_specific[1]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000869 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000870 hex(ord(vendor_specific[2]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000871 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000872 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000873
Shad Ansari2825d012018-02-22 23:57:46 +0000874 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400875 self.log.debug('No updates here now, all is done in logical flows '
876 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000877
Shad Ansari2825d012018-02-22 23:57:46 +0000878
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400879 def update_logical_flows(self, flows_to_add, flows_to_remove,
880 device_rules_map):
Shad Ansari2825d012018-02-22 23:57:46 +0000881
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400882 self.log.debug('logical flows update', flows_to_add=flows_to_add,
883 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000884
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400885 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400886
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400887 try:
888 self.flow_mgr.add_flow(flow)
889 except grpc.RpcError as grpc_e:
890 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
891 self.log.warn('flow already exists', e=grpc_e,
892 flow=flow)
893 else:
894 self.log.error('failed to add flow', flow=flow,
895 grpc_error=grpc_e)
896 except Exception as e:
897 self.log.error('failed to add flow', flow=flow, e=e)
898
899 self.flow_mgr.update_children_flows(device_rules_map)
900
901 for flow in flows_to_remove:
902
903 try:
904 self.flow_mgr.remove_flow(flow)
905 except Exception as e:
906 self.log.error('failed to remove flow', flow=flow, e=e)
907
908 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000909 def ip_hex(self, ip):
910 octets = ip.split(".")
911 hex_ip = []
912 for octet in octets:
913 octet_hex = hex(int(octet))
914 octet_hex = octet_hex.split('0x')[1]
915 octet_hex = octet_hex.rjust(2, '0')
916 hex_ip.append(octet_hex)
917 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400918
919 def stringify_serial_number(self, serial_number):
920 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000921 self.stringify_vendor_specific(
922 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400923
924 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400925 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000926 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400927
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400928 try:
929 # Send grpc call
930 self.stub.DisableOlt(openolt_pb2.Empty())
931 # The resulting indication will bring the OLT down
932 # self.go_state_down()
933 self.log.info('openolt device disabled')
934 except Exception as e:
935 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400936
Jonathan Davis0f917a22018-05-30 14:39:45 -0400937
938 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400939 self.log.info('deleting-olt', device_id=self.device_id,
940 logical_device_id=self.logical_device_id)
941
942 try:
943 # Rebooting to reset the state
944 self.reboot()
945 # Removing logical device
946 self.proxy.remove('/logical_devices/{}'.
947 format(self.logical_device_id))
948 except Exception as e:
949 self.log.error('Failure to delete openolt device', error=e)
950 raise e
951 else:
952 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400953
Jonathan Davis0f917a22018-05-30 14:39:45 -0400954
955 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400956 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400957
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400958 try:
959 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400960
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400961 self.log.info('enabling-all-ports', device_id=self.device_id)
962 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400963 except Exception as e:
964 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400965 else:
966 self.log.info('openolt device reenabled')
967
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400968
Jonathan Davisb45bb372018-07-19 15:05:15 -0400969
970 def disable_child_device(self, child_device):
971 self.log.debug('sending-disable-onu',
972 olt_device_id=self.device_id,
973 onu_device=child_device,
974 onu_serial_number=child_device.serial_number)
975 vendor_id = child_device.vendor_id.encode('hex')
Shad Ansari3cd9bf22018-07-25 19:29:39 +0000976 vendor_specific = child_device.serial_number.replace(
977 child_device.vendor_id, '').encode('hex')
978 serial_number = openolt_pb2.SerialNumber(
979 vendor_id=vendor_id, vendor_specific=vendor_specific)
Jonathan Davisb45bb372018-07-19 15:05:15 -0400980 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
981 onu_id=child_device.proxy_address.onu_id,
982 serial_number=serial_number)
983 self.stub.DeactivateOnu(onu)
984
985 def delete_child_device(self, child_device):
986 self.log.debug('sending-deactivate-onu',
987 olt_device_id=self.device_id,
988 onu_device=child_device,
989 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400990 try:
991 self.adapter_agent.delete_child_device(self.device_id,
992 child_device.id, child_device)
993 except Exception as e:
994 self.log.error('adapter_agent error', error=e)
995 try:
996 self.delete_logical_port(child_device.id)
997 except Exception as e:
998 self.log.error('logical_port delete error', error=e)
999 try:
1000 self.delete_port(child_device.serial_number)
1001 except Exception as e:
1002 self.log.error('port delete error', error=e)
Jonathan Davisb45bb372018-07-19 15:05:15 -04001003 vendor_id = child_device.vendor_id.encode('hex')
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001004 vendor_specific = child_device.serial_number.replace(
1005 child_device.vendor_id, '').encode('hex')
1006 serial_number = openolt_pb2.SerialNumber(
1007 vendor_id=vendor_id, vendor_specific=vendor_specific)
Jonathan Davisb45bb372018-07-19 15:05:15 -04001008 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1009 onu_id=child_device.proxy_address.onu_id,
1010 serial_number=serial_number)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001011 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001012
1013 def reboot(self):
1014 self.log.debug('rebooting openolt device', device_id = self.device_id)
1015 try:
1016 self.stub.Reboot(openolt_pb2.Empty())
1017 except Exception as e:
1018 self.log.error('something went wrong with the reboot', error=e)
1019 else:
1020 self.log.info('device rebooted')
1021