Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 2 | #include <nlohmann/json.hpp> |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 3 | |
| 4 | #include <map> |
Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 5 | #include <optional> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | namespace openpower |
| 10 | { |
| 11 | namespace pels |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @class AdditionalData |
| 16 | * |
| 17 | * This class takes in the contents of the AdditionalData OpenBMC |
| 18 | * event log property, and provides access to its values based on |
| 19 | * their keys. |
| 20 | * |
| 21 | * The property is a vector of strings of the form: "KEY=VALUE", |
| 22 | * and this class provides a getValue("KEY") API that would return |
| 23 | * "VALUE". |
| 24 | */ |
| 25 | class AdditionalData |
| 26 | { |
| 27 | public: |
Matt Spinler | 935a25e | 2019-10-14 16:28:08 -0500 | [diff] [blame] | 28 | AdditionalData() = default; |
Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 29 | ~AdditionalData() = default; |
| 30 | AdditionalData(const AdditionalData&) = default; |
| 31 | AdditionalData& operator=(const AdditionalData&) = default; |
| 32 | AdditionalData(AdditionalData&&) = default; |
| 33 | AdditionalData& operator=(AdditionalData&&) = default; |
| 34 | |
| 35 | /** |
| 36 | * @brief constructor |
| 37 | * |
| 38 | * @param[in] ad - the AdditionalData property vector with |
| 39 | * entries of "KEY=VALUE" |
| 40 | */ |
Matt Spinler | 7ae2fa6 | 2019-12-18 13:45:05 -0600 | [diff] [blame] | 41 | explicit AdditionalData(const std::vector<std::string>& ad) |
Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 42 | { |
| 43 | for (auto& item : ad) |
| 44 | { |
| 45 | auto pos = item.find_first_of('='); |
| 46 | if (pos == std::string::npos || pos == 0) |
| 47 | { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | _data[item.substr(0, pos)] = std::move(item.substr(pos + 1)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @brief Returns the value of the AdditionalData item for the |
| 57 | * key passed in. |
| 58 | * @param[in] key - the key to search for |
| 59 | * |
| 60 | * @return optional<string> - the value, if found |
| 61 | */ |
Matt Spinler | 935a25e | 2019-10-14 16:28:08 -0500 | [diff] [blame] | 62 | std::optional<std::string> getValue(const std::string& key) const |
Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 63 | { |
| 64 | auto entry = _data.find(key); |
| 65 | if (entry != _data.end()) |
| 66 | { |
| 67 | return entry->second; |
| 68 | } |
| 69 | return std::nullopt; |
| 70 | } |
| 71 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 72 | /** |
| 73 | * @brief Remove a key/value pair from the contained data |
| 74 | * |
| 75 | * @param[in] key - The key of the entry to remove |
| 76 | */ |
| 77 | void remove(const std::string& key) |
| 78 | { |
| 79 | _data.erase(key); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @brief Says if the object has no data |
| 84 | * |
| 85 | * @return bool true if the object is empty |
| 86 | */ |
| 87 | inline bool empty() const |
| 88 | { |
| 89 | return _data.empty(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @brief Returns the contained data as a JSON object |
| 94 | * |
| 95 | * Looks like: {"key1":"value1","key2":"value2"} |
| 96 | * |
| 97 | * @return json - The JSON object |
| 98 | */ |
| 99 | nlohmann::json toJSON() const |
| 100 | { |
| 101 | nlohmann::json j = _data; |
| 102 | return j; |
| 103 | } |
| 104 | |
Matt Spinler | 7ae2fa6 | 2019-12-18 13:45:05 -0600 | [diff] [blame] | 105 | /** |
| 106 | * @brief Returns the underlying map of data |
| 107 | * |
| 108 | * @return const std::map<std::string, std::string>& - The data |
| 109 | */ |
| 110 | const std::map<std::string, std::string>& getData() const |
| 111 | { |
| 112 | return _data; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @brief Adds a key/value pair to the object |
| 117 | * |
| 118 | * @param[in] key - The key |
| 119 | * @param[in] value - The value |
| 120 | */ |
| 121 | void add(const std::string& key, const std::string& value) |
| 122 | { |
| 123 | _data.emplace(key, value); |
| 124 | } |
| 125 | |
Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 126 | private: |
| 127 | /** |
| 128 | * @brief a map of keys to values |
| 129 | */ |
| 130 | std::map<std::string, std::string> _data; |
| 131 | }; |
| 132 | } // namespace pels |
| 133 | } // namespace openpower |