Optimize resource event schema
Very similar to how the base registry was optimized for binary size,
optimize the resource event registry, by making the getLogFromRegistry a
common method for both registries.
Tested: Only usage of these calls appears to be in management console.
The code and pattern we're using here is well unit tested, and seems
reasonable that we could rely on the compile time checks, but if not, I
could use some help testing this.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I9cc442966df2ed301b14547727a5eb727c0c3a29
diff --git a/redfish-core/include/registries.hpp b/redfish-core/include/registries.hpp
index 4247aa8..d3d6c5f 100644
--- a/redfish-core/include/registries.hpp
+++ b/redfish-core/include/registries.hpp
@@ -15,6 +15,8 @@
*/
#pragma once
+#include <nlohmann/json.hpp>
+
#include <array>
#include <charconv>
#include <cstddef>
@@ -89,4 +91,33 @@
return ret;
}
+inline nlohmann::json::object_t
+ getLogFromRegistry(const Header& header,
+ std::span<const MessageEntry> registry, size_t index,
+ std::span<const std::string_view> args)
+{
+ const redfish::registries::MessageEntry& entry = registry[index];
+ // Intentionally make a copy of the string, so we can append in the
+ // parameters.
+ std::string msg = entry.second.message;
+ redfish::registries::fillMessageArgs(args, msg);
+ nlohmann::json jArgs = nlohmann::json::array();
+ for (const std::string_view arg : args)
+ {
+ jArgs.push_back(arg);
+ }
+ std::string msgId = header.id;
+ msgId += ".";
+ msgId += entry.first;
+
+ nlohmann::json::object_t response;
+ response["@odata.type"] = "#Message.v1_1_1.Message";
+ response["MessageId"] = std::move(msgId);
+ response["Message"] = std::move(msg);
+ response["MessageArgs"] = std::move(jArgs);
+ response["MessageSeverity"] = entry.second.messageSeverity;
+ response["Resolution"] = entry.second.resolution;
+ return response;
+}
+
} // namespace redfish::registries
diff --git a/redfish-core/include/resource_messages.hpp b/redfish-core/include/resource_messages.hpp
index 95ace94..e66ae6f 100644
--- a/redfish-core/include/resource_messages.hpp
+++ b/redfish-core/include/resource_messages.hpp
@@ -1,41 +1,43 @@
#pragma once
+#include <nlohmann/json.hpp>
+#include <registries/resource_event_message_registry.hpp>
+
namespace redfish
{
namespace messages
{
+nlohmann::json
+ getLogResourceEvent(redfish::registries::resource_event::Index name,
+ std::span<const std::string_view> args)
+{
+ size_t index = static_cast<size_t>(name);
+ if (index >= redfish::registries::resource_event::registry.size())
+ {
+ return {};
+ }
+ return getLogFromRegistry(redfish::registries::resource_event::header,
+ redfish::registries::resource_event::registry,
+ index, args);
+}
+
inline nlohmann::json resourceChanged()
{
- return nlohmann::json{
- {"EventType", "ResourceChanged"},
- {"MessageId", "ResourceEvent.1.0.3.ResourceChanged"},
- {"Message", "One or more resource properties have changed."},
- {"MessageArgs", {}},
- {"Severity", "OK"},
- {"MessageSeverity", "OK"}};
+ return getLogResourceEvent(
+ redfish::registries::resource_event::Index::resourceChanged, {});
}
inline nlohmann::json resourceCreated()
{
- return nlohmann::json{
- {"EventType", "ResourceAdded"},
- {"MessageId", "ResourceEvent.1.0.3.ResourceCreated"},
- {"Message", "The resource has been created successfully."},
- {"MessageArgs", {}},
- {"Severity", "OK"},
- {"MessageSeverity", "OK"}};
+ return getLogResourceEvent(
+ redfish::registries::resource_event::Index::resourceCreated, {});
}
inline nlohmann::json resourceRemoved()
{
- return nlohmann::json{
- {"EventType", "ResourceRemoved"},
- {"MessageId", "ResourceEvent.1.0.3.ResourceRemoved"},
- {"Message", "The resource has been removed successfully."},
- {"MessageArgs", {}},
- {"Severity", "OK"},
- {"MessageSeverity", "OK"}};
+ return getLogResourceEvent(
+ redfish::registries::resource_event::Index::resourceCreated, {});
}
} // namespace messages