140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3d1d411f9SSui Chen #include "registries.hpp" 4d1d411f9SSui Chen 5*4a102cd4SPatrick Williams // We need the registries_selector pulled into some cpp part so that the 6*4a102cd4SPatrick Williams // registration hooks run. 7*4a102cd4SPatrick Williams // NOLINTNEXTLINE(misc-include-cleaner) 8d109e2b6SAlexander Hansen #include "registries_selector.hpp" 9d1d411f9SSui Chen #include "str_utility.hpp" 10d1d411f9SSui Chen 11f0b59af4SEd Tanous #include <algorithm> 12f0b59af4SEd Tanous #include <cstring> 13*4a102cd4SPatrick Williams #include <functional> 14*4a102cd4SPatrick Williams #include <map> 15*4a102cd4SPatrick Williams #include <optional> 163544d2a7SEd Tanous #include <ranges> 17f0b59af4SEd Tanous #include <span> 18d1d411f9SSui Chen #include <string> 19f0b59af4SEd Tanous #include <string_view> 20d1d411f9SSui Chen #include <vector> 21d1d411f9SSui Chen 22d1d411f9SSui Chen namespace redfish::registries 23d1d411f9SSui Chen { 24d1d411f9SSui Chen 25*4a102cd4SPatrick Williams auto allRegistries() -> std::map<std::string, RegistryEntry>& 26*4a102cd4SPatrick Williams { 27*4a102cd4SPatrick Williams static std::map<std::string, RegistryEntry> registries; 28*4a102cd4SPatrick Williams return registries; 29*4a102cd4SPatrick Williams } 30*4a102cd4SPatrick Williams 31*4a102cd4SPatrick Williams auto getRegistryFromPrefix(const std::string& registryName) 32*4a102cd4SPatrick Williams -> std::optional<RegistryEntryRef> 33*4a102cd4SPatrick Williams { 34*4a102cd4SPatrick Williams auto& registries = allRegistries(); 35*4a102cd4SPatrick Williams if (auto it = registries.find(registryName); it != registries.end()) 36*4a102cd4SPatrick Williams { 37*4a102cd4SPatrick Williams return std::ref(it->second); 38*4a102cd4SPatrick Williams } 39*4a102cd4SPatrick Williams 40*4a102cd4SPatrick Williams return std::nullopt; 41*4a102cd4SPatrick Williams } 42*4a102cd4SPatrick Williams 43*4a102cd4SPatrick Williams auto getRegistryMessagesFromPrefix(const std::string& registryName) 44*4a102cd4SPatrick Williams -> MessageEntries 45*4a102cd4SPatrick Williams { 46*4a102cd4SPatrick Williams auto registry = getRegistryFromPrefix(registryName); 47*4a102cd4SPatrick Williams if (!registry) 48*4a102cd4SPatrick Williams { 49*4a102cd4SPatrick Williams return {}; 50*4a102cd4SPatrick Williams } 51*4a102cd4SPatrick Williams 52*4a102cd4SPatrick Williams return registry->get().entries; 53*4a102cd4SPatrick Williams } 54*4a102cd4SPatrick Williams 55d1d411f9SSui Chen const Message* getMessageFromRegistry(const std::string& messageKey, 56d1d411f9SSui Chen std::span<const MessageEntry> registry) 57d1d411f9SSui Chen { 583544d2a7SEd Tanous std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( 593544d2a7SEd Tanous registry, [&messageKey](const MessageEntry& messageEntry) { 60d1d411f9SSui Chen return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; 61d1d411f9SSui Chen }); 62d1d411f9SSui Chen if (messageIt != registry.end()) 63d1d411f9SSui Chen { 64d1d411f9SSui Chen return &messageIt->second; 65d1d411f9SSui Chen } 66d1d411f9SSui Chen 67d1d411f9SSui Chen return nullptr; 68d1d411f9SSui Chen } 69d1d411f9SSui Chen 70d1d411f9SSui Chen const Message* getMessage(std::string_view messageID) 71d1d411f9SSui Chen { 72d1d411f9SSui Chen // Redfish MessageIds are in the form 73d1d411f9SSui Chen // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 74d1d411f9SSui Chen // the right Message 75d1d411f9SSui Chen std::vector<std::string> fields; 76d1d411f9SSui Chen fields.reserve(4); 77d1d411f9SSui Chen bmcweb::split(fields, messageID, '.'); 78d109e2b6SAlexander Hansen if (fields.size() != 4) 79d109e2b6SAlexander Hansen { 80d109e2b6SAlexander Hansen return nullptr; 81d109e2b6SAlexander Hansen } 82d109e2b6SAlexander Hansen 83d1d411f9SSui Chen const std::string& registryName = fields[0]; 84d1d411f9SSui Chen const std::string& messageKey = fields[3]; 85d1d411f9SSui Chen 86d1d411f9SSui Chen // Find the right registry and check it for the MessageKey 87d109e2b6SAlexander Hansen return getMessageFromRegistry(messageKey, 88*4a102cd4SPatrick Williams getRegistryMessagesFromPrefix(registryName)); 89d1d411f9SSui Chen } 90d1d411f9SSui Chen 91d1d411f9SSui Chen } // namespace redfish::registries 92