xref: /openbmc/ibm-logging/policy_table.hpp (revision c57aa4b9310eed09a0ad3abd1232386c15d71064)
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 namespace optional_ns = std::experimental;
30 
31 using DetailsList = std::vector<Details>;
32 using DetailsReference = std::reference_wrapper<const Details>;
33 using FindResult = optional_ns::optional<DetailsReference>;
34 
35 using PolicyMap = std::map<std::string, DetailsList>;
36 
37 /**
38  * @class Table
39  *
40  * This class wraps the error policy table data, and provides the
41  * ability to find a policy table entry based on the error and a
42  * search modifier.  This data contains additional information
43  * about error logs and may be system specific.
44  */
45 class Table
46 {
47   public:
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     FindResult find(const std::string& error,
83                     const std::string& modifier) const;
84 
85     /**
86      * The default event ID to use when a match in the table
87      * wasn't found.
88      *
89      * @return std::string
90      */
91     inline std::string defaultEID() const
92     {
93         return defaultPolicyEID;
94     }
95 
96     /**
97      * The default error message to use when a match in the table
98      * wasn't found.
99      *
100      * @return std::string
101      */
102     inline std::string defaultMsg() const
103     {
104         return defaultPolicyMessage;
105     }
106 
107   private:
108     /**
109      * The default event ID
110      */
111     const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
112 
113     /**
114      * The default event message
115      */
116     const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
117 
118     /**
119      * Loads the JSON data into the PolicyMap map
120      *
121      * @param[in] jsonFile - the path to the .json file
122      */
123     void load(const std::string& jsonFile);
124 
125     /**
126      * Reflects if the JSON was successfully loaded or not.
127      */
128     bool loaded = false;
129 
130     /**
131      * The policy table
132      */
133     PolicyMap policies;
134 };
135 } // namespace policy
136 } // namespace logging
137 } // namespace ibm
138