blob: d6bf3077bcb834f9d9c80f3fcb4e5f032505db7c [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 Spinler8c0a63f2018-03-27 12:27:23 -05005#include <map>
Matt Spinler54ea3ad2018-10-23 10:40:09 -05006#include <optional>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05007#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
29using DetailsList = std::vector<Details>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050030using DetailsReference = std::reference_wrapper<const Details>;
Matt Spinler54ea3ad2018-10-23 10:40:09 -050031using FindResult = std::optional<DetailsReference>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050032
Matt Spinler8c0a63f2018-03-27 12:27:23 -050033using PolicyMap = std::map<std::string, DetailsList>;
34
35/**
36 * @class Table
37 *
38 * This class wraps the error policy table data, and provides the
39 * ability to find a policy table entry based on the error and a
40 * search modifier. This data contains additional information
41 * about error logs and may be system specific.
42 */
43class Table
44{
Matt Spinler259e7272018-03-29 10:57:17 -050045 public:
46 Table() = delete;
47 ~Table() = default;
48 Table(const Table&) = default;
49 Table& operator=(const Table&) = default;
50 Table(Table&&) = default;
51 Table& operator=(Table&&) = default;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050052
Matt Spinler259e7272018-03-29 10:57:17 -050053 /**
54 * Constructor
55 *
56 * @param[in] jsonFile - the path to the policy JSON.
57 */
58 explicit Table(const std::string& jsonFile);
Matt Spinler8c0a63f2018-03-27 12:27:23 -050059
Matt Spinler259e7272018-03-29 10:57:17 -050060 /**
61 * Says if the JSON has been loaded successfully.
62 *
63 * @return bool
64 */
65 inline bool isLoaded() const
66 {
67 return loaded;
68 }
Matt Spinler8c0a63f2018-03-27 12:27:23 -050069
Matt Spinler259e7272018-03-29 10:57:17 -050070 /**
71 * Finds an entry in the policy table based on the
72 * error and the search modifier.
73 *
74 * @param[in] error - the error, like xyz.openbmc_project.Error.X
75 * @param[in] modifier - the search modifier, used to find the entry
76 * when multiple ones share the same error
77 *
78 * @return optional<DetailsReference> - the details entry
79 */
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050080 FindResult find(const std::string& error,
81 const std::string& modifier) const;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050082
Matt Spinler259e7272018-03-29 10:57:17 -050083 /**
84 * The default event ID to use when a match in the table
85 * wasn't found.
86 *
87 * @return std::string
88 */
89 inline std::string defaultEID() const
90 {
91 return defaultPolicyEID;
92 }
Matt Spinler3c9e3012018-03-27 12:36:53 -050093
Matt Spinler259e7272018-03-29 10:57:17 -050094 /**
95 * The default error message to use when a match in the table
96 * wasn't found.
97 *
98 * @return std::string
99 */
100 inline std::string defaultMsg() const
101 {
102 return defaultPolicyMessage;
103 }
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500104
Matt Spinler259e7272018-03-29 10:57:17 -0500105 private:
106 /**
107 * The default event ID
108 */
109 const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500110
Matt Spinler259e7272018-03-29 10:57:17 -0500111 /**
112 * The default event message
113 */
114 const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500115
Matt Spinler259e7272018-03-29 10:57:17 -0500116 /**
117 * Loads the JSON data into the PolicyMap map
118 *
119 * @param[in] jsonFile - the path to the .json file
120 */
121 void load(const std::string& jsonFile);
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500122
Matt Spinler259e7272018-03-29 10:57:17 -0500123 /**
124 * Reflects if the JSON was successfully loaded or not.
125 */
126 bool loaded = false;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500127
Matt Spinler259e7272018-03-29 10:57:17 -0500128 /**
129 * The policy table
130 */
131 PolicyMap policies;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500132};
Matt Spinler66e07072018-09-12 10:36:14 -0500133} // namespace policy
134} // namespace logging
135} // namespace ibm