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