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     /**
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) {}
114 };
115 
116 struct 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.
124 using AppCaptureList = std::vector<AppCapture>;
125 using JournalCapture = std::variant<size_t, AppCaptureList>;
126 
127 /**
128  * @brief Represents a message registry entry, which is used for creating a
129  *        PEL from an OpenBMC event log.
130  */
131 struct Entry
132 {
133     /**
134      * @brief The error name, like "xyz.openbmc_project.Error.Foo".
135      */
136     std::string name;
137 
138     /**
139      * @brief The component ID of the PEL creator.
140      */
141     uint16_t componentID;
142 
143     /**
144      * @brief The PEL subsystem field.
145      */
146     std::optional<uint8_t> subsystem;
147 
148     /**
149      * @brief The optional PEL severity field.  If not specified, the PEL
150      *        will use the severity of the OpenBMC event log.
151      *
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.
154      */
155     std::optional<std::vector<RegistrySeverity>> severity;
156 
157     /**
158      * @brief The optional severity field to use when in manufacturing tolerance
159      *        mode.  It behaves like the severity field above.
160      */
161     std::optional<std::vector<RegistrySeverity>> mfgSeverity;
162 
163     /**
164      * @brief The PEL action flags field.
165      */
166     std::optional<uint16_t> actionFlags;
167 
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 
186     /**
187      * The SRC related fields.
188      */
189     SRC src;
190 
191     /**
192      * The Documentation related fields.
193      */
194     DOC doc;
195 
196     /**
197      * @brief The callout JSON, if the entry has callouts.
198      */
199     std::optional<nlohmann::json> callouts;
200 
201     /**
202      * @brief The journal capture instructions, if present.
203      */
204     std::optional<JournalCapture> journalCapture;
205 };
206 
207 /**
208  * @brief Holds callout information pulled out of the JSON.
209  */
210 struct RegistryCallout
211 {
212     std::string priority;
213     std::string locCode;
214     std::string procedure;
215     std::string symbolicFRU;
216     std::string symbolicFRUTrusted;
217     bool useInventoryLocCode;
218 };
219 
220 /**
221  * @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  */
230 class 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
242      *
243      * Will load the callout JSON.
244      *
245      * @param[in] registryFile - The path to the file.
246      */
247     explicit Registry(const std::filesystem::path& registryFile) :
248         Registry(registryFile, true)
249     {}
250 
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)
265     {}
266 
267     /**
268      * @brief Find a registry entry based on its error name or reason code.
269      *
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
275      *                 - OR
276      *                 - The reason code, like 0x1001
277      * @param[in] type - LookupType enum value
278      * @param[in] toCache - boolean to cache registry in memory
279      * @return optional<Entry> A filled in message registry structure if
280      *                         found, otherwise an empty optional object.
281      */
282     std::optional<Entry> lookup(const std::string& name, LookupType type,
283                                 bool toCache = false);
284 
285     /**
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
295      * @param[in] systemNames - List of compatible system type names
296      * @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,
302                     const std::vector<std::string>& systemNames,
303                     const AdditionalData& additionalData);
304 
305   private:
306     /**
307      * @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     /**
316      * @brief The path to the registry JSON file.
317      */
318     std::filesystem::path _registryFile;
319 
320     /**
321      * @brief The full message registry object.
322      */
323     std::optional<nlohmann::json> _registry;
324 
325     /**
326      * @brief If the callout JSON should be saved in the Entry on lookup.
327      */
328     bool _loadCallouts;
329 };
330 
331 namespace 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  */
342 uint8_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  */
352 uint8_t getSeverity(const std::string& severityName);
353 
354 /**
355  * @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  */
376 std::vector<RegistrySeverity> getSeverities(const nlohmann::json& severity);
377 
378 /**
379  * @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  */
386 uint16_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  */
396 uint8_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  */
406 uint8_t getEventScope(const std::string& eventScopeName);
407 
408 /**
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  */
417 uint16_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  */
428 uint8_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  */
440 std::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  */
452 std::optional<std::vector<SRC::WordNum>>
453     getSRCSymptomIDFields(const nlohmann::json& src, const std::string& name);
454 
455 /**
456  * @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  */
462 bool getSRCDeconfigFlag(const nlohmann::json& src);
463 
464 /**
465  * @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  */
479 uint16_t getComponentID(uint8_t srcType, uint16_t reasonCode,
480                         const nlohmann::json& pelEntry,
481                         const std::string& name);
482 
483 } // namespace helper
484 
485 } // namespace message
486 
487 } // namespace pels
488 } // namespace openpower
489