xref: /openbmc/openpower-vpd-parser/store.hpp (revision c78d887c)
1 #pragma once
2 
3 #include "defines.hpp"
4 #include "types.hpp"
5 
6 #include <iostream>
7 #include <string>
8 #include <unordered_map>
9 
10 namespace openpower
11 {
12 namespace vpd
13 {
14 
15 /** @brief Parsed VPD is represented as a dictionary of records, where
16  *         each record in itself is a dictionary of keywords */
17 using Parsed = std::unordered_map<std::string,
18                                   std::unordered_map<std::string, std::string>>;
19 
20 /** @class Store
21  *  @brief Store for parsed OpenPOWER VPD
22  *
23  *  A Store object stores parsed OpenPOWER VPD, and provides access
24  *  to the VPD, specified by record and keyword. Parsed VPD is typically
25  *  provided by the Parser class.
26  */
27 class Store final
28 {
29   public:
30     Store() = delete;
31     Store(const Store&) = delete;
32     Store& operator=(const Store&) = delete;
33     Store(Store&&) = default;
34     Store& operator=(Store&&) = default;
35     ~Store() = default;
36 
37     /** @brief Construct a Store
38      *
39      *  @param[in] vpdBuffer - A parsed VPD object
40      */
Store(Parsed && vpdBuffer)41     explicit Store(Parsed&& vpdBuffer) : vpd(std::move(vpdBuffer)) {}
42 
43     /** @brief Retrieves VPD from Store as a Parsed object
44      *
45      *  @returns VPD as a Parsed object
46      */
getVpdMap()47     inline Parsed& getVpdMap()
48     {
49         return vpd;
50     }
51 
52     /** @brief Retrieves VPD from Store
53      *
54      *  @tparam R - VPD record
55      *  @tparam K - VPD keyword
56      *  @returns VPD stored in input record:keyword
57      */
58     template <Record R, record::Keyword K>
59     inline const std::string& get() const;
60 
61     /** @brief Checks if VPD exists in store
62      *
63      *  @tparam R - VPD record
64      *  @tparam K - VPD keyword
65      *  @returns true if {R,K} exists
66      */
67     template <Record R, record::Keyword K>
exists() const68     bool exists() const
69     {
70         static const std::string record = getRecord<R>();
71         static const std::string keyword = record::getKeyword<K>();
72         return vpd.count(record) && vpd.at(record).count(keyword);
73     }
74 
75     /** @brief Displays all data in the store to stdout
76      */
dump() const77     void dump() const
78     {
79         for (const auto& [vpdname, avpd] : vpd)
80         {
81             std::cout << vpdname << ": " << std::endl;
82 
83             for (const auto& [key, val] : avpd)
84             {
85                 std::cout << "\t" << key << " : " << val << std::endl;
86             }
87         }
88     }
89 
90   private:
91     /** @brief The store for parsed VPD */
92     Parsed vpd;
93 };
94 
95 template <Record R, record::Keyword K>
get() const96 inline const std::string& Store::get() const
97 {
98     static const std::string record = getRecord<R>();
99     static const std::string keyword = record::getKeyword<K>();
100     static const std::string empty = "";
101     auto kw = vpd.find(record);
102     if (vpd.end() != kw)
103     {
104         auto value = (kw->second).find(keyword);
105         if ((kw->second).end() != value)
106         {
107             return value->second;
108         }
109     }
110     return empty;
111 }
112 
113 } // namespace vpd
114 } // namespace openpower
115