1 #pragma once
2 
3 #include "dbus_utility.hpp"
4 #include "http/utility.hpp"
5 #include "logging.hpp"
6 #include "utility.hpp"
7 
8 #include <boost/container/flat_map.hpp>
9 #include <boost/container/flat_set.hpp>
10 #include <sdbusplus/message/native_types.hpp>
11 
12 #include <string>
13 
14 namespace redfish
15 {
16 
17 namespace telemetry
18 {
19 constexpr const char* service = "xyz.openbmc_project.Telemetry";
20 constexpr const char* reportInterface = "xyz.openbmc_project.Telemetry.Report";
21 
22 inline std::string getDbusReportPath(const std::string& id)
23 {
24     sdbusplus::message::object_path reportsPath(
25         "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
26     return {reportsPath / id};
27 }
28 
29 inline std::string getDbusTriggerPath(const std::string& id)
30 {
31     sdbusplus::message::object_path triggersPath(
32         "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService");
33     return {triggersPath / id};
34 }
35 
36 struct IncorrectMetricUri
37 {
38     std::string uri;
39     size_t index;
40 };
41 
42 inline std::optional<IncorrectMetricUri> getChassisSensorNode(
43     const std::vector<std::string>& uris,
44     boost::container::flat_set<std::pair<std::string, std::string>>& matched)
45 {
46     size_t uriIdx = 0;
47     for (const std::string& uri : uris)
48     {
49         boost::urls::result<boost::urls::url_view> parsed =
50             boost::urls::parse_relative_ref(uri);
51 
52         if (!parsed)
53         {
54             BMCWEB_LOG_ERROR << "Failed to get chassis and sensor Node "
55                                 "from "
56                              << uri;
57             return std::make_optional<IncorrectMetricUri>({uri, uriIdx});
58         }
59 
60         std::string chassis;
61         std::string node;
62 
63         if (crow::utility::readUrlSegments(*parsed, "redfish", "v1", "Chassis",
64                                            std::ref(chassis), std::ref(node)))
65         {
66             matched.emplace(std::move(chassis), std::move(node));
67             uriIdx++;
68             continue;
69         }
70 
71         // Those 2 segments cannot be validated here, as we don't know which
72         // sensors exist at the moment of parsing.
73         std::string ignoredSenorId;
74 
75         if (crow::utility::readUrlSegments(*parsed, "redfish", "v1", "Chassis",
76                                            std::ref(chassis), "Sensors",
77                                            std::ref(ignoredSenorId)))
78         {
79             matched.emplace(std::move(chassis), "Sensors");
80             uriIdx++;
81             continue;
82         }
83 
84         BMCWEB_LOG_ERROR << "Failed to get chassis and sensor Node "
85                             "from "
86                          << uri;
87         return std::make_optional<IncorrectMetricUri>({uri, uriIdx});
88     }
89     return std::nullopt;
90 }
91 
92 } // namespace telemetry
93 } // namespace redfish
94