Make telemetry use the common collection utilities

getReportCollection is almost a 1:1 copy of getCollectionMembers, but
hardcoded for report interface.  This commit moves to using the common
implementation, rather than duplicating the code.

In the course of writing this patchset, it looks like the definition in
metric_report_definition.hpp is incorrect, as we pulling metric reports
instead of pulling metric report definitions.  This commit has the same
behavior as the old, but will need to be looked at in the future.

Tested:
Basic touch testing done by Szymon Dompke. "Everything looks good to me"

Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I643da1849aacc38848db1da3eeaad0c111521d34
diff --git a/redfish-core/include/utils/telemetry_utils.hpp b/redfish-core/include/utils/telemetry_utils.hpp
index 5872350..76ffe96 100644
--- a/redfish-core/include/utils/telemetry_utils.hpp
+++ b/redfish-core/include/utils/telemetry_utils.hpp
@@ -11,56 +11,9 @@
 constexpr const char* service = "xyz.openbmc_project.Telemetry";
 constexpr const char* reportInterface = "xyz.openbmc_project.Telemetry.Report";
 constexpr const char* metricReportDefinitionUri =
-    "/redfish/v1/TelemetryService/MetricReportDefinitions/";
+    "/redfish/v1/TelemetryService/MetricReportDefinitions";
 constexpr const char* metricReportUri =
-    "/redfish/v1/TelemetryService/MetricReports/";
-
-inline void
-    getReportCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                        const std::string& uri)
-{
-    const std::array<const char*, 1> interfaces = {reportInterface};
-
-    crow::connections::systemBus->async_method_call(
-        [asyncResp, uri](const boost::system::error_code ec,
-                         const std::vector<std::string>& reports) {
-            if (ec == boost::system::errc::io_error)
-            {
-                asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
-                asyncResp->res.jsonValue["Members@odata.count"] = 0;
-                return;
-            }
-            if (ec)
-            {
-                BMCWEB_LOG_ERROR << "Dbus method call failed: " << ec;
-                messages::internalError(asyncResp->res);
-                return;
-            }
-
-            nlohmann::json& members = asyncResp->res.jsonValue["Members"];
-            members = nlohmann::json::array();
-
-            for (const std::string& report : reports)
-            {
-                sdbusplus::message::object_path path(report);
-                std::string name = path.filename();
-                if (name.empty())
-                {
-                    BMCWEB_LOG_ERROR << "Received invalid path: " << report;
-                    messages::internalError(asyncResp->res);
-                    return;
-                }
-                members.push_back({{"@odata.id", uri + name}});
-            }
-
-            asyncResp->res.jsonValue["Members@odata.count"] = members.size();
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
-        "/xyz/openbmc_project/Telemetry/Reports/TelemetryService", 1,
-        interfaces);
-}
+    "/redfish/v1/TelemetryService/MetricReports";
 
 inline std::string getDbusReportPath(const std::string& id)
 {
diff --git a/redfish-core/lib/metric_report.hpp b/redfish-core/lib/metric_report.hpp
index 63c8c19..159968a 100644
--- a/redfish-core/lib/metric_report.hpp
+++ b/redfish-core/lib/metric_report.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "utils/collection.hpp"
 #include "utils/telemetry_utils.hpp"
 
 #include <app.hpp>
@@ -39,11 +40,12 @@
 {
     asyncResp->res.jsonValue["@odata.type"] =
         "#MetricReport.v1_3_0.MetricReport";
-    asyncResp->res.jsonValue["@odata.id"] = telemetry::metricReportUri + id;
+    asyncResp->res.jsonValue["@odata.id"] =
+        telemetry::metricReportUri + std::string("/") + id;
     asyncResp->res.jsonValue["Id"] = id;
     asyncResp->res.jsonValue["Name"] = id;
     asyncResp->res.jsonValue["MetricReportDefinition"]["@odata.id"] =
-        telemetry::metricReportDefinitionUri + id;
+        telemetry::metricReportDefinitionUri + std::string("/") + id;
 
     const TimestampReadings* timestampReadings =
         std::get_if<TimestampReadings>(&var);
@@ -73,9 +75,11 @@
                 asyncResp->res.jsonValue["@odata.id"] =
                     "/redfish/v1/TelemetryService/MetricReports";
                 asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
-
-                telemetry::getReportCollection(asyncResp,
-                                               telemetry::metricReportUri);
+                const std::vector<const char*> interfaces{
+                    telemetry::reportInterface};
+                collection_util::getCollectionMembers(
+                    asyncResp, telemetry::metricReportUri, interfaces,
+                    "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
             });
 }
 
diff --git a/redfish-core/lib/metric_report_definition.hpp b/redfish-core/lib/metric_report_definition.hpp
index a0c4f1d..fc717e4 100644
--- a/redfish-core/lib/metric_report_definition.hpp
+++ b/redfish-core/lib/metric_report_definition.hpp
@@ -30,11 +30,11 @@
     asyncResp->res.jsonValue["@odata.type"] =
         "#MetricReportDefinition.v1_3_0.MetricReportDefinition";
     asyncResp->res.jsonValue["@odata.id"] =
-        telemetry::metricReportDefinitionUri + id;
+        telemetry::metricReportDefinitionUri + std::string("/") + id;
     asyncResp->res.jsonValue["Id"] = id;
     asyncResp->res.jsonValue["Name"] = id;
     asyncResp->res.jsonValue["MetricReport"]["@odata.id"] =
-        telemetry::metricReportUri + id;
+        telemetry::metricReportUri + std::string("/") + id;
     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
     asyncResp->res.jsonValue["ReportUpdates"] = "Overwrite";
 
@@ -362,9 +362,11 @@
                     "/redfish/v1/TelemetryService/MetricReportDefinitions";
                 asyncResp->res.jsonValue["Name"] =
                     "Metric Definition Collection";
-
-                telemetry::getReportCollection(
-                    asyncResp, telemetry::metricReportDefinitionUri);
+                const std::vector<const char*> interfaces{
+                    telemetry::reportInterface};
+                collection_util::getCollectionMembers(
+                    asyncResp, telemetry::metricReportDefinitionUri, interfaces,
+                    "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
             });
 
     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/")