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 | |
Matt Spinler | c57aa4b | 2018-09-28 10:32:24 -0500 | [diff] [blame^] | 29 | namespace optional_ns = std::experimental; |
| 30 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 31 | using DetailsList = std::vector<Details>; |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 32 | using DetailsReference = std::reference_wrapper<const Details>; |
Matt Spinler | c57aa4b | 2018-09-28 10:32:24 -0500 | [diff] [blame^] | 33 | using FindResult = optional_ns::optional<DetailsReference>; |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 34 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 35 | using 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 | */ |
| 45 | class Table |
| 46 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 47 | 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 54 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 55 | /** |
| 56 | * Constructor |
| 57 | * |
| 58 | * @param[in] jsonFile - the path to the policy JSON. |
| 59 | */ |
| 60 | explicit Table(const std::string& jsonFile); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 62 | /** |
| 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 71 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 72 | /** |
| 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 Spinler | c57aa4b | 2018-09-28 10:32:24 -0500 | [diff] [blame^] | 82 | FindResult find(const std::string& error, |
| 83 | const std::string& modifier) const; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 84 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 85 | /** |
| 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 Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 95 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 96 | /** |
| 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 Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 106 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 107 | private: |
| 108 | /** |
| 109 | * The default event ID |
| 110 | */ |
| 111 | const std::string defaultPolicyEID{DEFAULT_POLICY_EID}; |
Matt Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 112 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 113 | /** |
| 114 | * The default event message |
| 115 | */ |
| 116 | const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG}; |
Matt Spinler | 7ce3eef | 2018-03-27 13:54:48 -0500 | [diff] [blame] | 117 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 118 | /** |
| 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 Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 124 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 125 | /** |
| 126 | * Reflects if the JSON was successfully loaded or not. |
| 127 | */ |
| 128 | bool loaded = false; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 129 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 130 | /** |
| 131 | * The policy table |
| 132 | */ |
| 133 | PolicyMap policies; |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 134 | }; |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 135 | } // namespace policy |
| 136 | } // namespace logging |
| 137 | } // namespace ibm |