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