diff options
author | Sui Chen <suichen@google.com> | 2022-04-11 09:09:36 +0300 |
---|---|---|
committer | Ed Tanous <edtanous@google.com> | 2023-02-28 00:45:22 +0300 |
commit | d1d411f9eebc1d2ca49eec0700670ba564dac5c8 (patch) | |
tree | 076d80209a4ca8688690cc3c48f83fdb6042b4ec /redfish-core/src/registries.cpp | |
parent | af20dd1ccc481af63b528a851bce390a0070ab32 (diff) | |
download | bmcweb-d1d411f9eebc1d2ca49eec0700670ba564dac5c8.tar.xz |
Move getMessage and getMessageFromRegistry to cpp and add test
This change moves getMessage and getMessageFromRegistry to a .cpp file
so that they can be easily tested.
Tested: Unit test passes
Signed-off-by: Sui Chen <suichen@google.com>
Change-Id: Ia9fc91e5a47036198bf013ff3ea21ea9f6d5259a
Diffstat (limited to 'redfish-core/src/registries.cpp')
-rw-r--r-- | redfish-core/src/registries.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/redfish-core/src/registries.cpp b/redfish-core/src/registries.cpp new file mode 100644 index 0000000000..3bfe0591d0 --- /dev/null +++ b/redfish-core/src/registries.cpp @@ -0,0 +1,54 @@ +#include "registries.hpp" + +#include "registries/base_message_registry.hpp" +#include "registries/openbmc_message_registry.hpp" +#include "str_utility.hpp" + +#include <string> +#include <vector> + +namespace redfish::registries +{ + +const Message* getMessageFromRegistry(const std::string& messageKey, + std::span<const MessageEntry> registry) +{ + std::span<const MessageEntry>::iterator messageIt = + std::find_if(registry.begin(), registry.end(), + [&messageKey](const MessageEntry& messageEntry) { + return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; + }); + if (messageIt != registry.end()) + { + return &messageIt->second; + } + + return nullptr; +} + +const Message* getMessage(std::string_view messageID) +{ + // Redfish MessageIds are in the form + // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find + // the right Message + std::vector<std::string> fields; + fields.reserve(4); + bmcweb::split(fields, messageID, '.'); + const std::string& registryName = fields[0]; + const std::string& messageKey = fields[3]; + + // Find the right registry and check it for the MessageKey + if (std::string(base::header.registryPrefix) == registryName) + { + return getMessageFromRegistry( + messageKey, std::span<const MessageEntry>(base::registry)); + } + if (std::string(openbmc::header.registryPrefix) == registryName) + { + return getMessageFromRegistry( + messageKey, std::span<const MessageEntry>(openbmc::registry)); + } + return nullptr; +} + +} // namespace redfish::registries |