blob: f7a8df3ed63e8c5279dc9e02989660406f793887 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
Jason M. Bills70304cb2019-03-27 12:03:59 -07004#pragma once
Ed Tanousc5ba4c22022-02-07 09:59:55 -08005
Ed Tanous56b81992024-12-02 10:36:37 -08006#include "bmcweb_config.h"
7
Ed Tanous65e4f1f2022-02-08 00:23:54 -08008#include <nlohmann/json.hpp>
9
Nan Zhoud5c80ad2022-07-11 01:16:31 +000010#include <array>
Ed Tanous80f595e2022-02-14 09:32:05 -080011#include <charconv>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000012#include <cstddef>
Ed Tanous56b81992024-12-02 10:36:37 -080013#include <format>
Patrick Williams4a102cd2025-02-27 14:52:54 -050014#include <functional>
15#include <map>
16#include <optional>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080017#include <span>
18#include <string>
19#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <system_error>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000021#include <utility>
22
Ed Tanousfffb8c12022-02-07 23:53:03 -080023namespace redfish::registries
Jason M. Bills70304cb2019-03-27 12:03:59 -070024{
Jason M. Bills351d3062019-03-27 12:58:21 -070025struct Header
26{
27 const char* copyright;
28 const char* type;
Ed Tanous56b81992024-12-02 10:36:37 -080029 unsigned int versionMajor;
30 unsigned int versionMinor;
31 unsigned int versionPatch;
Jason M. Bills351d3062019-03-27 12:58:21 -070032 const char* name;
33 const char* language;
34 const char* description;
35 const char* registryPrefix;
Jason M. Bills351d3062019-03-27 12:58:21 -070036 const char* owningEntity;
37};
Jason M. Bills70304cb2019-03-27 12:03:59 -070038
39struct Message
40{
41 const char* description;
42 const char* message;
Gunnar Millse7808c92020-07-08 21:17:44 -050043 const char* messageSeverity;
Ed Tanous271584a2019-07-09 16:24:22 -070044 const size_t numberOfArgs;
Jason M. Bills70304cb2019-03-27 12:03:59 -070045 std::array<const char*, 5> paramTypes;
46 const char* resolution;
47};
48using MessageEntry = std::pair<const char*, const Message>;
Patrick Williams4a102cd2025-02-27 14:52:54 -050049using MessageEntries = std::span<const MessageEntry>;
50
51struct RegistryEntry
52{
53 const Header& header;
54 const char* url;
55 MessageEntries entries;
56};
57using RegistryEntryRef = std::reference_wrapper<RegistryEntry>;
58
59auto allRegistries() -> std::map<std::string, RegistryEntry>&;
60
61auto getRegistryFromPrefix(const std::string& registryName)
62 -> std::optional<RegistryEntryRef>;
63
64auto getRegistryMessagesFromPrefix(const std::string& registryName)
65 -> MessageEntries;
66
67template <typename T>
68void registerRegistry()
69{
70 allRegistries().emplace(T::header.registryPrefix,
71 RegistryEntry{T::header, T::url, T::registry});
72}
Ed Tanousc5ba4c22022-02-07 09:59:55 -080073
Patrick Williamsbd79bce2024-08-16 15:22:20 -040074inline std::string fillMessageArgs(
75 const std::span<const std::string_view> messageArgs, std::string_view msg)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080076{
Ed Tanous80f595e2022-02-14 09:32:05 -080077 std::string ret;
78 size_t reserve = msg.size();
Ed Tanous26ccae32023-02-16 10:28:44 -080079 for (std::string_view arg : messageArgs)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080080 {
Ed Tanous80f595e2022-02-14 09:32:05 -080081 reserve += arg.size();
Ed Tanousc5ba4c22022-02-07 09:59:55 -080082 }
Ed Tanous80f595e2022-02-14 09:32:05 -080083 ret.reserve(reserve);
84
85 for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos;
86 stringIndex = msg.find('%'))
87 {
88 ret += msg.substr(0, stringIndex);
89 msg.remove_prefix(stringIndex + 1);
90 size_t number = 0;
Ed Tanous7da633f2024-12-02 08:25:38 -080091 auto it = std::from_chars(&*msg.begin(), &*msg.end(), number);
Ed Tanous80f595e2022-02-14 09:32:05 -080092 if (it.ec != std::errc())
93 {
94 return "";
95 }
96 msg.remove_prefix(1);
97 // Redfish message args are 1 indexed.
98 number--;
99 if (number >= messageArgs.size())
100 {
101 return "";
102 }
103 ret += messageArgs[number];
104 }
105 ret += msg;
106 return ret;
Ed Tanousc5ba4c22022-02-07 09:59:55 -0800107}
108
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400109inline nlohmann::json::object_t getLogFromRegistry(
110 const Header& header, std::span<const MessageEntry> registry, size_t index,
111 std::span<const std::string_view> args)
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800112{
113 const redfish::registries::MessageEntry& entry = registry[index];
114 // Intentionally make a copy of the string, so we can append in the
115 // parameters.
Krzysztof Grobelny2e30bc22022-09-09 10:13:41 +0200116 std::string msg =
117 redfish::registries::fillMessageArgs(args, entry.second.message);
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800118 nlohmann::json jArgs = nlohmann::json::array();
Ed Tanous26ccae32023-02-16 10:28:44 -0800119 for (std::string_view arg : args)
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800120 {
121 jArgs.push_back(arg);
122 }
Ed Tanous56b81992024-12-02 10:36:37 -0800123 std::string msgId;
124 if (BMCWEB_REDFISH_USE_3_DIGIT_MESSAGEID)
125 {
126 msgId = std::format("{}.{}.{}.{}.{}", header.registryPrefix,
127 header.versionMajor, header.versionMinor,
128 header.versionPatch, entry.first);
129 }
130 else
131 {
132 msgId =
133 std::format("{}.{}.{}.{}", header.registryPrefix,
134 header.versionMajor, header.versionMinor, entry.first);
135 }
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800136 nlohmann::json::object_t response;
137 response["@odata.type"] = "#Message.v1_1_1.Message";
138 response["MessageId"] = std::move(msgId);
139 response["Message"] = std::move(msg);
140 response["MessageArgs"] = std::move(jArgs);
141 response["MessageSeverity"] = entry.second.messageSeverity;
142 response["Resolution"] = entry.second.resolution;
143 return response;
144}
145
Sui Chend1d411f2022-04-10 23:09:36 -0700146const Message* getMessage(std::string_view messageID);
147
148const Message* getMessageFromRegistry(const std::string& messageKey,
149 std::span<const MessageEntry> registry);
150
Ed Tanousfffb8c12022-02-07 23:53:03 -0800151} // namespace redfish::registries