blob: 8084a5115847e8c91083f4d7c66040921ef19903 [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>
Thang Trand9495962025-10-16 07:07:30 +000020#include <utility>
Sui Chend1d411f2022-04-10 23:09:36 -070021#include <vector>
22
23namespace redfish::registries
24{
25
Patrick Williams4a102cd2025-02-27 14:52:54 -050026auto allRegistries() -> std::map<std::string, RegistryEntry>&
27{
28 static std::map<std::string, RegistryEntry> registries;
29 return registries;
30}
31
32auto 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
44auto 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 Chend1d411f2022-04-10 23:09:36 -070056const Message* getMessageFromRegistry(const std::string& messageKey,
57 std::span<const MessageEntry> registry)
58{
Ed Tanous3544d2a2023-08-06 18:12:20 -070059 std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
60 registry, [&messageKey](const MessageEntry& messageEntry) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040061 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
62 });
Sui Chend1d411f2022-04-10 23:09:36 -070063 if (messageIt != registry.end())
64 {
65 return &messageIt->second;
66 }
67
68 return nullptr;
69}
70
Thang Trand9495962025-10-16 07:07:30 +000071std::optional<MessageId> getMessageComponents(std::string_view message)
Sui Chend1d411f2022-04-10 23:09:36 -070072{
Thang Trand9495962025-10-16 07:07:30 +000073 // Redfish Message are in the form
74 // RegistryName.MajorVersion.MinorVersion.MessageKey
Sui Chend1d411f2022-04-10 23:09:36 -070075 std::vector<std::string> fields;
76 fields.reserve(4);
Thang Trand9495962025-10-16 07:07:30 +000077 bmcweb::split(fields, message, '.');
Alexander Hansend109e2b2024-11-18 14:38:06 +010078 if (fields.size() != 4)
79 {
Thang Trand9495962025-10-16 07:07:30 +000080 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
87const Message* getMessage(std::string_view messageID)
88{
89 std::optional<MessageId> msgComponents = getMessageComponents(messageID);
90 if (!msgComponents)
91 {
Alexander Hansend109e2b2024-11-18 14:38:06 +010092 return nullptr;
93 }
94
Sui Chend1d411f2022-04-10 23:09:36 -070095 // Find the right registry and check it for the MessageKey
Thang Trand9495962025-10-16 07:07:30 +000096 return getMessageFromRegistry(
97 msgComponents->messageKey,
98 getRegistryMessagesFromPrefix(msgComponents->registryName));
Sui Chend1d411f2022-04-10 23:09:36 -070099}
100
101} // namespace redfish::registries