blob: e72fafb16d012488639acd31043cbb0013696081 [file] [log] [blame]
Matt Spinler367144c2019-09-19 15:33:52 -05001#pragma once
Matt Spinler6b427cc2020-04-09 09:42:59 -05002#include "additional_data.hpp"
3
Matt Spinler367144c2019-09-19 15:33:52 -05004#include <nlohmann/json.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -05005
6#include <filesystem>
Matt Spinler367144c2019-09-19 15:33:52 -05007#include <optional>
8#include <string>
Matt Spinler711f1122022-12-15 11:41:20 -06009#include <variant>
Matt Spinler367144c2019-09-19 15:33:52 -050010#include <vector>
11
12namespace openpower
13{
14namespace pels
15{
16namespace message
17{
18
19constexpr auto registryFileName = "message_registry.json";
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080020enum class LookupType
21{
22 name = 0,
23 reasonCode = 1
24};
25
26/**
Matt Spinleraadccc82020-04-10 14:33:42 -050027 * @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 */
32struct RegistrySeverity
33{
34 std::string system;
35 uint8_t severity;
36};
37
38/**
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080039 * @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 */
46struct 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};
Matt Spinler367144c2019-09-19 15:33:52 -050065
66/**
Matt Spinler93e29322019-09-20 11:16:15 -050067 * @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 */
74struct 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 /**
Matt Spinler93e29322019-09-20 11:16:15 -050087 * @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
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +080099 * "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"
Matt Spinler93e29322019-09-20 11:16:15 -0500104 */
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800105 using AdditionalDataField = std::tuple<std::string, std::string>;
Matt Spinler93e29322019-09-20 11:16:15 -0500106 std::optional<std::map<WordNum, AdditionalDataField>> hexwordADFields;
107
Matt Spinler3fe93e92023-04-14 14:06:59 -0500108 /**
109 * @brief If the Deconfigured flag should be set in hex word 5
110 */
111 bool deconfigFlag;
112
113 SRC() : type(0), reasonCode(0), deconfigFlag(false) {}
Matt Spinler93e29322019-09-20 11:16:15 -0500114};
115
Matt Spinler711f1122022-12-15 11:41:20 -0600116struct AppCapture
117{
118 std::string syslogID;
119 size_t numLines;
120};
121
122// Can specify either the syslog IDs to capture along with how many
123// entries of each, or just how many entries to get the full journal.
124using AppCaptureList = std::vector<AppCapture>;
125using JournalCapture = std::variant<size_t, AppCaptureList>;
126
Matt Spinler93e29322019-09-20 11:16:15 -0500127/**
Matt Spinler367144c2019-09-19 15:33:52 -0500128 * @brief Represents a message registry entry, which is used for creating a
129 * PEL from an OpenBMC event log.
130 */
131struct Entry
132{
133 /**
134 * @brief The error name, like "xyz.openbmc_project.Error.Foo".
135 */
136 std::string name;
137
138 /**
Matt Spinler93e29322019-09-20 11:16:15 -0500139 * @brief The component ID of the PEL creator.
140 */
141 uint16_t componentID;
142
143 /**
Matt Spinler367144c2019-09-19 15:33:52 -0500144 * @brief The PEL subsystem field.
145 */
Matt Spinler23970b02022-02-25 16:34:46 -0600146 std::optional<uint8_t> subsystem;
Matt Spinler367144c2019-09-19 15:33:52 -0500147
148 /**
149 * @brief The optional PEL severity field. If not specified, the PEL
150 * will use the severity of the OpenBMC event log.
Matt Spinleraadccc82020-04-10 14:33:42 -0500151 *
152 * If the system type is specified in any of the entries in the vector,
153 * then the system type will be needed to find the actual severity.
Matt Spinler367144c2019-09-19 15:33:52 -0500154 */
Matt Spinleraadccc82020-04-10 14:33:42 -0500155 std::optional<std::vector<RegistrySeverity>> severity;
Matt Spinler367144c2019-09-19 15:33:52 -0500156
157 /**
158 * @brief The optional severity field to use when in manufacturing tolerance
Matt Spinleraadccc82020-04-10 14:33:42 -0500159 * mode. It behaves like the severity field above.
Matt Spinler367144c2019-09-19 15:33:52 -0500160 */
Matt Spinleraadccc82020-04-10 14:33:42 -0500161 std::optional<std::vector<RegistrySeverity>> mfgSeverity;
Matt Spinler367144c2019-09-19 15:33:52 -0500162
163 /**
164 * @brief The PEL action flags field.
165 */
Matt Spinlere07f9152019-11-01 10:48:36 -0500166 std::optional<uint16_t> actionFlags;
Matt Spinler367144c2019-09-19 15:33:52 -0500167
168 /**
169 * @brief The optional action flags to use instead when in manufacturing
170 * tolerance mode.
171 */
172 std::optional<uint16_t> mfgActionFlags;
173
174 /**
175 * @brief The PEL event type field. If not specified, higher level code
176 * will decide the value.
177 */
178 std::optional<uint8_t> eventType;
179
180 /**
181 * @brief The PEL event scope field. If not specified, higher level code
182 * will decide the value.
183 */
184 std::optional<uint8_t> eventScope;
185
Matt Spinler93e29322019-09-20 11:16:15 -0500186 /**
187 * The SRC related fields.
188 */
189 SRC src;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800190
191 /**
192 * The Documentation related fields.
193 */
194 DOC doc;
Matt Spinlerd8e29002020-04-09 09:11:22 -0500195
196 /**
197 * @brief The callout JSON, if the entry has callouts.
198 */
199 std::optional<nlohmann::json> callouts;
Matt Spinler711f1122022-12-15 11:41:20 -0600200
201 /**
202 * @brief The journal capture instructions, if present.
203 */
204 std::optional<JournalCapture> journalCapture;
Matt Spinler367144c2019-09-19 15:33:52 -0500205};
206
207/**
Matt Spinler6b427cc2020-04-09 09:42:59 -0500208 * @brief Holds callout information pulled out of the JSON.
209 */
210struct RegistryCallout
211{
212 std::string priority;
213 std::string locCode;
214 std::string procedure;
215 std::string symbolicFRU;
216 std::string symbolicFRUTrusted;
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500217 bool useInventoryLocCode;
Matt Spinler6b427cc2020-04-09 09:42:59 -0500218};
219
220/**
Matt Spinler367144c2019-09-19 15:33:52 -0500221 * @class Registry
222 *
223 * This class wraps the message registry JSON data and allows one to find
224 * the message registry entry pertaining to the error name.
225 *
226 * So that new registry files can easily be tested, the code will look for
227 * /etc/phosphor-logging/message_registry.json before looking for the real
228 * path.
229 */
230class Registry
231{
232 public:
233 Registry() = delete;
234 ~Registry() = default;
235 Registry(const Registry&) = default;
236 Registry& operator=(const Registry&) = default;
237 Registry(Registry&&) = default;
238 Registry& operator=(Registry&&) = default;
239
240 /**
241 * @brief Constructor
Matt Spinlerd8e29002020-04-09 09:11:22 -0500242 *
243 * Will load the callout JSON.
244 *
Matt Spinler367144c2019-09-19 15:33:52 -0500245 * @param[in] registryFile - The path to the file.
246 */
247 explicit Registry(const std::filesystem::path& registryFile) :
Matt Spinlerd8e29002020-04-09 09:11:22 -0500248 Registry(registryFile, true)
Patrick Williams2544b412022-10-04 08:41:06 -0500249 {}
Matt Spinlerd8e29002020-04-09 09:11:22 -0500250
251 /**
252 * @brief Constructor
253 *
254 * This version contains a parameter that allows the callout JSON
255 * to be saved in the Entry struct or not, as it isn't needed at
256 * all in some cases.
257 *
258 * @param[in] registryFile - The path to the file.
259 * @param[in] loadCallouts - If the callout JSON should be saved.
260 */
261 explicit Registry(const std::filesystem::path& registryFile,
262 bool loadCallouts) :
263 _registryFile(registryFile),
264 _loadCallouts(loadCallouts)
Patrick Williams2544b412022-10-04 08:41:06 -0500265 {}
Matt Spinler367144c2019-09-19 15:33:52 -0500266
267 /**
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800268 * @brief Find a registry entry based on its error name or reason code.
Matt Spinler367144c2019-09-19 15:33:52 -0500269 *
270 * This function does do some basic sanity checking on the JSON contents,
271 * but there is also an external program that enforces a schema on the
272 * registry JSON that should catch all of these problems ahead of time.
273 *
274 * @param[in] name - The error name, like xyz.openbmc_project.Error.Foo
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800275 * - OR
276 * - The reason code, like 0x1001
277 * @param[in] type - LookupType enum value
278 * @param[in] toCache - boolean to cache registry in memory
Matt Spinler367144c2019-09-19 15:33:52 -0500279 * @return optional<Entry> A filled in message registry structure if
280 * found, otherwise an empty optional object.
281 */
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800282 std::optional<Entry> lookup(const std::string& name, LookupType type,
283 bool toCache = false);
Matt Spinler367144c2019-09-19 15:33:52 -0500284
Matt Spinler6b427cc2020-04-09 09:42:59 -0500285 /**
286 * @brief Find the callouts to put into the PEL based on the calloutJSON
287 * data.
288 *
289 * The system type and AdditionalData are used to index into the correct
290 * callout table.
291 *
292 * Throws exceptions on failures.
293 *
294 * @param[in] calloutJSON - Where to look up the callouts
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500295 * @param[in] systemNames - List of compatible system type names
Matt Spinler6b427cc2020-04-09 09:42:59 -0500296 * @param[in] additionalData - The AdditionalData property
297 *
298 * @return std::vector<RegistryCallout> - The callouts to use
299 */
300 static std::vector<RegistryCallout>
301 getCallouts(const nlohmann::json& calloutJSON,
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500302 const std::vector<std::string>& systemNames,
Matt Spinler6b427cc2020-04-09 09:42:59 -0500303 const AdditionalData& additionalData);
304
Matt Spinler367144c2019-09-19 15:33:52 -0500305 private:
306 /**
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800307 * @brief Parse message registry file using nlohmann::json
308 * @param[in] registryFile - The message registry JSON file
309 * @return optional<nlohmann::json> The full message registry object or an
310 * empty optional object upon failure.
311 */
312 std::optional<nlohmann::json>
313 readRegistry(const std::filesystem::path& registryFile);
314
315 /**
Matt Spinler367144c2019-09-19 15:33:52 -0500316 * @brief The path to the registry JSON file.
317 */
318 std::filesystem::path _registryFile;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800319
320 /**
321 * @brief The full message registry object.
322 */
323 std::optional<nlohmann::json> _registry;
Matt Spinlerd8e29002020-04-09 09:11:22 -0500324
325 /**
326 * @brief If the callout JSON should be saved in the Entry on lookup.
327 */
328 bool _loadCallouts;
Matt Spinler367144c2019-09-19 15:33:52 -0500329};
330
331namespace helper
332{
333
334/**
335 * @brief A helper function to get the PEL subsystem value based on
336 * the registry subsystem name.
337 *
338 * @param[in] subsystemName - The registry name for the subsystem
339 *
340 * @return uint8_t The PEL subsystem value
341 */
342uint8_t getSubsystem(const std::string& subsystemName);
343
344/**
345 * @brief A helper function to get the PEL severity value based on
346 * the registry severity name.
347 *
348 * @param[in] severityName - The registry name for the severity
349 *
350 * @return uint8_t The PEL severity value
351 */
352uint8_t getSeverity(const std::string& severityName);
353
354/**
Matt Spinleraadccc82020-04-10 14:33:42 -0500355 * @brief Returns all of the system type/severity values found
356 * in the severity JSON passed in.
357 *
358 * The JSON is either a simple string, like:
359 * "unrecoverable"
360 * or an array of system type/severity pairs, like:
361 * [
362 * {
363 * "System": "1",
364 * "SevValue": "predictive"
365 * },
366 * {
367 * "System": "2",
368 * "SevValue": "recovered"
369 * }
370 * ]
371 *
372 * @param[in] severity - The severity JSON
373 * @return The list of severity/system combinations. If the System key
374 * wasn't used, then that field will be empty in the structure.
375 */
376std::vector<RegistrySeverity> getSeverities(const nlohmann::json& severity);
377
378/**
Matt Spinler367144c2019-09-19 15:33:52 -0500379 * @brief A helper function to get the action flags value based on
380 * the action flag names used in the registry.
381 *
382 * @param[in] flags - The list of flag names from the registry.
383 *
384 * @return uint16_t - The bitfield of flags used in the PEL.
385 */
386uint16_t getActionFlags(const std::vector<std::string>& flags);
387
388/**
389 * @brief A helper function to get the PEL event type value based on
390 * the registry event type name.
391 *
392 * @param[in] eventTypeName - The registry name for the event type
393 *
394 * @return uint8_t The PEL event type value
395 */
396uint8_t getEventType(const std::string& eventTypeName);
397
398/**
399 * @brief A helper function to get the PEL event scope value based on
400 * the registry event scope name.
401 *
402 * @param[in] eventScopeName - The registry name for the event scope
403 *
404 * @return uint8_t The PEL event scope value
405 */
406uint8_t getEventScope(const std::string& eventScopeName);
407
Matt Spinler93e29322019-09-20 11:16:15 -0500408/**
409 * @brief Reads the "ReasonCode" field out of JSON and converts the string value
410 * such as "0x5555" to a uint16 like 0x5555.
411 *
412 * @param[in] src - The message registry SRC dictionary to read from
413 * @param[in] name - The error name, to use in a trace if things go awry.
414 *
415 * @return uint16_t - The reason code
416 */
417uint16_t getSRCReasonCode(const nlohmann::json& src, const std::string& name);
418
419/**
420 * @brief Reads the "Type" field out of JSON and converts it to the SRC::Type
421 * value.
422 *
423 * @param[in] src - The message registry SRC dictionary to read from
424 * @param[in] name - The error name, to use in a trace if things go awry.
425 *
426 * @return uint8_t - The SRC type value, like 0x11
427 */
428uint8_t getSRCType(const nlohmann::json& src, const std::string& name);
429
430/**
431 * @brief Reads the "Words6To9" field out of JSON and converts it to a map
432 * of the SRC word number to the AdditionalData property field used
433 * to fill it in with.
434 *
435 * @param[in] src - The message registry SRC dictionary to read from
436 * @param[in] name - The error name, to use in a trace if things go awry.
437 *
438 * @return std::optional<std::map<SRC::WordNum, SRC::AdditionalDataField>>
439 */
440std::optional<std::map<SRC::WordNum, SRC::AdditionalDataField>>
441 getSRCHexwordFields(const nlohmann::json& src, const std::string& name);
442
443/**
444 * @brief Reads the "SymptomIDFields" field out of JSON and converts it to
445 * a vector of SRC word numbers.
446 *
447 * @param[in] src - The message registry SRC dictionary to read from
448 * @param[in] name - The error name, to use in a trace if things go awry.
449 *
450 * @return std::optional<std::vector<SRC::WordNum>>
451 */
452std::optional<std::vector<SRC::WordNum>>
453 getSRCSymptomIDFields(const nlohmann::json& src, const std::string& name);
454
455/**
Matt Spinler3fe93e92023-04-14 14:06:59 -0500456 * @brief Returns the value of the 'DeconfigFlag' field.
457 *
458 * @param[in] src - The message registry SRC dictionary to read from
459 *
460 * @return bool - The field value
461 */
462bool getSRCDeconfigFlag(const nlohmann::json& src);
463
464/**
Matt Spinler93e29322019-09-20 11:16:15 -0500465 * @brief Reads the "ComponentID" field out of JSON and converts it to a
466 * uint16_t like 0xFF00.
467 *
468 * The ComponentID JSON field is only required if the SRC type isn't a BD
469 * BMC SRC, because for those SRCs it can be inferred from the upper byte
470 * of the SRC reasoncode.
471 *
472 * @param[in] srcType - The SRC type
473 * @param[in] reasonCode - The SRC reason code
474 * @param[in] pelEntry - The PEL entry JSON
475 * @param[in] name - The error name, to use in a trace if things go awry.
476 *
477 * @return uin16_t - The component ID, like 0xFF00
478 */
479uint16_t getComponentID(uint8_t srcType, uint16_t reasonCode,
480 const nlohmann::json& pelEntry,
481 const std::string& name);
482
Matt Spinler367144c2019-09-19 15:33:52 -0500483} // namespace helper
484
485} // namespace message
486
487} // namespace pels
488} // namespace openpower