blob: ca4a47d4a0589d506569438d9674760fe94b7e0f [file] [log] [blame]
Matt Spinler15ee6ae2019-07-08 16:50:06 -05001#pragma once
2#include <map>
Matt Spinlerafa857c2019-10-24 13:03:46 -05003#include <nlohmann/json.hpp>
Matt Spinler15ee6ae2019-07-08 16:50:06 -05004#include <optional>
5#include <string>
6#include <vector>
7
8namespace openpower
9{
10namespace 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 */
24class AdditionalData
25{
26 public:
Matt Spinler935a25e2019-10-14 16:28:08 -050027 AdditionalData() = default;
Matt Spinler15ee6ae2019-07-08 16:50:06 -050028 ~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 */
Matt Spinler7ae2fa62019-12-18 13:45:05 -060040 explicit AdditionalData(const std::vector<std::string>& ad)
Matt Spinler15ee6ae2019-07-08 16:50:06 -050041 {
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 Spinler935a25e2019-10-14 16:28:08 -050061 std::optional<std::string> getValue(const std::string& key) const
Matt Spinler15ee6ae2019-07-08 16:50:06 -050062 {
63 auto entry = _data.find(key);
64 if (entry != _data.end())
65 {
66 return entry->second;
67 }
68 return std::nullopt;
69 }
70
Matt Spinlerafa857c2019-10-24 13:03:46 -050071 /**
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 Spinler7ae2fa62019-12-18 13:45:05 -0600104 /**
105 * @brief Returns the underlying map of data
106 *
107 * @return const std::map<std::string, std::string>& - The data
108 */
109 const std::map<std::string, std::string>& getData() const
110 {
111 return _data;
112 }
113
114 /**
115 * @brief Adds a key/value pair to the object
116 *
117 * @param[in] key - The key
118 * @param[in] value - The value
119 */
120 void add(const std::string& key, const std::string& value)
121 {
122 _data.emplace(key, value);
123 }
124
Matt Spinler15ee6ae2019-07-08 16:50:06 -0500125 private:
126 /**
127 * @brief a map of keys to values
128 */
129 std::map<std::string, std::string> _data;
130};
131} // namespace pels
132} // namespace openpower