regulators: Add device_id to validation tool

Enhance the configuration file validation tool to verify that any
"device_id" properties refer to a valid device ID.  This property exists
in the new phase_fault_detection object.

Add the following new automated tests
* Test where phase_fault_detection object specifies an invalid rule_id
* Test where phase_fault_detection object specifies an invalid device_id

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I242ced0a2d0c4483d0dd7c4d36781a9f588a7e4f
diff --git a/phosphor-regulators/test/validate-regulators-config_tests.cpp b/phosphor-regulators/test/validate-regulators-config_tests.cpp
index 8faf6b8..18eef6f 100644
--- a/phosphor-regulators/test/validate-regulators-config_tests.cpp
+++ b/phosphor-regulators/test/validate-regulators-config_tests.cpp
@@ -3211,7 +3211,15 @@
     {
         json configFile = validConfigFile;
         configFile["chassis"][0]["devices"][0]["presence_detection"]
-                  ["rule_id"] = "set_voltage_rule2";
+                  ["rule_id"] = "detect_presence_rule2";
+        EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", "");
+    }
+    // Invalid: test rule_id property in phase_fault_detection specifies a rule
+    // ID that does not exist.
+    {
+        json configFile = validConfigFile;
+        configFile["chassis"][0]["devices"][0]["phase_fault_detection"]
+                  ["rule_id"] = "detect_phase_faults_rule2";
         EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", "");
     }
     // Invalid: test rule_id property in sensor_monitoring specifies a rule ID
@@ -3219,11 +3227,25 @@
     {
         json configFile = validConfigFile;
         configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"]
-                  ["rule_id"] = "set_voltage_rule2";
+                  ["rule_id"] = "read_sensors_rule2";
         EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", "");
     }
 }
 
+TEST(ValidateRegulatorsConfigTest, DeviceIDExists)
+{
+    // Invalid: test device_id property in phase_fault_detection specifies a
+    // device ID that does not exist.
+    {
+        json configFile = validConfigFile;
+        configFile["chassis"][0]["devices"][0]["phase_fault_detection"]
+                  ["device_id"] = "vdd_regulator2";
+        configFile["chassis"][0]["devices"][0]["phase_fault_detection"]
+                  ["rule_id"] = "detect_phase_faults_rule";
+        EXPECT_JSON_INVALID(configFile, "Error: Device ID does not exist.", "");
+    }
+}
+
 TEST(ValidateRegulatorsConfigTest, NumberOfElementsInMasks)
 {
     // Invalid: test number of elements in masks not equal to number in values
diff --git a/phosphor-regulators/tools/validate-regulators-config.py b/phosphor-regulators/tools/validate-regulators-config.py
index 125bd0c..f70d9b4 100755
--- a/phosphor-regulators/tools/validate-regulators-config.py
+++ b/phosphor-regulators/tools/validate-regulators-config.py
@@ -102,6 +102,21 @@
             rule_id+'\n')
             handle_validation_error()
 
+def check_device_id_exists(config_json):
+    r"""
+    Check if a device_id property specifies a device ID that does not exist.
+    config_json: Configuration file JSON
+    """
+
+    device_ids = get_values(config_json, 'device_id')
+    valid_device_ids = get_device_ids(config_json)
+    for device_id in device_ids:
+        if device_id not in valid_device_ids:
+            sys.stderr.write("Error: Device ID does not exist.\n"+\
+            "Found device_id value that specifies invalid device ID "+\
+            device_id+'\n')
+            handle_validation_error()
+
 def check_set_device_value_exists(config_json):
     r"""
     Check if a set_device action specifies a device ID that does not exist.
@@ -342,4 +357,6 @@
 
     check_rule_id_exists(config_json)
 
+    check_device_id_exists(config_json)
+
     check_number_of_elements_in_masks(config_json)