From ca3e71da0a35da06be90e641636b246efd7a24d2 Mon Sep 17 00:00:00 2001 From: judyjoseph <53951155+judyjoseph@users.noreply.github.com> Date: Sun, 30 Aug 2020 08:24:29 -0700 Subject: [PATCH] [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. --- .../sonic_py_common/interface.py | 42 +++++++++++++++++++ .../sonic_py_common/multi_asic.py | 12 ++++++ src/sonic-py-common/tests/interface_test.py | 41 ++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/sonic-py-common/tests/interface_test.py diff --git a/src/sonic-py-common/sonic_py_common/interface.py b/src/sonic-py-common/sonic_py_common/interface.py index 9136b71f78..10452c6131 100644 --- a/src/sonic-py-common/sonic_py_common/interface.py +++ b/src/sonic-py-common/sonic_py_common/interface.py @@ -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 "" diff --git a/src/sonic-py-common/sonic_py_common/multi_asic.py b/src/sonic-py-common/sonic_py_common/multi_asic.py index 78bc6d72ba..d5925ea37d 100644 --- a/src/sonic-py-common/sonic_py_common/multi_asic.py +++ b/src/sonic-py-common/sonic_py_common/multi_asic.py @@ -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 diff --git a/src/sonic-py-common/tests/interface_test.py b/src/sonic-py-common/tests/interface_test.py new file mode 100644 index 0000000000..bb90479733 --- /dev/null +++ b/src/sonic-py-common/tests/interface_test.py @@ -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")