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::map<std::string,std::string> & ad)41 explicit AdditionalData(const std::map<std::string, std::string>& ad) : 42 _data(ad) 43 {} 44 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 */ getValue(const std::string & key) const52 std::optional<std::string> getValue(const std::string& key) const 53 { 54 auto entry = _data.find(key); 55 if (entry != _data.end()) 56 { 57 return entry->second; 58 } 59 return std::nullopt; 60 } 61 62 /** 63 * @brief Remove a key/value pair from the contained data 64 * 65 * @param[in] key - The key of the entry to remove 66 */ remove(const std::string & key)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 */ empty() const77 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 */ toJSON() const89 nlohmann::json toJSON() const 90 { 91 nlohmann::json j = _data; 92 return j; 93 } 94 95 /** 96 * @brief Returns the underlying map of data 97 * 98 * @return const std::map<std::string, std::string>& - The data 99 */ getData() const100 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 */ add(const std::string & key,const std::string & value)111 void add(const std::string& key, const std::string& value) 112 { 113 _data.emplace(key, value); 114 } 115 116 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 124