Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 1 | #include "registries.hpp" |
| 2 | |
Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 3 | #include "registries_selector.hpp" |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 4 | #include "str_utility.hpp" |
| 5 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 6 | #include <algorithm> |
| 7 | #include <cstring> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 8 | #include <ranges> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 9 | #include <span> |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 10 | #include <string> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 11 | #include <string_view> |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | namespace redfish::registries |
| 15 | { |
| 16 | |
| 17 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 18 | std::span<const MessageEntry> registry) |
| 19 | { |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 20 | std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( |
| 21 | registry, [&messageKey](const MessageEntry& messageEntry) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 22 | return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; |
| 23 | }); |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 24 | if (messageIt != registry.end()) |
| 25 | { |
| 26 | return &messageIt->second; |
| 27 | } |
| 28 | |
| 29 | return nullptr; |
| 30 | } |
| 31 | |
| 32 | const Message* getMessage(std::string_view messageID) |
| 33 | { |
| 34 | // Redfish MessageIds are in the form |
| 35 | // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find |
| 36 | // the right Message |
| 37 | std::vector<std::string> fields; |
| 38 | fields.reserve(4); |
| 39 | bmcweb::split(fields, messageID, '.'); |
Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 40 | if (fields.size() != 4) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 45 | const std::string& registryName = fields[0]; |
| 46 | const std::string& messageKey = fields[3]; |
| 47 | |
| 48 | // Find the right registry and check it for the MessageKey |
Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 49 | // Find the right registry and check it for the MessageKey |
| 50 | return getMessageFromRegistry(messageKey, |
| 51 | getRegistryFromPrefix(registryName)); |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace redfish::registries |