2121
2222PORT_STR = "Ethernet"
2323BRKOUT_MODE = "default_brkout_mode"
24+ CUR_BRKOUT_MODE = "brkout_mode"
2425
2526BRKOUT_PATTERN = r'(\d{1,3})x(\d{1,3}G)(\[\d{1,3}G\])?(\((\d{1,3})\))?'
2627
28+ #
29+ # Helper Functions
30+ #
31+ def readJson (port_config_file ):
32+ # Read 'platform.json' file
33+ try :
34+ with open (port_config_file ) as fp :
35+ try :
36+ data = json .load (fp )
37+ except json .JSONDecodeError :
38+ print ("Json file does not exist" )
39+ port_dict = ast .literal_eval (json .dumps (data ))
40+ return port_dict
41+ except :
42+ print ("error occurred while parsing json:" , sys .exc_info ()[1 ])
43+ return None
44+
2745def db_connect_configdb ():
2846 """
2947 Connect to configdb
@@ -70,7 +88,7 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None):
7088 config_db = db_connect_configdb ()
7189
7290 # If available, Read from CONFIG DB first
73- if config_db is not None :
91+ if config_db is not None and port_config_file is None :
7492
7593 port_data = config_db .get_table ("PORT" )
7694 if port_data is not None :
@@ -139,9 +157,18 @@ def gen_port_config(ports, parent_intf_id, index, alias_at_lanes, lanes, k, off
139157 ports [intf_name ]['alias' ] = alias_at_lanes .split ("," )[alias_start ]
140158 ports [intf_name ]['lanes' ] = ',' .join (lanes .split ("," )[alias_start :alias_start + step ])
141159 if speed :
142- ports [intf_name ]['speed' ] = speed
160+ speed_pat = re .search ("^((\d+)G|\d+)$" , speed .upper ())
161+ if speed_pat is None :
162+ raise Exception ('{} speed is not Supported...' .format (speed ))
163+ speed_G , speed_orig = speed_pat .group (2 ), speed_pat .group (1 )
164+ if speed_G :
165+ conv_speed = int (speed_G )* 1000
166+ else :
167+ conv_speed = int (speed_orig )
168+ ports [intf_name ]['speed' ] = str (conv_speed )
143169 else :
144170 raise Exception ('Regex return for speed is None...' )
171+
145172 ports [intf_name ]['index' ] = index .split ("," )[alias_start ]
146173 ports [intf_name ]['admin_status' ] = "up"
147174
@@ -157,17 +184,9 @@ def parse_platform_json_file(port_config_file, interface_name=None, target_brkou
157184 ports = {}
158185 port_alias_map = {}
159186
160- # Read 'platform.json' file
161- try :
162- with open (port_config_file ) as fp :
163- try :
164- data = json .load (fp )
165- except json .JSONDecodeError as e :
166- raise Exception ("JSONDecodeError:" , e )
167- global port_dict
168- port_dict = ast .literal_eval (json .dumps (data ))
169- except :
170- print ("error occurred while parsing json:" , sys .exc_info ()[1 ])
187+ port_dict = readJson (port_config_file )
188+ if not port_dict :
189+ raise Exception ("port_dict is none" )
171190
172191 for intf in port_dict :
173192 if str (interface_name ) == intf :
@@ -222,3 +241,25 @@ def parse_platform_json_file(port_config_file, interface_name=None, target_brkou
222241 for i in ports .keys ():
223242 port_alias_map [ports [i ]["alias" ]]= i
224243 return (ports , port_alias_map )
244+
245+
246+ def get_breakout_mode (hwsku = None , platform = None , port_config_file = None ):
247+ if not port_config_file :
248+ port_config_file = get_port_config_file_name (hwsku , platform )
249+ if not port_config_file :
250+ return None
251+ if port_config_file .endswith ('.json' ):
252+ return parse_breakout_mode (port_config_file )
253+ else :
254+ return None
255+
256+ def parse_breakout_mode (port_config_file ):
257+ brkout_table = {}
258+ port_dict = readJson (port_config_file )
259+ if not port_dict :
260+ raise Exception ("Port_dict is empty" )
261+
262+ for intf in port_dict :
263+ brkout_table [intf ] = {}
264+ brkout_table [intf ][CUR_BRKOUT_MODE ] = port_dict [intf ][BRKOUT_MODE ]
265+ return brkout_table
0 commit comments