blob: a2e73465bf3ef0456f9f1d0ad81613410e8668cd [file] [log] [blame]
Matt Spinler8c0a63f2018-03-27 12:27:23 -05001#pragma once
2
Matt Spinler66e07072018-09-12 10:36:14 -05003#include "config.h"
4
Matt Spinler3c9e3012018-03-27 12:36:53 -05005#include <experimental/optional>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05006#include <map>
7#include <vector>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05008
9namespace ibm
10{
11namespace logging
12{
13namespace policy
14{
15
Matt Spinler8c0a63f2018-03-27 12:27:23 -050016/**
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
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050029namespace optional_ns = std::experimental;
30
Matt Spinler8c0a63f2018-03-27 12:27:23 -050031using DetailsList = std::vector<Details>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050032using DetailsReference = std::reference_wrapper<const Details>;
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050033using FindResult = optional_ns::optional<DetailsReference>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050034
Matt Spinler8c0a63f2018-03-27 12:27:23 -050035using 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 */
45class Table
46{
Matt Spinler259e7272018-03-29 10:57:17 -050047 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;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050054
Matt Spinler259e7272018-03-29 10:57:17 -050055 /**
56 * Constructor
57 *
58 * @param[in] jsonFile - the path to the policy JSON.
59 */
60 explicit Table(const std::string& jsonFile);
Matt Spinler8c0a63f2018-03-27 12:27:23 -050061
Matt Spinler259e7272018-03-29 10:57:17 -050062 /**
63 * Says if the JSON has been loaded successfully.
64 *
65 * @return bool
66 */
67 inline bool isLoaded() const
68 {
69 return loaded;
70 }
Matt Spinler8c0a63f2018-03-27 12:27:23 -050071
Matt Spinler259e7272018-03-29 10:57:17 -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 */
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050082 FindResult find(const std::string& error,
83 const std::string& modifier) const;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050084
Matt Spinler259e7272018-03-29 10:57:17 -050085 /**
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 }
Matt Spinler3c9e3012018-03-27 12:36:53 -050095
Matt Spinler259e7272018-03-29 10:57:17 -050096 /**
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 }
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500106
Matt Spinler259e7272018-03-29 10:57:17 -0500107 private:
108 /**
109 * The default event ID
110 */
111 const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500112
Matt Spinler259e7272018-03-29 10:57:17 -0500113 /**
114 * The default event message
115 */
116 const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500117
Matt Spinler259e7272018-03-29 10:57:17 -0500118 /**
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);
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500124
Matt Spinler259e7272018-03-29 10:57:17 -0500125 /**
126 * Reflects if the JSON was successfully loaded or not.
127 */
128 bool loaded = false;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500129
Matt Spinler259e7272018-03-29 10:57:17 -0500130 /**
131 * The policy table
132 */
133 PolicyMap policies;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500134};
Matt Spinler66e07072018-09-12 10:36:14 -0500135} // namespace policy
136} // namespace logging
137} // namespace ibm