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