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
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 7104356..e04f81f 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -27,6 +27,7 @@
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
#include "i2c_write_bytes_action.hpp"
+#include "if_action.hpp"
#include "not_action.hpp"
#include "or_action.hpp"
#include "pmbus_write_vout_command_action.hpp"
@@ -417,6 +418,18 @@
parseI2CWriteBytes(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an if action.
+ *
+ * Returns the corresponding C++ IfAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return IfAction object
+ */
+std::unique_ptr<IfAction> parseIf(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an 8-bit signed integer.
*
* Returns the corresponding C++ int8_t value.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 4c53c6c..5223e54 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -349,7 +349,20 @@
}
// Test where works: if action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "if":
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: not action type specified
{
@@ -2516,6 +2529,157 @@
}
}
+TEST(ConfigFileParserTests, ParseIf)
+{
+ // Test where works: Only required properties specified
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": [ { "run_rule": "configure_downlevel_regulator" },
+ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ std::unique_ptr<IfAction> action = parseIf(element);
+ EXPECT_NE(action->getConditionAction().get(), nullptr);
+ EXPECT_EQ(action->getThenActions().size(), 2);
+ EXPECT_EQ(action->getElseActions().size(), 0);
+ }
+
+ // Test where works: All properties specified
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ std::unique_ptr<IfAction> action = parseIf(element);
+ EXPECT_NE(action->getConditionAction().get(), nullptr);
+ EXPECT_EQ(action->getThenActions().size(), 1);
+ EXPECT_EQ(action->getElseActions().size(), 1);
+ }
+
+ // Test where fails: Required condition property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: condition");
+ }
+
+ // Test where fails: Required then property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: then");
+ }
+
+ // Test where fails: condition value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "condition": 1,
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an object");
+ }
+
+ // Test where fails: then value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": "foo",
+ "else": [ { "run_rule": "configure_standard_regulator" } ]
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an array");
+ }
+
+ // Test where fails: else value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "else": 1
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an array");
+ }
+
+ // Test where fails: Invalid property specified
+ try
+ {
+ const json element = R"(
+ {
+ "condition": { "run_rule": "is_downlevel_regulator" },
+ "then": [ { "run_rule": "configure_downlevel_regulator" } ],
+ "foo": "bar"
+ }
+ )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element contains an invalid property");
+ }
+
+ // Test where fails: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseIf(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an object");
+ }
+}
+
TEST(ConfigFileParserTests, ParseInt8)
{
// Test where works: INT8_MIN