regulators: Implements support for compare_presence action

Enhance the configuration file parser to support the
compare_presence action element.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I9bfa3d260e0a6f7d8f41bed473b436148141ea50
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index b94e308..7a3d497 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -72,9 +72,8 @@
     }
     else if (element.contains("compare_presence"))
     {
-        // TODO: Not implemented yet
-        // action = parseComparePresence(element["compare_presence"]);
-        // ++propertyCount;
+        action = parseComparePresence(element["compare_presence"]);
+        ++propertyCount;
     }
     else if (element.contains("compare_vpd"))
     {
@@ -232,6 +231,27 @@
     return chassis;
 }
 
+std::unique_ptr<ComparePresenceAction> parseComparePresence(const json& element)
+{
+    verifyIsObject(element);
+    unsigned int propertyCount{0};
+
+    // Required fru property
+    const json& fruElement = getRequiredProperty(element, "fru");
+    std::string fru = parseString(fruElement);
+    ++propertyCount;
+
+    // Required value property
+    const json& valueElement = getRequiredProperty(element, "value");
+    bool value = parseBoolean(valueElement);
+    ++propertyCount;
+
+    // Verify no invalid properties exist
+    verifyPropertyCount(element, propertyCount);
+
+    return std::make_unique<ComparePresenceAction>(fru, value);
+}
+
 std::unique_ptr<Configuration> parseConfiguration(const json& element)
 {
     verifyIsObject(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index e3f6bde..706c609 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -18,6 +18,7 @@
 #include "action.hpp"
 #include "and_action.hpp"
 #include "chassis.hpp"
+#include "compare_presence_action.hpp"
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_compare_bit_action.hpp"
@@ -223,6 +224,19 @@
     parseChassisArray(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing a compare_presence action.
+ *
+ * Returns the corresponding C++ ComparePresenceAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return ComparePresenceAction object
+ */
+std::unique_ptr<ComparePresenceAction>
+    parseComparePresence(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a configuration.
  *
  * Returns the corresponding C++ Configuration object.