blob: 0fe20a86a6f74d85c1702972a4ad4c967751b034 [file] [log] [blame]
Shad Ansari2825d012018-02-22 23:57:46 +00001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Shad Ansari2825d012018-02-22 23:57:46 +000016import threading
Shad Ansari94250fc2018-07-04 06:52:11 +000017import binascii
Shad Ansari2825d012018-02-22 23:57:46 +000018import grpc
Matteo Scandolo00041762018-11-15 18:12:54 -080019import socket
20import re
Shad Ansari94250fc2018-07-04 06:52:11 +000021import structlog
Shad Ansari15928d12018-04-17 02:42:13 +000022from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000023from scapy.layers.l2 import Ether, Dot1Q
Shad Ansari3cd9bf22018-07-25 19:29:39 +000024from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000025
Shad Ansari2825d012018-02-22 23:57:46 +000026from voltha.protos.device_pb2 import Port, Device
27from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
28from voltha.protos.logical_device_pb2 import LogicalDevice
Shad Ansarif9d2d102018-06-13 02:15:26 +000029from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
30 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, \
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -040031 OFPC_TABLE_STATS, OFPC_FLOW_STATS, ofp_switch_features, ofp_port, \
Jonathan Hart05845412018-07-19 09:55:43 -070032 ofp_port_stats, ofp_desc
Shad Ansarif9d2d102018-06-13 02:15:26 +000033from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.core.logical_device_agent import mac_str_to_tuple
35from voltha.registry import registry
36from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040037from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
38from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
Shad Ansari2825d012018-02-22 23:57:46 +000039
mzadig384783a2018-08-09 08:52:40 -040040from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
Shad Ansarif9d2d102018-06-13 02:15:26 +000041
42
Shad Ansari2825d012018-02-22 23:57:46 +000043class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000044 """
45 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000046
Shad Ansari94250fc2018-07-04 06:52:11 +000047 null ----> init ------> connected -----> up -----> down
48 ^ ^ | ^ | |
49 | | | | | |
50 | +-------------+ +---------+ |
51 | |
52 +-----------------------------------------+
53 """
54 # pylint: disable=too-many-instance-attributes
55 # pylint: disable=R0904
56 states = [
57 'state_null',
58 'state_init',
59 'state_connected',
60 'state_up',
61 'state_down']
62
Shad Ansari22efe832018-05-19 05:37:03 +000063 transitions = [
Shad Ansari0e7ad962018-09-28 01:42:26 +000064 {'trigger': 'go_state_init',
65 'source': ['state_null', 'state_connected', 'state_down'],
66 'dest': 'state_init',
67 'before': 'do_state_init',
68 'after': 'post_init'},
69 {'trigger': 'go_state_connected',
70 'source': 'state_init',
71 'dest': 'state_connected',
72 'before': 'do_state_connected'},
73 {'trigger': 'go_state_up',
74 'source': ['state_connected', 'state_down'],
75 'dest': 'state_up',
76 'before': 'do_state_up'},
77 {'trigger': 'go_state_down',
78 'source': ['state_up'],
79 'dest': 'state_down',
80 'before': 'do_state_down',
81 'after': 'post_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000082
Shad Ansari2825d012018-02-22 23:57:46 +000083 def __init__(self, **kwargs):
84 super(OpenoltDevice, self).__init__()
85
86 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000087 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000088 device = kwargs['device']
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +090089 dpid = kwargs.get('dp_id')
Shad Ansaricd20a6d2018-10-02 14:36:33 +000090
91 self.platform_class = kwargs['support_classes']['platform']
Girish Gowdru1e77ea02018-09-24 09:10:35 -070092 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
Shad Ansaricd20a6d2018-10-02 14:36:33 +000093 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
94 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
95 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
96 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
97
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040098 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000099 self.device_id = device.id
100 self.host_and_port = device.host_and_port
Girish Gowdru141ced82018-09-17 20:19:14 -0700101 self.extra_args = device.extra_args
Shad Ansarif9d2d102018-06-13 02:15:26 +0000102 self.log = structlog.get_logger(id=self.device_id,
103 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400104 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +0000105
Matteo Scandolo00041762018-11-15 18:12:54 -0800106 self.log.info('openolt-device-init')
107
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400108 # Device already set in the event of reconciliation
109 if not is_reconciliation:
Matteo Scandolo00041762018-11-15 18:12:54 -0800110 self.log.info('updating-device')
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400111 # It is a new device
112 # Update device
113 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000114 device.serial_number = self.host_and_port # FIXME
Shad Ansari94250fc2018-07-04 06:52:11 +0000115 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400116 device.oper_status = OperStatus.ACTIVATING
117 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000118
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400119 # If logical device does not exist create it
Shad Ansari94250fc2018-07-04 06:52:11 +0000120 if not device.parent_id:
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900121 if dpid == None:
Matteo Scandolo00041762018-11-15 18:12:54 -0800122 uri = self.host_and_port.split(":")[0]
123 try:
124 socket.inet_pton(socket.AF_INET, uri)
125 dpid = '00:00:' + self.ip_hex(uri)
126 except socket.error:
127 # this is not an IP
128 dpid = self.stringToMacAddr(uri)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400129
Matteo Scandolo00041762018-11-15 18:12:54 -0800130 self.log.info('creating-openolt-logical-device', dp_id=dpid)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400131 # Create logical OF device
132 ld = LogicalDevice(
133 root_device_id=self.device_id,
134 switch_features=ofp_switch_features(
135 n_buffers=256, # TODO fake for now
136 n_tables=2, # TODO ditto
137 capabilities=( # TODO and ditto
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700138 OFPC_FLOW_STATS
139 | OFPC_TABLE_STATS
140 | OFPC_PORT_STATS
141 | OFPC_GROUP_STATS
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400142 )
Jonathan Hart05845412018-07-19 09:55:43 -0700143 ),
144 desc=ofp_desc(
145 serial_num=device.serial_number
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400146 )
147 )
148 ld_init = self.adapter_agent.create_logical_device(ld,
149 dpid=dpid)
150 self.logical_device_id = ld_init.id
Matteo Scandolo00041762018-11-15 18:12:54 -0800151
152 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400153 else:
154 # logical device already exists
155 self.logical_device_id = device.parent_id
156 if is_reconciliation:
157 self.adapter_agent.reconcile_logical_device(
158 self.logical_device_id)
159
Shad Ansari94250fc2018-07-04 06:52:11 +0000160 # Initialize the OLT state machine
161 self.machine = Machine(model=self, states=OpenoltDevice.states,
162 transitions=OpenoltDevice.transitions,
163 send_event=True, initial='state_null')
164 self.go_state_init()
165
Matteo Scandolo00041762018-11-15 18:12:54 -0800166 def stringToMacAddr(self, uri):
167 regex = re.compile('[^a-zA-Z]')
168 uri = regex.sub('', uri)
169
170 l = len(uri)
171 if l > 6:
172 uri = uri[0:6]
173 else:
174 uri = uri + uri[0:6 - l]
175
176 print uri
177
178 return ":".join([hex(ord(x))[-2:] for x in uri])
179
Shad Ansari94250fc2018-07-04 06:52:11 +0000180 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000181 # Initialize gRPC
182 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000183 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400184
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400185 self.log.info('openolt-device-created', device_id=self.device_id)
186
187 def post_init(self, event):
188 self.log.debug('post_init')
189
190 # We have reached init state, starting the indications thread
191
jasonhuang5f3e63b2018-07-27 01:32:48 +0800192 # Catch RuntimeError exception
193 try:
194 # Start indications thread
195 self.indications_thread_handle = threading.Thread(
196 target=self.indications_thread)
Shad Ansarif9521ad2018-09-08 10:46:43 +0000197 # Old getter/setter API for daemon; use it directly as a
jasonhuang5f3e63b2018-07-27 01:32:48 +0800198 # property instead. The Jinkins error will happon on the reason of
Shad Ansarif9521ad2018-09-08 10:46:43 +0000199 # Exception in thread Thread-1 (most likely raised # during
jasonhuang5f3e63b2018-07-27 01:32:48 +0800200 # interpreter shutdown)
201 self.indications_thread_handle.setDaemon(True)
202 self.indications_thread_handle.start()
Shad Ansarif9521ad2018-09-08 10:46:43 +0000203 except Exception as e:
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400204 self.log.exception('post_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000205
206 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400207 self.log.debug("do_state_connected")
208
Shad Ansari94250fc2018-07-04 06:52:11 +0000209 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000210
211 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400212
213 device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
214 self.log.info('Device connected', device_info=device_info)
215
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000216 self.platform = self.platform_class(self.log, device_info)
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700217 self.resource_mgr = self.resource_mgr_class(self.device_id,
Shad Ansari78de2be2018-10-12 22:13:54 +0000218 self.host_and_port,
219 self.extra_args,
220 device_info)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000221
222 self.flow_mgr = self.flow_mgr_class(self.log, self.stub,
223 self.device_id,
224 self.logical_device_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700225 self.platform,
226 self.resource_mgr)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000227 self.alarm_mgr = self.alarm_mgr_class(self.log, self.adapter_agent,
228 self.device_id,
229 self.logical_device_id,
230 self.platform)
231 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
232 self.bw_mgr = self.bw_mgr_class(self.log, self.proxy)
233
Nicolas Palpacuer33c2d3d2018-09-06 15:01:14 -0400234 device.vendor = device_info.vendor
235 device.model = device_info.model
236 device.hardware_version = device_info.hardware_version
237 device.firmware_version = device_info.firmware_version
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700238
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700239 # TODO: check for uptime and reboot if too long (VOL-1192)
240
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400241 device.connect_status = ConnectStatus.REACHABLE
242 self.adapter_agent.update_device(device)
243
Shad Ansari94250fc2018-07-04 06:52:11 +0000244 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400245 self.log.debug("do_state_up")
246
Shad Ansari94250fc2018-07-04 06:52:11 +0000247 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000248
Shad Ansari94250fc2018-07-04 06:52:11 +0000249 # Update phys OF device
250 device.parent_id = self.logical_device_id
251 device.oper_status = OperStatus.ACTIVE
252 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400253
Shad Ansari94250fc2018-07-04 06:52:11 +0000254 def do_state_down(self, event):
255 self.log.debug("do_state_down")
256 oper_state = OperStatus.UNKNOWN
257 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400258
Shad Ansari94250fc2018-07-04 06:52:11 +0000259 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400260
Shad Ansari94250fc2018-07-04 06:52:11 +0000261 # Children ports
262 child_devices = self.adapter_agent.get_child_devices(self.device_id)
263 for onu_device in child_devices:
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000264 uni_no = self.platform.mk_uni_port_num(
Shad Ansari94250fc2018-07-04 06:52:11 +0000265 onu_device.proxy_address.channel_id,
266 onu_device.proxy_address.onu_id)
267 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
268 serial_number=onu_device.serial_number)
Shad Ansari0e7ad962018-09-28 01:42:26 +0000269 onu_adapter_agent = \
270 registry('adapter_loader').get_agent(onu_device.adapter)
271 onu_adapter_agent.update_interface(onu_device,
272 {'oper_state': 'down'})
Shad Ansari94250fc2018-07-04 06:52:11 +0000273 self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
274 # Children devices
275 self.adapter_agent.update_child_devices_state(
276 self.device_id, oper_status=oper_state,
277 connect_status=connect_state)
278 # Device Ports
279 device_ports = self.adapter_agent.get_ports(self.device_id,
280 Port.ETHERNET_NNI)
281 logical_ports_ids = [port.label for port in device_ports]
282 device_ports += self.adapter_agent.get_ports(self.device_id,
283 Port.PON_OLT)
284
285 for port in device_ports:
286 port.oper_status = oper_state
287 self.adapter_agent.add_port(self.device_id, port)
288
289 # Device logical port
290 for logical_port_id in logical_ports_ids:
291 logical_port = self.adapter_agent.get_logical_port(
292 self.logical_device_id, logical_port_id)
293 logical_port.ofp_port.state = OFPPS_LINK_DOWN
294 self.adapter_agent.update_logical_port(self.logical_device_id,
295 logical_port)
296
297 # Device
298 device = self.adapter_agent.get_device(self.device_id)
299 device.oper_status = oper_state
300 device.connect_status = connect_state
301
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400302 reactor.callLater(2, self.adapter_agent.update_device, device)
303
304 # def post_up(self, event):
305 # self.log.debug('post-up')
306 # self.flow_mgr.reseed_flows()
307
308 def post_down(self, event):
309 self.log.debug('post_down')
310 self.flow_mgr.reset_flows()
311
Shad Ansari94250fc2018-07-04 06:52:11 +0000312 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400313 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000314 self.log.debug('connecting to olt', device_id=self.device_id)
315 self.channel_ready_future.result() # blocking call
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400316 self.log.info('connected to olt', device_id=self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000317 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000318
Shad Ansari15928d12018-04-17 02:42:13 +0000319 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000320
Shad Ansari94250fc2018-07-04 06:52:11 +0000321 while True:
nick47b74372018-05-25 18:22:49 -0400322 try:
323 # get the next indication from olt
324 ind = next(self.indications)
325 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000326 self.log.warn('gRPC connection lost', error=e)
327 reactor.callFromThread(self.go_state_down)
328 reactor.callFromThread(self.go_state_init)
329 break
nick47b74372018-05-25 18:22:49 -0400330 else:
331 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000332
nick47b74372018-05-25 18:22:49 -0400333 # indication handlers run in the main event loop
334 if ind.HasField('olt_ind'):
335 reactor.callFromThread(self.olt_indication, ind.olt_ind)
336 elif ind.HasField('intf_ind'):
337 reactor.callFromThread(self.intf_indication, ind.intf_ind)
338 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000339 reactor.callFromThread(self.intf_oper_indication,
340 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400341 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000342 reactor.callFromThread(self.onu_discovery_indication,
343 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400344 elif ind.HasField('onu_ind'):
345 reactor.callFromThread(self.onu_indication, ind.onu_ind)
346 elif ind.HasField('omci_ind'):
347 reactor.callFromThread(self.omci_indication, ind.omci_ind)
348 elif ind.HasField('pkt_ind'):
349 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400350 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400351 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700352 self.stats_mgr.port_statistics_indication,
353 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400354 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400355 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700356 self.stats_mgr.flow_statistics_indication,
357 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000358 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400359 reactor.callFromThread(self.alarm_mgr.process_alarms,
360 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400361 else:
362 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400363
Shad Ansari2825d012018-02-22 23:57:46 +0000364 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000365 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000366 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000367 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000368 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000369
370 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400371 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000372 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000373
374 if intf_indication.oper_state == "up":
375 oper_status = OperStatus.ACTIVE
376 else:
377 oper_status = OperStatus.DISCOVERED
378
nick47b74372018-05-25 18:22:49 -0400379 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000380 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
381
382 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000383 self.log.debug("Received interface oper state change indication",
384 intf_id=intf_oper_indication.intf_id,
385 type=intf_oper_indication.type,
386 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000387
388 if intf_oper_indication.oper_state == "up":
389 oper_state = OperStatus.ACTIVE
390 else:
391 oper_state = OperStatus.DISCOVERED
392
393 if intf_oper_indication.type == "nni":
394
Shad Ansari0346f0d2018-04-26 06:54:09 +0000395 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000396 if intf_oper_indication.intf_id != 0:
397 return
398
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400399 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000400 port_no, label = self.add_port(intf_oper_indication.intf_id,
401 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400402 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000403 self.add_logical_port(port_no, intf_oper_indication.intf_id,
404 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000405
406 elif intf_oper_indication.type == "pon":
407 # FIXME - handle PON oper state change
408 pass
409
410 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000411 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000412 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000413
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400414 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000415
Shad Ansarif9d2d102018-06-13 02:15:26 +0000416 self.log.debug("onu discovery indication", intf_id=intf_id,
417 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400418
mzadig384783a2018-08-09 08:52:40 -0400419 # Post ONU Discover alarm 20180809_0805
420 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400421 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
422 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400423 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400424 self.log.exception("onu-discovery-alarm-error",
425 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400426 # continue for now.
427
Shad Ansarif9d2d102018-06-13 02:15:26 +0000428 onu_device = self.adapter_agent.get_child_device(
429 self.device_id,
430 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400431
432 if onu_device is None:
Shad Ansari15928d12018-04-17 02:42:13 +0000433 try:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700434 onu_id = self.resource_mgr.get_onu_id(intf_id)
435 if onu_id is None:
436 raise Exception("onu-id-unavailable")
437
438 pon_intf_onu_id = (intf_id, onu_id)
439 alloc_id = self.resource_mgr.get_alloc_id(
440 pon_intf_onu_id)
441 if alloc_id is None:
442 # Free up other PON resources if are unable to
443 # proceed ahead
444 self.resource_mgr.free_onu_id(intf_id, onu_id)
445 raise Exception("alloc-id-unavailable")
446
Shad Ansarif9d2d102018-06-13 02:15:26 +0000447 self.add_onu_device(
448 intf_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000449 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000450 onu_id, serial_number)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400451 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700452 serial_number_str, alloc_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400453 except Exception as e:
454 self.log.exception('onu-activation-failed', e=e)
455
Shad Ansari2825d012018-02-22 23:57:46 +0000456 else:
nick47b74372018-05-25 18:22:49 -0400457 if onu_device.connect_status != ConnectStatus.REACHABLE:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700458 onu_device.connect_status = ConnectStatus.REACHABLE
459 self.adapter_agent.update_device(onu_device)
nick47b74372018-05-25 18:22:49 -0400460
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400461 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000462 if onu_device.oper_status == OperStatus.DISCOVERED \
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700463 or onu_device.oper_status == OperStatus.ACTIVATING:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000464 self.log.debug("ignore onu discovery indication, \
465 the onu has been discovered and should be \
466 activating shorlty", intf_id=intf_id,
467 onu_id=onu_id, state=onu_device.oper_status)
468 elif onu_device.oper_status == OperStatus.ACTIVE:
469 self.log.warn("onu discovery indication whereas onu is \
470 supposed to be active",
471 intf_id=intf_id, onu_id=onu_id,
472 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400473 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000474 self.log.info("onu in unknown state, recovering from olt \
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400475 reboot probably, activate onu", intf_id=intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000476 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400477
478 onu_device.oper_status = OperStatus.DISCOVERED
479 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400480 try:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700481 pon_intf_onu_id = (intf_id, onu_id)
482 # The ONU is already in the VOLTHA DB and resources were
483 # already allocated for this ONU. So we fetch the resource
484 # from local cache and not KV store.
485 alloc_id = self.resource_mgr.get_alloc_id(
486 pon_intf_onu_id)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400487 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700488 serial_number_str, alloc_id)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400489 except Exception as e:
490 self.log.error('onu-activation-error',
491 serial_number=serial_number_str, error=e)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400492 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000493 self.log.warn('unexpected state', onu_id=onu_id,
494 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000495
Shad Ansari2825d012018-02-22 23:57:46 +0000496 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000497 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000498 onu_id=onu_indication.onu_id,
499 serial_number=onu_indication.serial_number,
500 oper_state=onu_indication.oper_state,
501 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400502 try:
503 serial_number_str = self.stringify_serial_number(
504 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000505 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400506 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000507
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400508 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400509 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700510 self.device_id,
511 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000512 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400513 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700514 self.device_id,
515 parent_port_no=self.platform.intf_id_to_port_no(
516 onu_indication.intf_id, Port.PON_OLT),
517 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400518
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400519 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000520 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000521 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000522 return
523
Shad Ansari78de2be2018-10-12 22:13:54 +0000524 # We will use this alloc_id, gemport_id to pass on to the onu adapter
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700525 pon_intf_onu_id = (onu_indication.intf_id, onu_indication.onu_id)
526 alloc_id = self.resource_mgr.get_alloc_id(pon_intf_onu_id)
527 gemport_id = self.resource_mgr.get_gemport_id(pon_intf_onu_id)
528
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000529 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
Shad Ansarif9521ad2018-09-08 10:46:43 +0000530 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400531 self.log.warn('ONU-is-on-a-different-intf-id-now',
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900532 previous_intf_id=self.platform.intf_id_from_pon_port_no(
533 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400534 current_intf_id=onu_indication.intf_id)
535 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000536
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400537 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
538 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400539 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
540 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000541 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400542 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000543
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000544 uni_no = self.platform.mk_uni_port_num(onu_indication.intf_id,
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900545 onu_indication.onu_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000546 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
547 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000548
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400549 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000550
Shad Ansarif9d2d102018-06-13 02:15:26 +0000551 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400552 if onu_indication.admin_state == 'down':
553 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000554 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
555 oper_state=onu_indication.oper_state)
556 # Forcing the oper state change code to execute
557 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400558
Shad Ansarif9d2d102018-06-13 02:15:26 +0000559 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400560
561 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400562 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400563
564 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000565 self.log.warn('Invalid-or-not-implemented-admin-state',
566 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400567
568 self.log.debug('admin-state-dealt-with')
569
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400570 onu_adapter_agent = \
571 registry('adapter_loader').get_agent(onu_device.adapter)
572 if onu_adapter_agent is None:
573 self.log.error('onu_adapter_agent-could-not-be-retrieved',
574 onu_device=onu_device)
575 return
576
Shad Ansarif9d2d102018-06-13 02:15:26 +0000577 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400578 if onu_indication.oper_state == 'down':
Nicolas Palpacuer4ea3a652018-08-22 10:33:17 -0400579
580 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
581 onu_device.connect_status = ConnectStatus.UNREACHABLE
582 self.adapter_agent.update_device(onu_device)
583
Shad Ansarif9d2d102018-06-13 02:15:26 +0000584 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400585 self.log.debug('onu-oper-state-is-down')
586
587 if onu_device.oper_status != OperStatus.DISCOVERED:
588 onu_device.oper_status = OperStatus.DISCOVERED
589 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000590 # Set port oper state to Discovered
591 self.onu_ports_down(onu_device, uni_no, uni_name,
592 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400593
Shad Ansari0e7ad962018-09-28 01:42:26 +0000594 onu_adapter_agent.update_interface(onu_device,
595 {'oper_state': 'down'})
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400596
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400597 elif onu_indication.oper_state == 'up':
598
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400599 if onu_device.connect_status != ConnectStatus.REACHABLE:
600 onu_device.connect_status = ConnectStatus.REACHABLE
601 self.adapter_agent.update_device(onu_device)
602
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400603 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000604 self.log.debug("ignore onu indication",
605 intf_id=onu_indication.intf_id,
606 onu_id=onu_indication.onu_id,
607 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400608 msg_oper_state=onu_indication.oper_state)
609 return
610
Shad Ansarif9d2d102018-06-13 02:15:26 +0000611 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400612
Shad Ansarif9d2d102018-06-13 02:15:26 +0000613 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400614
Shad Ansari0e7ad962018-09-28 01:42:26 +0000615 # tcont creation (onu)
616 tcont = TcontsConfigData()
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700617 tcont.alloc_id = alloc_id
Matt Jeannerete6a70332018-07-20 16:11:25 -0400618
Shad Ansari0e7ad962018-09-28 01:42:26 +0000619 # gem port creation
620 gem_port = GemportsConfigData()
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700621 gem_port.gemport_id = gemport_id
Matt Jeannerete6a70332018-07-20 16:11:25 -0400622
Shad Ansari0e7ad962018-09-28 01:42:26 +0000623 gem_port.tcont_ref = str(tcont.alloc_id)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400624
Shad Ansari0e7ad962018-09-28 01:42:26 +0000625 self.log.info('inject-tcont-gem-data-onu-handler',
626 onu_indication=onu_indication, tcont=tcont,
627 gem_port=gem_port)
Girish Gowdru141ced82018-09-17 20:19:14 -0700628
Shad Ansari0e7ad962018-09-28 01:42:26 +0000629 onu_adapter_agent.create_tcont(onu_device, tcont,
630 traffic_descriptor_data=None)
631 onu_adapter_agent.create_gemport(onu_device, gem_port)
632 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400633
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400634 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000635 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
636 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000637
nick47b74372018-05-25 18:22:49 -0400638 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
639 # Set port oper state to Discovered
640 # add port will update port if it exists
641 self.adapter_agent.add_port(
642 self.device_id,
643 Port(
644 port_no=uni_no,
645 label=uni_name,
646 type=Port.ETHERNET_UNI,
647 admin_state=onu_device.admin_state,
648 oper_status=oper_state))
649
650 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400651 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
652 onu_port_id = None
653 for onu_port in onu_ports:
654 if onu_port.port_no == uni_no:
655 onu_port_id = onu_port.label
656 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000657 self.log.error('matching-onu-port-label-not-found',
658 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400659 onu_ports=onu_ports)
660 return
661 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000662 onu_logical_port = self.adapter_agent.get_logical_port(
663 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400664 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000665 self.adapter_agent.update_logical_port(
666 logical_device_id=self.logical_device_id,
667 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400668 self.log.debug('cascading-oper-state-to-port-and-logical-port')
669 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000670 self.log.error('matching-onu-port-label-invalid',
671 onu_id=onu_device.id, olt_id=self.device_id,
672 onu_ports=onu_ports, onu_port_id=onu_port_id,
673 error=e)
nick47b74372018-05-25 18:22:49 -0400674
Shad Ansari2825d012018-02-22 23:57:46 +0000675 def omci_indication(self, omci_indication):
676
677 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000678 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000679
Shad Ansarif9d2d102018-06-13 02:15:26 +0000680 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400681 self.device_id, onu_id=omci_indication.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000682 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700683 omci_indication.intf_id, Port.PON_OLT), )
Shad Ansari0efa6512018-04-28 06:42:54 +0000684
685 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000686 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000687
Shad Ansari42db7342018-04-25 21:39:46 +0000688 def packet_indication(self, pkt_indication):
689
Shad Ansari0ff82622018-09-30 09:32:04 +0000690 self.log.debug("packet indication",
691 intf_type=pkt_indication.intf_type,
692 intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000693 gemport_id=pkt_indication.gemport_id,
694 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000695
Shad Ansari0ff82622018-09-30 09:32:04 +0000696 if pkt_indication.intf_type == "pon":
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900697 pon_intf_gemport = (pkt_indication.intf_id, pkt_indication.gemport_id)
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700698 try:
699 onu_id = int(self.resource_mgr.kv_store[pon_intf_gemport])
700 if onu_id is None:
701 raise Exception("onu-id-none")
702 except Exception as e:
703 self.log.error("no-onu-reference-for-gem",
704 gemport_id=pkt_indication.gemport_id, e=e)
705 return
706
Keita NISHIMOTO11bb10d2018-10-11 07:17:35 +0900707 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id,
708 onu_id)
Shad Ansari0ff82622018-09-30 09:32:04 +0000709 elif pkt_indication.intf_type == "nni":
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000710 logical_port_num = self.platform.intf_id_to_port_no(
Shad Ansari0ff82622018-09-30 09:32:04 +0000711 pkt_indication.intf_id,
712 Port.ETHERNET_NNI)
Shad Ansari42db7342018-04-25 21:39:46 +0000713
714 pkt = Ether(pkt_indication.pkt)
Shad Ansari0ff82622018-09-30 09:32:04 +0000715
716 self.log.debug("packet indication",
717 logical_device_id=self.logical_device_id,
718 logical_port_no=logical_port_num)
719
720 self.adapter_agent.send_packet_in(
721 logical_device_id=self.logical_device_id,
722 logical_port_no=logical_port_num,
723 packet=str(pkt))
Shad Ansari42db7342018-04-25 21:39:46 +0000724
725 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000726 pkt = Ether(msg)
Saurav Dasf87d6552018-09-26 17:05:42 -0700727 self.log.debug('packet out', egress_port=egress_port,
728 device_id=self.device_id,
729 logical_device_id=self.logical_device_id,
730 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400731
732 # Find port type
733 egress_port_type = self.port_type(egress_port)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400734 if egress_port_type == Port.ETHERNET_UNI:
735
736 if pkt.haslayer(Dot1Q):
737 outer_shim = pkt.getlayer(Dot1Q)
738 if isinstance(outer_shim.payload, Dot1Q):
739 # If double tag, remove the outer tag
740 payload = (
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700741 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
742 outer_shim.payload
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400743 )
744 else:
745 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000746 else:
747 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400748
749 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
750
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400751 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000752 'sending-packet-to-ONU', egress_port=egress_port,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000753 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
754 onu_id=self.platform.onu_id_from_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000755 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400756
Shad Ansarif9d2d102018-06-13 02:15:26 +0000757 onu_pkt = openolt_pb2.OnuPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000758 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
759 onu_id=self.platform.onu_id_from_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000760 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400761
762 self.stub.OnuPacketOut(onu_pkt)
763
764 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400765 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000766 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400767
768 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
769
Shad Ansarif9d2d102018-06-13 02:15:26 +0000770 uplink_pkt = openolt_pb2.UplinkPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000771 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000772 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400773
774 self.stub.UplinkPacketOut(uplink_pkt)
775
Shad Ansari0346f0d2018-04-26 06:54:09 +0000776 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000777 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
778 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400779 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000780
Shad Ansari2825d012018-02-22 23:57:46 +0000781 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9521ad2018-09-08 10:46:43 +0000782 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru141ced82018-09-17 20:19:14 -0700783 self.device_id, onu_id=proxy_address.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000784 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700785 proxy_address.channel_id, Port.PON_OLT)
786 )
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400787 if onu_device.connect_status != ConnectStatus.REACHABLE:
788 self.log.debug('ONU is not reachable, cannot send OMCI',
789 serial_number=onu_device.serial_number,
790 intf_id=onu_device.proxy_address.channel_id,
791 onu_id=onu_device.proxy_address.onu_id)
792 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000793 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
794 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000795 self.stub.OmciMsgOut(omci)
796
797 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000798 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000799 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000800
801 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000802 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000803 channel_id=intf_id, onu_id=onu_id,
804 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000805
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400806 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000807
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400808 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000809
Shad Ansarif9d2d102018-06-13 02:15:26 +0000810 self.adapter_agent.add_onu_device(
811 parent_device_id=self.device_id, parent_port_no=port_no,
812 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
813 root=True, serial_number=serial_number_str,
814 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000815
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000816 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000817 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400818 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000819 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000820 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000821 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400822 if serial_number is not None:
823 return serial_number
824 else:
825 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000826
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400827 def port_type(self, port_no):
828 ports = self.adapter_agent.get_ports(self.device_id)
829 for port in ports:
830 if port.port_no == port_no:
831 return port.type
832 return None
833
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400834 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000835 self.log.info('adding-logical-port', port_no=port_no)
836
837 label = self.port_name(port_no, Port.ETHERNET_NNI)
838
839 cap = OFPPF_1GB_FD | OFPPF_FIBER
840 curr_speed = OFPPF_1GB_FD
841 max_speed = OFPPF_1GB_FD
842
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400843 if oper_state == OperStatus.ACTIVE:
844 of_oper_state = OFPPS_LIVE
845 else:
846 of_oper_state = OFPPS_LINK_DOWN
847
Shad Ansarif9d2d102018-06-13 02:15:26 +0000848 ofp = ofp_port(
849 port_no=port_no,
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400850 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000851 name=label, config=0, state=of_oper_state, curr=cap,
852 advertised=cap, peer=cap, curr_speed=curr_speed,
853 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000854
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400855 ofp_stats = ofp_port_stats(port_no=port_no)
856
Shad Ansarif9d2d102018-06-13 02:15:26 +0000857 logical_port = LogicalPort(
858 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400859 device_port_no=port_no, root_port=True,
860 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000861
Shad Ansarif9d2d102018-06-13 02:15:26 +0000862 self.adapter_agent.add_logical_port(self.logical_device_id,
863 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000864
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400865 def _get_mac_form_port_no(self, port_no):
866 mac = ''
867 for i in range(4):
868 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
869 return '00:00' + mac
870
Shad Ansari2825d012018-02-22 23:57:46 +0000871 def add_port(self, intf_id, port_type, oper_status):
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000872 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000873
Shad Ansari4a232ca2018-05-05 05:24:17 +0000874 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000875
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400876 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000877 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000878
879 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000880 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000881
Shad Ansari2825d012018-02-22 23:57:46 +0000882 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000883
Shad Ansari2825d012018-02-22 23:57:46 +0000884 return port_no, label
885
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400886 def delete_logical_port(self, child_device_id):
887 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
888 self.logical_device_id))
889 for logical_port in logical_ports:
890 if logical_port.device_id == child_device_id:
891 self.log.debug('delete-logical-port',
892 onu_device_id=child_device_id,
893 logical_port=logical_port)
894 self.adapter_agent.delete_logical_port(
895 self.logical_device_id, logical_port)
896 return
Shad Ansarif9521ad2018-09-08 10:46:43 +0000897
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400898 def delete_port(self, child_serial_number):
899 ports = self.proxy.get('/devices/{}/ports'.format(
900 self.device_id))
901 for port in ports:
902 if port.label == child_serial_number:
903 self.log.debug('delete-port',
904 onu_serial_number=child_serial_number,
905 port=port)
906 self.adapter_agent.delete_port(self.device_id, port)
907 return
908
Shad Ansari2825d012018-02-22 23:57:46 +0000909 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400910 self.log.debug('No updates here now, all is done in logical flows '
911 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000912
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400913 def update_logical_flows(self, flows_to_add, flows_to_remove,
914 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400915 if not self.is_state_up():
916 self.log.info('The OLT is not up, we cannot update flows',
917 flows_to_add=[f.id for f in flows_to_add],
918 flows_to_remove=[f.id for f in flows_to_remove])
919 return
920
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400921 try:
922 self.flow_mgr.update_children_flows(device_rules_map)
923 except Exception as e:
924 self.log.error('Error updating children flows', error=e)
Shad Ansari2825d012018-02-22 23:57:46 +0000925
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400926 self.log.debug('logical flows update', flows_to_add=flows_to_add,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000927 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000928
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400929 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400930
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400931 try:
932 self.flow_mgr.add_flow(flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400933 except Exception as e:
934 self.log.error('failed to add flow', flow=flow, e=e)
935
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400936 for flow in flows_to_remove:
937
938 try:
939 self.flow_mgr.remove_flow(flow)
940 except Exception as e:
941 self.log.error('failed to remove flow', flow=flow, e=e)
942
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400943 self.flow_mgr.repush_all_different_flows()
944
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400945 # There has to be a better way to do this
Shad Ansari89b09d52018-05-21 07:28:14 +0000946 def ip_hex(self, ip):
947 octets = ip.split(".")
948 hex_ip = []
949 for octet in octets:
950 octet_hex = hex(int(octet))
951 octet_hex = octet_hex.split('0x')[1]
952 octet_hex = octet_hex.rjust(2, '0')
953 hex_ip.append(octet_hex)
954 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400955
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400956 def stringify_vendor_specific(self, vendor_specific):
957 return ''.join(str(i) for i in [
958 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
959 hex(ord(vendor_specific[0]) & 0x0f)[2:],
960 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
961 hex(ord(vendor_specific[1]) & 0x0f)[2:],
962 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
963 hex(ord(vendor_specific[2]) & 0x0f)[2:],
964 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
965 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
966
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400967 def stringify_serial_number(self, serial_number):
968 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000969 self.stringify_vendor_specific(
970 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400971
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400972 def destringify_serial_number(self, serial_number_str):
973 serial_number = openolt_pb2.SerialNumber(
974 vendor_id=serial_number_str[:4].encode('utf-8'),
975 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
976 return serial_number
977
Jonathan Davis0f917a22018-05-30 14:39:45 -0400978 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400979 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9521ad2018-09-08 10:46:43 +0000980 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400981
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400982 try:
983 # Send grpc call
984 self.stub.DisableOlt(openolt_pb2.Empty())
985 # The resulting indication will bring the OLT down
986 # self.go_state_down()
987 self.log.info('openolt device disabled')
988 except Exception as e:
989 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400990
Jonathan Davis0f917a22018-05-30 14:39:45 -0400991 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400992 self.log.info('deleting-olt', device_id=self.device_id,
993 logical_device_id=self.logical_device_id)
994
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700995 # Clears up the data from the resource manager KV store
996 # for the device
997 del self.resource_mgr
998
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400999 try:
1000 # Rebooting to reset the state
1001 self.reboot()
1002 # Removing logical device
Saurav Dasf87d6552018-09-26 17:05:42 -07001003 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
1004 self.adapter_agent.delete_logical_device(ld)
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -04001005 except Exception as e:
1006 self.log.error('Failure to delete openolt device', error=e)
1007 raise e
1008 else:
1009 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -04001010
Jonathan Davis0f917a22018-05-30 14:39:45 -04001011 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001012 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -04001013
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001014 try:
1015 self.stub.ReenableOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -04001016
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001017 self.log.info('enabling-all-ports', device_id=self.device_id)
1018 self.adapter_agent.enable_all_ports(self.device_id)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001019 except Exception as e:
1020 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -04001021 else:
1022 self.log.info('openolt device reenabled')
1023
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001024 def activate_onu(self, intf_id, onu_id, serial_number,
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001025 serial_number_str, alloc_id):
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001026 pir = self.bw_mgr.pir(serial_number_str)
1027 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +00001028 serial_number_str=serial_number_str,
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001029 serial_number=serial_number, pir=pir,
1030 alloc_id=alloc_id)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001031 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001032 serial_number=serial_number, pir=pir,
1033 alloc_id=alloc_id)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -04001034 self.stub.ActivateOnu(onu)
1035 self.log.info('onu-activated', serial_number=serial_number_str)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -04001036
Jonathan Davisb45bb372018-07-19 15:05:15 -04001037 def delete_child_device(self, child_device):
1038 self.log.debug('sending-deactivate-onu',
1039 olt_device_id=self.device_id,
1040 onu_device=child_device,
1041 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001042 try:
1043 self.adapter_agent.delete_child_device(self.device_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +00001044 child_device.id,
1045 child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -04001046 except Exception as e:
1047 self.log.error('adapter_agent error', error=e)
1048 try:
1049 self.delete_logical_port(child_device.id)
1050 except Exception as e:
1051 self.log.error('logical_port delete error', error=e)
1052 try:
1053 self.delete_port(child_device.serial_number)
1054 except Exception as e:
1055 self.log.error('port delete error', error=e)
Shad Ansarif9521ad2018-09-08 10:46:43 +00001056 serial_number = self.destringify_serial_number(
1057 child_device.serial_number)
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001058 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1059 child_device.proxy_address.onu_id)
1060 alloc_id = self.resource_mgr.get_alloc_id(pon_intf_id_onu_id)
1061 # Free any PON resources that were reserved for the ONU
1062 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1063
Jonathan Davisb45bb372018-07-19 15:05:15 -04001064 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1065 onu_id=child_device.proxy_address.onu_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -07001066 serial_number=serial_number,
1067 alloc_id=alloc_id)
Shad Ansari3cd9bf22018-07-25 19:29:39 +00001068 self.stub.DeleteOnu(onu)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001069
1070 def reboot(self):
Shad Ansarif9521ad2018-09-08 10:46:43 +00001071 self.log.debug('rebooting openolt device', device_id=self.device_id)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -04001072 try:
1073 self.stub.Reboot(openolt_pb2.Empty())
1074 except Exception as e:
1075 self.log.error('something went wrong with the reboot', error=e)
1076 else:
1077 self.log.info('device rebooted')
1078
Nicolas Palpacuer30027f42018-09-06 15:55:54 -04001079 def trigger_statistics_collection(self):
1080 try:
1081 self.stub.CollectStatistics(openolt_pb2.Empty())
1082 except Exception as e:
1083 self.log.error('Error while triggering statistics collection',
1084 error=e)
1085 else:
1086 self.log.info('statistics requested')
Scott Bakerd3190952018-09-04 15:47:28 -07001087
1088 def simulate_alarm(self, alarm):
1089 self.alarm_mgr.simulate_alarm(alarm)