Skip to content

Commit

Permalink
Added support for front panel port prefix regex
Browse files Browse the repository at this point in the history
  • Loading branch information
itamar-talmon authored and Itamar Talmon committed Jan 12, 2023
1 parent 97161ae commit bd60757
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')
Expand All @@ -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!")
Expand Down Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion src/sonic-device-data/tests/hwsku_json_checker
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion src/sonic-device-data/tests/platform_json_checker
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 14 additions & 2 deletions src/sonic-py-common/sonic_py_common/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit bd60757

Please sign in to comment.