xref: /openbmc/ibm-logging/policy_table.hpp (revision 62011e26)
1 #pragma once
2 
3 #include <experimental/optional>
4 #include <map>
5 #include <vector>
6 #include "config.h"
7 
8 namespace ibm
9 {
10 namespace logging
11 {
12 namespace policy
13 {
14 
15 
16 /**
17  *  The details of a policy table entry:
18  *  - search modifier
19  *  - error message
20  *  - common error event ID
21  */
22 struct Details
23 {
24     std::string modifier;
25     std::string msg;
26     std::string ceid;
27 };
28 
29 using DetailsList = std::vector<Details>;
30 using DetailsReference = std::reference_wrapper<const Details>;
31 
32 using PolicyMap = std::map<std::string, DetailsList>;
33 
34 namespace optional_ns = std::experimental;
35 
36 /**
37  * @class Table
38  *
39  * This class wraps the error policy table data, and provides the
40  * ability to find a policy table entry based on the error and a
41  * search modifier.  This data contains additional information
42  * about error logs and may be system specific.
43  */
44 class Table
45 {
46     public:
47 
48         Table() = delete;
49         ~Table() = default;
50         Table(const Table&) = default;
51         Table& operator=(const Table&) = default;
52         Table(Table&&) = default;
53         Table& operator=(Table&&) = default;
54 
55         /**
56          * Constructor
57          *
58          * @param[in] jsonFile - the path to the policy JSON.
59          */
60         explicit Table(const std::string& jsonFile);
61 
62         /**
63          * Says if the JSON has been loaded successfully.
64          *
65          * @return bool
66          */
67         inline bool isLoaded() const
68         {
69             return loaded;
70         }
71 
72         /**
73          * Finds an entry in the policy table based on the
74          * error and the search modifier.
75          *
76          * @param[in] error - the error, like xyz.openbmc_project.Error.X
77          * @param[in] modifier - the search modifier, used to find the entry
78          *                   when multiple ones share the same error
79          *
80          * @return optional<DetailsReference> - the details entry
81          */
82         optional_ns::optional<DetailsReference> find(
83                 const std::string& error,
84                 const std::string& modifier) const;
85 
86         /**
87          * The default event ID to use when a match in the table
88          * wasn't found.
89          *
90          * @return std::string
91          */
92         inline std::string defaultEID() const
93         {
94             return defaultPolicyEID;
95         }
96 
97         /**
98          * The default error message to use when a match in the table
99          * wasn't found.
100          *
101          * @return std::string
102          */
103         inline std::string defaultMsg() const
104         {
105             return defaultPolicyMessage;
106         }
107 
108     private:
109         /**
110          * The default event ID
111          */
112         const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
113 
114         /**
115          * The default event message
116          */
117         const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
118 
119         /**
120          * Loads the JSON data into the PolicyMap map
121          *
122          * @param[in] jsonFile - the path to the .json file
123          */
124         void load(const std::string& jsonFile);
125 
126         /**
127          * Reflects if the JSON was successfully loaded or not.
128          */
129         bool loaded = false;
130 
131         /**
132          * The policy table
133          */
134         PolicyMap policies;
135 };
136 
137 
138 }
139 }
140 }
141