diff options
author | Matt Spinler <spinler@us.ibm.com> | 2019-07-08 16:50:06 -0500 |
---|---|---|
committer | Matt Spinler <spinler@us.ibm.com> | 2019-07-26 12:38:38 -0500 |
commit | 15ee6aee19ec5357392078dfe692b7f02baeb725 (patch) | |
tree | 26c2cedd42e75f9d970a117b1b12655cf4a93880 | |
parent | 1787c5215b337ba55530b15fd51acaf5164ad3ea (diff) | |
download | phosphor-logging-15ee6aee19ec5357392078dfe692b7f02baeb725.tar.gz phosphor-logging-15ee6aee19ec5357392078dfe692b7f02baeb725.zip |
PEL: Add class to wrap AdditionalData
The AdditionalData property on the xyz.openbmc_project.Logging.Entry
interface is a vector of strings of the form: "KEY=VALUE". The
PEL processing code will be interested in those keys and values, and
this class adds a way to get at those values based on a key without
having to do string parsing each time. It returns an
std::optional<std::string> value, and if the key isn't found, then the
std::optional value will be empty.
For Example:
AdditionalData ad{additionalDataPropertyValue};
// Get the value for the FOO key
std::optional<std::string> val = ad.getValue("FOO");
if (val)
std::cout << (*val).size();
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I6ba458840278784b1cc6a0ed88a7fece8794df7d
-rw-r--r-- | extensions/openpower-pels/additional_data.hpp | 77 | ||||
-rw-r--r-- | test/Makefile.am | 4 | ||||
-rw-r--r-- | test/openpower-pels/Makefile.include | 10 | ||||
-rw-r--r-- | test/openpower-pels/additional_data_test.cpp | 27 |
4 files changed, 118 insertions, 0 deletions
diff --git a/extensions/openpower-pels/additional_data.hpp b/extensions/openpower-pels/additional_data.hpp new file mode 100644 index 0000000..5cea69b --- /dev/null +++ b/extensions/openpower-pels/additional_data.hpp @@ -0,0 +1,77 @@ +#pragma once +#include <map> +#include <optional> +#include <string> +#include <vector> + +namespace openpower +{ +namespace pels +{ + +/** + * @class AdditionalData + * + * This class takes in the contents of the AdditionalData OpenBMC + * event log property, and provides access to its values based on + * their keys. + * + * The property is a vector of strings of the form: "KEY=VALUE", + * and this class provides a getValue("KEY") API that would return + * "VALUE". + */ +class AdditionalData +{ + public: + AdditionalData() = delete; + ~AdditionalData() = default; + AdditionalData(const AdditionalData&) = default; + AdditionalData& operator=(const AdditionalData&) = default; + AdditionalData(AdditionalData&&) = default; + AdditionalData& operator=(AdditionalData&&) = default; + + /** + * @brief constructor + * + * @param[in] ad - the AdditionalData property vector with + * entries of "KEY=VALUE" + */ + AdditionalData(const std::vector<std::string>& ad) + { + for (auto& item : ad) + { + auto pos = item.find_first_of('='); + if (pos == std::string::npos || pos == 0) + { + continue; + } + + _data[item.substr(0, pos)] = std::move(item.substr(pos + 1)); + } + } + + /** + * @brief Returns the value of the AdditionalData item for the + * key passed in. + * @param[in] key - the key to search for + * + * @return optional<string> - the value, if found + */ + std::optional<std::string> getValue(const std::string& key) + { + auto entry = _data.find(key); + if (entry != _data.end()) + { + return entry->second; + } + return std::nullopt; + } + + private: + /** + * @brief a map of keys to values + */ + std::map<std::string, std::string> _data; +}; +} // namespace pels +} // namespace openpower diff --git a/test/Makefile.am b/test/Makefile.am index a95677d..6499764 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -110,3 +110,7 @@ extensions_test_LDFLAGS = $(test_ldflags) # TODO Remove once the test-case failure is resolved openbmc/phosphor-logging#11 XFAIL_TESTS = elog_errorwrap_test + +if ENABLE_PEL_EXTENSION +include openpower-pels/Makefile.include +endif diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include new file mode 100644 index 0000000..98a0acd --- /dev/null +++ b/test/openpower-pels/Makefile.include @@ -0,0 +1,10 @@ +TESTS += $(check_PROGRAMS) + +check_PROGRAMS += \ + additional_data_test + +additional_data_test_SOURCES = %reldir%/additional_data_test.cpp +additional_data_test_CPPFLAGS = $(test_cppflags) +additional_data_test_CXXFLAGS = $(test_cxxflags) +additional_data_test_LDADD = $(test_ldadd) +additional_data_test_LDFLAGS = $(test_ldflags) diff --git a/test/openpower-pels/additional_data_test.cpp b/test/openpower-pels/additional_data_test.cpp new file mode 100644 index 0000000..2be06c4 --- /dev/null +++ b/test/openpower-pels/additional_data_test.cpp @@ -0,0 +1,27 @@ +#include "extensions/openpower-pels/additional_data.hpp" + +#include <gtest/gtest.h> + +using namespace openpower::pels; + +TEST(AdditionalDataTest, GetKeywords) +{ + std::vector<std::string> data{"KEY1=VALUE1", "KEY2=VALUE2", + "KEY3=", "HELLOWORLD", "=VALUE5"}; + AdditionalData ad{data}; + + EXPECT_TRUE(ad.getValue("KEY1")); + EXPECT_EQ(*(ad.getValue("KEY1")), "VALUE1"); + + EXPECT_TRUE(ad.getValue("KEY2")); + EXPECT_EQ(*(ad.getValue("KEY2")), "VALUE2"); + + EXPECT_FALSE(ad.getValue("x")); + + auto value3 = ad.getValue("KEY3"); + EXPECT_TRUE(value3); + EXPECT_TRUE((*value3).empty()); + + EXPECT_FALSE(ad.getValue("HELLOWORLD")); + EXPECT_FALSE(ad.getValue("VALUE5")); +} |