Skip to content

Commit

Permalink
Adding a new function to create BRKOUT_CFG TABLE in config db (sonic-…
Browse files Browse the repository at this point in the history
…net#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>
  • Loading branch information
samaity authored and zhenggen-xu committed Dec 13, 2019
1 parent 5bfb34a commit f071cf7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
67 changes: 54 additions & 13 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"

Expand All @@ -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:
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f071cf7

Please sign in to comment.