[sonic-py-common] Relocate some functions from sonic-utilities (#5269)
* Relocate interface related common functions to py-common * Add unit tests for interface API's.
This commit is contained in:
parent
f973d90c76
commit
ca3e71da0a
@ -1,3 +1,7 @@
|
|||||||
|
"""
|
||||||
|
SONiC interface types and access functions.
|
||||||
|
"""
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Dictionary of SONIC interface name prefixes. Each entry in the format
|
Dictionary of SONIC interface name prefixes. Each entry in the format
|
||||||
"Human readable interface string":"Sonic interface prefix"
|
"Human readable interface string":"Sonic interface prefix"
|
||||||
@ -12,6 +16,8 @@ SONIC_INTERFACE_PREFIXES = {
|
|||||||
"Ethernet-Backplane": "Ethernet-BP"
|
"Ethernet-Backplane": "Ethernet-BP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VLAN_SUB_INTERFACE_SEPARATOR = '.'
|
||||||
|
|
||||||
def front_panel_prefix():
|
def front_panel_prefix():
|
||||||
"""
|
"""
|
||||||
Retrieves the SONIC front panel interface name prefix.
|
Retrieves the SONIC front panel interface name prefix.
|
||||||
@ -41,3 +47,39 @@ def loopback_prefix():
|
|||||||
Retrieves the SONIC Loopback interface name prefix.
|
Retrieves the SONIC Loopback interface name prefix.
|
||||||
"""
|
"""
|
||||||
return SONIC_INTERFACE_PREFIXES["Loopback"]
|
return SONIC_INTERFACE_PREFIXES["Loopback"]
|
||||||
|
|
||||||
|
def get_interface_table_name(interface_name):
|
||||||
|
"""Get table name by interface_name prefix
|
||||||
|
"""
|
||||||
|
if interface_name.startswith(front_panel_prefix()):
|
||||||
|
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
|
||||||
|
return "VLAN_SUB_INTERFACE"
|
||||||
|
return "INTERFACE"
|
||||||
|
elif interface_name.startswith(portchannel_prefix()):
|
||||||
|
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
|
||||||
|
return "VLAN_SUB_INTERFACE"
|
||||||
|
return "PORTCHANNEL_INTERFACE"
|
||||||
|
elif interface_name.startswith(vlan_prefix()):
|
||||||
|
return "VLAN_INTERFACE"
|
||||||
|
elif interface_name.startswith(loopback_prefix()):
|
||||||
|
return "LOOPBACK_INTERFACE"
|
||||||
|
else:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def get_port_table_name(interface_name):
|
||||||
|
"""Get table name by port_name prefix
|
||||||
|
"""
|
||||||
|
if interface_name.startswith(front_panel_prefix()):
|
||||||
|
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
|
||||||
|
return "VLAN_SUB_INTERFACE"
|
||||||
|
return "PORT"
|
||||||
|
elif interface_name.startswith(portchannel_prefix()):
|
||||||
|
if VLAN_SUB_INTERFACE_SEPARATOR in interface_name:
|
||||||
|
return "VLAN_SUB_INTERFACE"
|
||||||
|
return "PORTCHANNEL"
|
||||||
|
elif interface_name.startswith(vlan_prefix()):
|
||||||
|
return "VLAN_INTERFACE"
|
||||||
|
elif interface_name.startswith(loopback_prefix()):
|
||||||
|
return "LOOPBACK_INTERFACE"
|
||||||
|
else:
|
||||||
|
return ""
|
||||||
|
@ -344,3 +344,15 @@ def get_asic_index_from_namespace(namespace):
|
|||||||
return int(get_asic_id_from_name(namespace))
|
return int(get_asic_id_from_name(namespace))
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
# Validate whether a given namespace name is valid in the device.
|
||||||
|
# This API is significant in multi-asic platforms.
|
||||||
|
def validate_namespace(namespace):
|
||||||
|
if not is_multi_asic():
|
||||||
|
return True
|
||||||
|
|
||||||
|
namespaces = get_all_namespaces()
|
||||||
|
if namespace in namespaces['front_ns'] + namespaces['back_ns']:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
41
src/sonic-py-common/tests/interface_test.py
Normal file
41
src/sonic-py-common/tests/interface_test.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from sonic_py_common import interface
|
||||||
|
|
||||||
|
class TestInterface(object):
|
||||||
|
@classmethod
|
||||||
|
def setup_class(cls):
|
||||||
|
print("SETUP")
|
||||||
|
|
||||||
|
def test_get_interface_table_name(self):
|
||||||
|
result = interface.get_interface_table_name("Ethernet0")
|
||||||
|
assert result == "INTERFACE"
|
||||||
|
result = interface.get_interface_table_name("Ethernet0.100")
|
||||||
|
assert result == "VLAN_SUB_INTERFACE"
|
||||||
|
result = interface.get_interface_table_name("PortChannel0")
|
||||||
|
assert result == "PORTCHANNEL_INTERFACE"
|
||||||
|
result = interface.get_interface_table_name("PortChannel0.100")
|
||||||
|
assert result == "VLAN_SUB_INTERFACE"
|
||||||
|
result = interface.get_interface_table_name("Vlan100")
|
||||||
|
assert result == "VLAN_INTERFACE"
|
||||||
|
result = interface.get_interface_table_name("Loopback0")
|
||||||
|
assert result == "LOOPBACK_INTERFACE"
|
||||||
|
|
||||||
|
def test_get_port_table_name(self):
|
||||||
|
result = interface.get_port_table_name("Ethernet0")
|
||||||
|
assert result == "PORT"
|
||||||
|
result = interface.get_port_table_name("Ethernet0.100")
|
||||||
|
assert result == "VLAN_SUB_INTERFACE"
|
||||||
|
result = interface.get_port_table_name("PortChannel0")
|
||||||
|
assert result == "PORTCHANNEL"
|
||||||
|
result = interface.get_port_table_name("PortChannel0.100")
|
||||||
|
assert result == "VLAN_SUB_INTERFACE"
|
||||||
|
result = interface.get_port_table_name("Vlan100")
|
||||||
|
assert result == "VLAN_INTERFACE"
|
||||||
|
result = interface.get_port_table_name("Loopback0")
|
||||||
|
assert result == "LOOPBACK_INTERFACE"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def teardown_class(cls):
|
||||||
|
print("TEARDOWN")
|
Loading…
Reference in New Issue
Block a user