| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 3 | #include "registries.hpp" |
| 4 | |
| Patrick Williams | 4a102cd | 2025-02-27 14:52:54 -0500 | [diff] [blame] | 5 | // We need the registries_selector pulled into some cpp part so that the |
| 6 | // registration hooks run. |
| 7 | // NOLINTNEXTLINE(misc-include-cleaner) |
| Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 8 | #include "registries_selector.hpp" |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 9 | #include "str_utility.hpp" |
| 10 | |
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <cstring> |
| Patrick Williams | 4a102cd | 2025-02-27 14:52:54 -0500 | [diff] [blame] | 13 | #include <functional> |
| 14 | #include <map> |
| 15 | #include <optional> |
| Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 16 | #include <ranges> |
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 17 | #include <span> |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 18 | #include <string> |
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 19 | #include <string_view> |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 20 | #include <utility> |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | namespace redfish::registries |
| 24 | { |
| 25 | |
| Patrick Williams | 4a102cd | 2025-02-27 14:52:54 -0500 | [diff] [blame] | 26 | auto allRegistries() -> std::map<std::string, RegistryEntry>& |
| 27 | { |
| 28 | static std::map<std::string, RegistryEntry> registries; |
| 29 | return registries; |
| 30 | } |
| 31 | |
| 32 | auto getRegistryFromPrefix(const std::string& registryName) |
| 33 | -> std::optional<RegistryEntryRef> |
| 34 | { |
| 35 | auto& registries = allRegistries(); |
| 36 | if (auto it = registries.find(registryName); it != registries.end()) |
| 37 | { |
| 38 | return std::ref(it->second); |
| 39 | } |
| 40 | |
| 41 | return std::nullopt; |
| 42 | } |
| 43 | |
| 44 | auto getRegistryMessagesFromPrefix(const std::string& registryName) |
| 45 | -> MessageEntries |
| 46 | { |
| 47 | auto registry = getRegistryFromPrefix(registryName); |
| 48 | if (!registry) |
| 49 | { |
| 50 | return {}; |
| 51 | } |
| 52 | |
| 53 | return registry->get().entries; |
| 54 | } |
| 55 | |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 56 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 57 | std::span<const MessageEntry> registry) |
| 58 | { |
| Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 59 | std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( |
| 60 | registry, [&messageKey](const MessageEntry& messageEntry) { |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 61 | return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; |
| 62 | }); |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 63 | if (messageIt != registry.end()) |
| 64 | { |
| 65 | return &messageIt->second; |
| 66 | } |
| 67 | |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 71 | std::optional<MessageId> getMessageComponents(std::string_view message) |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 72 | { |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 73 | // Redfish Message are in the form |
| 74 | // RegistryName.MajorVersion.MinorVersion.MessageKey |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 75 | std::vector<std::string> fields; |
| 76 | fields.reserve(4); |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 77 | bmcweb::split(fields, message, '.'); |
| Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 78 | if (fields.size() != 4) |
| 79 | { |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 80 | return std::nullopt; |
| 81 | } |
| 82 | |
| 83 | return MessageId(std::move(fields[0]), std::move(fields[1]), |
| 84 | std::move(fields[2]), std::move(fields[3])); |
| 85 | } |
| 86 | |
| 87 | const Message* getMessage(std::string_view messageID) |
| 88 | { |
| 89 | std::optional<MessageId> msgComponents = getMessageComponents(messageID); |
| 90 | if (!msgComponents) |
| 91 | { |
| Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 92 | return nullptr; |
| 93 | } |
| 94 | |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 95 | // Find the right registry and check it for the MessageKey |
| Thang Tran | d949596 | 2025-10-16 07:07:30 +0000 | [diff] [blame^] | 96 | return getMessageFromRegistry( |
| 97 | msgComponents->messageKey, |
| 98 | getRegistryMessagesFromPrefix(msgComponents->registryName)); |
| Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace redfish::registries |