blob: 43e7ee47b8a4a8fc05a4ad7efa236e60263434f1 [file] [log] [blame]
Matt Spinler15ee6ae2019-07-08 16:50:06 -05001#pragma once
Matt Spinlerafa857c2019-10-24 13:03:46 -05002#include <nlohmann/json.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -05003
4#include <map>
Matt Spinler15ee6ae2019-07-08 16:50:06 -05005#include <optional>
6#include <string>
Matt Spinler15ee6ae2019-07-08 16:50:06 -05007
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 */
Patrick Williamse5940632024-11-22 20:47:58 -050040 explicit AdditionalData(const std::map<std::string, std::string>& ad) :
41 _data(ad)
42 {}
Matt Spinler15ee6ae2019-07-08 16:50:06 -050043
44 /**
45 * @brief Returns the value of the AdditionalData item for the
46 * key passed in.
47 * @param[in] key - the key to search for
48 *
49 * @return optional<string> - the value, if found
50 */
Matt Spinler935a25e2019-10-14 16:28:08 -050051 std::optional<std::string> getValue(const std::string& key) const
Matt Spinler15ee6ae2019-07-08 16:50:06 -050052 {
53 auto entry = _data.find(key);
54 if (entry != _data.end())
55 {
56 return entry->second;
57 }
58 return std::nullopt;
59 }
60
Matt Spinlerafa857c2019-10-24 13:03:46 -050061 /**
62 * @brief Remove a key/value pair from the contained data
63 *
64 * @param[in] key - The key of the entry to remove
65 */
66 void remove(const std::string& key)
67 {
68 _data.erase(key);
69 }
70
71 /**
72 * @brief Says if the object has no data
73 *
74 * @return bool true if the object is empty
75 */
76 inline bool empty() const
77 {
78 return _data.empty();
79 }
80
81 /**
82 * @brief Returns the contained data as a JSON object
83 *
84 * Looks like: {"key1":"value1","key2":"value2"}
85 *
86 * @return json - The JSON object
87 */
88 nlohmann::json toJSON() const
89 {
90 nlohmann::json j = _data;
91 return j;
92 }
93
Matt Spinler7ae2fa62019-12-18 13:45:05 -060094 /**
95 * @brief Returns the underlying map of data
96 *
97 * @return const std::map<std::string, std::string>& - The data
98 */
99 const std::map<std::string, std::string>& getData() const
100 {
101 return _data;
102 }
103
104 /**
105 * @brief Adds a key/value pair to the object
106 *
107 * @param[in] key - The key
108 * @param[in] value - The value
109 */
110 void add(const std::string& key, const std::string& value)
111 {
112 _data.emplace(key, value);
113 }
114
Matt Spinler15ee6ae2019-07-08 16:50:06 -0500115 private:
116 /**
117 * @brief a map of keys to values
118 */
119 std::map<std::string, std::string> _data;
120};
121} // namespace pels
122} // namespace openpower