blob: 7ee59796282b793947e794d17b9d4682d9fc01d3 [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
Patrick Williams2544b412022-10-04 08:41:06 -0500108 SRC() : type(0), reasonCode(0) {}
Matt Spinler93e29322019-09-20 11:16:15 -0500109};
110
Matt Spinler711f1122022-12-15 11:41:20 -0600111struct 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.
119using AppCaptureList = std::vector<AppCapture>;
120using JournalCapture = std::variant<size_t, AppCaptureList>;
121
Matt Spinler93e29322019-09-20 11:16:15 -0500122/**
Matt Spinler367144c2019-09-19 15:33:52 -0500123 * @brief Represents a message registry entry, which is used for creating a
124 * PEL from an OpenBMC event log.
125 */
126struct Entry
127{
128 /**
129 * @brief The error name, like "xyz.openbmc_project.Error.Foo".
130 */
131 std::string name;
132
133 /**
Matt Spinler93e29322019-09-20 11:16:15 -0500134 * @brief The component ID of the PEL creator.
135 */
136 uint16_t componentID;
137
138 /**
Matt Spinler367144c2019-09-19 15:33:52 -0500139 * @brief The PEL subsystem field.
140 */
Matt Spinler23970b02022-02-25 16:34:46 -0600141 std::optional<uint8_t> subsystem;
Matt Spinler367144c2019-09-19 15:33:52 -0500142
143 /**
144 * @brief The optional PEL severity field. If not specified, the PEL
145 * will use the severity of the OpenBMC event log.
Matt Spinleraadccc82020-04-10 14:33:42 -0500146 *
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.
Matt Spinler367144c2019-09-19 15:33:52 -0500149 */
Matt Spinleraadccc82020-04-10 14:33:42 -0500150 std::optional<std::vector<RegistrySeverity>> severity;
Matt Spinler367144c2019-09-19 15:33:52 -0500151
152 /**
153 * @brief The optional severity field to use when in manufacturing tolerance
Matt Spinleraadccc82020-04-10 14:33:42 -0500154 * mode. It behaves like the severity field above.
Matt Spinler367144c2019-09-19 15:33:52 -0500155 */
Matt Spinleraadccc82020-04-10 14:33:42 -0500156 std::optional<std::vector<RegistrySeverity>> mfgSeverity;
Matt Spinler367144c2019-09-19 15:33:52 -0500157
158 /**
159 * @brief The PEL action flags field.
160 */
Matt Spinlere07f9152019-11-01 10:48:36 -0500161 std::optional<uint16_t> actionFlags;
Matt Spinler367144c2019-09-19 15:33:52 -0500162
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
Matt Spinler93e29322019-09-20 11:16:15 -0500181 /**
182 * The SRC related fields.
183 */
184 SRC src;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800185
186 /**
187 * The Documentation related fields.
188 */
189 DOC doc;
Matt Spinlerd8e29002020-04-09 09:11:22 -0500190
191 /**
192 * @brief The callout JSON, if the entry has callouts.
193 */
194 std::optional<nlohmann::json> callouts;
Matt Spinler711f1122022-12-15 11:41:20 -0600195
196 /**
197 * @brief The journal capture instructions, if present.
198 */
199 std::optional<JournalCapture> journalCapture;
Matt Spinler367144c2019-09-19 15:33:52 -0500200};
201
202/**
Matt Spinler6b427cc2020-04-09 09:42:59 -0500203 * @brief Holds callout information pulled out of the JSON.
204 */
205struct RegistryCallout
206{
207 std::string priority;
208 std::string locCode;
209 std::string procedure;
210 std::string symbolicFRU;
211 std::string symbolicFRUTrusted;
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500212 bool useInventoryLocCode;
Matt Spinler6b427cc2020-04-09 09:42:59 -0500213};
214
215/**
Matt Spinler367144c2019-09-19 15:33:52 -0500216 * @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 */
225class 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
Matt Spinlerd8e29002020-04-09 09:11:22 -0500237 *
238 * Will load the callout JSON.
239 *
Matt Spinler367144c2019-09-19 15:33:52 -0500240 * @param[in] registryFile - The path to the file.
241 */
242 explicit Registry(const std::filesystem::path& registryFile) :
Matt Spinlerd8e29002020-04-09 09:11:22 -0500243 Registry(registryFile, true)
Patrick Williams2544b412022-10-04 08:41:06 -0500244 {}
Matt Spinlerd8e29002020-04-09 09:11:22 -0500245
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)
Patrick Williams2544b412022-10-04 08:41:06 -0500260 {}
Matt Spinler367144c2019-09-19 15:33:52 -0500261
262 /**
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800263 * @brief Find a registry entry based on its error name or reason code.
Matt Spinler367144c2019-09-19 15:33:52 -0500264 *
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
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800270 * - OR
271 * - The reason code, like 0x1001
272 * @param[in] type - LookupType enum value
273 * @param[in] toCache - boolean to cache registry in memory
Matt Spinler367144c2019-09-19 15:33:52 -0500274 * @return optional<Entry> A filled in message registry structure if
275 * found, otherwise an empty optional object.
276 */
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800277 std::optional<Entry> lookup(const std::string& name, LookupType type,
278 bool toCache = false);
Matt Spinler367144c2019-09-19 15:33:52 -0500279
Matt Spinler6b427cc2020-04-09 09:42:59 -0500280 /**
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
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500290 * @param[in] systemNames - List of compatible system type names
Matt Spinler6b427cc2020-04-09 09:42:59 -0500291 * @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,
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500297 const std::vector<std::string>& systemNames,
Matt Spinler6b427cc2020-04-09 09:42:59 -0500298 const AdditionalData& additionalData);
299
Matt Spinler367144c2019-09-19 15:33:52 -0500300 private:
301 /**
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800302 * @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 /**
Matt Spinler367144c2019-09-19 15:33:52 -0500311 * @brief The path to the registry JSON file.
312 */
313 std::filesystem::path _registryFile;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800314
315 /**
316 * @brief The full message registry object.
317 */
318 std::optional<nlohmann::json> _registry;
Matt Spinlerd8e29002020-04-09 09:11:22 -0500319
320 /**
321 * @brief If the callout JSON should be saved in the Entry on lookup.
322 */
323 bool _loadCallouts;
Matt Spinler367144c2019-09-19 15:33:52 -0500324};
325
326namespace 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 */
337uint8_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 */
347uint8_t getSeverity(const std::string& severityName);
348
349/**
Matt Spinleraadccc82020-04-10 14:33:42 -0500350 * @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 */
371std::vector<RegistrySeverity> getSeverities(const nlohmann::json& severity);
372
373/**
Matt Spinler367144c2019-09-19 15:33:52 -0500374 * @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 */
381uint16_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 */
391uint8_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 */
401uint8_t getEventScope(const std::string& eventScopeName);
402
Matt Spinler93e29322019-09-20 11:16:15 -0500403/**
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 */
412uint16_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 */
423uint8_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 */
435std::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 */
447std::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 */
465uint16_t getComponentID(uint8_t srcType, uint16_t reasonCode,
466 const nlohmann::json& pelEntry,
467 const std::string& name);
468
Matt Spinler367144c2019-09-19 15:33:52 -0500469} // namespace helper
470
471} // namespace message
472
473} // namespace pels
474} // namespace openpower