blob: 1549dd319ae0c8fa20ae8018f76db4b331715b2b [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
Alexander Hansend109e2b2024-11-18 14:38:06 +01005#include "registries_selector.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 Williamsbd79bce2024-08-16 15:22:20 -040024 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, '.');
Alexander Hansend109e2b2024-11-18 14:38:06 +010042 if (fields.size() != 4)
43 {
44 return nullptr;
45 }
46
Sui Chend1d411f2022-04-10 23:09:36 -070047 const std::string& registryName = fields[0];
48 const std::string& messageKey = fields[3];
49
50 // Find the right registry and check it for the MessageKey
Alexander Hansend109e2b2024-11-18 14:38:06 +010051 // Find the right registry and check it for the MessageKey
52 return getMessageFromRegistry(messageKey,
53 getRegistryFromPrefix(registryName));
Sui Chend1d411f2022-04-10 23:09:36 -070054}
55
56} // namespace redfish::registries