summaryrefslogtreecommitdiffstats
path: root/extensions/openpower-pels/additional_data.hpp
blob: ca4a47d4a0589d506569438d9674760fe94b7e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once
#include <map>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>

namespace openpower
{
namespace pels
{

/**
 * @class AdditionalData
 *
 * This class takes in the contents of the AdditionalData OpenBMC
 * event log property, and provides access to its values based on
 * their keys.
 *
 * The property is a vector of strings of the form: "KEY=VALUE",
 * and this class provides a getValue("KEY") API that would return
 * "VALUE".
 */
class AdditionalData
{
  public:
    AdditionalData() = default;
    ~AdditionalData() = default;
    AdditionalData(const AdditionalData&) = default;
    AdditionalData& operator=(const AdditionalData&) = default;
    AdditionalData(AdditionalData&&) = default;
    AdditionalData& operator=(AdditionalData&&) = default;

    /**
     * @brief constructor
     *
     * @param[in] ad - the AdditionalData property vector with
     *                 entries of "KEY=VALUE"
     */
    explicit AdditionalData(const std::vector<std::string>& ad)
    {
        for (auto& item : ad)
        {
            auto pos = item.find_first_of('=');
            if (pos == std::string::npos || pos == 0)
            {
                continue;
            }

            _data[item.substr(0, pos)] = std::move(item.substr(pos + 1));
        }
    }

    /**
     * @brief Returns the value of the AdditionalData item for the
     *        key passed in.
     * @param[in] key - the key to search for
     *
     * @return optional<string> - the value, if found
     */
    std::optional<std::string> getValue(const std::string& key) const
    {
        auto entry = _data.find(key);
        if (entry != _data.end())
        {
            return entry->second;
        }
        return std::nullopt;
    }

    /**
     * @brief Remove a key/value pair from the contained data
     *
     * @param[in] key - The key of the entry to remove
     */
    void remove(const std::string& key)
    {
        _data.erase(key);
    }

    /**
     * @brief Says if the object has no data
     *
     * @return bool true if the object is empty
     */
    inline bool empty() const
    {
        return _data.empty();
    }

    /**
     * @brief Returns the contained data as a JSON object
     *
     * Looks like: {"key1":"value1","key2":"value2"}
     *
     * @return json - The JSON object
     */
    nlohmann::json toJSON() const
    {
        nlohmann::json j = _data;
        return j;
    }

    /**
     * @brief Returns the underlying map of data
     *
     * @return const std::map<std::string, std::string>& - The data
     */
    const std::map<std::string, std::string>& getData() const
    {
        return _data;
    }

    /**
     * @brief Adds a key/value pair to the object
     *
     * @param[in] key - The key
     * @param[in] value - The value
     */
    void add(const std::string& key, const std::string& value)
    {
        _data.emplace(key, value);
    }

  private:
    /**
     * @brief a map of keys to values
     */
    std::map<std::string, std::string> _data;
};
} // namespace pels
} // namespace openpower
OpenPOWER on IntegriCloud