diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index 8a3177f31b9a..478198e5af84 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -24,6 +24,7 @@ BRKOUT_MODE = "default_brkout_mode" CUR_BRKOUT_MODE = "brkout_mode" INTF_KEY = "interfaces" +OPTIONAL_HWSKU_ATTRIBUTES = ["fec", "autoneg"] BRKOUT_PATTERN = r'(\d{1,3})x(\d{1,3}G)(\[\d{1,3}G\])?(\((\d{1,3})\))?' @@ -250,6 +251,12 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file): brkout_mode = hwsku_dict[INTF_KEY][intf][BRKOUT_MODE] child_ports = get_child_ports(intf, brkout_mode, platform_json_file) + + # take optional fields from hwsku.json + for key, item in hwsku_dict[INTF_KEY][intf].items(): + if key in OPTIONAL_HWSKU_ATTRIBUTES: + child_ports.get(intf)[key] = item + ports.update(child_ports) if not ports: diff --git a/src/sonic-device-data/src/Makefile b/src/sonic-device-data/src/Makefile index 10525fdeb27e..5ecefbf37960 100644 --- a/src/sonic-device-data/src/Makefile +++ b/src/sonic-device-data/src/Makefile @@ -14,4 +14,7 @@ test: for f in $$(find ../../../device -name platform.json); do ./platform_json_checker $$f done + for f in $$(find ../../../device -name hwsku.json); do + ./hwsku_json_checker $$f + done popd diff --git a/src/sonic-device-data/tests/hwsku_json_checker b/src/sonic-device-data/tests/hwsku_json_checker new file mode 100755 index 000000000000..6fd3d82c4cfb --- /dev/null +++ b/src/sonic-device-data/tests/hwsku_json_checker @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +import glob +import json +import re +import sys + +# Global variable +PORT_ATTRIBUTES = ["default_brkout_mode"] +OPTIONAL_PORT_ATTRIBUTES = ["fec", "autoneg"] +PORT_REG = "Ethernet(\d+)" +HWSKU_JSON = '*hwsku.json' +INTF_KEY = "interfaces" + +def usage(): + print("Usage: " + sys.argv[0] + " ") + sys.exit(1) + +def check_port_attr(port_attr): + for each_key in port_attr: + if each_key not in PORT_ATTRIBUTES and each_key not in OPTIONAL_PORT_ATTRIBUTES: + print("Error: " + each_key + " is not the correct Port attribute.") + return False + if not port_attr[each_key]: + print("Error: " + each_key + " has no value.") + return False + if not isinstance(port_attr[each_key], str): + print("Error:value type of " + each_key + " must be string.") + return False + return True + +def check_file(hwsku_json_file): + try: + hwsku_cap_file = open(hwsku_json_file,"r") + hwsku_data_data = hwsku_cap_file.read() + hwsku_dict = json.loads(hwsku_data_data) + + for each_port in hwsku_dict[INTF_KEY]: + # Validate port at top level + port_id = re.search(PORT_REG, each_port) + if port_id is None: + print("Error: Unknown Interface " + str(each_port) + " at top level") + return False + + port_attr = hwsku_dict[INTF_KEY][each_port] + + # Check mandatory attributes + for each_key in PORT_ATTRIBUTES: + if each_key not in port_attr: + print("Error: " + each_key + " of " + each_port + " is/are missing") + return False + + #Validate port attributes for each port + if not check_port_attr(port_attr): + return False + except IOError: + print("Error: Cannot open file " + hwsku_json_file) + return False + except ValueError as e: + print("Error in parsing json file " + hwsku_json_file + " ") + print(str(e)) + return False + return True + +def main(argv): + if len(argv) > 0 and argv[0] == "-h": + usage() + + # Load target file + if len(argv) == 0: + files = glob.glob(HWSKU_JSON) + else: + files = argv + + all_good = True + + for f in files: + good = check_file(f) + if good: + print("File " + f + " passed validity check") + else: + print("File " + f + " failed validity check") + + all_good = all_good and good + + if not all_good: + sys.exit(-1) + + +if __name__ == "__main__": + main(sys.argv[1:])