diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index 183e9517211c..5cc6d12b679e 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 @@ -70,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: @@ -139,9 +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: - ports[intf_name]['speed'] = speed + 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" @@ -157,17 +184,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 +241,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: