regulators: Implements support for and action
Enhance the configuration file parser to support the and action element.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I9099bf06606fb52ea7eaa374e2a2b1f924904d4f
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 64b6121..e458ba7 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -67,9 +67,8 @@
std::unique_ptr<Action> action{};
if (element.contains("and"))
{
- // TODO: Not implemented yet
- // action = parseAnd(element["and"]);
- // ++propertyCount;
+ action = parseAnd(element["and"]);
+ ++propertyCount;
}
else if (element.contains("compare_presence"))
{
@@ -176,6 +175,21 @@
return actions;
}
+std::unique_ptr<AndAction> parseAnd(const json& element)
+{
+ verifyIsArray(element);
+
+ // Verify if array size less than 2
+ if (element.size() < 2)
+ {
+ throw std::invalid_argument{"Array must contain two or more actions"};
+ }
+ // Array of two or more actions
+ std::vector<std::unique_ptr<Action>> actions = parseActionArray(element);
+
+ return std::make_unique<AndAction>(std::move(actions));
+}
+
std::unique_ptr<Chassis> parseChassis(const json& element)
{
verifyIsObject(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index b653c00..d81e403 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -16,6 +16,7 @@
#pragma once
#include "action.hpp"
+#include "and_action.hpp"
#include "chassis.hpp"
#include "configuration.hpp"
#include "device.hpp"
@@ -111,6 +112,18 @@
parseActionArray(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an and action.
+ *
+ * Returns the corresponding C++ AndAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return AndAction object
+ */
+std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing a bit position (from 0-7).
*
* Returns the corresponding C++ uint8_t value.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 6953e07..ed3c271 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "action.hpp"
+#include "and_action.hpp"
#include "chassis.hpp"
#include "config_file_parser.hpp"
#include "config_file_parser_error.hpp"
@@ -240,7 +241,18 @@
}
// Test where works: and action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "and": [
+ { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } },
+ { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } }
+ ]
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: compare_presence action type specified
// TODO: Not implemented yet
@@ -470,6 +482,53 @@
}
}
+TEST(ConfigFileParserTests, ParseAnd)
+{
+ // Test where works: Element is an array with 2 actions
+ {
+ const json element = R"(
+ [
+ { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } },
+ { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } }
+ ]
+ )"_json;
+ std::unique_ptr<AndAction> action = parseAnd(element);
+ EXPECT_EQ(action->getActions().size(), 2);
+ }
+
+ // Test where fails: Element is an array with 1 action
+ try
+ {
+ const json element = R"(
+ [
+ { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } }
+ ]
+ )"_json;
+ parseAnd(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Array must contain two or more actions");
+ }
+
+ // Test where fails: Element is not an array
+ try
+ {
+ const json element = R"(
+ {
+ "foo": "bar"
+ }
+ )"_json;
+ parseAnd(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(ConfigFileParserTests, ParseBitPosition)
{
// Test where works: 0