Skip to content

Commit 35bad16

Browse files
authored
[sfputil]Refactoring read_porttab_mappings (#264)
* Refactoring read_porttab_mappings Signed-off-by: Sudharsan Dhamal Gopalarathnam <[email protected]> * Addressing review comment
1 parent 83c4345 commit 35bad16

File tree

1 file changed

+27
-120
lines changed

1 file changed

+27
-120
lines changed

sonic_platform_base/sonic_sfp/sfputilhelper.py

+27-120
Original file line numberDiff line numberDiff line change
@@ -49,139 +49,46 @@ def read_porttab_mappings(self, porttabfile, asic_inst=0):
4949
logical = []
5050
logical_to_physical = {}
5151
physical_to_logical = {}
52-
last_fp_port_index = 0
53-
last_portname = ""
54-
first = 1
55-
port_pos_in_file = 0
56-
parse_fmt_port_config_ini = False
57-
parse_fmt_platform_json = False
58-
59-
parse_fmt_port_config_ini = (os.path.basename(porttabfile) == PORT_CONFIG_INI)
60-
parse_fmt_platform_json = (os.path.basename(porttabfile) == PLATFORM_JSON)
52+
fp_port_index = 1
6153

6254
(platform, hwsku) = device_info.get_platform_and_hwsku()
63-
if(parse_fmt_platform_json):
64-
ports, _, _ = get_port_config(hwsku, platform)
55+
ports, _, _ = get_port_config(hwsku, platform)
56+
57+
if not ports:
58+
ports, _, _ = get_port_config(hwsku, platform, porttabfile)
6559
if not ports:
6660
print('Failed to get port config', file=sys.stderr)
6761
sys.exit(1)
68-
else:
69-
logical_list = []
70-
for intf in ports.keys():
71-
logical_list.append(intf)
72-
73-
logical = natsorted(logical_list, key=lambda y: y.lower())
74-
logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict()
75-
76-
for intf_name in logical:
77-
bcm_port = str(port_pos_in_file)
78-
79-
if 'index' in ports[intf_name].keys():
80-
fp_port_index = int(ports[intf_name]['index'])
81-
logical_to_physical[intf_name] = [fp_port_index]
82-
83-
if physical_to_logical.get(fp_port_index) is None:
84-
physical_to_logical[fp_port_index] = [intf_name]
85-
else:
86-
physical_to_logical[fp_port_index].append(intf_name)
87-
88-
# Mapping of logical port names available on a system to ASIC instance
89-
self.logical_to_asic[intf_name] = asic_inst
90-
port_pos_in_file +=1
91-
92-
self.logical = logical
93-
self.logical_to_physical = logical_to_physical
94-
self.physical_to_logical = physical_to_logical
95-
96-
"""
97-
print("logical: {}".format(self.logical))
98-
print("logical to physical: {}".format(self.logical_to_physical))
99-
print("physical to logical: {}".format( self.physical_to_logical))
100-
"""
101-
return None
102-
103-
104-
try:
105-
f = open(porttabfile)
106-
except Exception:
107-
raise
108-
109-
# Read the porttab file and generate dicts
110-
# with mapping for future reference.
111-
#
112-
# TODO: Refactor this to use the portconfig.py module that now
113-
# exists as part of the sonic-config-engine package.
114-
title = []
115-
for line in f:
116-
line.strip()
117-
if re.search("^#", line) is not None:
118-
# The current format is: # name lanes alias index speed
119-
# Where the ordering of the columns can vary
120-
title = line.split()[1:]
121-
continue
122-
123-
# Parsing logic for 'port_config.ini' file
124-
if (parse_fmt_port_config_ini):
125-
# bcm_port is not explicitly listed in port_config.ini format
126-
# Currently we assume ports are listed in numerical order according to bcm_port
127-
# so we use the port's position in the file (zero-based) as bcm_port
128-
portname = line.split()[0]
129-
130-
# Ignore if this is an internal backplane interface and Inband interface
131-
if portname.startswith((backplane_prefix(), inband_prefix(), recirc_prefix())):
132-
continue
133-
134-
bcm_port = str(port_pos_in_file)
135-
136-
if "index" in title:
137-
fp_port_index = int(line.split()[title.index("index")])
138-
# Leave the old code for backward compatibility
139-
elif "asic_port_name" not in title and len(line.split()) >= 4:
140-
fp_port_index = int(line.split()[3])
141-
else:
142-
fp_port_index = portname.split("Ethernet").pop()
143-
fp_port_index = int(fp_port_index.split("s").pop(0))/4
144-
else: # Parsing logic for older 'portmap.ini' file
145-
(portname, bcm_port) = line.split("=")[1].split(",")[:2]
146-
147-
fp_port_index = portname.split("Ethernet").pop()
148-
fp_port_index = int(fp_port_index.split("s").pop(0))/4
149-
150-
if ((len(self.sfp_ports) > 0) and (fp_port_index not in self.sfp_ports)):
151-
continue
152-
153-
if first == 1:
154-
# Initialize last_[physical|logical]_port
155-
# to the first valid port
156-
last_fp_port_index = fp_port_index
157-
last_portname = portname
158-
first = 0
159-
160-
logical.append(portname)
16162

162-
# Mapping of logical port names available on a system to ASIC instance
163-
self.logical_to_asic[portname] = asic_inst
63+
logical_list = []
64+
for intf in ports.keys():
65+
logical_list.append(intf)
66+
67+
# Ignore if this is an internal backplane interface and Inband interface
68+
logical = [name for name in logical
69+
if not name.startswith((backplane_prefix(), inband_prefix(), recirc_prefix()))]
70+
logical = natsorted(logical_list, key=lambda y: y.lower())
71+
logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict()
72+
73+
for intf_name in logical:
74+
if 'index' in ports[intf_name].keys():
75+
fp_port_index = int(ports[intf_name]['index'])
76+
logical_to_physical[intf_name] = [fp_port_index]
16477

165-
logical_to_physical[portname] = [fp_port_index]
16678
if physical_to_logical.get(fp_port_index) is None:
167-
physical_to_logical[fp_port_index] = [portname]
79+
physical_to_logical[fp_port_index] = [intf_name]
16880
else:
169-
physical_to_logical[fp_port_index].append(portname)
81+
physical_to_logical[fp_port_index].append(intf_name)
17082

171-
last_fp_port_index = fp_port_index
172-
last_portname = portname
83+
# Mapping of logical port names available on a system to ASIC instance
84+
self.logical_to_asic[intf_name] = asic_inst
17385

174-
port_pos_in_file += 1
86+
self.logical = logical
87+
self.logical_to_physical = logical_to_physical
88+
self.physical_to_logical = physical_to_logical
17589

176-
self.logical.extend(logical)
177-
self.logical_to_physical.update(logical_to_physical)
178-
self.physical_to_logical.update(physical_to_logical)
90+
return None
17991

180-
"""
181-
print("logical: " + self.logical)
182-
print("logical to physical: " + self.logical_to_physical)
183-
print("physical to logical: " + self.physical_to_logical)
184-
"""
18592

18693
def read_all_porttab_mappings(self, platform_dir, num_asic_inst):
18794
# In multi asic scenario, get all the port_config files for different asics

0 commit comments

Comments
 (0)