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