diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index ea9985a3beb0..a02f49d169ad 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -238,7 +238,8 @@ def __hash__(self): return hash((self.num_ports, tuple(self.supported_speed), self.num_assigned_lanes)) def __init__(self, name, bmode, properties): - self._interface_base_id = int(name.replace(PORT_STR, '')) + self._name = name + self._front_panel_prefix, self._interface_base_id = self._parse_name_to_prefix_and_id() self._properties = properties self._lanes = properties ['lanes'].split(',') self._indexes = properties ['index'].split(',') @@ -254,6 +255,12 @@ def __init__(self, name, bmode, properties): if not self._breakout_capabilities: raise RuntimeError("Unsupported breakout mode {}!".format(bmode)) + def _parse_name_to_prefix_and_id(self): + match = re.match(swsscommon.FRONT_PANEL_PORT_REGEX, self._name) + if not match: + raise RuntimeError("Failed to parse front panel prefix of {}!", self._name) + return match.group(1), int(match.group(2)) + def _re_group_to_entry(self, group): if len(group) != BRKOUT_PATTERN_GROUPS: raise RuntimeError("Unsupported breakout mode format!") @@ -300,7 +307,7 @@ def get_config(self): lanes_per_port = entry.num_assigned_lanes // entry.num_ports for port in range(entry.num_ports): - interface_name = PORT_STR + str(self._interface_base_id + lane_id) + interface_name = self._front_panel_prefix + str(self._interface_base_id + lane_id) lanes = self._lanes[lane_id:lane_id + lanes_per_port] diff --git a/src/sonic-device-data/tests/hwsku_json_checker b/src/sonic-device-data/tests/hwsku_json_checker index ee15c6fb4b5e..73015872c7e7 100755 --- a/src/sonic-device-data/tests/hwsku_json_checker +++ b/src/sonic-device-data/tests/hwsku_json_checker @@ -8,7 +8,7 @@ import sys # Global variable PORT_ATTRIBUTES = ["default_brkout_mode"] OPTIONAL_PORT_ATTRIBUTES = ["fec", "autoneg", "port_type"] -PORT_REG = "Ethernet(\d+)" +PORT_REG = "(Ethernet)(\d+)" HWSKU_JSON = '*hwsku.json' INTF_KEY = "interfaces" diff --git a/src/sonic-device-data/tests/platform_json_checker b/src/sonic-device-data/tests/platform_json_checker index 7b92936b972f..c47e21894cbb 100755 --- a/src/sonic-device-data/tests/platform_json_checker +++ b/src/sonic-device-data/tests/platform_json_checker @@ -8,7 +8,8 @@ import sys # Global variable PORT_ATTRIBUTES = ["index", "lanes", "breakout_modes"] ATTR_LEN = len(PORT_ATTRIBUTES) -PORT_REG = "Ethernet(\d+)" +# PORT_REG should be taken from scheme.h FRONT_PANEL_PORT_PREFIX_REGEX +PORT_REG = "(Ethernet)(\d+)" PLATFORM_JSON = '*platform.json' INTF_KEY = "interfaces" CHASSIS_KEY = "chassis" diff --git a/src/sonic-py-common/sonic_py_common/interface.py b/src/sonic-py-common/sonic_py_common/interface.py index 286f28e531a2..758fb366e6d9 100644 --- a/src/sonic-py-common/sonic_py_common/interface.py +++ b/src/sonic-py-common/sonic_py_common/interface.py @@ -7,8 +7,12 @@ "Human readable interface string":"Sonic interface prefix" Currently this is a static mapping, but in future could be stored as metadata in DB. """ +import re +from swsscommon.swsscommon import FRONT_PANEL_PORT_PREFIX_REGEX + SONIC_INTERFACE_PREFIXES = { + "FrontPanel" : FRONT_PANEL_PORT_PREFIX_REGEX, "Ethernet-FrontPanel": "Ethernet", "PortChannel": "PortChannel", "Vlan": "Vlan", @@ -22,6 +26,14 @@ VLAN_SUB_INTERFACE_SEPARATOR = '.' + +def front_panel_prefix_regex(): + """ + Retrieves the SONIC front panel interface name prefix regex. + """ + return SONIC_INTERFACE_PREFIXES["FrontPanel"] + + def front_panel_prefix(): """ Retrieves the SONIC front panel interface name prefix. @@ -79,7 +91,7 @@ def portchannel_subinterface_prefix(): def get_interface_table_name(interface_name): """Get table name by interface_name prefix """ - if interface_name.startswith(front_panel_prefix()): + if re.match(front_panel_prefix_regex(), interface_name): if VLAN_SUB_INTERFACE_SEPARATOR in interface_name: return "VLAN_SUB_INTERFACE" return "INTERFACE" @@ -100,7 +112,7 @@ def get_interface_table_name(interface_name): def get_port_table_name(interface_name): """Get table name by port_name prefix """ - if interface_name.startswith(front_panel_prefix()): + if re.match(front_panel_prefix_regex(), interface_name): if VLAN_SUB_INTERFACE_SEPARATOR in interface_name: return "VLAN_SUB_INTERFACE" return "PORT"