blob: 09dbd200fb5ad60fced7db41ec2c05c1a009fa8b [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>
7#include <vector>
8
9namespace openpower
10{
11namespace 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 */
25class AdditionalData
26{
27 public:
Matt Spinler935a25e2019-10-14 16:28:08 -050028 AdditionalData() = default;
Matt Spinler15ee6ae2019-07-08 16:50:06 -050029 ~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 Spinler7ae2fa62019-12-18 13:45:05 -060041 explicit AdditionalData(const std::vector<std::string>& ad)
Matt Spinler15ee6ae2019-07-08 16:50:06 -050042 {
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 Spinler935a25e2019-10-14 16:28:08 -050062 std::optional<std::string> getValue(const std::string& key) const
Matt Spinler15ee6ae2019-07-08 16:50:06 -050063 {
64 auto entry = _data.find(key);
65 if (entry != _data.end())
66 {
67 return entry->second;
68 }
69 return std::nullopt;
70 }
71
Matt Spinlerafa857c2019-10-24 13:03:46 -050072 /**
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 Spinler7ae2fa62019-12-18 13:45:05 -0600105 /**
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 Spinler15ee6ae2019-07-08 16:50:06 -0500126 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