xref: /openbmc/ibm-logging/policy_table.hpp (revision 29c2ec6d)
1 #pragma once
2 
3 #include "config.h"
4 
5 #include <map>
6 #include <optional>
7 #include <string>
8 #include <vector>
9 
10 namespace ibm
11 {
12 namespace logging
13 {
14 namespace policy
15 {
16 
17 /**
18  *  The details of a policy table entry:
19  *  - search modifier
20  *  - error message
21  *  - common error event ID
22  */
23 struct Details
24 {
25     std::string modifier;
26     std::string msg;
27     std::string ceid;
28 };
29 
30 using DetailsList = std::vector<Details>;
31 using DetailsReference = std::reference_wrapper<const Details>;
32 using FindResult = std::optional<DetailsReference>;
33 
34 using PolicyMap = std::map<std::string, DetailsList>;
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      */
isLoaded() const66     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     FindResult find(const std::string& error,
82                     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      */
defaultEID() const90     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      */
defaultMsg() const101     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