blob: 013578f40280602c5fee5d3132d12e887a3ae78d [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>
Andrew Geissler29c2ec62020-05-16 13:48:44 -05007#include <string>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05008#include <vector>
Matt Spinler8c0a63f2018-03-27 12:27:23 -05009
10namespace ibm
11{
12namespace logging
13{
14namespace policy
15{
16
Matt Spinler8c0a63f2018-03-27 12:27:23 -050017/**
18 * The details of a policy table entry:
19 * - search modifier
20 * - error message
21 * - common error event ID
22 */
23struct Details
24{
25 std::string modifier;
26 std::string msg;
27 std::string ceid;
28};
29
30using DetailsList = std::vector<Details>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050031using DetailsReference = std::reference_wrapper<const Details>;
Matt Spinler54ea3ad2018-10-23 10:40:09 -050032using FindResult = std::optional<DetailsReference>;
Matt Spinler3c9e3012018-03-27 12:36:53 -050033
Matt Spinler8c0a63f2018-03-27 12:27:23 -050034using PolicyMap = std::map<std::string, DetailsList>;
35
36/**
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{
Matt Spinler259e7272018-03-29 10:57:17 -050046 public:
47 Table() = delete;
48 ~Table() = default;
49 Table(const Table&) = default;
50 Table& operator=(const Table&) = default;
51 Table(Table&&) = default;
52 Table& operator=(Table&&) = default;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050053
Matt Spinler259e7272018-03-29 10:57:17 -050054 /**
55 * Constructor
56 *
57 * @param[in] jsonFile - the path to the policy JSON.
58 */
59 explicit Table(const std::string& jsonFile);
Matt Spinler8c0a63f2018-03-27 12:27:23 -050060
Matt Spinler259e7272018-03-29 10:57:17 -050061 /**
62 * Says if the JSON has been loaded successfully.
63 *
64 * @return bool
65 */
66 inline bool isLoaded() const
67 {
68 return loaded;
69 }
Matt Spinler8c0a63f2018-03-27 12:27:23 -050070
Matt Spinler259e7272018-03-29 10:57:17 -050071 /**
72 * Finds an entry in the policy table based on the
73 * error and the search modifier.
74 *
75 * @param[in] error - the error, like xyz.openbmc_project.Error.X
76 * @param[in] modifier - the search modifier, used to find the entry
77 * when multiple ones share the same error
78 *
79 * @return optional<DetailsReference> - the details entry
80 */
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050081 FindResult find(const std::string& error,
82 const std::string& modifier) const;
Matt Spinler8c0a63f2018-03-27 12:27:23 -050083
Matt Spinler259e7272018-03-29 10:57:17 -050084 /**
85 * The default event ID to use when a match in the table
86 * wasn't found.
87 *
88 * @return std::string
89 */
90 inline std::string defaultEID() const
91 {
92 return defaultPolicyEID;
93 }
Matt Spinler3c9e3012018-03-27 12:36:53 -050094
Matt Spinler259e7272018-03-29 10:57:17 -050095 /**
96 * The default error message to use when a match in the table
97 * wasn't found.
98 *
99 * @return std::string
100 */
101 inline std::string defaultMsg() const
102 {
103 return defaultPolicyMessage;
104 }
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500105
Matt Spinler259e7272018-03-29 10:57:17 -0500106 private:
107 /**
108 * The default event ID
109 */
110 const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500111
Matt Spinler259e7272018-03-29 10:57:17 -0500112 /**
113 * The default event message
114 */
115 const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
Matt Spinler7ce3eef2018-03-27 13:54:48 -0500116
Matt Spinler259e7272018-03-29 10:57:17 -0500117 /**
118 * Loads the JSON data into the PolicyMap map
119 *
120 * @param[in] jsonFile - the path to the .json file
121 */
122 void load(const std::string& jsonFile);
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500123
Matt Spinler259e7272018-03-29 10:57:17 -0500124 /**
125 * Reflects if the JSON was successfully loaded or not.
126 */
127 bool loaded = false;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500128
Matt Spinler259e7272018-03-29 10:57:17 -0500129 /**
130 * The policy table
131 */
132 PolicyMap policies;
Matt Spinler8c0a63f2018-03-27 12:27:23 -0500133};
Matt Spinler66e07072018-09-12 10:36:14 -0500134} // namespace policy
135} // namespace logging
136} // namespace ibm