[sonic-cfggen] make minigraph parser fail when speed and lanes are not in PORT table (#10228)

Why I did it
Config db schema generated by minigraph can’t pass yang validation, PORT table does not have 'lanes' and 'speed' field.

How I did it
Make cfggen command fail when 'lanes' and 'speed' are not provided

How to verify it
Run 'sonic-cfggen -m xxx.xml --print-data' to make sure command fail when 'lanes' and 'speed' not in PORT table
This commit is contained in:
jingwenxie 2022-04-10 18:56:58 -07:00 committed by GitHub
parent 011c21d8f5
commit cd330f0e70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -247,6 +247,19 @@ def _get_jinja2_env(paths):
return env return env
def _must_field_by_yang(data, table, must_fields):
"""
Check if table contains must field based on yang definition
"""
if table not in data:
return
for must_field in must_fields:
for _, fields in data[table].items():
if must_field not in fields:
print(must_field, 'is a must field in', table, file=sys.stderr)
sys.exit(1)
def main(): def main():
parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.") parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.")
group = parser.add_mutually_exclusive_group() group = parser.add_mutually_exclusive_group()
@ -335,6 +348,8 @@ def main():
deep_update(data, parse_xml(minigraph, platform, asic_name=asic_name)) deep_update(data, parse_xml(minigraph, platform, asic_name=asic_name))
else: else:
deep_update(data, parse_xml(minigraph, port_config_file=args.port_config, asic_name=asic_name, hwsku_config_file=args.hwsku_config)) deep_update(data, parse_xml(minigraph, port_config_file=args.port_config, asic_name=asic_name, hwsku_config_file=args.hwsku_config))
# check if minigraph parser has speed and lanes in PORT table
_must_field_by_yang(data, 'PORT', ['speed', 'lanes'])
if args.device_description is not None: if args.device_description is not None:
deep_update(data, parse_device_desc_xml(args.device_description)) deep_update(data, parse_device_desc_xml(args.device_description))