From 2051f41540de46d2bfbe922d584c79c25da01afc Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Wed, 11 Dec 2019 00:55:29 +0000 Subject: [PATCH 1/3] Adding a new function to create BRKOUT_CFG TABLE in config db Signed-off-by: Sangita Maity --- src/sonic-config-engine/portconfig.py | 54 +++++++++++++++++++++------ src/sonic-config-engine/sonic-cfggen | 6 ++- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index 183e9517211c..f3a37e15064f 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -21,9 +21,27 @@ PORT_STR = "Ethernet" BRKOUT_MODE = "default_brkout_mode" +CUR_BRKOUT_MODE = "brkout_mode" BRKOUT_PATTERN = r'(\d{1,3})x(\d{1,3}G)(\[\d{1,3}G\])?(\((\d{1,3})\))?' +# +# Helper Functions +# +def readJson(port_config_file): + # Read 'platform.json' file + try: + with open(port_config_file) as fp: + try: + data = json.load(fp) + except json.JSONDecodeError: + print("Json file does not exist") + port_dict = ast.literal_eval(json.dumps(data)) + return port_dict + except: + print("error occurred while parsing json:", sys.exc_info()[1]) + return None + def db_connect_configdb(): """ Connect to configdb @@ -157,17 +175,9 @@ def parse_platform_json_file(port_config_file, interface_name=None, target_brkou ports = {} port_alias_map = {} - # Read 'platform.json' file - try: - with open(port_config_file) as fp: - try: - data = json.load(fp) - except json.JSONDecodeError as e: - raise Exception("JSONDecodeError:", e) - global port_dict - port_dict = ast.literal_eval(json.dumps(data)) - except: - print("error occurred while parsing json:", sys.exc_info()[1]) + port_dict = readJson(port_config_file) + if not port_dict: + raise Exception("port_dict is none") for intf in port_dict: if str(interface_name) == intf: @@ -222,3 +232,25 @@ def parse_platform_json_file(port_config_file, interface_name=None, target_brkou for i in ports.keys(): port_alias_map[ports[i]["alias"]]= i return (ports, port_alias_map) + + +def get_breakout_mode(hwsku=None, platform=None, port_config_file=None): + if not port_config_file: + port_config_file = get_port_config_file_name(hwsku, platform) + if not port_config_file: + return None + if port_config_file.endswith('.json'): + return parse_breakout_mode(port_config_file) + else: + return None + +def parse_breakout_mode(port_config_file): + brkout_table = {} + port_dict = readJson(port_config_file) + if not port_dict: + raise Exception("Port_dict is empty") + + for intf in port_dict: + brkout_table[intf] = {} + brkout_table[intf][CUR_BRKOUT_MODE] = port_dict[intf][BRKOUT_MODE] + return brkout_table diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 7cff6c9fb384..13ce040d7884 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -37,7 +37,7 @@ from functools import partial from minigraph import minigraph_encoder from minigraph import parse_xml from minigraph import parse_device_desc_xml -from portconfig import get_port_config +from portconfig import get_port_config, get_breakout_mode from sonic_device_util import get_machine_info from sonic_device_util import get_platform_info from sonic_device_util import get_system_mac @@ -233,6 +233,10 @@ def main(): sys.exit(1) deep_update(data, {'PORT': ports}) + brkout_table = get_breakout_mode(hwsku, platform, args.port_config) + if brkout_table is not None: + deep_update(data, {'BREAKOUT_CFG': brkout_table}) + if args.minigraph != None: minigraph = args.minigraph if platform: From e87b26c71af82919f26e64b07c0e0879a895a143 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Thu, 12 Dec 2019 21:40:49 +0000 Subject: [PATCH 2/3] Speed fix and portconfig file check Signed-off-by: Sangita Maity --- src/sonic-config-engine/portconfig.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index f3a37e15064f..c3d82d6d7a73 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -88,7 +88,7 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None): config_db = db_connect_configdb() # If available, Read from CONFIG DB first - if config_db is not None: + if config_db is not None and port_config_file is None: port_data = config_db.get_table("PORT") if port_data is not None: @@ -157,7 +157,11 @@ def gen_port_config(ports, parent_intf_id, index, alias_at_lanes, lanes, k, off ports[intf_name]['alias'] = alias_at_lanes.split(",")[alias_start] ports[intf_name]['lanes'] = ','.join(lanes.split(",")[alias_start:alias_start+step]) if speed: - ports[intf_name]['speed'] = speed + if 'G' not in speed.upper(): + raise Exception('{} speed is not Supported...'.format(speed)) + num = re.split("G", speed.upper()) + conv_speed = int(num[0])*1000 + ports[intf_name]['speed'] = str(conv_speed) else: raise Exception('Regex return for speed is None...') ports[intf_name]['index'] = index.split(",")[alias_start] From b171d65ba597125309ae44c2b752b19049460e69 Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Fri, 13 Dec 2019 20:06:24 +0000 Subject: [PATCH 3/3] Minor update for speed Signed-off-by: Sangita Maity --- src/sonic-config-engine/portconfig.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index c3d82d6d7a73..5cc6d12b679e 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -157,13 +157,18 @@ def gen_port_config(ports, parent_intf_id, index, alias_at_lanes, lanes, k, off ports[intf_name]['alias'] = alias_at_lanes.split(",")[alias_start] ports[intf_name]['lanes'] = ','.join(lanes.split(",")[alias_start:alias_start+step]) if speed: - if 'G' not in speed.upper(): - raise Exception('{} speed is not Supported...'.format(speed)) - num = re.split("G", speed.upper()) - conv_speed = int(num[0])*1000 + speed_pat = re.search("^((\d+)G|\d+)$", speed.upper()) + if speed_pat is None: + raise Exception('{} speed is not Supported...'.format(speed)) + speed_G, speed_orig = speed_pat.group(2), speed_pat.group(1) + if speed_G: + conv_speed = int(speed_G)*1000 + else: + conv_speed = int(speed_orig) ports[intf_name]['speed'] = str(conv_speed) else: raise Exception('Regex return for speed is None...') + ports[intf_name]['index'] = index.split(",")[alias_start] ports[intf_name]['admin_status'] = "up"