Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 15 | */ |
| 16 | #pragma once |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 17 | |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 18 | #include <nlohmann/json.hpp> |
| 19 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 20 | #include <array> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 21 | #include <charconv> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 22 | #include <cstddef> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 23 | #include <numeric> |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 24 | #include <span> |
| 25 | #include <string> |
| 26 | #include <string_view> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 27 | #include <utility> |
| 28 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 29 | namespace redfish::registries |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 30 | { |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 31 | struct Header |
| 32 | { |
| 33 | const char* copyright; |
| 34 | const char* type; |
| 35 | const char* id; |
| 36 | const char* name; |
| 37 | const char* language; |
| 38 | const char* description; |
| 39 | const char* registryPrefix; |
| 40 | const char* registryVersion; |
| 41 | const char* owningEntity; |
| 42 | }; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 43 | |
| 44 | struct Message |
| 45 | { |
| 46 | const char* description; |
| 47 | const char* message; |
Gunnar Mills | e7808c9 | 2020-07-08 21:17:44 -0500 | [diff] [blame] | 48 | const char* messageSeverity; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 49 | const size_t numberOfArgs; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 50 | std::array<const char*, 5> paramTypes; |
| 51 | const char* resolution; |
| 52 | }; |
| 53 | using MessageEntry = std::pair<const char*, const Message>; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 54 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 55 | inline std::string fillMessageArgs( |
| 56 | const std::span<const std::string_view> messageArgs, std::string_view msg) |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 57 | { |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 58 | std::string ret; |
| 59 | size_t reserve = msg.size(); |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 60 | for (std::string_view arg : messageArgs) |
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 | reserve += arg.size(); |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 63 | } |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame] | 64 | ret.reserve(reserve); |
| 65 | |
| 66 | for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos; |
| 67 | stringIndex = msg.find('%')) |
| 68 | { |
| 69 | ret += msg.substr(0, stringIndex); |
| 70 | msg.remove_prefix(stringIndex + 1); |
| 71 | size_t number = 0; |
| 72 | auto it = std::from_chars(msg.data(), &*msg.end(), number); |
| 73 | if (it.ec != std::errc()) |
| 74 | { |
| 75 | return ""; |
| 76 | } |
| 77 | msg.remove_prefix(1); |
| 78 | // Redfish message args are 1 indexed. |
| 79 | number--; |
| 80 | if (number >= messageArgs.size()) |
| 81 | { |
| 82 | return ""; |
| 83 | } |
| 84 | ret += messageArgs[number]; |
| 85 | } |
| 86 | ret += msg; |
| 87 | return ret; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 90 | inline nlohmann::json::object_t getLogFromRegistry( |
| 91 | const Header& header, std::span<const MessageEntry> registry, size_t index, |
| 92 | std::span<const std::string_view> args) |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 93 | { |
| 94 | const redfish::registries::MessageEntry& entry = registry[index]; |
| 95 | // Intentionally make a copy of the string, so we can append in the |
| 96 | // parameters. |
Krzysztof Grobelny | 2e30bc2 | 2022-09-09 10:13:41 +0200 | [diff] [blame] | 97 | std::string msg = |
| 98 | redfish::registries::fillMessageArgs(args, entry.second.message); |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 99 | nlohmann::json jArgs = nlohmann::json::array(); |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 100 | for (std::string_view arg : args) |
Ed Tanous | 65e4f1f | 2022-02-08 00:23:54 -0800 | [diff] [blame] | 101 | { |
| 102 | jArgs.push_back(arg); |
| 103 | } |
| 104 | std::string msgId = header.id; |
| 105 | msgId += "."; |
| 106 | msgId += entry.first; |
| 107 | |
| 108 | nlohmann::json::object_t response; |
| 109 | response["@odata.type"] = "#Message.v1_1_1.Message"; |
| 110 | response["MessageId"] = std::move(msgId); |
| 111 | response["Message"] = std::move(msg); |
| 112 | response["MessageArgs"] = std::move(jArgs); |
| 113 | response["MessageSeverity"] = entry.second.messageSeverity; |
| 114 | response["Resolution"] = entry.second.resolution; |
| 115 | return response; |
| 116 | } |
| 117 | |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 118 | const Message* getMessage(std::string_view messageID); |
| 119 | |
| 120 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 121 | std::span<const MessageEntry> registry); |
| 122 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 123 | } // namespace redfish::registries |