Dynamic EventLogService handler

In order to reuse the handler for the EventLogService under Managers,
extract the logic from the handler and put it into a separate function
in the eventlog util. Add an additional argument that specifies
the redfish resource, so we can handle Systems and Managers resource
with the same handler.

Tested: Code compiles. Redfish validation succeeds.
Additionally the curl output with and without the changes has been
diffed. No differences observed.

Change-Id: I48825b55b41afeafa02283dc91cf4cb1cd4cd7c3
Signed-off-by: Oliver Brewka <oliver.brewka@9elements.com>
diff --git a/redfish-core/include/utils/eventlog_utils.hpp b/redfish-core/include/utils/eventlog_utils.hpp
index 7d34ad7..0f6da3d 100644
--- a/redfish-core/include/utils/eventlog_utils.hpp
+++ b/redfish-core/include/utils/eventlog_utils.hpp
@@ -6,11 +6,14 @@
 #include "async_resp.hpp"
 #include "dbus_utility.hpp"
 #include "error_messages.hpp"
+#include "generated/enums/log_service.hpp"
 #include "http_response.hpp"
 #include "logging.hpp"
 #include "registries.hpp"
 #include "str_utility.hpp"
+#include "utils/etag_utils.hpp"
 #include "utils/query_param.hpp"
+#include "utils/time_utils.hpp"
 
 #include <boost/beast/http/field.hpp>
 #include <boost/beast/http/status.hpp>
@@ -103,6 +106,47 @@
     return descriptor;
 }
 
+inline void handleSystemsAndManagersEventLogServiceGet(
+    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+    LogServiceParent parent)
+{
+    const std::string parentStr = logServiceParentToString(parent);
+    const std::string_view childId = getChildIdFromParent(parent);
+    const std::string logEntryDescriptor = getLogEntryDescriptor(parent);
+
+    if (parentStr.empty() || childId.empty() || logEntryDescriptor.empty())
+    {
+        messages::internalError(asyncResp->res);
+        return;
+    }
+
+    asyncResp->res.jsonValue["@odata.id"] = std::format(
+        "/redfish/v1/{}/{}/LogServices/EventLog", parentStr, childId);
+    asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
+    asyncResp->res.jsonValue["Name"] = "Event Log Service";
+    asyncResp->res.jsonValue["Description"] =
+        std::format("{} Event Log Service", logEntryDescriptor);
+    asyncResp->res.jsonValue["Id"] = "EventLog";
+    asyncResp->res.jsonValue["OverWritePolicy"] =
+        log_service::OverWritePolicy::WrapsWhenFull;
+
+    std::pair<std::string, std::string> redfishDateTimeOffset =
+        redfish::time_utils::getDateTimeOffsetNow();
+
+    asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
+    asyncResp->res.jsonValue["DateTimeLocalOffset"] =
+        redfishDateTimeOffset.second;
+
+    asyncResp->res.jsonValue["Entries"]["@odata.id"] = std::format(
+        "/redfish/v1/{}/{}/LogServices/EventLog/Entries", parentStr, childId);
+    asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"]
+
+        = std::format(
+            "/redfish/v1/{}/{}/LogServices/EventLog/Actions/LogService.ClearLog",
+            parentStr, childId);
+    etag_utils::setEtagOmitDateTimeHandler(asyncResp);
+}
+
 /*
  * Journal EventLog utilities
  * */
@@ -231,8 +275,8 @@
     }
 
     // Get the Created time from the timestamp. The log timestamp is in RFC3339
-    // format which matches the Redfish format except for the fractional seconds
-    // between the '.' and the '+', so just remove them.
+    // format which matches the Redfish format except for the
+    // fractional seconds between the '.' and the '+', so just remove them.
     std::size_t dot = timestamp.find_first_of('.');
     std::size_t plus = timestamp.find_first_of('+');
     if (dot != std::string::npos && plus != std::string::npos)
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index 015360c..982f74e 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -24,6 +24,7 @@
 #include "task_messages.hpp"
 #include "utils/dbus_utils.hpp"
 #include "utils/etag_utils.hpp"
+#include "utils/eventlog_utils.hpp"
 #include "utils/json_utils.hpp"
 #include "utils/log_services_utils.hpp"
 #include "utils/time_utils.hpp"
@@ -1010,56 +1011,27 @@
         });
 }
 
-inline void requestRoutesEventLogService(App& app)
+inline void requestRoutesSystemsEventLogService(App& app)
 {
     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
         .privileges(redfish::privileges::getLogService)
-        .methods(
-            boost::beast::http::verb::
-                get)([&app](const crow::Request& req,
-                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                            const std::string& systemName) {
-            if (!redfish::setUpRedfishRoute(app, req, asyncResp))
-            {
-                return;
-            }
-            if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
-            {
-                messages::resourceNotFound(asyncResp->res, "ComputerSystem",
-                                           systemName);
-                return;
-            }
-            asyncResp->res.jsonValue["@odata.id"] =
-                std::format("/redfish/v1/Systems/{}/LogServices/EventLog",
-                            BMCWEB_REDFISH_SYSTEM_URI_NAME);
-            asyncResp->res.jsonValue["@odata.type"] =
-                "#LogService.v1_2_0.LogService";
-            asyncResp->res.jsonValue["Name"] = "Event Log Service";
-            asyncResp->res.jsonValue["Description"] =
-                "System Event Log Service";
-            asyncResp->res.jsonValue["Id"] = "EventLog";
-            asyncResp->res.jsonValue["OverWritePolicy"] =
-                log_service::OverWritePolicy::WrapsWhenFull;
-
-            std::pair<std::string, std::string> redfishDateTimeOffset =
-                redfish::time_utils::getDateTimeOffsetNow();
-
-            asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
-            asyncResp->res.jsonValue["DateTimeLocalOffset"] =
-                redfishDateTimeOffset.second;
-
-            asyncResp->res.jsonValue["Entries"]["@odata.id"] = std::format(
-                "/redfish/v1/Systems/{}/LogServices/EventLog/Entries",
-                BMCWEB_REDFISH_SYSTEM_URI_NAME);
-            asyncResp->res
-                .jsonValue["Actions"]["#LogService.ClearLog"]["target"]
-
-                = std::format(
-                    "/redfish/v1/Systems/{}/LogServices/EventLog/Actions/LogService.ClearLog",
-                    BMCWEB_REDFISH_SYSTEM_URI_NAME);
-
-            etag_utils::setEtagOmitDateTimeHandler(asyncResp);
-        });
+        .methods(boost::beast::http::verb::get)(
+            [&app](const crow::Request& req,
+                   const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                   const std::string& systemName) {
+                if (!redfish::setUpRedfishRoute(app, req, asyncResp))
+                {
+                    return;
+                }
+                if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
+                {
+                    messages::resourceNotFound(asyncResp->res, "ComputerSystem",
+                                               systemName);
+                    return;
+                }
+                eventlog_utils::handleSystemsAndManagersEventLogServiceGet(
+                    asyncResp, eventlog_utils::LogServiceParent::Systems);
+            });
 }
 
 inline void handleBMCLogServicesCollectionGet(
diff --git a/redfish-core/src/redfish.cpp b/redfish-core/src/redfish.cpp
index 3eac59d..6f61f91 100644
--- a/redfish-core/src/redfish.cpp
+++ b/redfish-core/src/redfish.cpp
@@ -115,7 +115,7 @@
     requestRoutesCableCollection(app);
 
     requestRoutesSystemLogServiceCollection(app);
-    requestRoutesEventLogService(app);
+    requestRoutesSystemsEventLogService(app);
 
     requestRoutesSystemsLogServicesPostCode(app);