blob: c184e37676cd81e886474ed3512de56b0920c593 [file] [log] [blame]
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06001#pragma once
2
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06003#include "defines.hpp"
4#include "types.hpp"
5
Joel Stanleyd934d4d2020-04-17 12:19:12 +09306#include <iostream>
Patrick Venturec83c4dc2018-11-01 16:29:18 -07007#include <string>
8#include <unordered_map>
9
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060010namespace openpower
11{
12namespace 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 */
Patrick Venturec83c4dc2018-11-01 16:29:18 -070017using Parsed = std::unordered_map<std::string,
18 std::unordered_map<std::string, std::string>>;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060019
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 */
27class Store final
28{
Patrick Venturec83c4dc2018-11-01 16:29:18 -070029 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;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060036
Patrick Venturec83c4dc2018-11-01 16:29:18 -070037 /** @brief Construct a Store
38 *
39 * @param[in] vpdBuffer - A parsed VPD object
40 */
41 explicit Store(Parsed&& vpdBuffer) : vpd(std::move(vpdBuffer))
42 {
43 }
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060044
Patrick Venturec83c4dc2018-11-01 16:29:18 -070045 /** @brief Retrieves VPD from Store
46 *
47 * @tparam R - VPD record
48 * @tparam K - VPD keyword
49 * @returns VPD stored in input record:keyword
50 */
51 template <Record R, record::Keyword K>
52 inline const std::string& get() const;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060053
Patrick Venturec83c4dc2018-11-01 16:29:18 -070054 /** @brief Checks if VPD exists in store
55 *
56 * @tparam R - VPD record
57 * @tparam K - VPD keyword
58 * @returns true if {R,K} exists
59 */
60 template <Record R, record::Keyword K>
61 bool exists() const
62 {
63 static const std::string record = getRecord<R>();
64 static const std::string keyword = record::getKeyword<K>();
65 return vpd.count(record) && vpd.at(record).count(keyword);
66 }
Deepak Kodihalli7e7821c2017-09-11 23:32:01 -050067
Joel Stanleyd934d4d2020-04-17 12:19:12 +093068 /** @brief Displays all data in the store to stdout
69 */
70 void dump() const
71 {
72 for (auto const& [vpdname, avpd] : vpd)
73 {
74 std::cout << vpdname << ": " << std::endl;
75
76 for (auto const& [key, val] : avpd)
77 {
78
79 std::cout << "\t" << key << " : " << val << std::endl;
80 }
81 }
82 }
83
Patrick Venturec83c4dc2018-11-01 16:29:18 -070084 private:
85 /** @brief The store for parsed VPD */
86 Parsed vpd;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060087};
88
Patrick Venturec83c4dc2018-11-01 16:29:18 -070089template <Record R, record::Keyword K>
Deepak Kodihalli158c0462016-11-21 21:33:28 -060090inline const std::string& Store::get() const
91{
92 static const std::string record = getRecord<R>();
93 static const std::string keyword = record::getKeyword<K>();
94 static const std::string empty = "";
95 auto kw = vpd.find(record);
96 if (vpd.end() != kw)
97 {
98 auto value = (kw->second).find(keyword);
99 if ((kw->second).end() != value)
100 {
101 return value->second;
102 }
103 }
104 return empty;
105}
106
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -0600107} // namespace vpd
108} // namespace openpower