diff --git a/sonic_platform_base/sonic_sfp/sfputilbase.py b/sonic_platform_base/sonic_sfp/sfputilbase.py index a4492ce69..ad3c07a07 100644 --- a/sonic_platform_base/sonic_sfp/sfputilbase.py +++ b/sonic_platform_base/sonic_sfp/sfputilbase.py @@ -17,9 +17,18 @@ from .sff8436 import sff8436InterfaceId # Dot module supports both Python 2 and Python 3 using explicit relative import methods from .sff8436 import sff8436Dom # Dot module supports both Python 2 and Python 3 using explicit relative import methods from .inf8628 import inf8628InterfaceId # Dot module supports both Python 2 and Python 3 using explicit relative import methods + from portconfig import get_port_config + from collections import OrderedDict + from natsort import natsorted + from sonic_daemon_base.daemon_base import DaemonBase + except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# Global Variable +PLATFORM_JSON = 'platform.json' +PORT_CONFIG_INI = 'portconfig.ini' + # definitions of the offset and width for values in XCVR info eeprom XCVR_INTFACE_BULK_OFFSET = 0 XCVR_INTFACE_BULK_WIDTH_QSFP = 20 @@ -369,14 +378,59 @@ def read_porttab_mappings(self, porttabfile): first = 1 port_pos_in_file = 0 parse_fmt_port_config_ini = False + parse_fmt_platform_json = False + + parse_fmt_port_config_ini = (os.path.basename(porttabfile) == PORT_CONFIG_INI) + parse_fmt_platform_json = (os.path.basename(porttabfile) == PLATFORM_JSON) + + (platform, hwsku) = DaemonBase.get_platform_and_hwsku() + if(parse_fmt_platform_json): + ports, _ = get_port_config(hwsku, platform) + if not ports: + print('Failed to get port config', file=sys.stderr) + sys.exit(1) + else: + logical_list = [] + for intf in ports.keys(): + logical_list.append(intf) + + logical = natsorted(logical_list, key=lambda y: y.lower()) + logical_to_bcm, logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict(), OrderedDict() + + for intf_name in logical: + bcm_port = str(port_pos_in_file) + logical_to_bcm[intf_name] = "xe"+ bcm_port + + if 'index' in ports[intf_name].keys(): + fp_port_index = ports[intf_name]['index'] + logical_to_physical[intf_name] = [fp_port_index] + + if physical_to_logical.get(fp_port_index) is None: + physical_to_logical[fp_port_index] = [intf_name] + else: + physical_to_logical[fp_port_index].append(intf_name) + + port_pos_in_file +=1 + + self.logical = logical + self.logical_to_bcm = logical_to_bcm + self.logical_to_physical = logical_to_physical + self.physical_to_logical = physical_to_logical + + """ + print("logical: {}".format(self.logical)) + print("logical to bcm: {}".format(self.logical_to_bcm)) + print("logical to physical: {}".format(self.logical_to_physical)) + print("physical to logical: {}".format( self.physical_to_logical)) + """ + sys.exit(0) + try: f = open(porttabfile) except: raise - parse_fmt_port_config_ini = (os.path.basename(porttabfile) == "port_config.ini") - # Read the porttab file and generate dicts # with mapping for future reference. # @@ -431,8 +485,7 @@ def read_porttab_mappings(self, porttabfile): if physical_to_logical.get(fp_port_index) is None: physical_to_logical[fp_port_index] = [portname] else: - physical_to_logical[fp_port_index].append( - portname) + physical_to_logical[fp_port_index].append(portname) last_fp_port_index = fp_port_index last_portname = portname diff --git a/sonic_platform_base/sonic_sfp/sfputilhelper.py b/sonic_platform_base/sonic_sfp/sfputilhelper.py index 49e9ef92d..7102c88c5 100644 --- a/sonic_platform_base/sonic_sfp/sfputilhelper.py +++ b/sonic_platform_base/sonic_sfp/sfputilhelper.py @@ -10,9 +10,18 @@ import binascii import os import re + from portconfig import get_port_config + from collections import OrderedDict + from natsort import natsorted + from sonic_daemon_base.daemon_base import DaemonBase + except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# Global Variable +PLATFORM_JSON = 'platform.json' +PORT_CONFIG_INI = 'portconfig.ini' + class SfpUtilHelper(object): # List to specify filter for sfp_ports # Needed by platforms like dni-6448 which @@ -41,14 +50,56 @@ def read_porttab_mappings(self, porttabfile): first = 1 port_pos_in_file = 0 parse_fmt_port_config_ini = False + parse_fmt_platform_json = False + + parse_fmt_port_config_ini = (os.path.basename(porttabfile) == PORT_CONFIG_INI) + parse_fmt_platform_json = (os.path.basename(porttabfile) == PLATFORM_JSON) + + (platform, hwsku) = DaemonBase.get_platform_and_hwsku() + if(parse_fmt_platform_json): + ports, _ = get_port_config(hwsku, platform) + if not ports: + print('Failed to get port config', file=sys.stderr) + sys.exit(1) + else: + logical_list = [] + for intf in ports.keys(): + logical_list.append(intf) + + logical = natsorted(logical_list, key=lambda y: y.lower()) + logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict() + + for intf_name in logical: + bcm_port = str(port_pos_in_file) + + if 'index' in ports[intf_name].keys(): + fp_port_index = ports[intf_name]['index'] + logical_to_physical[intf_name] = [fp_port_index] + + if physical_to_logical.get(fp_port_index) is None: + physical_to_logical[fp_port_index] = [intf_name] + else: + physical_to_logical[fp_port_index].append(intf_name) + + port_pos_in_file +=1 + + self.logical = logical + self.logical_to_physical = logical_to_physical + self.physical_to_logical = physical_to_logical + + """ + print("logical: {}".format(self.logical)) + print("logical to physical: {}".format(self.logical_to_physical)) + print("physical to logical: {}".format( self.physical_to_logical)) + """ + sys.exit(0) + try: f = open(porttabfile) except: raise - parse_fmt_port_config_ini = (os.path.basename(porttabfile) == "port_config.ini") - # Read the porttab file and generate dicts # with mapping for future reference. # @@ -102,8 +153,7 @@ def read_porttab_mappings(self, porttabfile): if physical_to_logical.get(fp_port_index) is None: physical_to_logical[fp_port_index] = [portname] else: - physical_to_logical[fp_port_index].append( - portname) + physical_to_logical[fp_port_index].append(portname) last_fp_port_index = fp_port_index last_portname = portname @@ -119,7 +169,6 @@ def read_porttab_mappings(self, porttabfile): print("logical to physical: " + self.logical_to_physical) print("physical to logical: " + self.physical_to_logical) """ - def get_physical_to_logical(self, port_num): """Returns list of logical ports for the given physical port"""