blob: ed3dc060a952157d3983fb22b1ea66b5e679d8db [file] [log] [blame]
Shad Ansari2825d012018-02-22 23:57:46 +00001#
Shad Ansarie8cbc6f2019-02-14 15:50:54 -08002# Copyright 2019 the original author or authors.
Shad Ansari2825d012018-02-22 23:57:46 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Shad Ansari2825d012018-02-22 23:57:46 +000016import threading
Shad Ansari94250fc2018-07-04 06:52:11 +000017import binascii
Shad Ansari2825d012018-02-22 23:57:46 +000018import grpc
Shad Ansari94250fc2018-07-04 06:52:11 +000019import structlog
Shad Ansari06019382019-02-07 22:56:16 -080020import time
Shad Ansari15928d12018-04-17 02:42:13 +000021from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000022from scapy.layers.l2 import Ether, Dot1Q
Shad Ansari3cd9bf22018-07-25 19:29:39 +000023from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000024
Shad Ansari2825d012018-02-22 23:57:46 +000025from voltha.protos.device_pb2 import Port, Device
26from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
Shad Ansari134947d2019-02-14 23:45:03 -080027from voltha.protos.openflow_13_pb2 import OFPPS_LINK_DOWN
Shad Ansari2825d012018-02-22 23:57:46 +000028from voltha.registry import registry
29from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Shad Ansarie8cbc6f2019-02-14 15:50:54 -080030from voltha.adapters.openolt.openolt_utils import OpenoltUtils
mzadig384783a2018-08-09 08:52:40 -040031from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
Shad Ansarif9d2d102018-06-13 02:15:26 +000032
33
Shad Ansari2825d012018-02-22 23:57:46 +000034class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000035 """
36 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000037
Shad Ansari94250fc2018-07-04 06:52:11 +000038 null ----> init ------> connected -----> up -----> down
39 ^ ^ | ^ | |
40 | | | | | |
41 | +-------------+ +---------+ |
42 | |
43 +-----------------------------------------+
44 """
45 # pylint: disable=too-many-instance-attributes
46 # pylint: disable=R0904
47 states = [
48 'state_null',
49 'state_init',
50 'state_connected',
51 'state_up',
52 'state_down']
53
Shad Ansari22efe832018-05-19 05:37:03 +000054 transitions = [
Shad Ansari0e7ad962018-09-28 01:42:26 +000055 {'trigger': 'go_state_init',
56 'source': ['state_null', 'state_connected', 'state_down'],
57 'dest': 'state_init',
58 'before': 'do_state_init',
59 'after': 'post_init'},
60 {'trigger': 'go_state_connected',
61 'source': 'state_init',
62 'dest': 'state_connected',
63 'before': 'do_state_connected'},
64 {'trigger': 'go_state_up',
65 'source': ['state_connected', 'state_down'],
66 'dest': 'state_up',
67 'before': 'do_state_up'},
68 {'trigger': 'go_state_down',
69 'source': ['state_up'],
70 'dest': 'state_down',
71 'before': 'do_state_down',
72 'after': 'post_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000073
Shad Ansari2825d012018-02-22 23:57:46 +000074 def __init__(self, **kwargs):
75 super(OpenoltDevice, self).__init__()
76
Shad Ansari7c73c1a2019-02-04 15:39:47 -080077 self.admin_state = "up"
78
Shad Ansari134947d2019-02-14 23:45:03 -080079 self.data_model = kwargs['data_model']
Shad Ansari2825d012018-02-22 23:57:46 +000080 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000081 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000082 device = kwargs['device']
Shad Ansaricd20a6d2018-10-02 14:36:33 +000083
84 self.platform_class = kwargs['support_classes']['platform']
Girish Gowdru1e77ea02018-09-24 09:10:35 -070085 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
Shad Ansaricd20a6d2018-10-02 14:36:33 +000086 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
87 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
88 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
89 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
90
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040091 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000092 self.device_id = device.id
93 self.host_and_port = device.host_and_port
Girish Gowdru141ced82018-09-17 20:19:14 -070094 self.extra_args = device.extra_args
Shad Ansarif9d2d102018-06-13 02:15:26 +000095 self.log = structlog.get_logger(id=self.device_id,
96 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040097 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +000098
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -080099 self.log.info('openolt-device-init')
100
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400101 # Device already set in the event of reconciliation
102 if not is_reconciliation:
Matteo Scandolobf5ae0c2018-11-15 18:12:54 -0800103 self.log.info('updating-device')
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400104 # It is a new device
105 # Update device
106 device.root = True
Shad Ansari94250fc2018-07-04 06:52:11 +0000107 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400108 device.oper_status = OperStatus.ACTIVATING
109 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000110
Shad Ansaric4085df2019-02-13 16:47:07 -0800111 # If logical device does exist use it, else create one after connecting
112 # to device
Craig Lutgen109d4072018-12-11 17:01:16 -0600113 if device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400114 # logical device already exists
115 self.logical_device_id = device.parent_id
116 if is_reconciliation:
117 self.adapter_agent.reconcile_logical_device(
118 self.logical_device_id)
Shad Ansaric4085df2019-02-13 16:47:07 -0800119 else:
120 self.logical_device_id = None
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400121
Shad Ansari94250fc2018-07-04 06:52:11 +0000122 # Initialize the OLT state machine
123 self.machine = Machine(model=self, states=OpenoltDevice.states,
124 transitions=OpenoltDevice.transitions,
125 send_event=True, initial='state_null')
Shad Ansaric4085df2019-02-13 16:47:07 -0800126
127 self.device_info = None
128
Shad Ansari94250fc2018-07-04 06:52:11 +0000129 self.go_state_init()
130
131 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000132 # Initialize gRPC
133 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000134 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400135
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400136 self.log.info('openolt-device-created', device_id=self.device_id)
137
138 def post_init(self, event):
139 self.log.debug('post_init')
140
141 # We have reached init state, starting the indications thread
142
jasonhuang5f3e63b2018-07-27 01:32:48 +0800143 # Catch RuntimeError exception
144 try:
145 # Start indications thread
146 self.indications_thread_handle = threading.Thread(
147 target=self.indications_thread)
Shad Ansarif9521ad2018-09-08 10:46:43 +0000148 # Old getter/setter API for daemon; use it directly as a
jasonhuang5f3e63b2018-07-27 01:32:48 +0800149 # property instead. The Jinkins error will happon on the reason of
Shad Ansarif9521ad2018-09-08 10:46:43 +0000150 # Exception in thread Thread-1 (most likely raised # during
jasonhuang5f3e63b2018-07-27 01:32:48 +0800151 # interpreter shutdown)
152 self.indications_thread_handle.setDaemon(True)
153 self.indications_thread_handle.start()
Shad Ansarif9521ad2018-09-08 10:46:43 +0000154 except Exception as e:
Nicolas Palpacuer7183a3b2018-09-10 17:16:49 -0400155 self.log.exception('post_init failed', e=e)
Shad Ansari94250fc2018-07-04 06:52:11 +0000156
157 def do_state_connected(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400158 self.log.debug("do_state_connected")
159
Shad Ansarif34da592019-02-13 23:05:35 -0800160 # Check that device_info was successfully retrieved
161 assert(self.device_info is not None
162 and self.device_info.device_serial_number is not None
163 and self.device_info.device_serial_number != '')
164
Shad Ansari94250fc2018-07-04 06:52:11 +0000165 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari94250fc2018-07-04 06:52:11 +0000166
Shad Ansaric4085df2019-02-13 16:47:07 -0800167 if self.logical_device_id is None:
168 # first time connect to olt
Shad Ansari134947d2019-02-14 23:45:03 -0800169 self.logical_device_id = self.data_model.create_logical_device(
170 self.device_id, self.device_info)
Shad Ansaric4085df2019-02-13 16:47:07 -0800171 else:
172 # reconnect to olt (e.g. olt reboot)
173 # TODO - Update logical device with new device_info
174 pass
Craig Lutgen109d4072018-12-11 17:01:16 -0600175
Shad Ansarif34da592019-02-13 23:05:35 -0800176 device.serial_number = self.device_info.device_serial_number
Craig Lutgen109d4072018-12-11 17:01:16 -0600177
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700178 self.resource_mgr = self.resource_mgr_class(self.device_id,
Shad Ansari78de2be2018-10-12 22:13:54 +0000179 self.host_and_port,
180 self.extra_args,
Shad Ansaric4085df2019-02-13 16:47:07 -0800181 self.device_info)
Shad Ansarid9642382019-02-19 23:01:28 -0800182 self.platform = self.platform_class()
Girish Gowdruab836e92018-10-25 01:17:57 -0700183 self.flow_mgr = self.flow_mgr_class(self.adapter_agent, self.log,
184 self.stub, self.device_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000185 self.logical_device_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700186 self.platform, self.resource_mgr)
187
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000188 self.alarm_mgr = self.alarm_mgr_class(self.log, self.adapter_agent,
189 self.device_id,
190 self.logical_device_id,
191 self.platform)
192 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
193 self.bw_mgr = self.bw_mgr_class(self.log, self.proxy)
194
Shad Ansaric4085df2019-02-13 16:47:07 -0800195 device.vendor = self.device_info.vendor
196 device.model = self.device_info.model
197 device.hardware_version = self.device_info.hardware_version
198 device.firmware_version = self.device_info.firmware_version
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700199
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700200 # TODO: check for uptime and reboot if too long (VOL-1192)
201
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400202 device.connect_status = ConnectStatus.REACHABLE
203 self.adapter_agent.update_device(device)
204
Shad Ansari94250fc2018-07-04 06:52:11 +0000205 def do_state_up(self, event):
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400206 self.log.debug("do_state_up")
207
Shad Ansari94250fc2018-07-04 06:52:11 +0000208 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000209
Shad Ansari94250fc2018-07-04 06:52:11 +0000210 # Update phys OF device
211 device.parent_id = self.logical_device_id
212 device.oper_status = OperStatus.ACTIVE
213 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400214
Shad Ansari94250fc2018-07-04 06:52:11 +0000215 def do_state_down(self, event):
216 self.log.debug("do_state_down")
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400217
Shad Ansari134947d2019-02-14 23:45:03 -0800218 self.data_model.disable_logical_device(self.device_id)
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400219
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400220 def post_down(self, event):
221 self.log.debug('post_down')
222 self.flow_mgr.reset_flows()
223
Shad Ansari94250fc2018-07-04 06:52:11 +0000224 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400225 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000226 self.log.debug('connecting to olt', device_id=self.device_id)
Shad Ansaric4085df2019-02-13 16:47:07 -0800227
228 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
229
230 timeout = 60*60
231 delay = 1
232 exponential_back_off = False
233 while True:
234 try:
235 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
236 break
237 except Exception as e:
238 if delay > timeout:
239 self.log.error("timed out connecting to olt")
240 return
241 else:
242 self.log.warn("retry connecting to olt in %ds: %s"
243 % (delay, repr(e)))
244 time.sleep(delay)
245 if exponential_back_off:
246 delay += delay
247 else:
248 delay += 1
249
250 self.log.info('connected to olt', device_info=self.device_info)
251
Shad Ansari94250fc2018-07-04 06:52:11 +0000252 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000253
Shad Ansari15928d12018-04-17 02:42:13 +0000254 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000255
Shad Ansari94250fc2018-07-04 06:52:11 +0000256 while True:
nick47b74372018-05-25 18:22:49 -0400257 try:
258 # get the next indication from olt
259 ind = next(self.indications)
260 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000261 self.log.warn('gRPC connection lost', error=e)
262 reactor.callFromThread(self.go_state_down)
263 reactor.callFromThread(self.go_state_init)
264 break
nick47b74372018-05-25 18:22:49 -0400265 else:
266 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000267
Shad Ansari7c73c1a2019-02-04 15:39:47 -0800268 if self.admin_state is "down":
269 if ind.HasField('intf_oper_ind') \
270 and (ind.intf_oper_ind.type == "nni"):
271 self.log.warn('olt is admin down, allow nni ind',
272 admin_state=self.admin_state,
273 indications=ind)
274 else:
275 self.log.warn('olt is admin down, ignore indication',
276 admin_state=self.admin_state,
277 indications=ind)
278 continue
279
nick47b74372018-05-25 18:22:49 -0400280 # indication handlers run in the main event loop
281 if ind.HasField('olt_ind'):
282 reactor.callFromThread(self.olt_indication, ind.olt_ind)
283 elif ind.HasField('intf_ind'):
284 reactor.callFromThread(self.intf_indication, ind.intf_ind)
285 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000286 reactor.callFromThread(self.intf_oper_indication,
287 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400288 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000289 reactor.callFromThread(self.onu_discovery_indication,
290 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400291 elif ind.HasField('onu_ind'):
292 reactor.callFromThread(self.onu_indication, ind.onu_ind)
293 elif ind.HasField('omci_ind'):
294 reactor.callFromThread(self.omci_indication, ind.omci_ind)
295 elif ind.HasField('pkt_ind'):
296 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400297 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400298 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700299 self.stats_mgr.port_statistics_indication,
300 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400301 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400302 reactor.callFromThread(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700303 self.stats_mgr.flow_statistics_indication,
304 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000305 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400306 reactor.callFromThread(self.alarm_mgr.process_alarms,
307 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400308 else:
309 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400310
Shad Ansari2825d012018-02-22 23:57:46 +0000311 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000312 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000313 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000314 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000315 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000316
317 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400318 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000319 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000320
321 if intf_indication.oper_state == "up":
322 oper_status = OperStatus.ACTIVE
323 else:
324 oper_status = OperStatus.DISCOVERED
325
nick47b74372018-05-25 18:22:49 -0400326 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000327 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
328
329 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000330 self.log.debug("Received interface oper state change indication",
331 intf_id=intf_oper_indication.intf_id,
332 type=intf_oper_indication.type,
333 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000334
335 if intf_oper_indication.oper_state == "up":
336 oper_state = OperStatus.ACTIVE
337 else:
338 oper_state = OperStatus.DISCOVERED
339
340 if intf_oper_indication.type == "nni":
341
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400342 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000343 port_no, label = self.add_port(intf_oper_indication.intf_id,
344 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400345 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansari134947d2019-02-14 23:45:03 -0800346 self.data_model.add_logical_port(self.logical_device_id,
347 self.device_id, port_no,
348 intf_oper_indication.intf_id,
349 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000350
351 elif intf_oper_indication.type == "pon":
352 # FIXME - handle PON oper state change
353 pass
354
355 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000356 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000357 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000358
Shad Ansarie8cbc6f2019-02-14 15:50:54 -0800359 serial_number_str = OpenoltUtils.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000360
Shad Ansarif9d2d102018-06-13 02:15:26 +0000361 self.log.debug("onu discovery indication", intf_id=intf_id,
362 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400363
mzadig384783a2018-08-09 08:52:40 -0400364 # Post ONU Discover alarm 20180809_0805
365 try:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400366 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
367 serial_number=serial_number_str).raise_alarm()
mzadig384783a2018-08-09 08:52:40 -0400368 except Exception as disc_alarm_error:
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400369 self.log.exception("onu-discovery-alarm-error",
370 errmsg=disc_alarm_error.message)
mzadig384783a2018-08-09 08:52:40 -0400371 # continue for now.
372
Shad Ansarif9d2d102018-06-13 02:15:26 +0000373 onu_device = self.adapter_agent.get_child_device(
374 self.device_id,
375 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400376
377 if onu_device is None:
Shad Ansari15928d12018-04-17 02:42:13 +0000378 try:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700379 onu_id = self.resource_mgr.get_onu_id(intf_id)
380 if onu_id is None:
381 raise Exception("onu-id-unavailable")
382
Shad Ansarif9d2d102018-06-13 02:15:26 +0000383 self.add_onu_device(
384 intf_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000385 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000386 onu_id, serial_number)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400387 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -0700388 serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400389 except Exception as e:
390 self.log.exception('onu-activation-failed', e=e)
391
Shad Ansari2825d012018-02-22 23:57:46 +0000392 else:
nick47b74372018-05-25 18:22:49 -0400393 if onu_device.connect_status != ConnectStatus.REACHABLE:
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700394 onu_device.connect_status = ConnectStatus.REACHABLE
395 self.adapter_agent.update_device(onu_device)
nick47b74372018-05-25 18:22:49 -0400396
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400397 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000398 if onu_device.oper_status == OperStatus.DISCOVERED \
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700399 or onu_device.oper_status == OperStatus.ACTIVATING:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000400 self.log.debug("ignore onu discovery indication, \
401 the onu has been discovered and should be \
402 activating shorlty", intf_id=intf_id,
403 onu_id=onu_id, state=onu_device.oper_status)
404 elif onu_device.oper_status == OperStatus.ACTIVE:
405 self.log.warn("onu discovery indication whereas onu is \
406 supposed to be active",
407 intf_id=intf_id, onu_id=onu_id,
408 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400409 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000410 self.log.info("onu in unknown state, recovering from olt \
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400411 reboot probably, activate onu", intf_id=intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000412 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400413
414 onu_device.oper_status = OperStatus.DISCOVERED
415 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400416 try:
417 self.activate_onu(intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -0700418 serial_number_str)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400419 except Exception as e:
420 self.log.error('onu-activation-error',
421 serial_number=serial_number_str, error=e)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400422 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000423 self.log.warn('unexpected state', onu_id=onu_id,
424 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000425
Shad Ansari2825d012018-02-22 23:57:46 +0000426 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000427 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000428 onu_id=onu_indication.onu_id,
429 serial_number=onu_indication.serial_number,
430 oper_state=onu_indication.oper_state,
431 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400432 try:
Shad Ansarie8cbc6f2019-02-14 15:50:54 -0800433 serial_number_str = OpenoltUtils.stringify_serial_number(
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400434 onu_indication.serial_number)
Shad Ansari25299be2019-02-13 22:02:39 -0800435 except: # noqa: E722
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400436 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000437
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400438 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400439 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700440 self.device_id,
441 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000442 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400443 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700444 self.device_id,
445 parent_port_no=self.platform.intf_id_to_port_no(
446 onu_indication.intf_id, Port.PON_OLT),
447 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400448
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400449 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000450 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000451 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000452 return
453
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000454 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
Shad Ansarif9521ad2018-09-08 10:46:43 +0000455 != onu_indication.intf_id:
Shad Ansari25299be2019-02-13 22:02:39 -0800456 self.log.warn(
457 'ONU-is-on-a-different-intf-id-now',
458 previous_intf_id=self.platform.intf_id_from_pon_port_no(
459 onu_device.parent_port_no),
460 current_intf_id=onu_indication.intf_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400461 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000462
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400463 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
464 # FIXME - handle onu id mismatch
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400465 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
466 'the olt rebooted',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000467 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400468 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000469
Shad Ansarif9d2d102018-06-13 02:15:26 +0000470 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400471 if onu_indication.admin_state == 'down':
472 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000473 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
474 oper_state=onu_indication.oper_state)
475 # Forcing the oper state change code to execute
476 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400477
Shad Ansarif9d2d102018-06-13 02:15:26 +0000478 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400479
480 elif onu_indication.admin_state == 'up':
Nicolas Palpacuer921f8cf2018-08-14 18:23:09 -0400481 pass
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400482
483 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000484 self.log.warn('Invalid-or-not-implemented-admin-state',
485 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400486
487 self.log.debug('admin-state-dealt-with')
488
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400489 onu_adapter_agent = \
490 registry('adapter_loader').get_agent(onu_device.adapter)
491 if onu_adapter_agent is None:
492 self.log.error('onu_adapter_agent-could-not-be-retrieved',
493 onu_device=onu_device)
494 return
495
Shad Ansarif9d2d102018-06-13 02:15:26 +0000496 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400497 if onu_indication.oper_state == 'down':
Nicolas Palpacuer4ea3a652018-08-22 10:33:17 -0400498
499 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
500 onu_device.connect_status = ConnectStatus.UNREACHABLE
501 self.adapter_agent.update_device(onu_device)
502
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
Craig Lutgenabd9c842018-11-15 23:58:27 +0000510 self.onu_ports_down(onu_device, OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400511
Shad Ansari0e7ad962018-09-28 01:42:26 +0000512 onu_adapter_agent.update_interface(onu_device,
513 {'oper_state': 'down'})
Matt Jeanneret12cd5d02018-08-07 15:30:19 -0400514
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400515 elif onu_indication.oper_state == 'up':
516
Nicolas Palpacuer2824a992018-08-20 18:07:41 -0400517 if onu_device.connect_status != ConnectStatus.REACHABLE:
518 onu_device.connect_status = ConnectStatus.REACHABLE
519 self.adapter_agent.update_device(onu_device)
520
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400521 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000522 self.log.debug("ignore onu indication",
523 intf_id=onu_indication.intf_id,
524 onu_id=onu_indication.onu_id,
525 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400526 msg_oper_state=onu_indication.oper_state)
527 return
528
Shad Ansarif9d2d102018-06-13 02:15:26 +0000529 # Device was in Discovered state, setting it to active
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400530
Shad Ansarif9d2d102018-06-13 02:15:26 +0000531 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400532
Shad Ansari0e7ad962018-09-28 01:42:26 +0000533 onu_adapter_agent.create_interface(onu_device, onu_indication)
Matt Jeannerete6a70332018-07-20 16:11:25 -0400534
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400535 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000536 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
537 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000538
Craig Lutgenabd9c842018-11-15 23:58:27 +0000539 def onu_ports_down(self, onu_device, oper_state):
nick47b74372018-05-25 18:22:49 -0400540 # Set port oper state to Discovered
541 # add port will update port if it exists
Craig Lutgenabd9c842018-11-15 23:58:27 +0000542 # self.adapter_agent.add_port(
543 # self.device_id,
544 # Port(
545 # port_no=uni_no,
546 # label=uni_name,
547 # type=Port.ETHERNET_UNI,
548 # admin_state=onu_device.admin_state,
549 # oper_status=oper_state))
550 # TODO this should be downning ports in onu adatper
nick47b74372018-05-25 18:22:49 -0400551
552 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400553 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
nick47b74372018-05-25 18:22:49 -0400554 for onu_port in onu_ports:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000555 self.log.debug('onu-ports-down', onu_port=onu_port)
556 onu_port_id = onu_port.label
557 try:
558 onu_logical_port = self.adapter_agent.get_logical_port(
Shad Ansari25299be2019-02-13 22:02:39 -0800559 logical_device_id=self.logical_device_id,
560 port_id=onu_port_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000561 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
562 self.adapter_agent.update_logical_port(
563 logical_device_id=self.logical_device_id,
564 port=onu_logical_port)
565 self.log.debug('cascading-oper-state-to-port-and-logical-port')
566 except KeyError as e:
567 self.log.error('matching-onu-port-label-invalid',
568 onu_id=onu_device.id, olt_id=self.device_id,
569 onu_ports=onu_ports, onu_port_id=onu_port_id,
570 error=e)
nick47b74372018-05-25 18:22:49 -0400571
Shad Ansari2825d012018-02-22 23:57:46 +0000572 def omci_indication(self, omci_indication):
573
574 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000575 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000576
Shad Ansarif9d2d102018-06-13 02:15:26 +0000577 onu_device = self.adapter_agent.get_child_device(
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400578 self.device_id, onu_id=omci_indication.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000579 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700580 omci_indication.intf_id, Port.PON_OLT), )
Shad Ansari0efa6512018-04-28 06:42:54 +0000581
582 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000583 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000584
Shad Ansari42db7342018-04-25 21:39:46 +0000585 def packet_indication(self, pkt_indication):
586
Shad Ansari0ff82622018-09-30 09:32:04 +0000587 self.log.debug("packet indication",
588 intf_type=pkt_indication.intf_type,
589 intf_id=pkt_indication.intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000590 port_no=pkt_indication.port_no,
591 cookie=pkt_indication.cookie,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000592 gemport_id=pkt_indication.gemport_id,
593 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000594
Shad Ansari0ff82622018-09-30 09:32:04 +0000595 if pkt_indication.intf_type == "pon":
Craig Lutgenabd9c842018-11-15 23:58:27 +0000596 if pkt_indication.port_no:
597 logical_port_num = pkt_indication.port_no
Shad Ansari25299be2019-02-13 22:02:39 -0800598 else:
599 # TODO Remove this else block after openolt device has been
600 # fully rolled out with cookie protobuf change
Craig Lutgenabd9c842018-11-15 23:58:27 +0000601 try:
Shad Ansari25299be2019-02-13 22:02:39 -0800602 onu_id_uni_id = self.resource_mgr. \
603 get_onu_uni_from_ponport_gemport(
604 pkt_indication.intf_id, pkt_indication.gemport_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000605 onu_id = int(onu_id_uni_id[0])
606 uni_id = int(onu_id_uni_id[1])
Shad Ansari25299be2019-02-13 22:02:39 -0800607 self.log.debug("packet indication-kv", onu_id=onu_id,
608 uni_id=uni_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000609 if onu_id is None:
610 raise Exception("onu-id-none")
611 if uni_id is None:
612 raise Exception("uni-id-none")
Shad Ansari25299be2019-02-13 22:02:39 -0800613 logical_port_num = self.platform.mk_uni_port_num(
614 pkt_indication.intf_id, onu_id, uni_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000615 except Exception as e:
616 self.log.error("no-onu-reference-for-gem",
617 gemport_id=pkt_indication.gemport_id, e=e)
618 return
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700619
Shad Ansari0ff82622018-09-30 09:32:04 +0000620 elif pkt_indication.intf_type == "nni":
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000621 logical_port_num = self.platform.intf_id_to_port_no(
Shad Ansari0ff82622018-09-30 09:32:04 +0000622 pkt_indication.intf_id,
623 Port.ETHERNET_NNI)
Shad Ansari42db7342018-04-25 21:39:46 +0000624
625 pkt = Ether(pkt_indication.pkt)
Shad Ansari0ff82622018-09-30 09:32:04 +0000626
627 self.log.debug("packet indication",
628 logical_device_id=self.logical_device_id,
629 logical_port_no=logical_port_num)
630
631 self.adapter_agent.send_packet_in(
632 logical_device_id=self.logical_device_id,
633 logical_port_no=logical_port_num,
634 packet=str(pkt))
Shad Ansari42db7342018-04-25 21:39:46 +0000635
636 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000637 pkt = Ether(msg)
Saurav Dasf87d6552018-09-26 17:05:42 -0700638 self.log.debug('packet out', egress_port=egress_port,
639 device_id=self.device_id,
640 logical_device_id=self.logical_device_id,
641 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400642
643 # Find port type
Craig Lutgenabd9c842018-11-15 23:58:27 +0000644 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400645 if egress_port_type == Port.ETHERNET_UNI:
646
647 if pkt.haslayer(Dot1Q):
648 outer_shim = pkt.getlayer(Dot1Q)
649 if isinstance(outer_shim.payload, Dot1Q):
650 # If double tag, remove the outer tag
651 payload = (
Shad Ansari25299be2019-02-13 22:02:39 -0800652 Ether(src=pkt.src, dst=pkt.dst,
653 type=outer_shim.type) /
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700654 outer_shim.payload
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400655 )
656 else:
657 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000658 else:
659 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400660
661 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
662
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400663 self.log.debug(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000664 'sending-packet-to-ONU', egress_port=egress_port,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000665 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
666 onu_id=self.platform.onu_id_from_port_num(egress_port),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000667 uni_id=self.platform.uni_id_from_port_num(egress_port),
668 port_no=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000669 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400670
Shad Ansarif9d2d102018-06-13 02:15:26 +0000671 onu_pkt = openolt_pb2.OnuPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000672 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
673 onu_id=self.platform.onu_id_from_port_num(egress_port),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000674 port_no=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000675 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400676
677 self.stub.OnuPacketOut(onu_pkt)
678
679 elif egress_port_type == Port.ETHERNET_NNI:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400680 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000681 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400682
683 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
684
Shad Ansarif9d2d102018-06-13 02:15:26 +0000685 uplink_pkt = openolt_pb2.UplinkPacket(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000686 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000687 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400688
689 self.stub.UplinkPacketOut(uplink_pkt)
690
Shad Ansari0346f0d2018-04-26 06:54:09 +0000691 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000692 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
693 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400694 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000695
Shad Ansari2825d012018-02-22 23:57:46 +0000696 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9521ad2018-09-08 10:46:43 +0000697 onu_device = self.adapter_agent.get_child_device(
Girish Gowdru141ced82018-09-17 20:19:14 -0700698 self.device_id, onu_id=proxy_address.onu_id,
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000699 parent_port_no=self.platform.intf_id_to_port_no(
Girish Gowdru141ced82018-09-17 20:19:14 -0700700 proxy_address.channel_id, Port.PON_OLT)
701 )
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400702 if onu_device.connect_status != ConnectStatus.REACHABLE:
703 self.log.debug('ONU is not reachable, cannot send OMCI',
704 serial_number=onu_device.serial_number,
705 intf_id=onu_device.proxy_address.channel_id,
706 onu_id=onu_device.proxy_address.onu_id)
707 return
Shad Ansarif9d2d102018-06-13 02:15:26 +0000708 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
709 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000710 self.stub.OmciMsgOut(omci)
711
712 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000713 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000714 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000715
716 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000717 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000718 channel_id=intf_id, onu_id=onu_id,
719 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000720
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400721 self.log.debug("Adding ONU", proxy_address=proxy_address)
Shad Ansari2825d012018-02-22 23:57:46 +0000722
Shad Ansarie8cbc6f2019-02-14 15:50:54 -0800723 serial_number_str = OpenoltUtils.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000724
Shad Ansarif9d2d102018-06-13 02:15:26 +0000725 self.adapter_agent.add_onu_device(
726 parent_device_id=self.device_id, parent_port_no=port_no,
727 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000728 root=False, serial_number=serial_number_str,
Shad Ansari25299be2019-02-13 22:02:39 -0800729 admin_state=AdminState.ENABLED
Craig Lutgenabd9c842018-11-15 23:58:27 +0000730 )
Shad Ansari2825d012018-02-22 23:57:46 +0000731
Shad Ansari2825d012018-02-22 23:57:46 +0000732 def add_port(self, intf_id, port_type, oper_status):
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000733 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000734
Shad Ansarie8cbc6f2019-02-14 15:50:54 -0800735 label = OpenoltUtils.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000736
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400737 self.log.debug('adding-port', port_no=port_no, label=label,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000738 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000739
740 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000741 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000742
Shad Ansari2825d012018-02-22 23:57:46 +0000743 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000744
Shad Ansari2825d012018-02-22 23:57:46 +0000745 return port_no, label
746
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800747 def get_uni_ofp_port_name(self, child_device):
748 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
749 self.logical_device_id))
750 for logical_port in logical_ports:
751 if logical_port.device_id == child_device.id:
752 return logical_port.ofp_port.name
753 return None
754
755
Girish Gowdruab836e92018-10-25 01:17:57 -0700756 def delete_logical_port(self, child_device):
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400757 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
758 self.logical_device_id))
759 for logical_port in logical_ports:
Girish Gowdruab836e92018-10-25 01:17:57 -0700760 if logical_port.device_id == child_device.id:
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400761 self.log.debug('delete-logical-port',
Girish Gowdruab836e92018-10-25 01:17:57 -0700762 onu_device_id=child_device.id,
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400763 logical_port=logical_port)
Girish Gowdruab836e92018-10-25 01:17:57 -0700764 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
765 child_device, logical_port)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400766 self.adapter_agent.delete_logical_port(
767 self.logical_device_id, logical_port)
768 return
Shad Ansarif9521ad2018-09-08 10:46:43 +0000769
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400770 def delete_port(self, child_serial_number):
771 ports = self.proxy.get('/devices/{}/ports'.format(
772 self.device_id))
773 for port in ports:
774 if port.label == child_serial_number:
775 self.log.debug('delete-port',
776 onu_serial_number=child_serial_number,
777 port=port)
778 self.adapter_agent.delete_port(self.device_id, port)
779 return
780
Shad Ansari2825d012018-02-22 23:57:46 +0000781 def update_flow_table(self, flows):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400782 self.log.debug('No updates here now, all is done in logical flows '
783 'update')
Shad Ansari5df91f62018-07-25 23:59:46 +0000784
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400785 def update_logical_flows(self, flows_to_add, flows_to_remove,
786 device_rules_map):
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400787 if not self.is_state_up():
788 self.log.info('The OLT is not up, we cannot update flows',
789 flows_to_add=[f.id for f in flows_to_add],
790 flows_to_remove=[f.id for f in flows_to_remove])
791 return
792
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400793 try:
794 self.flow_mgr.update_children_flows(device_rules_map)
795 except Exception as e:
796 self.log.error('Error updating children flows', error=e)
Shad Ansari2825d012018-02-22 23:57:46 +0000797
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400798 self.log.debug('logical flows update', flows_to_add=flows_to_add,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000799 flows_to_remove=flows_to_remove)
Shad Ansari2825d012018-02-22 23:57:46 +0000800
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400801 for flow in flows_to_add:
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400802
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400803 try:
804 self.flow_mgr.add_flow(flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400805 except Exception as e:
806 self.log.error('failed to add flow', flow=flow, e=e)
807
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400808 for flow in flows_to_remove:
809
810 try:
811 self.flow_mgr.remove_flow(flow)
812 except Exception as e:
813 self.log.error('failed to remove flow', flow=flow, e=e)
814
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400815 self.flow_mgr.repush_all_different_flows()
816
Jonathan Davis0f917a22018-05-30 14:39:45 -0400817 def disable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400818 self.log.debug('sending-deactivate-olt-message',
Shad Ansarif9521ad2018-09-08 10:46:43 +0000819 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400820
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400821 try:
822 # Send grpc call
823 self.stub.DisableOlt(openolt_pb2.Empty())
Shad Ansari7c73c1a2019-02-04 15:39:47 -0800824 self.admin_state = "down"
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400825 self.log.info('openolt device disabled')
826 except Exception as e:
827 self.log.error('Failure to disable openolt device', error=e)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400828
Jonathan Davis0f917a22018-05-30 14:39:45 -0400829 def delete(self):
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400830 self.log.info('deleting-olt', device_id=self.device_id,
831 logical_device_id=self.logical_device_id)
832
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700833 # Clears up the data from the resource manager KV store
834 # for the device
835 del self.resource_mgr
836
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400837 try:
838 # Rebooting to reset the state
839 self.reboot()
840 # Removing logical device
Saurav Dasf87d6552018-09-26 17:05:42 -0700841 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
842 self.adapter_agent.delete_logical_device(ld)
Nicolas Palpacuer0d44e682018-08-06 10:30:26 -0400843 except Exception as e:
844 self.log.error('Failure to delete openolt device', error=e)
845 raise e
846 else:
847 self.log.info('successfully-deleted-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400848
Jonathan Davis0f917a22018-05-30 14:39:45 -0400849 def reenable(self):
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400850 self.log.debug('reenabling-olt', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400851
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400852 try:
853 self.stub.ReenableOlt(openolt_pb2.Empty())
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400854 except Exception as e:
855 self.log.error('Failure to reenable openolt device', error=e)
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400856 else:
857 self.log.info('openolt device reenabled')
Shad Ansari7c73c1a2019-02-04 15:39:47 -0800858 self.admin_state = "up"
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400859
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400860 def activate_onu(self, intf_id, onu_id, serial_number,
Girish Gowdruab836e92018-10-25 01:17:57 -0700861 serial_number_str):
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400862 pir = self.bw_mgr.pir(serial_number_str)
863 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000864 serial_number_str=serial_number_str,
Girish Gowdruab836e92018-10-25 01:17:57 -0700865 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400866 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700867 serial_number=serial_number, pir=pir)
Nicolas Palpacuer131790b2018-08-20 09:59:34 -0400868 self.stub.ActivateOnu(onu)
869 self.log.info('onu-activated', serial_number=serial_number_str)
Nicolas Palpacuer62dbb9c2018-08-02 15:03:35 -0400870
Jonathan Davisb45bb372018-07-19 15:05:15 -0400871 def delete_child_device(self, child_device):
872 self.log.debug('sending-deactivate-onu',
873 olt_device_id=self.device_id,
874 onu_device=child_device,
875 onu_serial_number=child_device.serial_number)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400876 try:
877 self.adapter_agent.delete_child_device(self.device_id,
Shad Ansarif9521ad2018-09-08 10:46:43 +0000878 child_device.id,
879 child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400880 except Exception as e:
881 self.log.error('adapter_agent error', error=e)
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800882
883 ofp_port_name = self.get_uni_ofp_port_name(child_device)
884 if ofp_port_name is None:
885 self.log.exception("uni-ofp-port-not-found")
886 return
887
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400888 try:
Girish Gowdruab836e92018-10-25 01:17:57 -0700889 self.delete_logical_port(child_device)
Nicolas Palpacuer3bd62092018-08-15 09:42:53 -0400890 except Exception as e:
891 self.log.error('logical_port delete error', error=e)
892 try:
893 self.delete_port(child_device.serial_number)
894 except Exception as e:
895 self.log.error('port delete error', error=e)
Shad Ansarie8cbc6f2019-02-14 15:50:54 -0800896 serial_number = OpenoltUtils.destringify_serial_number(
Shad Ansarif9521ad2018-09-08 10:46:43 +0000897 child_device.serial_number)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000898 # TODO FIXME - For each uni.
899 # TODO FIXME - Flows are not deleted
900 uni_id = 0 # FIXME
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800901 try:
902 self.flow_mgr.delete_tech_profile_instance(
903 child_device.proxy_address.channel_id,
904 child_device.proxy_address.onu_id,
905 uni_id,
906 ofp_port_name
907 )
908 except Exception as e:
909 self.log.exception("error-removing-tp-instance")
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700910
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800911 try:
912 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
913 child_device.proxy_address.onu_id,
914 uni_id)
915 # Free any PON resources that were reserved for the ONU
916 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
917 except Exception as e:
918 self.log.exception("error-removing-pon-resources-for-onu")
919
920 try:
921 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
922 onu_id=child_device.proxy_address.onu_id,
923 serial_number=serial_number)
924 self.stub.DeleteOnu(onu)
925 except Exception as e:
926 self.log.exception("error-deleting-the-onu-on-olt-device")
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -0400927
928 def reboot(self):
Shad Ansarif9521ad2018-09-08 10:46:43 +0000929 self.log.debug('rebooting openolt device', device_id=self.device_id)
Nicolas Palpacuerfd365ac2018-08-02 11:37:37 -0400930 try:
931 self.stub.Reboot(openolt_pb2.Empty())
932 except Exception as e:
933 self.log.error('something went wrong with the reboot', error=e)
934 else:
935 self.log.info('device rebooted')
936
Nicolas Palpacuer30027f42018-09-06 15:55:54 -0400937 def trigger_statistics_collection(self):
938 try:
939 self.stub.CollectStatistics(openolt_pb2.Empty())
940 except Exception as e:
941 self.log.error('Error while triggering statistics collection',
942 error=e)
943 else:
944 self.log.info('statistics requested')
Scott Bakerd3190952018-09-04 15:47:28 -0700945
946 def simulate_alarm(self, alarm):
947 self.alarm_mgr.simulate_alarm(alarm)