blob: 61b27b57b4663c6cd279ec9be96b0aac28eaf173 [file] [log] [blame]
Sui Chend1d411f2022-04-10 23:09:36 -07001#include "registries.hpp"
2
3#include "registries/base_message_registry.hpp"
4#include "registries/openbmc_message_registry.hpp"
Michal Orzel86987602024-06-24 08:19:49 +02005#include "registries/telemetry_message_registry.hpp"
Sui Chend1d411f2022-04-10 23:09:36 -07006#include "str_utility.hpp"
7
Ed Tanousf0b59af2024-03-20 13:38:04 -07008#include <algorithm>
9#include <cstring>
Ed Tanous3544d2a2023-08-06 18:12:20 -070010#include <ranges>
Ed Tanousf0b59af2024-03-20 13:38:04 -070011#include <span>
Sui Chend1d411f2022-04-10 23:09:36 -070012#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070013#include <string_view>
Sui Chend1d411f2022-04-10 23:09:36 -070014#include <vector>
15
16namespace redfish::registries
17{
18
19const Message* getMessageFromRegistry(const std::string& messageKey,
20 std::span<const MessageEntry> registry)
21{
Ed Tanous3544d2a2023-08-06 18:12:20 -070022 std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
23 registry, [&messageKey](const MessageEntry& messageEntry) {
Patrick Williams5a39f772023-10-20 11:20:21 -050024 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
25 });
Sui Chend1d411f2022-04-10 23:09:36 -070026 if (messageIt != registry.end())
27 {
28 return &messageIt->second;
29 }
30
31 return nullptr;
32}
33
34const Message* getMessage(std::string_view messageID)
35{
36 // Redfish MessageIds are in the form
37 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
38 // the right Message
39 std::vector<std::string> fields;
40 fields.reserve(4);
41 bmcweb::split(fields, messageID, '.');
42 const std::string& registryName = fields[0];
43 const std::string& messageKey = fields[3];
44
45 // Find the right registry and check it for the MessageKey
46 if (std::string(base::header.registryPrefix) == registryName)
47 {
48 return getMessageFromRegistry(
49 messageKey, std::span<const MessageEntry>(base::registry));
50 }
51 if (std::string(openbmc::header.registryPrefix) == registryName)
52 {
53 return getMessageFromRegistry(
54 messageKey, std::span<const MessageEntry>(openbmc::registry));
55 }
Michal Orzel86987602024-06-24 08:19:49 +020056 if (std::string(telemetry::header.registryPrefix) == registryName)
57 {
58 return getMessageFromRegistry(
59 messageKey, std::span<const MessageEntry>(telemetry::registry));
60 }
Sui Chend1d411f2022-04-10 23:09:36 -070061 return nullptr;
62}
63
64} // namespace redfish::registries