regulators: Add check duplicate ID function.

Add check duplicate id and number functions.
check_duplicate_rule_id
check_duplicate_chassis_number
check_duplicate_device_id
check_duplicate_rail_id

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ic7874037ee46614ca34f88113adc4d246608b718
diff --git a/phosphor-regulators/tools/validate-regulators-config.py b/phosphor-regulators/tools/validate-regulators-config.py
index 38ed495..b413c63 100644
--- a/phosphor-regulators/tools/validate-regulators-config.py
+++ b/phosphor-regulators/tools/validate-regulators-config.py
@@ -10,6 +10,84 @@
 schema as well as doing some extra checks that can't be encoded in the schema.
 """
 
+def handle_validation_error():
+    sys.exit("Validation failed.")
+
+def check_duplicate_rule_id(config_json):
+    r"""
+    Check that there aren't any "rule" elements with the same 'id' field.
+    config_json: Configuration file JSON
+    """
+    rule_ids = []
+    for rule in config_json.get('rules', {}):
+        rule_id = rule['id']
+        if rule_id in rule_ids:
+            sys.stderr.write("Error: Duplicate rule ID.\n"+\
+            "Found multiple rules with the ID "+rule_id+'\n')
+            handle_validation_error()
+        else:
+            rule_ids.append(rule_id)
+
+def check_duplicate_chassis_number(config_json):
+    r"""
+    Check that there aren't any "chassis" elements with the same 'number' field.
+    config_json: Configuration file JSON
+    """
+    numbers = []
+    for chassis in config_json.get('chassis', {}):
+        number = chassis['number']
+        if number in numbers:
+            sys.stderr.write("Error: Duplicate chassis number.\n"+\
+            "Found multiple chassis with the number "+str(number)+'\n')
+            handle_validation_error()
+        else:
+            numbers.append(number)
+
+def check_duplicate_device_id(config_json):
+    r"""
+    Check that there aren't any "devices" with the same 'id' field.
+    config_json: Configuration file JSON
+    """
+    device_ids = []
+    for chassis in config_json.get('chassis', {}):
+        for device in chassis.get('devices', {}):
+            device_id = device['id']
+            if device_id in device_ids:
+                sys.stderr.write("Error: Duplicate device ID.\n"+\
+                "Found multiple devices with the ID "+device_id+'\n')
+                handle_validation_error()
+            else:
+                device_ids.append(device_id)
+
+def check_duplicate_rail_id(config_json):
+    r"""
+    Check that there aren't any "rails" with the same 'id' field.
+    config_json: Configuration file JSON
+    """
+    rail_ids = []
+    for chassis in config_json.get('chassis', {}):
+        for device in chassis.get('devices', {}):
+            for rail in device.get('rails', {}):
+                rail_id = rail['id']
+                if rail_id in rail_ids:
+                    sys.stderr.write("Error: Duplicate rail ID.\n"+\
+                    "Found multiple rails with the ID "+rail_id+'\n')
+                    handle_validation_error()
+                else:
+                    rail_ids.append(rail_id)
+
+def check_for_duplicates(config_json):
+    r"""
+    Check for duplicate ID.
+    """
+    check_duplicate_rule_id(config_json)
+
+    check_duplicate_chassis_number(config_json)
+
+    check_duplicate_device_id(config_json)
+
+    check_duplicate_rail_id(config_json)
+
 def validate_schema(config, schema):
     r"""
     Validates the specified config file using the specified
@@ -29,7 +107,9 @@
                 jsonschema.validate(config_json, schema_json)
             except jsonschema.ValidationError as e:
                 print(e)
-                sys.exit("Validation failed.")
+                handle_validation_error()
+
+    return config_json
 
 if __name__ == '__main__':
 
@@ -51,4 +131,5 @@
         parser.print_help()
         sys.exit("Error: Configuration file is required.")
 
-    validate_schema(args.configuration_file, args.schema_file)
+    config_json = validate_schema(args.configuration_file, args.schema_file)
+    check_for_duplicates(config_json)