1 #pragma once
2 #include "additional_data.hpp"
3 
4 #include <nlohmann/json.hpp>
5 
6 #include <filesystem>
7 #include <optional>
8 #include <string>
9 #include <variant>
10 #include <vector>
11 
12 namespace openpower
13 {
14 namespace pels
15 {
16 namespace message
17 {
18 
19 constexpr auto registryFileName = "message_registry.json";
20 enum class LookupType
21 {
22     name = 0,
23     reasonCode = 1
24 };
25 
26 /**
27  * @brief A possible severity/system type combination
28  *
29  * If there is no system type defined for this entry,
30  * then the system field will be empty.
31  */
32 struct RegistrySeverity
33 {
34     std::string system;
35     uint8_t severity;
36 };
37 
38 /**
39  * @brief Represents the Documentation related fields in the message registry.
40  *        It is part of the 'Entry' structure that will be filled in when
41  *        an error is looked up in the registry.
42  *
43  * If a field is wrapped by std::optional, it means the field is
44  * optional in the JSON and higher level code knows how to handle it.
45  */
46 struct DOC
47 {
48     /**
49      * @brief Description of error
50      */
51     std::string description;
52 
53     /**
54      * @brief Error message field
55      */
56     std::string message;
57 
58     /**
59      * @brief An optional vector of SRC word 6-9 to use as the source of the
60      *        numeric arguments that will be substituted into any placeholder
61      *        in the Message field.
62      */
63     std::optional<std::vector<std::string>> messageArgSources;
64 };
65 
66 /**
67  * @brief Represents the SRC related fields in the message registry.
68  *        It is part of the 'Entry' structure that will be filled in when
69  *        an error is looked up in the registry.
70  *
71  * If a field is wrapped by std::optional, it means the field is
72  * optional in the JSON and higher level code knows how to handle it.
73  */
74 struct SRC
75 {
76     /**
77      * @brief SRC type - The first byte of the ASCII string
78      */
79     uint8_t type;
80 
81     /**
82      * @brief The SRC reason code (2nd half of 4B 'ASCII string' word)
83      */
84     uint16_t reasonCode;
85 
86     /**
87      * @brief An optional vector of SRC hexword numbers that should be used
88      *        along with the SRC ASCII string to build the Symptom ID, which
89      *        is a field in the Extended Header section.
90      */
91     using WordNum = size_t;
92     std::optional<std::vector<WordNum>> symptomID;
93 
94     /**
95      * @brief Which AdditionalData fields to use to fill in the user defined
96      *        SRC hexwords.
97      *
98      * For example, if the AdditionalData event log property contained
99      * "CHIPNUM=42" and this map contained {6, {"CHIPNUM", "DESC"}}, then the
100      * code would put 42 into SRC hexword 6.
101      *
102      * AdditionalDataField specifies two fields from the SRC entry in the
103      * message registry: "AdditionalDataPropSource" and "Description"
104      */
105     using AdditionalDataField = std::tuple<std::string, std::string>;
106     std::optional<std::map<WordNum, AdditionalDataField>> hexwordADFields;
107 
108     SRC() : type(0), reasonCode(0) {}
109 };
110 
111 struct AppCapture
112 {
113     std::string syslogID;
114     size_t numLines;
115 };
116 
117 // Can specify either the syslog IDs to capture along with how many
118 // entries of each, or just how many entries to get the full journal.
119 using AppCaptureList = std::vector<AppCapture>;
120 using JournalCapture = std::variant<size_t, AppCaptureList>;
121 
122 /**
123  * @brief Represents a message registry entry, which is used for creating a
124  *        PEL from an OpenBMC event log.
125  */
126 struct Entry
127 {
128     /**
129      * @brief The error name, like "xyz.openbmc_project.Error.Foo".
130      */
131     std::string name;
132 
133     /**
134      * @brief The component ID of the PEL creator.
135      */
136     uint16_t componentID;
137 
138     /**
139      * @brief The PEL subsystem field.
140      */
141     std::optional<uint8_t> subsystem;
142 
143     /**
144      * @brief The optional PEL severity field.  If not specified, the PEL
145      *        will use the severity of the OpenBMC event log.
146      *
147      * If the system type is specified in any of the entries in the vector,
148      * then the system type will be needed to find the actual severity.
149      */
150     std::optional<std::vector<RegistrySeverity>> severity;
151 
152     /**
153      * @brief The optional severity field to use when in manufacturing tolerance
154      *        mode.  It behaves like the severity field above.
155      */
156     std::optional<std::vector<RegistrySeverity>> mfgSeverity;
157 
158     /**
159      * @brief The PEL action flags field.
160      */
161     std::optional<uint16_t> actionFlags;
162 
163     /**
164      * @brief  The optional action flags to use instead when in manufacturing
165      * tolerance mode.
166      */
167     std::optional<uint16_t> mfgActionFlags;
168 
169     /**
170      * @brief The PEL event type field.  If not specified, higher level code
171      *        will decide the value.
172      */
173     std::optional<uint8_t> eventType;
174 
175     /**
176      * @brief The PEL event scope field.  If not specified, higher level code
177      *        will decide the value.
178      */
179     std::optional<uint8_t> eventScope;
180 
181     /**
182      * The SRC related fields.
183      */
184     SRC src;
185 
186     /**
187      * The Documentation related fields.
188      */
189     DOC doc;
190 
191     /**
192      * @brief The callout JSON, if the entry has callouts.
193      */
194     std::optional<nlohmann::json> callouts;
195 
196     /**
197      * @brief The journal capture instructions, if present.
198      */
199     std::optional<JournalCapture> journalCapture;
200 };
201 
202 /**
203  * @brief Holds callout information pulled out of the JSON.
204  */
205 struct RegistryCallout
206 {
207     std::string priority;
208     std::string locCode;
209     std::string procedure;
210     std::string symbolicFRU;
211     std::string symbolicFRUTrusted;
212     bool useInventoryLocCode;
213 };
214 
215 /**
216  * @class Registry
217  *
218  * This class wraps the message registry JSON data and allows one to find
219  * the message registry entry pertaining to the error name.
220  *
221  * So that new registry files can easily be tested, the code will look for
222  * /etc/phosphor-logging/message_registry.json before looking for the real
223  * path.
224  */
225 class Registry
226 {
227   public:
228     Registry() = delete;
229     ~Registry() = default;
230     Registry(const Registry&) = default;
231     Registry& operator=(const Registry&) = default;
232     Registry(Registry&&) = default;
233     Registry& operator=(Registry&&) = default;
234 
235     /**
236      * @brief Constructor
237      *
238      * Will load the callout JSON.
239      *
240      * @param[in] registryFile - The path to the file.
241      */
242     explicit Registry(const std::filesystem::path& registryFile) :
243         Registry(registryFile, true)
244     {}
245 
246     /**
247      * @brief Constructor
248      *
249      * This version contains a parameter that allows the callout JSON
250      * to be saved in the Entry struct or not, as it isn't needed at
251      * all in some cases.
252      *
253      * @param[in] registryFile - The path to the file.
254      * @param[in] loadCallouts - If the callout JSON should be saved.
255      */
256     explicit Registry(const std::filesystem::path& registryFile,
257                       bool loadCallouts) :
258         _registryFile(registryFile),
259         _loadCallouts(loadCallouts)
260     {}
261 
262     /**
263      * @brief Find a registry entry based on its error name or reason code.
264      *
265      * This function does do some basic sanity checking on the JSON contents,
266      * but there is also an external program that enforces a schema on the
267      * registry JSON that should catch all of these problems ahead of time.
268      *
269      * @param[in] name - The error name, like xyz.openbmc_project.Error.Foo
270      *                 - OR
271      *                 - The reason code, like 0x1001
272      * @param[in] type - LookupType enum value
273      * @param[in] toCache - boolean to cache registry in memory
274      * @return optional<Entry> A filled in message registry structure if
275      *                         found, otherwise an empty optional object.
276      */
277     std::optional<Entry> lookup(const std::string& name, LookupType type,
278                                 bool toCache = false);
279 
280     /**
281      * @brief Find the callouts to put into the PEL based on the calloutJSON
282      *        data.
283      *
284      * The system type and AdditionalData are used to index into the correct
285      * callout table.
286      *
287      * Throws exceptions on failures.
288      *
289      * @param[in] calloutJSON - Where to look up the  callouts
290      * @param[in] systemNames - List of compatible system type names
291      * @param[in] additionalData - The AdditionalData property
292      *
293      * @return std::vector<RegistryCallout> - The callouts to use
294      */
295     static std::vector<RegistryCallout>
296         getCallouts(const nlohmann::json& calloutJSON,
297                     const std::vector<std::string>& systemNames,
298                     const AdditionalData& additionalData);
299 
300   private:
301     /**
302      * @brief Parse message registry file using nlohmann::json
303      * @param[in] registryFile - The message registry JSON file
304      * @return optional<nlohmann::json> The full message registry object or an
305      *                                  empty optional object upon failure.
306      */
307     std::optional<nlohmann::json>
308         readRegistry(const std::filesystem::path& registryFile);
309 
310     /**
311      * @brief The path to the registry JSON file.
312      */
313     std::filesystem::path _registryFile;
314 
315     /**
316      * @brief The full message registry object.
317      */
318     std::optional<nlohmann::json> _registry;
319 
320     /**
321      * @brief If the callout JSON should be saved in the Entry on lookup.
322      */
323     bool _loadCallouts;
324 };
325 
326 namespace helper
327 {
328 
329 /**
330  * @brief A helper function to get the PEL subsystem value based on
331  *        the registry subsystem name.
332  *
333  * @param[in] subsystemName - The registry name for the subsystem
334  *
335  * @return uint8_t The PEL subsystem value
336  */
337 uint8_t getSubsystem(const std::string& subsystemName);
338 
339 /**
340  * @brief A helper function to get the PEL severity value based on
341  *        the registry severity name.
342  *
343  * @param[in] severityName - The registry name for the severity
344  *
345  * @return uint8_t The PEL severity value
346  */
347 uint8_t getSeverity(const std::string& severityName);
348 
349 /**
350  * @brief Returns all of the system type/severity values found
351  * in the severity JSON passed in.
352  *
353  * The JSON is either a simple string, like:
354  *     "unrecoverable"
355  * or an array of system type/severity pairs, like:
356  *     [
357  *        {
358  *            "System": "1",
359  *            "SevValue": "predictive"
360  *        },
361  *        {
362  *            "System": "2",
363  *            "SevValue": "recovered"
364  *        }
365  *     ]
366  *
367  * @param[in] severity - The severity JSON
368  * @return The list of severity/system combinations.  If the System key
369  *         wasn't used, then that field will be empty in the structure.
370  */
371 std::vector<RegistrySeverity> getSeverities(const nlohmann::json& severity);
372 
373 /**
374  * @brief A helper function to get the action flags value based on
375  *        the action flag names used in the registry.
376  *
377  * @param[in] flags - The list of flag names from the registry.
378  *
379  * @return uint16_t - The bitfield of flags used in the PEL.
380  */
381 uint16_t getActionFlags(const std::vector<std::string>& flags);
382 
383 /**
384  * @brief A helper function to get the PEL event type value based on
385  *        the registry event type name.
386  *
387  * @param[in] eventTypeName - The registry name for the event type
388  *
389  * @return uint8_t The PEL event type value
390  */
391 uint8_t getEventType(const std::string& eventTypeName);
392 
393 /**
394  * @brief A helper function to get the PEL event scope value based on
395  *        the registry event scope name.
396  *
397  * @param[in] eventScopeName - The registry name for the event scope
398  *
399  * @return uint8_t The PEL event scope value
400  */
401 uint8_t getEventScope(const std::string& eventScopeName);
402 
403 /**
404  * @brief Reads the "ReasonCode" field out of JSON and converts the string value
405  *        such as "0x5555" to a uint16 like 0x5555.
406  *
407  * @param[in] src - The message registry SRC dictionary to read from
408  * @param[in] name - The error name, to use in a trace if things go awry.
409  *
410  * @return uint16_t - The reason code
411  */
412 uint16_t getSRCReasonCode(const nlohmann::json& src, const std::string& name);
413 
414 /**
415  * @brief Reads the "Type" field out of JSON and converts it to the SRC::Type
416  *        value.
417  *
418  * @param[in] src - The message registry SRC dictionary to read from
419  * @param[in] name - The error name, to use in a trace if things go awry.
420  *
421  * @return uint8_t - The SRC type value, like 0x11
422  */
423 uint8_t getSRCType(const nlohmann::json& src, const std::string& name);
424 
425 /**
426  * @brief Reads the "Words6To9" field out of JSON and converts it to a map
427  *        of the SRC word number to the AdditionalData property field used
428  *        to fill it in with.
429  *
430  * @param[in] src - The message registry SRC dictionary to read from
431  * @param[in] name - The error name, to use in a trace if things go awry.
432  *
433  * @return std::optional<std::map<SRC::WordNum, SRC::AdditionalDataField>>
434  */
435 std::optional<std::map<SRC::WordNum, SRC::AdditionalDataField>>
436     getSRCHexwordFields(const nlohmann::json& src, const std::string& name);
437 
438 /**
439  * @brief Reads the "SymptomIDFields" field out of JSON and converts it to
440  *        a vector of SRC word numbers.
441  *
442  * @param[in] src - The message registry SRC dictionary to read from
443  * @param[in] name - The error name, to use in a trace if things go awry.
444  *
445  * @return std::optional<std::vector<SRC::WordNum>>
446  */
447 std::optional<std::vector<SRC::WordNum>>
448     getSRCSymptomIDFields(const nlohmann::json& src, const std::string& name);
449 
450 /**
451  * @brief Reads the "ComponentID" field out of JSON and converts it to a
452  *        uint16_t like 0xFF00.
453  *
454  * The ComponentID JSON field is only required if the SRC type isn't a BD
455  * BMC SRC, because for those SRCs it can be inferred from the upper byte
456  * of the SRC reasoncode.
457  *
458  * @param[in] srcType - The SRC type
459  * @param[in] reasonCode - The SRC reason code
460  * @param[in] pelEntry - The PEL entry JSON
461  * @param[in] name - The error name, to use in a trace if things go awry.
462  *
463  * @return uin16_t - The component ID, like 0xFF00
464  */
465 uint16_t getComponentID(uint8_t srcType, uint16_t reasonCode,
466                         const nlohmann::json& pelEntry,
467                         const std::string& name);
468 
469 } // namespace helper
470 
471 } // namespace message
472 
473 } // namespace pels
474 } // namespace openpower
475