blob: f133e0fe24ec89d4af21433d4de395d5d01e6831 [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 */
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 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 Spinler15ee6ae2019-07-08 16:50:06 -0500104 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