Skip to content

Commit 4040326

Browse files
authored
[sfputilbase | sfputilhelper] Add support of platform.json (#72)
Signed-off-by: Sangita Maity <[email protected]>
1 parent 6e52993 commit 4040326

2 files changed

Lines changed: 111 additions & 9 deletions

File tree

sonic_platform_base/sonic_sfp/sfputilbase.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@
1717
from .sff8436 import sff8436InterfaceId # Dot module supports both Python 2 and Python 3 using explicit relative import methods
1818
from .sff8436 import sff8436Dom # Dot module supports both Python 2 and Python 3 using explicit relative import methods
1919
from .inf8628 import inf8628InterfaceId # Dot module supports both Python 2 and Python 3 using explicit relative import methods
20+
from portconfig import get_port_config
21+
from collections import OrderedDict
22+
from natsort import natsorted
23+
from sonic_daemon_base.daemon_base import DaemonBase
24+
import sys
2025
except ImportError as e:
2126
raise ImportError("%s - required module not found" % str(e))
2227

28+
# Global Variable
29+
PLATFORM_JSON = 'platform.json'
30+
PORT_CONFIG_INI = 'portconfig.ini'
31+
2332
# definitions of the offset and width for values in XCVR info eeprom
2433
XCVR_INTFACE_BULK_OFFSET = 0
2534
XCVR_INTFACE_BULK_WIDTH_QSFP = 20
@@ -370,14 +379,59 @@ def read_porttab_mappings(self, porttabfile):
370379
first = 1
371380
port_pos_in_file = 0
372381
parse_fmt_port_config_ini = False
382+
parse_fmt_platform_json = False
383+
384+
parse_fmt_port_config_ini = (os.path.basename(porttabfile) == PORT_CONFIG_INI)
385+
parse_fmt_platform_json = (os.path.basename(porttabfile) == PLATFORM_JSON)
386+
387+
(platform, hwsku) = DaemonBase().get_platform_and_hwsku()
388+
if(parse_fmt_platform_json):
389+
ports, _ = get_port_config(hwsku, platform)
390+
if not ports:
391+
print('Failed to get port config', file=sys.stderr)
392+
sys.exit(1)
393+
else:
394+
logical_list = []
395+
for intf in ports.keys():
396+
logical_list.append(intf)
397+
398+
logical = natsorted(logical_list, key=lambda y: y.lower())
399+
logical_to_bcm, logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict(), OrderedDict()
400+
401+
for intf_name in logical:
402+
bcm_port = str(port_pos_in_file)
403+
logical_to_bcm[intf_name] = "xe"+ bcm_port
404+
405+
if 'index' in ports[intf_name].keys():
406+
fp_port_index = ports[intf_name]['index']
407+
logical_to_physical[intf_name] = [fp_port_index]
408+
409+
if physical_to_logical.get(fp_port_index) is None:
410+
physical_to_logical[fp_port_index] = [intf_name]
411+
else:
412+
physical_to_logical[fp_port_index].append(intf_name)
413+
414+
port_pos_in_file +=1
415+
416+
self.logical = logical
417+
self.logical_to_bcm = logical_to_bcm
418+
self.logical_to_physical = logical_to_physical
419+
self.physical_to_logical = physical_to_logical
420+
421+
"""
422+
print("logical: {}".format(self.logical))
423+
print("logical to bcm: {}".format(self.logical_to_bcm))
424+
print("logical to physical: {}".format(self.logical_to_physical))
425+
print("physical to logical: {}".format( self.physical_to_logical))
426+
"""
427+
return None
428+
373429

374430
try:
375431
f = open(porttabfile)
376432
except:
377433
raise
378434

379-
parse_fmt_port_config_ini = (os.path.basename(porttabfile) == "port_config.ini")
380-
381435
# Read the porttab file and generate dicts
382436
# with mapping for future reference.
383437
#
@@ -432,8 +486,7 @@ def read_porttab_mappings(self, porttabfile):
432486
if physical_to_logical.get(fp_port_index) is None:
433487
physical_to_logical[fp_port_index] = [portname]
434488
else:
435-
physical_to_logical[fp_port_index].append(
436-
portname)
489+
physical_to_logical[fp_port_index].append(portname)
437490

438491
last_fp_port_index = fp_port_index
439492
last_portname = portname

sonic_platform_base/sonic_sfp/sfputilhelper.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
import binascii
1111
import os
1212
import re
13+
from portconfig import get_port_config
14+
from collections import OrderedDict
15+
from natsort import natsorted
16+
from sonic_daemon_base.daemon_base import DaemonBase
17+
import sys
1318
except ImportError as e:
1419
raise ImportError("%s - required module not found" % str(e))
1520

21+
# Global Variable
22+
PLATFORM_JSON = 'platform.json'
23+
PORT_CONFIG_INI = 'portconfig.ini'
24+
1625
class SfpUtilHelper(object):
1726
# List to specify filter for sfp_ports
1827
# Needed by platforms like dni-6448 which
@@ -41,14 +50,56 @@ def read_porttab_mappings(self, porttabfile):
4150
first = 1
4251
port_pos_in_file = 0
4352
parse_fmt_port_config_ini = False
53+
parse_fmt_platform_json = False
54+
55+
parse_fmt_port_config_ini = (os.path.basename(porttabfile) == PORT_CONFIG_INI)
56+
parse_fmt_platform_json = (os.path.basename(porttabfile) == PLATFORM_JSON)
57+
58+
(platform, hwsku) = DaemonBase().get_platform_and_hwsku()
59+
if(parse_fmt_platform_json):
60+
ports, _ = get_port_config(hwsku, platform)
61+
if not ports:
62+
print('Failed to get port config', file=sys.stderr)
63+
sys.exit(1)
64+
else:
65+
logical_list = []
66+
for intf in ports.keys():
67+
logical_list.append(intf)
68+
69+
logical = natsorted(logical_list, key=lambda y: y.lower())
70+
logical_to_physical, physical_to_logical = OrderedDict(), OrderedDict()
71+
72+
for intf_name in logical:
73+
bcm_port = str(port_pos_in_file)
74+
75+
if 'index' in ports[intf_name].keys():
76+
fp_port_index = ports[intf_name]['index']
77+
logical_to_physical[intf_name] = [fp_port_index]
78+
79+
if physical_to_logical.get(fp_port_index) is None:
80+
physical_to_logical[fp_port_index] = [intf_name]
81+
else:
82+
physical_to_logical[fp_port_index].append(intf_name)
83+
84+
port_pos_in_file +=1
85+
86+
self.logical = logical
87+
self.logical_to_physical = logical_to_physical
88+
self.physical_to_logical = physical_to_logical
89+
90+
"""
91+
print("logical: {}".format(self.logical))
92+
print("logical to physical: {}".format(self.logical_to_physical))
93+
print("physical to logical: {}".format( self.physical_to_logical))
94+
"""
95+
return None
96+
4497

4598
try:
4699
f = open(porttabfile)
47100
except:
48101
raise
49102

50-
parse_fmt_port_config_ini = (os.path.basename(porttabfile) == "port_config.ini")
51-
52103
# Read the porttab file and generate dicts
53104
# with mapping for future reference.
54105
#
@@ -102,8 +153,7 @@ def read_porttab_mappings(self, porttabfile):
102153
if physical_to_logical.get(fp_port_index) is None:
103154
physical_to_logical[fp_port_index] = [portname]
104155
else:
105-
physical_to_logical[fp_port_index].append(
106-
portname)
156+
physical_to_logical[fp_port_index].append(portname)
107157

108158
last_fp_port_index = fp_port_index
109159
last_portname = portname
@@ -119,7 +169,6 @@ def read_porttab_mappings(self, porttabfile):
119169
print("logical to physical: " + self.logical_to_physical)
120170
print("physical to logical: " + self.physical_to_logical)
121171
"""
122-
123172
def get_physical_to_logical(self, port_num):
124173
"""Returns list of logical ports for the given physical port"""
125174

0 commit comments

Comments
 (0)