regulators: Implements support for compare_vpd action

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

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ia8766a0d2f893898c5ff2655a757d60f7cdcc7df
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 7a3d497..11527b8 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -77,9 +77,8 @@
     }
     else if (element.contains("compare_vpd"))
     {
-        // TODO: Not implemented yet
-        // action = parseCompareVPD(element["compare_vpd"]);
-        // ++propertyCount;
+        action = parseCompareVPD(element["compare_vpd"]);
+        ++propertyCount;
     }
     else if (element.contains("i2c_compare_bit"))
     {
@@ -252,6 +251,32 @@
     return std::make_unique<ComparePresenceAction>(fru, value);
 }
 
+std::unique_ptr<CompareVPDAction> parseCompareVPD(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 keyword property
+    const json& keywordElement = getRequiredProperty(element, "keyword");
+    std::string keyword = parseString(keywordElement);
+    ++propertyCount;
+
+    // Required value property
+    const json& valueElement = getRequiredProperty(element, "value");
+    std::string value = parseString(valueElement);
+    ++propertyCount;
+
+    // Verify no invalid properties exist
+    verifyPropertyCount(element, propertyCount);
+
+    return std::make_unique<CompareVPDAction>(fru, keyword, 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 706c609..205f43a 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -19,6 +19,7 @@
 #include "and_action.hpp"
 #include "chassis.hpp"
 #include "compare_presence_action.hpp"
+#include "compare_vpd_action.hpp"
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_compare_bit_action.hpp"
@@ -237,6 +238,19 @@
     parseComparePresence(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing a compare_vpd action.
+ *
+ * Returns the corresponding C++ CompareVPDAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return CompareVPDAction object
+ */
+std::unique_ptr<CompareVPDAction>
+    parseCompareVPD(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a configuration.
  *
  * Returns the corresponding C++ Configuration object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 875b0a1..c6db12d 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -17,6 +17,7 @@
 #include "and_action.hpp"
 #include "chassis.hpp"
 #include "compare_presence_action.hpp"
+#include "compare_vpd_action.hpp"
 #include "config_file_parser.hpp"
 #include "config_file_parser_error.hpp"
 #include "configuration.hpp"
@@ -274,7 +275,20 @@
     }
 
     // Test where works: compare_vpd action type specified
-    // TODO: Not implemented yet
+    {
+        const json element = R"(
+            {
+              "compare_vpd":
+              {
+                "fru": "/system/chassis/disk_backplane",
+                "keyword": "CCIN",
+                "value": "2D35"
+              }
+            }
+        )"_json;
+        std::unique_ptr<Action> action = parseAction(element);
+        EXPECT_NE(action.get(), nullptr);
+    }
 
     // Test where works: i2c_compare_bit action type specified
     {
@@ -1014,6 +1028,160 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParseCompareVPD)
+{
+    // Test where works
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "keyword": "CCIN",
+              "value": "2D35"
+            }
+        )"_json;
+        std::unique_ptr<CompareVPDAction> action = parseCompareVPD(element);
+        EXPECT_EQ(action->getFRU(), "/system/chassis/disk_backplane");
+        EXPECT_EQ(action->getKeyword(), "CCIN");
+        EXPECT_EQ(action->getValue(), "2D35");
+    }
+
+    // Test where fails: Element is not an object
+    try
+    {
+        const json element = R"( [ "0xFF", "0x01" ] )"_json;
+        parseCompareVPD(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: Invalid property specified
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "keyword": "CCIN",
+              "value": "2D35",
+              "foo" : true
+            }
+        )"_json;
+        parseCompareVPD(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: Required fru property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "keyword": "CCIN",
+              "value": "2D35"
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Required property missing: fru");
+    }
+
+    // Test where fails: Required keyword property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "value": "2D35"
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Required property missing: keyword");
+    }
+
+    // Test where fails: Required value property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "keyword": "CCIN"
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Required property missing: value");
+    }
+
+    // Test where fails: fru value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "fru": 1,
+              "keyword": "CCIN",
+              "value": "2D35"
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not a string");
+    }
+
+    // Test where fails: keyword value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "keyword": 1,
+              "value": "2D35"
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not a string");
+    }
+
+    // Test where fails: value value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/disk_backplane",
+              "keyword": "CCIN",
+              "value": 1
+            }
+        )"_json;
+        parseCompareVPD(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not a string");
+    }
+}
+
 TEST(ConfigFileParserTests, ParseConfiguration)
 {
     // Test where works: actions required property specified