[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:
judyjoseph 2020-08-30 08:24:29 -07:00 committed by GitHub
parent f973d90c76
commit ca3e71da0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 0 deletions

View File

@ -1,3 +1,7 @@
"""
SONiC interface types and access functions.
"""
"""
Dictionary of SONIC interface name prefixes. Each entry in the format
"Human readable interface string":"Sonic interface prefix"
@ -12,6 +16,8 @@ SONIC_INTERFACE_PREFIXES = {
"Ethernet-Backplane": "Ethernet-BP"
}
VLAN_SUB_INTERFACE_SEPARATOR = '.'
def front_panel_prefix():
"""
Retrieves the SONIC front panel interface name prefix.
@ -41,3 +47,39 @@ def loopback_prefix():
Retrieves the SONIC Loopback interface name prefix.
"""
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 ""

View File

@ -344,3 +344,15 @@ def get_asic_index_from_namespace(namespace):
return int(get_asic_id_from_name(namespace))
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

View 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")