xref: /openbmc/phosphor-logging/extensions/openpower-pels/additional_data.hpp (revision 8a09b982ddeb0c1e13190d9cd196e06a778d6140)
1 #pragma once
2 #include <nlohmann/json.hpp>
3 
4 #include <map>
5 #include <optional>
6 #include <string>
7 
8 namespace openpower
9 {
10 namespace 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  */
24 class AdditionalData
25 {
26   public:
27     AdditionalData() = default;
28     ~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      */
AdditionalData(const std::map<std::string,std::string> & ad)40     explicit AdditionalData(const std::map<std::string, std::string>& ad) :
41         _data(ad)
42     {}
43 
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      */
getValue(const std::string & key) const51     std::optional<std::string> getValue(const std::string& key) const
52     {
53         auto entry = _data.find(key);
54         if (entry != _data.end())
55         {
56             return entry->second;
57         }
58         return std::nullopt;
59     }
60 
61     /**
62      * @brief Remove a key/value pair from the contained data
63      *
64      * @param[in] key - The key of the entry to remove
65      */
remove(const std::string & key)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      */
empty() const76     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      */
toJSON() const88     nlohmann::json toJSON() const
89     {
90         nlohmann::json j = _data;
91         return j;
92     }
93 
94     /**
95      * @brief Returns the underlying map of data
96      *
97      * @return const std::map<std::string, std::string>& - The data
98      */
getData() const99     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      */
add(const std::string & key,const std::string & value)110     void add(const std::string& key, const std::string& value)
111     {
112         _data.emplace(key, value);
113     }
114 
115   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
123