xref: /openbmc/ibm-logging/policy_table.hpp (revision 29c2ec6dd67df321662c906711ec95a4635b372f)
18c0a63f9SMatt Spinler #pragma once
28c0a63f9SMatt Spinler 
366e07073SMatt Spinler #include "config.h"
466e07073SMatt Spinler 
58c0a63f9SMatt Spinler #include <map>
654ea3ad8SMatt Spinler #include <optional>
7*29c2ec6dSAndrew Geissler #include <string>
88c0a63f9SMatt Spinler #include <vector>
98c0a63f9SMatt Spinler 
108c0a63f9SMatt Spinler namespace ibm
118c0a63f9SMatt Spinler {
128c0a63f9SMatt Spinler namespace logging
138c0a63f9SMatt Spinler {
148c0a63f9SMatt Spinler namespace policy
158c0a63f9SMatt Spinler {
168c0a63f9SMatt Spinler 
178c0a63f9SMatt Spinler /**
188c0a63f9SMatt Spinler  *  The details of a policy table entry:
198c0a63f9SMatt Spinler  *  - search modifier
208c0a63f9SMatt Spinler  *  - error message
218c0a63f9SMatt Spinler  *  - common error event ID
228c0a63f9SMatt Spinler  */
238c0a63f9SMatt Spinler struct Details
248c0a63f9SMatt Spinler {
258c0a63f9SMatt Spinler     std::string modifier;
268c0a63f9SMatt Spinler     std::string msg;
278c0a63f9SMatt Spinler     std::string ceid;
288c0a63f9SMatt Spinler };
298c0a63f9SMatt Spinler 
308c0a63f9SMatt Spinler using DetailsList = std::vector<Details>;
313c9e3016SMatt Spinler using DetailsReference = std::reference_wrapper<const Details>;
3254ea3ad8SMatt Spinler using FindResult = std::optional<DetailsReference>;
333c9e3016SMatt Spinler 
348c0a63f9SMatt Spinler using PolicyMap = std::map<std::string, DetailsList>;
358c0a63f9SMatt Spinler 
368c0a63f9SMatt Spinler /**
378c0a63f9SMatt Spinler  * @class Table
388c0a63f9SMatt Spinler  *
398c0a63f9SMatt Spinler  * This class wraps the error policy table data, and provides the
408c0a63f9SMatt Spinler  * ability to find a policy table entry based on the error and a
418c0a63f9SMatt Spinler  * search modifier.  This data contains additional information
428c0a63f9SMatt Spinler  * about error logs and may be system specific.
438c0a63f9SMatt Spinler  */
448c0a63f9SMatt Spinler class Table
458c0a63f9SMatt Spinler {
468c0a63f9SMatt Spinler   public:
478c0a63f9SMatt Spinler     Table() = delete;
488c0a63f9SMatt Spinler     ~Table() = default;
498c0a63f9SMatt Spinler     Table(const Table&) = default;
508c0a63f9SMatt Spinler     Table& operator=(const Table&) = default;
518c0a63f9SMatt Spinler     Table(Table&&) = default;
528c0a63f9SMatt Spinler     Table& operator=(Table&&) = default;
538c0a63f9SMatt Spinler 
548c0a63f9SMatt Spinler     /**
558c0a63f9SMatt Spinler      * Constructor
568c0a63f9SMatt Spinler      *
578c0a63f9SMatt Spinler      * @param[in] jsonFile - the path to the policy JSON.
588c0a63f9SMatt Spinler      */
598c0a63f9SMatt Spinler     explicit Table(const std::string& jsonFile);
608c0a63f9SMatt Spinler 
618c0a63f9SMatt Spinler     /**
628c0a63f9SMatt Spinler      * Says if the JSON has been loaded successfully.
638c0a63f9SMatt Spinler      *
648c0a63f9SMatt Spinler      * @return bool
658c0a63f9SMatt Spinler      */
isLoaded() const668c0a63f9SMatt Spinler     inline bool isLoaded() const
678c0a63f9SMatt Spinler     {
688c0a63f9SMatt Spinler         return loaded;
698c0a63f9SMatt Spinler     }
708c0a63f9SMatt Spinler 
713c9e3016SMatt Spinler     /**
723c9e3016SMatt Spinler      * Finds an entry in the policy table based on the
733c9e3016SMatt Spinler      * error and the search modifier.
743c9e3016SMatt Spinler      *
753c9e3016SMatt Spinler      * @param[in] error - the error, like xyz.openbmc_project.Error.X
763c9e3016SMatt Spinler      * @param[in] modifier - the search modifier, used to find the entry
773c9e3016SMatt Spinler      *                   when multiple ones share the same error
783c9e3016SMatt Spinler      *
793c9e3016SMatt Spinler      * @return optional<DetailsReference> - the details entry
803c9e3016SMatt Spinler      */
81c57aa4b9SMatt Spinler     FindResult find(const std::string& error,
82c57aa4b9SMatt Spinler                     const std::string& modifier) const;
833c9e3016SMatt Spinler 
847ce3eef9SMatt Spinler     /**
857ce3eef9SMatt Spinler      * The default event ID to use when a match in the table
867ce3eef9SMatt Spinler      * wasn't found.
877ce3eef9SMatt Spinler      *
887ce3eef9SMatt Spinler      * @return std::string
897ce3eef9SMatt Spinler      */
defaultEID() const907ce3eef9SMatt Spinler     inline std::string defaultEID() const
917ce3eef9SMatt Spinler     {
927ce3eef9SMatt Spinler         return defaultPolicyEID;
937ce3eef9SMatt Spinler     }
947ce3eef9SMatt Spinler 
957ce3eef9SMatt Spinler     /**
967ce3eef9SMatt Spinler      * The default error message to use when a match in the table
977ce3eef9SMatt Spinler      * wasn't found.
987ce3eef9SMatt Spinler      *
997ce3eef9SMatt Spinler      * @return std::string
1007ce3eef9SMatt Spinler      */
defaultMsg() const1017ce3eef9SMatt Spinler     inline std::string defaultMsg() const
1027ce3eef9SMatt Spinler     {
1037ce3eef9SMatt Spinler         return defaultPolicyMessage;
1047ce3eef9SMatt Spinler     }
1057ce3eef9SMatt Spinler 
1068c0a63f9SMatt Spinler   private:
1077ce3eef9SMatt Spinler     /**
1087ce3eef9SMatt Spinler      * The default event ID
1097ce3eef9SMatt Spinler      */
1107ce3eef9SMatt Spinler     const std::string defaultPolicyEID{DEFAULT_POLICY_EID};
1117ce3eef9SMatt Spinler 
1127ce3eef9SMatt Spinler     /**
1137ce3eef9SMatt Spinler      * The default event message
1147ce3eef9SMatt Spinler      */
1157ce3eef9SMatt Spinler     const std::string defaultPolicyMessage{DEFAULT_POLICY_MSG};
1168c0a63f9SMatt Spinler 
1178c0a63f9SMatt Spinler     /**
1188c0a63f9SMatt Spinler      * Loads the JSON data into the PolicyMap map
1198c0a63f9SMatt Spinler      *
1208c0a63f9SMatt Spinler      * @param[in] jsonFile - the path to the .json file
1218c0a63f9SMatt Spinler      */
1228c0a63f9SMatt Spinler     void load(const std::string& jsonFile);
1238c0a63f9SMatt Spinler 
1248c0a63f9SMatt Spinler     /**
1258c0a63f9SMatt Spinler      * Reflects if the JSON was successfully loaded or not.
1268c0a63f9SMatt Spinler      */
1278c0a63f9SMatt Spinler     bool loaded = false;
1288c0a63f9SMatt Spinler 
1298c0a63f9SMatt Spinler     /**
1308c0a63f9SMatt Spinler      * The policy table
1318c0a63f9SMatt Spinler      */
1328c0a63f9SMatt Spinler     PolicyMap policies;
1338c0a63f9SMatt Spinler };
13466e07073SMatt Spinler } // namespace policy
13566e07073SMatt Spinler } // namespace logging
13666e07073SMatt Spinler } // namespace ibm
137