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