blob: 9272daac8cf7a99a6dc57036fa149f7224a2a5ce [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
Matt Spinler8c0a63f2018-03-27 12:27:23 -050015/**
16 * The details of a policy table entry:
17 * - search modifier
18 * - error message
19 * - common error event ID
20 */
21struct Details
22{
23 std::string modifier;
24 std::string msg;
25 std::string ceid;
26};
27
28using DetailsList = std::vector<Details>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050029using DetailsReference = std::reference_wrapper<const Details>;
30
Matt Spinler8c0a63f2018-03-27 12:27:23 -050031using PolicyMap = std::map<std::string, DetailsList>;
32
Matt Spinler3c9e3012018-03-27 12:36:53 -050033namespace optional_ns = std::experimental;
34
Matt Spinler8c0a63f2018-03-27 12:27:23 -050035/**
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 */
80 optional_ns::optional<DetailsReference>
81 find(const std::string& error, 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 Spinler8c0a63f2018-03-27 12:27:23 -0500133}
134}
135}