Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 1 | /* |
Ed Tanous | 6be832e | 2024-09-10 11:44:48 -0700 | [diff] [blame] | 2 | Copyright (c) 2019 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 15 | */ |
| 16 | #pragma once |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 17 | |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame^] | 18 | #include "bmcweb_config.h" |
| 19 | |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 20 | #include <nlohmann/json.hpp> |
| 21 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 22 | #include <array> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 23 | #include <charconv> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 24 | #include <cstddef> |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame^] | 25 | #include <format> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 26 | #include <numeric> |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 27 | #include <span> |
| 28 | #include <string> |
| 29 | #include <string_view> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 30 | #include <utility> |
| 31 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 32 | namespace redfish::registries |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 33 | { |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 34 | struct Header |
| 35 | { |
| 36 | const char* copyright; |
| 37 | const char* type; |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame^] | 38 | unsigned int versionMajor; |
| 39 | unsigned int versionMinor; |
| 40 | unsigned int versionPatch; |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 41 | const char* name; |
| 42 | const char* language; |
| 43 | const char* description; |
| 44 | const char* registryPrefix; |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 45 | const char* owningEntity; |
| 46 | }; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 47 | |
| 48 | struct Message |
| 49 | { |
| 50 | const char* description; |
| 51 | const char* message; |
Gunnar Mills | e7808c9 | 2020-07-08 21:17:44 -0500 | [diff] [blame] | 52 | const char* messageSeverity; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 53 | const size_t numberOfArgs; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 54 | std::array<const char*, 5> paramTypes; |
| 55 | const char* resolution; |
| 56 | }; |
| 57 | using MessageEntry = std::pair<const char*, const Message>; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 58 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 59 | inline std::string fillMessageArgs( |
| 60 | const std::span<const std::string_view> messageArgs, std::string_view msg) |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 61 | { |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 62 | std::string ret; |
| 63 | size_t reserve = msg.size(); |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 64 | for (std::string_view arg : messageArgs) |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 65 | { |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 66 | reserve += arg.size(); |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 67 | } |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 68 | ret.reserve(reserve); |
| 69 | |
| 70 | for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos; |
| 71 | stringIndex = msg.find('%')) |
| 72 | { |
| 73 | ret += msg.substr(0, stringIndex); |
| 74 | msg.remove_prefix(stringIndex + 1); |
| 75 | size_t number = 0; |
| 76 | auto it = std::from_chars(msg.data(), &*msg.end(), number); |
| 77 | if (it.ec != std::errc()) |
| 78 | { |
| 79 | return ""; |
| 80 | } |
| 81 | msg.remove_prefix(1); |
| 82 | // Redfish message args are 1 indexed. |
| 83 | number--; |
| 84 | if (number >= messageArgs.size()) |
| 85 | { |
| 86 | return ""; |
| 87 | } |
| 88 | ret += messageArgs[number]; |
| 89 | } |
| 90 | ret += msg; |
| 91 | return ret; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 94 | inline nlohmann::json::object_t getLogFromRegistry( |
| 95 | const Header& header, std::span<const MessageEntry> registry, size_t index, |
| 96 | std::span<const std::string_view> args) |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 97 | { |
| 98 | const redfish::registries::MessageEntry& entry = registry[index]; |
| 99 | // Intentionally make a copy of the string, so we can append in the |
| 100 | // parameters. |
Krzysztof Grobelny | 2e30bc2 | 2022-09-09 10:13:41 +0200 | [diff] [blame] | 101 | std::string msg = |
| 102 | redfish::registries::fillMessageArgs(args, entry.second.message); |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 103 | nlohmann::json jArgs = nlohmann::json::array(); |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 104 | for (std::string_view arg : args) |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 105 | { |
| 106 | jArgs.push_back(arg); |
| 107 | } |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame^] | 108 | std::string msgId; |
| 109 | if (BMCWEB_REDFISH_USE_3_DIGIT_MESSAGEID) |
| 110 | { |
| 111 | msgId = std::format("{}.{}.{}.{}.{}", header.registryPrefix, |
| 112 | header.versionMajor, header.versionMinor, |
| 113 | header.versionPatch, entry.first); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | msgId = |
| 118 | std::format("{}.{}.{}.{}", header.registryPrefix, |
| 119 | header.versionMajor, header.versionMinor, entry.first); |
| 120 | } |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 121 | nlohmann::json::object_t response; |
| 122 | response["@odata.type"] = "#Message.v1_1_1.Message"; |
| 123 | response["MessageId"] = std::move(msgId); |
| 124 | response["Message"] = std::move(msg); |
| 125 | response["MessageArgs"] = std::move(jArgs); |
| 126 | response["MessageSeverity"] = entry.second.messageSeverity; |
| 127 | response["Resolution"] = entry.second.resolution; |
| 128 | return response; |
| 129 | } |
| 130 | |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 131 | const Message* getMessage(std::string_view messageID); |
| 132 | |
| 133 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 134 | std::span<const MessageEntry> registry); |
| 135 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 136 | } // namespace redfish::registries |