blob: 00ffaa4bac28ec6ddd26c62f9f381e049babbf79 [file] [log] [blame]
Shad Ansari134947d2019-02-14 23:45:03 -08001#
2# Copyright 2019 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#
16import structlog
17import socket
Shad Ansari8d068642019-02-28 23:09:03 -080018from scapy.layers.l2 import Ether
Shad Ansarif9b9b892019-02-27 17:10:27 -080019import voltha.core.flow_decomposer as fd
Shad Ansari134947d2019-02-14 23:45:03 -080020from voltha.adapters.openolt.openolt_utils import OpenoltUtils
Shad Ansarid5577972019-02-22 09:35:03 -080021from voltha.protos.device_pb2 import Port, Device
Shad Ansari134947d2019-02-14 23:45:03 -080022from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
23 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_PORT_STATS, OFPC_TABLE_STATS, \
24 OFPC_FLOW_STATS, OFPC_GROUP_STATS, ofp_port, ofp_port_stats, ofp_desc, \
25 ofp_switch_features
26from voltha.core.logical_device_agent import mac_str_to_tuple
27from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansarid5577972019-02-22 09:35:03 -080028from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
Shad Ansari134947d2019-02-14 23:45:03 -080029from voltha.protos.logical_device_pb2 import LogicalDevice
30from voltha.registry import registry
31
32
33class OpenOltDataModel(object):
Shad Ansari134947d2019-02-14 23:45:03 -080034
Shad Ansarid5577972019-02-22 09:35:03 -080035 def __init__(self, device, adapter_agent, platform):
Shad Ansari134947d2019-02-14 23:45:03 -080036 self.log = structlog.get_logger()
37
Shad Ansarid5577972019-02-22 09:35:03 -080038 self.device = device
39 self.adapter_agent = adapter_agent
40 self.platform = platform
41 self.logical_device_id = None
42
43 self.device.root = True
44 self.device.connect_status = ConnectStatus.UNREACHABLE
45 self.device.oper_status = OperStatus.ACTIVATING
46
47 self.adapter_agent.update_device(device)
48
Shad Ansarif9b9b892019-02-27 17:10:27 -080049 self.nni_intf_id = None
Shad Ansarief2029b2019-02-25 09:45:54 -080050
Shad Ansari18f60372019-03-04 10:59:41 -080051 self.proxy = registry('core').get_proxy('/')
52
Shad Ansarid5577972019-02-22 09:35:03 -080053 def reconcile(self):
54 assert self.logical_device_id is not None
55 self.adapter_agent.reconcile_logical_device(
56 self.logical_device_id)
Shad Ansari8d068642019-02-28 23:09:03 -080057 # Update device cache
Shad Ansari18f60372019-03-04 10:59:41 -080058 self.device = self.adapter_agent.get_device(self.device.id)
Shad Ansarid5577972019-02-22 09:35:03 -080059
60 def olt_create(self, device_info):
61 if self.logical_device_id is not None:
62 return
63
Shad Ansari134947d2019-02-14 23:45:03 -080064 dpid = device_info.device_id
65 serial_number = device_info.device_serial_number
66
Shad Ansari134947d2019-02-14 23:45:03 -080067 if dpid is None or dpid == '':
Shad Ansarid5577972019-02-22 09:35:03 -080068 uri = self.device.host_and_port.split(":")[0]
Shad Ansari134947d2019-02-14 23:45:03 -080069 try:
70 socket.inet_pton(socket.AF_INET, uri)
71 dpid = '00:00:' + OpenoltUtils.ip_hex(uri)
72 except socket.error:
73 # this is not an IP
74 dpid = OpenoltUtils.str_to_mac(uri)
75
76 self.log.info('creating-openolt-logical-device', dp_id=dpid,
77 serial_number=serial_number)
78
79 hw_desc = device_info.model
80 if device_info.hardware_version:
81 hw_desc += '-' + device_info.hardware_version
82
83 # Create logical OF device
84 ld = LogicalDevice(
Shad Ansarid5577972019-02-22 09:35:03 -080085 root_device_id=self.device.id,
Shad Ansari134947d2019-02-14 23:45:03 -080086 switch_features=ofp_switch_features(
87 n_buffers=256, # TODO fake for now
88 n_tables=2, # TODO ditto
89 capabilities=( # TODO and ditto
90 OFPC_FLOW_STATS
91 | OFPC_TABLE_STATS
92 | OFPC_PORT_STATS
93 | OFPC_GROUP_STATS
94 )
95 ),
96 desc=ofp_desc(
97 serial_num=serial_number
98 )
99 )
Shad Ansarid5577972019-02-22 09:35:03 -0800100 self.logical_device_id = \
101 self.adapter_agent.create_logical_device(ld, dpid=dpid).id
Shad Ansari134947d2019-02-14 23:45:03 -0800102
Shad Ansarid5577972019-02-22 09:35:03 -0800103 self.device.vendor = device_info.vendor
104 self.device.model = device_info.model
105 self.device.hardware_version = device_info.hardware_version
106 self.device.firmware_version = device_info.firmware_version
107 self.device.connect_status = ConnectStatus.REACHABLE
108 self.device.serial_number = serial_number
109
110 self.adapter_agent.update_device(self.device)
Shad Ansari134947d2019-02-14 23:45:03 -0800111
112 self.log.info('created-openolt-logical-device',
Shad Ansarid5577972019-02-22 09:35:03 -0800113 logical_device_id=self.logical_device_id)
Shad Ansari134947d2019-02-14 23:45:03 -0800114
Shad Ansarid5577972019-02-22 09:35:03 -0800115 return self.logical_device_id
Shad Ansari134947d2019-02-14 23:45:03 -0800116
Shad Ansarief2029b2019-02-25 09:45:54 -0800117 def olt_oper_up(self):
118 self.device.parent_id = self.logical_device_id
Shad Ansarid16ef0c2019-03-04 16:55:21 -0800119 self.device.connect_status = ConnectStatus.REACHABLE
Shad Ansarief2029b2019-02-25 09:45:54 -0800120 self.device.oper_status = OperStatus.ACTIVE
121 self.adapter_agent.update_device(self.device)
Shad Ansari134947d2019-02-14 23:45:03 -0800122
Shad Ansarief2029b2019-02-25 09:45:54 -0800123 def olt_oper_down(self):
Shad Ansarif9b9b892019-02-27 17:10:27 -0800124 self.__disable_logical_device()
Shad Ansarief2029b2019-02-25 09:45:54 -0800125
126 def olt_delete(self):
127 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
128 self.adapter_agent.delete_logical_device(ld)
129
Shad Ansarif9b9b892019-02-27 17:10:27 -0800130 def olt_port_add_update(self, intf_id, intf_type, oper):
131 if oper == "up":
132 oper_status = OperStatus.ACTIVE
133 else:
134 oper_status = OperStatus.DISCOVERED
135
136 if intf_type == "nni":
137 port_type = Port.ETHERNET_NNI
138 elif intf_type == "pon":
139 port_type = Port.PON_OLT
140
141 port_no, label = self.__add_port(intf_id, port_type, oper_status)
142
143 if intf_type == "nni":
144 self.__add_logical_port(port_no, intf_id, oper_status)
145 self.nni_intf_id = intf_id
146
147 def olt_nni_intf_id(self):
148 if self.nni_intf_id is not None:
149 return self.nni_intf_id
150
151 port_list = self.adapter_agent.get_ports(self.device.id,
152 Port.ETHERNET_NNI)
153 logical_port = self.adapter_agent.get_logical_port(
154 self.logical_device_id, port_list[0].label)
155 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(
156 logical_port.ofp_port.port_no)
157 self.log.debug("nni-intf-d ", nni_intf_id=self.nni_intf_id)
158 return self.nni_intf_id
159
Shad Ansarief2029b2019-02-25 09:45:54 -0800160 def onu_create(self, intf_id, onu_id, serial_number):
161 onu_device = self.adapter_agent.get_child_device(
162 self.device.id,
163 serial_number=serial_number)
164
165 if onu_device:
166 self.log.debug("data_model onu update", intf_id=intf_id,
167 onu_id=onu_id, serial_number=serial_number)
168 onu_device.oper_status = OperStatus.DISCOVERED
169 onu_device.connect_status = ConnectStatus.REACHABLE
170 self.adapter_agent.update_device(onu_device)
171 return
172
173 self.log.debug("data_model onu create", intf_id=intf_id,
174 onu_id=onu_id, serial_number=serial_number)
175
176 # NOTE - channel_id of onu is set to intf_id
177 proxy_address = Device.ProxyAddress(device_id=self.device.id,
178 channel_id=intf_id, onu_id=onu_id,
179 onu_session_id=onu_id)
180 port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
181 vendor_id = serial_number[:4]
182 self.adapter_agent.add_onu_device(
183 parent_device_id=self.device.id, parent_port_no=port_no,
184 vendor_id=vendor_id, proxy_address=proxy_address,
185 root=False, serial_number=serial_number,
186 admin_state=AdminState.ENABLED,
187 connect_status=ConnectStatus.REACHABLE
188 )
189
190 def onu_delete(self, serial_number):
191 onu_device = self.adapter_agent.get_child_device(
192 self.device.id,
193 serial_number=serial_number)
194 try:
Shad Ansarif9b9b892019-02-27 17:10:27 -0800195 self.adapter_agent.delete_child_device(self.device.id,
Shad Ansarief2029b2019-02-25 09:45:54 -0800196 onu_device.id, onu_device)
197 except Exception as e:
198 self.log.error('adapter_agent error', error=e)
199
Shad Ansarif9b9b892019-02-27 17:10:27 -0800200 ofp_port_name = self.__get_uni_ofp_port_name(onu_device)
Shad Ansarief2029b2019-02-25 09:45:54 -0800201 if ofp_port_name is None:
202 self.log.exception("uni-ofp-port-not-found")
203 return
204
205 try:
Shad Ansarif9b9b892019-02-27 17:10:27 -0800206 self.__delete_logical_port(onu_device)
Shad Ansarief2029b2019-02-25 09:45:54 -0800207 except Exception as e:
208 self.log.error('logical_port delete error', error=e)
209 try:
210 self.delete_port(onu_device.serial_number)
211 except Exception as e:
212 self.log.error('port delete error', error=e)
213
214 def onu_id(self, serial_number):
215 onu_device = self.adapter_agent.get_child_device(
216 self.device.id,
217 serial_number=serial_number)
218
219 if onu_device:
220 return onu_device.proxy_address.onu_id
221 else:
222 return 0 # Invalid onu id
223
224 def onu_oper_down(self, intf_id, onu_id):
225
226 onu_device = self.adapter_agent.get_child_device(
227 self.device.id,
228 parent_port_no=self.platform.intf_id_to_port_no(intf_id,
229 Port.PON_OLT),
230 onu_id=onu_id)
231
232 if onu_device is None:
233 self.log.error('onu not found', intf_id=intf_id, onu_id=onu_id)
234 return
235
236 onu_adapter_agent = \
237 registry('adapter_loader').get_agent(onu_device.adapter)
238 if onu_adapter_agent is None:
239 self.log.error('onu_adapter_agent-could-not-be-retrieved',
240 onu_device=onu_device)
241 return
242
243 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
244 onu_device.connect_status = ConnectStatus.UNREACHABLE
245 self.adapter_agent.update_device(onu_device)
246
247 # Move to discovered state
248 self.log.debug('onu-oper-state-is-down')
249
250 if onu_device.oper_status != OperStatus.DISCOVERED:
251 onu_device.oper_status = OperStatus.DISCOVERED
252 self.adapter_agent.update_device(onu_device)
253 # Set port oper state to Discovered
Shad Ansari8d068642019-02-28 23:09:03 -0800254 self.__onu_ports_down(onu_device)
Shad Ansarief2029b2019-02-25 09:45:54 -0800255
256 onu_adapter_agent.update_interface(onu_device,
257 {'oper_state': 'down'})
258
259 def onu_oper_up(self, intf_id, onu_id):
260
261 class _OnuIndication:
262 def __init__(self, intf_id, onu_id):
263 self.intf_id = intf_id
264 self.onu_id = onu_id
265
266 onu_device = self.adapter_agent.get_child_device(
267 self.device.id,
268 parent_port_no=self.platform.intf_id_to_port_no(intf_id,
269 Port.PON_OLT),
270 onu_id=onu_id)
271
272 if onu_device is None:
273 self.log.error('onu not found', intf_id=intf_id, onu_id=onu_id)
274 return
275
276 onu_adapter_agent = \
277 registry('adapter_loader').get_agent(onu_device.adapter)
278 if onu_adapter_agent is None:
279 self.log.error('onu_adapter_agent-could-not-be-retrieved',
280 onu_device=onu_device)
281 return
282 if onu_device.connect_status != ConnectStatus.REACHABLE:
283 onu_device.connect_status = ConnectStatus.REACHABLE
284 self.adapter_agent.update_device(onu_device)
285
286 if onu_device.oper_status != OperStatus.DISCOVERED:
287 self.log.debug("ignore onu indication",
288 intf_id=intf_id,
289 onu_id=onu_id,
290 state=onu_device.oper_status,
291 msg_oper_state="up")
292 return
293
294 onu_adapter_agent.create_interface(onu_device,
295 _OnuIndication(intf_id, onu_id))
296
Shad Ansarif9b9b892019-02-27 17:10:27 -0800297 def onu_download_tech_profile(self, intf_id, onu_id, uni_id, tp_path):
298 onu_device = self.adapter_agent.get_child_device(
299 self.device.id,
300 parent_port_no=self.platform.intf_id_to_port_no(intf_id,
301 Port.PON_OLT),
302 onu_id=onu_id)
303 msg = {'proxy_address': onu_device.proxy_address,
304 'uni_id': uni_id, 'event': 'download_tech_profile',
305 'event_data': tp_path}
306
307 # Send the event message to the ONU adapter
308 self.adapter_agent.publish_inter_adapter_message(
309 onu_device.id, msg)
310
Shad Ansari8d068642019-02-28 23:09:03 -0800311 def onu_omci_rx(self, intf_id, onu_id, pkt):
312 onu_device = self.adapter_agent.get_child_device(
313 self.device.id,
314 parent_port_no=self.platform.intf_id_to_port_no(intf_id,
315 Port.PON_OLT),
316 onu_id=onu_id)
317 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
318 pkt)
319
320 def onu_send_packet_in(self, intf_type, intf_id, port_no, pkt):
321 if intf_type == "pon":
322 if not port_no:
323 raise ValueError("invalid port_no")
324 logical_port_num = port_no
325 elif intf_type == "nni":
326 logical_port_num = self.platform.intf_id_to_port_no(
327 intf_id,
328 Port.ETHERNET_NNI)
329
330 ether_pkt = Ether(pkt)
331
332 self.adapter_agent.send_packet_in(
333 logical_device_id=self.logical_device_id,
334 logical_port_no=logical_port_num,
335 packet=str(ether_pkt))
336
Shad Ansarif9b9b892019-02-27 17:10:27 -0800337 # #######################################################################
338 # Flow decomposer utility functions
339 #
340 # Flow related functions that are used by the OpenOLT flow decomposer.
341 # These are all prefixed with _ to denote that they will likely be removed
342 # once OpenOLT adapter transitions back to using core's flow decomposer.
343 # #######################################################################
344
345 def _flow_extract_info(self, flow, flow_direction):
346 uni_port_no = None
347 child_device_id = None
348 if flow_direction == "upstream":
349 for field in fd.get_ofb_fields(flow):
350 if field.type == fd.IN_PORT:
351 is_uni, child_device_id = self.__is_uni_port(field.port)
352 if is_uni:
353 uni_port_no = field.port
354 elif flow_direction == "downstream":
355 for field in fd.get_ofb_fields(flow):
356 if field.type == fd.METADATA:
357 uni_port = field.table_metadata & 0xFFFFFFFF
358 is_uni, child_device_id = self.__is_uni_port(uni_port)
359 if is_uni:
360 uni_port_no = field.port
361
362 if uni_port_no is None:
363 for action in fd.get_actions(flow):
364 if action.type == fd.OUTPUT:
365 is_uni, child_device_id = \
366 self.__is_uni_port(action.output.port)
367 if is_uni:
368 uni_port_no = action.output.port
369
370 if child_device_id:
371 child_device = self.adapter_agent.get_device(child_device_id)
372 pon_intf = child_device.proxy_address.channel_id
373 onu_id = child_device.proxy_address.onu_id
374 uni_id = self.platform.uni_id_from_port_num(uni_port_no) \
375 if uni_port_no is not None else None
Shad Ansarief2029b2019-02-25 09:45:54 -0800376 else:
Shad Ansarif9b9b892019-02-27 17:10:27 -0800377 raise ValueError
Shad Ansarief2029b2019-02-25 09:45:54 -0800378
Shad Ansarif9b9b892019-02-27 17:10:27 -0800379 return pon_intf, onu_id, uni_id
Shad Ansarief2029b2019-02-25 09:45:54 -0800380
Shad Ansarif9b9b892019-02-27 17:10:27 -0800381 def _get_ofp_port_name(self, intf_id, onu_id, uni_id):
382 parent_port_no = self.platform.intf_id_to_port_no(intf_id,
383 Port.PON_OLT)
384 child_device = self.adapter_agent.get_child_device(
385 self.device.id, parent_port_no=parent_port_no, onu_id=onu_id)
386 if child_device is None:
387 self.log.error("could-not-find-child-device",
388 parent_port_no=intf_id, onu_id=onu_id)
389 return (None, None)
390 ports = self.adapter_agent.get_ports(child_device.id,
391 Port.ETHERNET_UNI)
392 logical_port = self.adapter_agent.get_logical_port(
393 self.logical_device_id, ports[uni_id].label)
394 ofp_port_name = (logical_port.ofp_port.name,
395 logical_port.ofp_port.port_no)
396 return ofp_port_name
Shad Ansarief2029b2019-02-25 09:45:54 -0800397
Shad Ansarif9b9b892019-02-27 17:10:27 -0800398 # #######################################################################
Shad Ansari8d068642019-02-28 23:09:03 -0800399 # Methods used by Alarm and Statistics Manager (TODO - re-visit)
400 # #######################################################################
401
402 def _adapter_name(self):
403 return self.adapter_agent.adapter_name
404
405 def _device_id(self):
406 return self.device.id
407
408 def _resolve_onu_id(self, onu_id, port_intf_id):
409 try:
410 onu_device = None
411 onu_device = self.adapter_agent.get_child_device(
412 self.device_id,
413 parent_port_no=self.platform.intf_id_to_port_no(
414 port_intf_id, Port.PON_OLT),
415 onu_id=onu_id)
416 except Exception as inner:
417 self.log.exception('resolve-onu-id', errmsg=inner.message)
418
419 return onu_device
420
421 def create_alarm(self, **kwargs):
harshawasthi90c915a442019-03-06 06:35:23 -0800422 return self.adapter_agent.create_alarm(**kwargs)
Shad Ansari8d068642019-02-28 23:09:03 -0800423
424 def submit_alarm(self, alarm_event):
425 self.adapter_agent.submit_alarm(self.device.id, alarm_event)
426
427 # #######################################################################
Shad Ansarief2029b2019-02-25 09:45:54 -0800428 # Private functions
Shad Ansarif9b9b892019-02-27 17:10:27 -0800429 #
430 # These functions are prefixed with __ to denote that they are private
431 # to openolt_data_model and should not be called directly from the adapter.
432 # #######################################################################
Shad Ansarief2029b2019-02-25 09:45:54 -0800433
Shad Ansarif9b9b892019-02-27 17:10:27 -0800434 def __disable_logical_device(self):
Shad Ansari134947d2019-02-14 23:45:03 -0800435 oper_state = OperStatus.UNKNOWN
436 connect_state = ConnectStatus.UNREACHABLE
437
Shad Ansarief2029b2019-02-25 09:45:54 -0800438 onu_devices = self.adapter_agent.get_child_devices(self.device.id)
439 for onu_device in onu_devices:
Shad Ansari134947d2019-02-14 23:45:03 -0800440 onu_adapter_agent = \
441 registry('adapter_loader').get_agent(onu_device.adapter)
442 onu_adapter_agent.update_interface(onu_device,
443 {'oper_state': 'down'})
Shad Ansari18f60372019-03-04 10:59:41 -0800444 self.__onu_ports_down(onu_device)
Shad Ansari134947d2019-02-14 23:45:03 -0800445
446 # Children devices
447 self.adapter_agent.update_child_devices_state(
Shad Ansarid5577972019-02-22 09:35:03 -0800448 self.device.id, oper_status=oper_state,
Shad Ansari134947d2019-02-14 23:45:03 -0800449 connect_status=connect_state)
450 # Device Ports
Shad Ansarid5577972019-02-22 09:35:03 -0800451 device_ports = self.adapter_agent.get_ports(self.device.id,
Shad Ansari134947d2019-02-14 23:45:03 -0800452 Port.ETHERNET_NNI)
453 logical_ports_ids = [port.label for port in device_ports]
Shad Ansarid5577972019-02-22 09:35:03 -0800454 device_ports += self.adapter_agent.get_ports(self.device.id,
Shad Ansari134947d2019-02-14 23:45:03 -0800455 Port.PON_OLT)
456
457 for port in device_ports:
458 port.oper_status = oper_state
Shad Ansarid5577972019-02-22 09:35:03 -0800459 self.adapter_agent.add_port(self.device.id, port)
Shad Ansari134947d2019-02-14 23:45:03 -0800460
461 # Device logical port
462 for logical_port_id in logical_ports_ids:
463 logical_port = self.adapter_agent.get_logical_port(
Shad Ansarid5577972019-02-22 09:35:03 -0800464 self.logical_device_id, logical_port_id)
Shad Ansari134947d2019-02-14 23:45:03 -0800465 logical_port.ofp_port.state = OFPPS_LINK_DOWN
466 self.adapter_agent.update_logical_port(self.logical_device_id,
467 logical_port)
Shad Ansarid5577972019-02-22 09:35:03 -0800468 self.device.oper_status = oper_state
469 self.device.connect_status = connect_state
470 self.adapter_agent.update_device(self.device)
Shad Ansari134947d2019-02-14 23:45:03 -0800471
Shad Ansarif9b9b892019-02-27 17:10:27 -0800472 def __add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari134947d2019-02-14 23:45:03 -0800473 self.log.info('adding-logical-port', port_no=port_no)
474
475 label = OpenoltUtils.port_name(port_no, Port.ETHERNET_NNI)
476
477 cap = OFPPF_1GB_FD | OFPPF_FIBER
478 curr_speed = OFPPF_1GB_FD
479 max_speed = OFPPF_1GB_FD
480
481 if oper_state == OperStatus.ACTIVE:
482 of_oper_state = OFPPS_LIVE
483 else:
484 of_oper_state = OFPPS_LINK_DOWN
485
486 ofp = ofp_port(
487 port_no=port_no,
488 hw_addr=mac_str_to_tuple(
489 OpenoltUtils.make_mac_from_port_no(port_no)),
490 name=label, config=0, state=of_oper_state, curr=cap,
491 advertised=cap, peer=cap, curr_speed=curr_speed,
492 max_speed=max_speed)
493
494 ofp_stats = ofp_port_stats(port_no=port_no)
495
496 logical_port = LogicalPort(
Shad Ansarid5577972019-02-22 09:35:03 -0800497 id=label, ofp_port=ofp, device_id=self.device.id,
Shad Ansari134947d2019-02-14 23:45:03 -0800498 device_port_no=port_no, root_port=True,
499 ofp_port_stats=ofp_stats)
500
Shad Ansarid5577972019-02-22 09:35:03 -0800501 self.adapter_agent.add_logical_port(self.logical_device_id,
502 logical_port)
503
Shad Ansarif9b9b892019-02-27 17:10:27 -0800504 def __delete_logical_port(self, child_device):
Shad Ansarief2029b2019-02-25 09:45:54 -0800505 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
Shad Ansari8d068642019-02-28 23:09:03 -0800506 self.logical_device_id))
Shad Ansarief2029b2019-02-25 09:45:54 -0800507 for logical_port in logical_ports:
508 if logical_port.device_id == child_device.id:
509 self.log.debug('delete-logical-port',
510 onu_device_id=child_device.id,
511 logical_port=logical_port)
512 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
513 child_device, logical_port)
514 self.adapter_agent.delete_logical_port(
Shad Ansari8d068642019-02-28 23:09:03 -0800515 self.logical_device_id, logical_port)
Shad Ansarief2029b2019-02-25 09:45:54 -0800516 return
Shad Ansarid5577972019-02-22 09:35:03 -0800517
Shad Ansarif9b9b892019-02-27 17:10:27 -0800518 def __onu_ports_down(self, onu_device):
Shad Ansarief2029b2019-02-25 09:45:54 -0800519 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
520 for onu_port in onu_ports:
521 self.log.debug('onu-ports-down', onu_port=onu_port)
522 onu_port_id = onu_port.label
523 try:
524 onu_logical_port = self.adapter_agent.get_logical_port(
Shad Ansari8d068642019-02-28 23:09:03 -0800525 logical_device_id=self.logical_device_id,
Shad Ansarief2029b2019-02-25 09:45:54 -0800526 port_id=onu_port_id)
527 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
528 self.adapter_agent.update_logical_port(
Shad Ansari8d068642019-02-28 23:09:03 -0800529 logical_device_id=self.logical_device_id,
Shad Ansarief2029b2019-02-25 09:45:54 -0800530 port=onu_logical_port)
531 self.log.debug('cascading-oper-state-to-port-and-logical-port')
532 except KeyError as e:
533 self.log.error('matching-onu-port-label-invalid',
534 onu_id=onu_device.id, olt_id=self.device.id,
535 onu_ports=onu_ports, onu_port_id=onu_port_id,
536 error=e)
Shad Ansarid5577972019-02-22 09:35:03 -0800537
Shad Ansarif9b9b892019-02-27 17:10:27 -0800538 def __add_port(self, intf_id, port_type, oper_status):
Shad Ansarief2029b2019-02-25 09:45:54 -0800539 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansarid5577972019-02-22 09:35:03 -0800540
Shad Ansarief2029b2019-02-25 09:45:54 -0800541 label = OpenoltUtils.port_name(port_no, port_type, intf_id)
Shad Ansarid5577972019-02-22 09:35:03 -0800542
Shad Ansarief2029b2019-02-25 09:45:54 -0800543 self.log.debug('adding-port', port_no=port_no, label=label,
544 port_type=port_type)
Shad Ansarid5577972019-02-22 09:35:03 -0800545
Shad Ansarief2029b2019-02-25 09:45:54 -0800546 port = Port(port_no=port_no, label=label, type=port_type,
547 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansarid5577972019-02-22 09:35:03 -0800548
Shad Ansarief2029b2019-02-25 09:45:54 -0800549 self.adapter_agent.add_port(self.device.id, port)
Shad Ansarid5577972019-02-22 09:35:03 -0800550
Shad Ansarief2029b2019-02-25 09:45:54 -0800551 return port_no, label
Shad Ansarid5577972019-02-22 09:35:03 -0800552
Shad Ansarif9b9b892019-02-27 17:10:27 -0800553 def __get_uni_ofp_port_name(self, child_device):
Shad Ansarief2029b2019-02-25 09:45:54 -0800554 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
Shad Ansari8d068642019-02-28 23:09:03 -0800555 self.logical_device_id))
Shad Ansarief2029b2019-02-25 09:45:54 -0800556 for logical_port in logical_ports:
557 if logical_port.device_id == child_device.id:
558 return logical_port.ofp_port.name
559 return None
Shad Ansarif9b9b892019-02-27 17:10:27 -0800560
561 def __is_uni_port(self, port_no):
562 try:
563 port = self.adapter_agent.get_logical_port(
564 self.logical_device_id, 'uni-{}'.format(port_no))
565 if port is not None:
566 return (not port.root_port), port.device_id
567 else:
568 return False, None
569 except Exception as e:
570 self.log.error("error-retrieving-port", e=e)
571 return False, None