blob: 2a426bfa9b3cd4b29bc9b661c91c86e9d0532692 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Sui Chend1d411f2022-04-10 23:09:36 -07003#include "registries.hpp"
4
Patrick Williams4a102cd2025-02-27 14:52:54 -05005// We need the registries_selector pulled into some cpp part so that the
6// registration hooks run.
7// NOLINTNEXTLINE(misc-include-cleaner)
Alexander Hansend109e2b2024-11-18 14:38:06 +01008#include "registries_selector.hpp"
Sui Chend1d411f2022-04-10 23:09:36 -07009#include "str_utility.hpp"
10
Ed Tanousf0b59af2024-03-20 13:38:04 -070011#include <algorithm>
12#include <cstring>
Patrick Williams4a102cd2025-02-27 14:52:54 -050013#include <functional>
14#include <map>
15#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070016#include <ranges>
Ed Tanousf0b59af2024-03-20 13:38:04 -070017#include <span>
Sui Chend1d411f2022-04-10 23:09:36 -070018#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070019#include <string_view>
Sui Chend1d411f2022-04-10 23:09:36 -070020#include <vector>
21
22namespace redfish::registries
23{
24
Patrick Williams4a102cd2025-02-27 14:52:54 -050025auto allRegistries() -> std::map<std::string, RegistryEntry>&
26{
27 static std::map<std::string, RegistryEntry> registries;
28 return registries;
29}
30
31auto getRegistryFromPrefix(const std::string& registryName)
32 -> std::optional<RegistryEntryRef>
33{
34 auto& registries = allRegistries();
35 if (auto it = registries.find(registryName); it != registries.end())
36 {
37 return std::ref(it->second);
38 }
39
40 return std::nullopt;
41}
42
43auto getRegistryMessagesFromPrefix(const std::string& registryName)
44 -> MessageEntries
45{
46 auto registry = getRegistryFromPrefix(registryName);
47 if (!registry)
48 {
49 return {};
50 }
51
52 return registry->get().entries;
53}
54
Sui Chend1d411f2022-04-10 23:09:36 -070055const Message* getMessageFromRegistry(const std::string& messageKey,
56 std::span<const MessageEntry> registry)
57{
Ed Tanous3544d2a2023-08-06 18:12:20 -070058 std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
59 registry, [&messageKey](const MessageEntry& messageEntry) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040060 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
61 });
Sui Chend1d411f2022-04-10 23:09:36 -070062 if (messageIt != registry.end())
63 {
64 return &messageIt->second;
65 }
66
67 return nullptr;
68}
69
70const Message* getMessage(std::string_view messageID)
71{
72 // Redfish MessageIds are in the form
73 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
74 // the right Message
75 std::vector<std::string> fields;
76 fields.reserve(4);
77 bmcweb::split(fields, messageID, '.');
Alexander Hansend109e2b2024-11-18 14:38:06 +010078 if (fields.size() != 4)
79 {
80 return nullptr;
81 }
82
Sui Chend1d411f2022-04-10 23:09:36 -070083 const std::string& registryName = fields[0];
84 const std::string& messageKey = fields[3];
85
86 // Find the right registry and check it for the MessageKey
Alexander Hansend109e2b2024-11-18 14:38:06 +010087 return getMessageFromRegistry(messageKey,
Patrick Williams4a102cd2025-02-27 14:52:54 -050088 getRegistryMessagesFromPrefix(registryName));
Sui Chend1d411f2022-04-10 23:09:36 -070089}
90
91} // namespace redfish::registries