Refactor Message Header and Url Selector
This is to refactor the access of header and url parts from
MessageRegistry to reduce the replicated comparison of the registry
name.
Tested:
- GET /redfish/v1/Registries/<str>/<str> like
```
- /redfish/v1/Registries/Base/Base
- /redfish/v1/Registries/TaskEvent/TaskEvent
- /redfish/v1/Registries/ResourceEvent/ResourceEvent
- /redfish/v1/Registries/OpenBMC/OpenBMC
- /redfish/v1/Registries/Telemetry/Telemetry
```
Change-Id: Id5806343709084292273e8021e0d0b4a114ac06f
Signed-off-by: Myung Bae <myungbae@us.ibm.com>
diff --git a/redfish-core/include/registries_selector.hpp b/redfish-core/include/registries_selector.hpp
index 8b9cdf3..706e715 100644
--- a/redfish-core/include/registries_selector.hpp
+++ b/redfish-core/include/registries_selector.hpp
@@ -1,32 +1,80 @@
#pragma once
+#include "registries.hpp"
#include "registries/base_message_registry.hpp"
#include "registries/heartbeat_event_message_registry.hpp"
#include "registries/openbmc_message_registry.hpp"
+#include "registries/resource_event_message_registry.hpp"
#include "registries/task_event_message_registry.hpp"
+#include "registries/telemetry_message_registry.hpp"
+#include <optional>
#include <span>
#include <string_view>
namespace redfish::registries
{
-inline std::span<const MessageEntry>
- getRegistryFromPrefix(std::string_view registryName)
+struct HeaderAndUrl
{
- if (task_event::header.registryPrefix == registryName)
+ const Header& header;
+ const char* url;
+};
+
+inline std::optional<registries::HeaderAndUrl>
+ getRegistryHeaderAndUrlFromPrefix(std::string_view registryName)
+{
+ if (base::header.registryPrefix == registryName)
{
- return {task_event::registry};
+ return HeaderAndUrl{base::header, base::url};
+ }
+ if (heartbeat_event::header.registryPrefix == registryName)
+ {
+ return HeaderAndUrl{heartbeat_event::header, heartbeat_event::url};
}
if (openbmc::header.registryPrefix == registryName)
{
- return {openbmc::registry};
+ return HeaderAndUrl{openbmc::header, openbmc::url};
+ }
+ if (resource_event::header.registryPrefix == registryName)
+ {
+ return HeaderAndUrl{resource_event::header, resource_event::url};
+ }
+ if (task_event::header.registryPrefix == registryName)
+ {
+ return HeaderAndUrl{task_event::header, task_event::url};
+ }
+ if (telemetry::header.registryPrefix == registryName)
+ {
+ return HeaderAndUrl{telemetry::header, telemetry::url};
+ }
+ return std::nullopt;
+}
+
+inline std::span<const MessageEntry>
+ getRegistryFromPrefix(std::string_view registryName)
+{
+ if (base::header.registryPrefix == registryName)
+ {
+ return {base::registry};
}
if (heartbeat_event::header.registryPrefix == registryName)
{
return {heartbeat_event::registry};
}
- if (base::header.registryPrefix == registryName)
+ if (openbmc::header.registryPrefix == registryName)
{
- return {base::registry};
+ return {openbmc::registry};
+ }
+ if (resource_event::header.registryPrefix == registryName)
+ {
+ return {resource_event::registry};
+ }
+ if (task_event::header.registryPrefix == registryName)
+ {
+ return {task_event::registry};
+ }
+ if (telemetry::header.registryPrefix == registryName)
+ {
+ return {telemetry::registry};
}
return {openbmc::registry};
}
diff --git a/redfish-core/lib/message_registries.hpp b/redfish-core/lib/message_registries.hpp
index a6c3674..a6e108e 100644
--- a/redfish-core/lib/message_registries.hpp
+++ b/redfish-core/lib/message_registries.hpp
@@ -18,13 +18,7 @@
#include "app.hpp"
#include "query.hpp"
#include "registries.hpp"
-#include "registries/base_message_registry.hpp"
-#include "registries/heartbeat_event_message_registry.hpp"
-#include "registries/openbmc_message_registry.hpp"
-#include "registries/privilege_registry.hpp"
-#include "registries/resource_event_message_registry.hpp"
-#include "registries/task_event_message_registry.hpp"
-#include "registries/telemetry_message_registry.hpp"
+#include "registries_selector.hpp"
#include <boost/url/format.hpp>
@@ -87,46 +81,22 @@
{
return;
}
- const registries::Header* header = nullptr;
std::string dmtf = "DMTF ";
- const char* url = nullptr;
+ std::optional<registries::HeaderAndUrl> headerAndUrl =
+ registries::getRegistryHeaderAndUrlFromPrefix(registry);
- if (registry == "Base")
- {
- header = ®istries::base::header;
- url = registries::base::url;
- }
- else if (registry == "TaskEvent")
- {
- header = ®istries::task_event::header;
- url = registries::task_event::url;
- }
- else if (registry == "OpenBMC")
- {
- header = ®istries::openbmc::header;
- dmtf.clear();
- }
- else if (registry == "ResourceEvent")
- {
- header = ®istries::resource_event::header;
- url = registries::resource_event::url;
- }
- else if (registry == "Telemetry")
- {
- header = ®istries::telemetry::header;
- url = registries::telemetry::url;
- }
- else if (registry == "HeartbeatEvent")
- {
- header = ®istries::heartbeat_event::header;
- url = registries::heartbeat_event::url;
- }
- else
+ if (!headerAndUrl)
{
messages::resourceNotFound(asyncResp->res, "MessageRegistryFile",
registry);
return;
}
+ if (registry == "OpenBMC")
+ {
+ dmtf.clear();
+ }
+ const registries::Header& header = headerAndUrl->header;
+ const char* url = headerAndUrl->url;
asyncResp->res.jsonValue["@odata.id"] =
boost::urls::format("/redfish/v1/Registries/{}", registry);
@@ -135,15 +105,15 @@
asyncResp->res.jsonValue["Name"] = registry + " Message Registry File";
asyncResp->res.jsonValue["Description"] =
dmtf + registry + " Message Registry File Location";
- asyncResp->res.jsonValue["Id"] = header->registryPrefix;
- asyncResp->res.jsonValue["Registry"] = header->id;
+ asyncResp->res.jsonValue["Id"] = header.registryPrefix;
+ asyncResp->res.jsonValue["Registry"] = header.id;
nlohmann::json::array_t languages;
- languages.emplace_back(header->language);
+ languages.emplace_back(header.language);
asyncResp->res.jsonValue["Languages@odata.count"] = languages.size();
asyncResp->res.jsonValue["Languages"] = std::move(languages);
nlohmann::json::array_t locationMembers;
nlohmann::json::object_t location;
- location["Language"] = header->language;
+ location["Language"] = header.language;
location["Uri"] = "/redfish/v1/Registries/" + registry + "/" + registry;
if (url != nullptr)
@@ -173,101 +143,53 @@
{
return;
}
- const registries::Header* header = nullptr;
- std::vector<const registries::MessageEntry*> registryEntries;
- if (registry == "Base")
- {
- header = ®istries::base::header;
- for (const registries::MessageEntry& entry : registries::base::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else if (registry == "TaskEvent")
- {
- header = ®istries::task_event::header;
- for (const registries::MessageEntry& entry :
- registries::task_event::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else if (registry == "OpenBMC")
- {
- header = ®istries::openbmc::header;
- for (const registries::MessageEntry& entry :
- registries::openbmc::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else if (registry == "ResourceEvent")
- {
- header = ®istries::resource_event::header;
- for (const registries::MessageEntry& entry :
- registries::resource_event::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else if (registry == "Telemetry")
- {
- header = ®istries::telemetry::header;
- for (const registries::MessageEntry& entry :
- registries::telemetry::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else if (registry == "HeartbeatEvent")
- {
- header = ®istries::heartbeat_event::header;
- for (const registries::MessageEntry& entry :
- registries::heartbeat_event::registry)
- {
- registryEntries.emplace_back(&entry);
- }
- }
- else
+
+ std::optional<registries::HeaderAndUrl> headerAndUrl =
+ registries::getRegistryHeaderAndUrlFromPrefix(registry);
+ if (!headerAndUrl)
{
messages::resourceNotFound(asyncResp->res, "MessageRegistryFile",
registry);
return;
}
+ const registries::Header& header = headerAndUrl->header;
if (registry != registryMatch)
{
- messages::resourceNotFound(asyncResp->res, header->type, registryMatch);
+ messages::resourceNotFound(asyncResp->res, header.type, registryMatch);
return;
}
- asyncResp->res.jsonValue["@Redfish.Copyright"] = header->copyright;
- asyncResp->res.jsonValue["@odata.type"] = header->type;
- asyncResp->res.jsonValue["Id"] = header->id;
- asyncResp->res.jsonValue["Name"] = header->name;
- asyncResp->res.jsonValue["Language"] = header->language;
- asyncResp->res.jsonValue["Description"] = header->description;
- asyncResp->res.jsonValue["RegistryPrefix"] = header->registryPrefix;
- asyncResp->res.jsonValue["RegistryVersion"] = header->registryVersion;
- asyncResp->res.jsonValue["OwningEntity"] = header->owningEntity;
+ asyncResp->res.jsonValue["@Redfish.Copyright"] = header.copyright;
+ asyncResp->res.jsonValue["@odata.type"] = header.type;
+ asyncResp->res.jsonValue["Id"] = header.id;
+ asyncResp->res.jsonValue["Name"] = header.name;
+ asyncResp->res.jsonValue["Language"] = header.language;
+ asyncResp->res.jsonValue["Description"] = header.description;
+ asyncResp->res.jsonValue["RegistryPrefix"] = header.registryPrefix;
+ asyncResp->res.jsonValue["RegistryVersion"] = header.registryVersion;
+ asyncResp->res.jsonValue["OwningEntity"] = header.owningEntity;
nlohmann::json& messageObj = asyncResp->res.jsonValue["Messages"];
// Go through the Message Registry and populate each Message
- for (const registries::MessageEntry* message : registryEntries)
+ const std::span<const registries::MessageEntry> registryEntries =
+ registries::getRegistryFromPrefix(registry);
+
+ for (const registries::MessageEntry& message : registryEntries)
{
- nlohmann::json& obj = messageObj[message->first];
- obj["Description"] = message->second.description;
- obj["Message"] = message->second.message;
- obj["Severity"] = message->second.messageSeverity;
- obj["MessageSeverity"] = message->second.messageSeverity;
- obj["NumberOfArgs"] = message->second.numberOfArgs;
- obj["Resolution"] = message->second.resolution;
- if (message->second.numberOfArgs > 0)
+ nlohmann::json& obj = messageObj[message.first];
+ obj["Description"] = message.second.description;
+ obj["Message"] = message.second.message;
+ obj["Severity"] = message.second.messageSeverity;
+ obj["MessageSeverity"] = message.second.messageSeverity;
+ obj["NumberOfArgs"] = message.second.numberOfArgs;
+ obj["Resolution"] = message.second.resolution;
+ if (message.second.numberOfArgs > 0)
{
nlohmann::json& messageParamArray = obj["ParamTypes"];
messageParamArray = nlohmann::json::array();
- for (const char* str : message->second.paramTypes)
+ for (const char* str : message.second.paramTypes)
{
if (str == nullptr)
{