Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 5 | #include <experimental/optional> |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 6 | #include <map> |
| 7 | #include <vector> |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 8 | |
| 9 | namespace ibm |
| 10 | { |
| 11 | namespace logging |
| 12 | { |
| 13 | namespace policy |
| 14 | { |
| 15 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 16 | /** |
| 17 | * The details of a policy table entry: |
| 18 | * - search modifier |
| 19 | * - error message |
| 20 | * - common error event ID |
| 21 | */ |
| 22 | struct Details |
| 23 | { |
| 24 | std::string modifier; |
| 25 | std::string msg; |
| 26 | std::string ceid; |
| 27 | }; |
| 28 | |
| 29 | using DetailsList = std::vector<Details>; |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 30 | using DetailsReference = std::reference_wrapper<const Details>; |
| 31 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 32 | using PolicyMap = std::map<std::string, DetailsList>; |
| 33 | |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 34 | namespace optional_ns = std::experimental; |
| 35 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 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 | */ |
| 44 | class Table |
| 45 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 46 | 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 53 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 54 | /** |
| 55 | * Constructor |
| 56 | * |
| 57 | * @param[in] jsonFile - the path to the policy JSON. |
| 58 | */ |
| 59 | explicit Table(const std::string& jsonFile); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 60 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 61 | /** |
| 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 70 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 71 | /** |
| 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 | */ |
| 81 | optional_ns::optional<DetailsReference> |
| 82 | find(const std::string& error, const std::string& modifier) const; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 83 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 84 | /** |
| 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 Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 94 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 95 | /** |
| 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 Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 105 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 106 | private: |
| 107 | /** |
| 108 | * The default event ID |
| 109 | */ |
| 110 | const std::string defaultPolicyEID{DEFAULT_POLICY_EID}; |
Matt Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 111 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 112 | /** |
| 113 | * The default event message |
| 114 | */ |
| 115 | const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG}; |
Matt Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 116 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 117 | /** |
| 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 123 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 124 | /** |
| 125 | * Reflects if the JSON was successfully loaded or not. |
| 126 | */ |
| 127 | bool loaded = false; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 128 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 129 | /** |
| 130 | * The policy table |
| 131 | */ |
| 132 | PolicyMap policies; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 133 | }; |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 134 | } // namespace policy |
| 135 | } // namespace logging |
| 136 | } // namespace ibm |