blob: 9ed261a9370dfa603874fc55be07722174d4df2c [file] [log] [blame]
Sui Chend1d411f2022-04-10 23:09:36 -07001#include "registries.hpp"
2
Alexander Hansend109e2b2024-11-18 14:38:06 +01003#include "registries_selector.hpp"
Sui Chend1d411f2022-04-10 23:09:36 -07004#include "str_utility.hpp"
5
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <algorithm>
7#include <cstring>
Ed Tanous3544d2a2023-08-06 18:12:20 -07008#include <ranges>
Ed Tanousf0b59af2024-03-20 13:38:04 -07009#include <span>
Sui Chend1d411f2022-04-10 23:09:36 -070010#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070011#include <string_view>
Sui Chend1d411f2022-04-10 23:09:36 -070012#include <vector>
13
14namespace redfish::registries
15{
16
17const Message* getMessageFromRegistry(const std::string& messageKey,
18 std::span<const MessageEntry> registry)
19{
Ed Tanous3544d2a2023-08-06 18:12:20 -070020 std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
21 registry, [&messageKey](const MessageEntry& messageEntry) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040022 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
23 });
Sui Chend1d411f2022-04-10 23:09:36 -070024 if (messageIt != registry.end())
25 {
26 return &messageIt->second;
27 }
28
29 return nullptr;
30}
31
32const Message* getMessage(std::string_view messageID)
33{
34 // Redfish MessageIds are in the form
35 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
36 // the right Message
37 std::vector<std::string> fields;
38 fields.reserve(4);
39 bmcweb::split(fields, messageID, '.');
Alexander Hansend109e2b2024-11-18 14:38:06 +010040 if (fields.size() != 4)
41 {
42 return nullptr;
43 }
44
Sui Chend1d411f2022-04-10 23:09:36 -070045 const std::string& registryName = fields[0];
46 const std::string& messageKey = fields[3];
47
48 // Find the right registry and check it for the MessageKey
Alexander Hansend109e2b2024-11-18 14:38:06 +010049 // Find the right registry and check it for the MessageKey
50 return getMessageFromRegistry(messageKey,
51 getRegistryFromPrefix(registryName));
Sui Chend1d411f2022-04-10 23:09:36 -070052}
53
54} // namespace redfish::registries