Skip to content

Commit f071cf7

Browse files
samaityzhenggen-xu
authored andcommitted
Adding a new function to create BRKOUT_CFG TABLE in config db (#17)
* Adding a new function to create BRKOUT_CFG TABLE in config db Signed-off-by: Sangita Maity <sangitamaity0211@gmail.com> * Speed fix and portconfig file check Signed-off-by: Sangita Maity <sangitamaity0211@gmail.com> * Minor update for speed Signed-off-by: Sangita Maity <sangitamaity0211@gmail.com>
1 parent 5bfb34a commit f071cf7

2 files changed

Lines changed: 59 additions & 14 deletions

File tree

src/sonic-config-engine/portconfig.py

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,27 @@
2121

2222
PORT_STR = "Ethernet"
2323
BRKOUT_MODE = "default_brkout_mode"
24+
CUR_BRKOUT_MODE = "brkout_mode"
2425

2526
BRKOUT_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+
2745
def 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

src/sonic-config-engine/sonic-cfggen

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ from functools import partial
3737
from minigraph import minigraph_encoder
3838
from minigraph import parse_xml
3939
from minigraph import parse_device_desc_xml
40-
from portconfig import get_port_config
40+
from portconfig import get_port_config, get_breakout_mode
4141
from sonic_device_util import get_machine_info
4242
from sonic_device_util import get_platform_info
4343
from sonic_device_util import get_system_mac
@@ -233,6 +233,10 @@ def main():
233233
sys.exit(1)
234234
deep_update(data, {'PORT': ports})
235235

236+
brkout_table = get_breakout_mode(hwsku, platform, args.port_config)
237+
if brkout_table is not None:
238+
deep_update(data, {'BREAKOUT_CFG': brkout_table})
239+
236240
if args.minigraph != None:
237241
minigraph = args.minigraph
238242
if platform:

0 commit comments

Comments
 (0)