regulators: Implements support for if action

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

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Iadb6835dd28151a1dd278f6b2a19568a3072ad5e
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 8c6dc12..9615d31 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -114,9 +114,8 @@
     }
     else if (element.contains("if"))
     {
-        // TODO: Not implemented yet
-        // action = parseIf(element["if"]);
-        // ++propertyCount;
+        action = parseIf(element["if"]);
+        ++propertyCount;
     }
     else if (element.contains("not"))
     {
@@ -574,6 +573,39 @@
     return std::make_unique<I2CWriteBytesAction>(reg, values, masks);
 }
 
+std::unique_ptr<IfAction> parseIf(const json& element)
+{
+    verifyIsObject(element);
+    unsigned int propertyCount{0};
+
+    // Required condition property
+    const json& conditionElement = getRequiredProperty(element, "condition");
+    std::unique_ptr<Action> conditionAction = parseAction(conditionElement);
+    ++propertyCount;
+
+    // Required then property
+    const json& thenElement = getRequiredProperty(element, "then");
+    std::vector<std::unique_ptr<Action>> thenActions =
+        parseActionArray(thenElement);
+    ++propertyCount;
+
+    // Optional else property
+    std::vector<std::unique_ptr<Action>> elseActions{};
+    auto elseIt = element.find("else");
+    if (elseIt != element.end())
+    {
+        elseActions = parseActionArray(*elseIt);
+        ++propertyCount;
+    }
+
+    // Verify no invalid properties exist
+    verifyPropertyCount(element, propertyCount);
+
+    return std::make_unique<IfAction>(std::move(conditionAction),
+                                      std::move(thenActions),
+                                      std::move(elseActions));
+}
+
 std::unique_ptr<NotAction> parseNot(const json& element)
 {
     // Required action to execute