Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/sonic-py-common/sonic_py_common/interface.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -12,6 +16,8 @@
"Ethernet-Backplane": "Ethernet-BP"
}

VLAN_SUB_INTERFACE_SEPARATOR = '.'

def front_panel_prefix():
"""
Retrieves the SONIC front panel interface name prefix.
Expand Down Expand Up @@ -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 ""
12 changes: 12 additions & 0 deletions src/sonic-py-common/sonic_py_common/multi_asic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 56 additions & 0 deletions src/sonic-py-common/tests/interface_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
import sys

# TODO: Remove this if/else block once we no longer support Python 2
if sys.version_info.major == 3:
from unittest import mock
else:
# Expect the 'mock' package for python 2
# https://pypi.python.org/pypi/mock
import mock

from sonic_py_common import interface


# TODO: Remove this if/else block once we no longer support Python 2
if sys.version_info.major == 3:
BUILTINS = "builtins"
else:
BUILTINS = "__builtin__"

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