1 #pragma once
2 #include <nlohmann/json.hpp>
3 
4 #include <map>
5 #include <optional>
6 #include <string>
7 #include <vector>
8 
9 namespace openpower
10 {
11 namespace 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  */
25 class AdditionalData
26 {
27   public:
28     AdditionalData() = default;
29     ~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      */
AdditionalData(const std::vector<std::string> & ad)41     explicit AdditionalData(const std::vector<std::string>& ad)
42     {
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      */
getValue(const std::string & key) const62     std::optional<std::string> getValue(const std::string& key) const
63     {
64         auto entry = _data.find(key);
65         if (entry != _data.end())
66         {
67             return entry->second;
68         }
69         return std::nullopt;
70     }
71 
72     /**
73      * @brief Remove a key/value pair from the contained data
74      *
75      * @param[in] key - The key of the entry to remove
76      */
remove(const std::string & key)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      */
empty() const87     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      */
toJSON() const99     nlohmann::json toJSON() const
100     {
101         nlohmann::json j = _data;
102         return j;
103     }
104 
105     /**
106      * @brief Returns the underlying map of data
107      *
108      * @return const std::map<std::string, std::string>& - The data
109      */
getData() const110     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      */
add(const std::string & key,const std::string & value)121     void add(const std::string& key, const std::string& value)
122     {
123         _data.emplace(key, value);
124     }
125 
126   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
134