1 #pragma once
2 #include <map>
3 #include <nlohmann/json.hpp>
4 #include <optional>
5 #include <string>
6 #include <vector>
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      */
40     explicit 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      */
61     std::optional<std::string> getValue(const std::string& key) const
62     {
63         auto entry = _data.find(key);
64         if (entry != _data.end())
65         {
66             return entry->second;
67         }
68         return std::nullopt;
69     }
70 
71     /**
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 
104     /**
105      * @brief Returns the underlying map of data
106      *
107      * @return const std::map<std::string, std::string>& - The data
108      */
109     const std::map<std::string, std::string>& getData() const
110     {
111         return _data;
112     }
113 
114     /**
115      * @brief Adds a key/value pair to the object
116      *
117      * @param[in] key - The key
118      * @param[in] value - The value
119      */
120     void add(const std::string& key, const std::string& value)
121     {
122         _data.emplace(key, value);
123     }
124 
125   private:
126     /**
127      * @brief a map of keys to values
128      */
129     std::map<std::string, std::string> _data;
130 };
131 } // namespace pels
132 } // namespace openpower
133