blob: 07226e66f04da8ad2ba339dd8428d3ae73d98cac [file] [log] [blame]
Matt Spinler8c0a63f2018-03-27 12:27:23 -05001#pragma once
2
Matt Spinler3c9e3012018-03-27 12:36:53 -05003#include <experimental/optional>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05004#include <map>
5#include <vector>
6#include "config.h"
7
8namespace ibm
9{
10namespace logging
11{
12namespace 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 */
22struct Details
23{
24 std::string modifier;
25 std::string msg;
26 std::string ceid;
27};
28
29using DetailsList = std::vector<Details>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050030using DetailsReference = std::reference_wrapper<const Details>;
31
Matt Spinler8c0a63f2018-03-27 12:27:23 -050032using PolicyMap = std::map<std::string, DetailsList>;
33
Matt Spinler3c9e3012018-03-27 12:36:53 -050034namespace optional_ns = std::experimental;
35
Matt Spinler8c0a63f2018-03-27 12:27:23 -050036/**
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 */
44class 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
Matt Spinler3c9e3012018-03-27 12:36:53 -050072 /**
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
Matt Spinler7ce3eef2018-03-27 13:54:48 -050086 /**
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
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500108 private:
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500109 /**
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};
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500118
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}