summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/telemetry/0003-Fix-Trigger-GET-functionality.patch
blob: f741142c41f2d656a0e3f62e0e36d99c6add0b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
From cc10d221a17e1136bf3ec7f62583a4ca44212adc Mon Sep 17 00:00:00 2001
From: Szymon Dompke <szymon.dompke@intel.com>
Date: Tue, 22 Feb 2022 13:58:00 +0100
Subject: [PATCH] Fix Trigger GET functionality

This change is fixing 2 issues related to GET on Trigger schema:
- Links to MetricReportDefintions were not parsed properly. Dbus is
  returning collection of strings prefixed with "TelemetryService/".
  This prefix was supposed to be removed.
- In case of error occurring during internal data parsing, GET could
  return both "Internal Error" message and incomplete Trigger
  representation. By swapping few lines of code, this issue is fixed:
  now either error is returned or full Trigger representation, but never
  both of them.

Testing done:
- Links are now returning proper uris of existing MRD.
- Internal Error is returned for malformed data returned by service.
- Other Trigger GET functionality remained unchanged.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I81ebbf3889a152199bef7230de56a73bb112731b
---
 redfish-core/lib/trigger.hpp | 65 ++++++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 25 deletions(-)

diff --git a/redfish-core/lib/trigger.hpp b/redfish-core/lib/trigger.hpp
index da6a5db..5e0897e 100644
--- a/redfish-core/lib/trigger.hpp
+++ b/redfish-core/lib/trigger.hpp
@@ -137,20 +137,29 @@ inline std::optional<nlohmann::json>
     return std::make_optional(thresholds);
 }
 
-inline nlohmann::json
+inline std::optional<nlohmann::json>
     getMetricReportDefinitions(const std::vector<std::string>& reportNames)
 {
     nlohmann::json reports = nlohmann::json::array();
+
     for (const std::string& name : reportNames)
     {
-        reports.push_back(
-            {{"@odata.id",
-              crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
-                                           "MetricReportDefinitions", name)
-                  .string()}});
+        sdbusplus::message::object_path path(name);
+        if (path.parent_path() != "TelemetryService")
+        {
+            BMCWEB_LOG_ERROR << "Property ReportNames contains invalid value: "
+                             << name;
+            return std::nullopt;
+        }
+        reports.push_back({
+            {"@odata.id", crow::utility::urlFromPieces(
+                              "redfish", "v1", "TelemetryService",
+                              "MetricReportDefinitions", path.filename())
+                              .string()},
+        });
     }
 
-    return reports;
+    return std::make_optional(reports);
 }
 
 inline std::vector<std::string>
@@ -215,12 +224,23 @@ inline bool fillTrigger(
         return false;
     }
 
-    json["@odata.type"] = "#Triggers.v1_2_0.Triggers";
-    json["@odata.id"] = crow::utility::urlFromPieces(
-                            "redfish", "v1", "TelemetryService", "Triggers", id)
-                            .string();
-    json["Id"] = id;
-    json["Name"] = *name;
+    std::optional<std::vector<std::string>> triggerActions =
+        getTriggerActions(*actions);
+    if (!triggerActions)
+    {
+        BMCWEB_LOG_ERROR << "Property TriggerActions is invalid in Trigger: "
+                         << id;
+        return false;
+    }
+
+    std::optional<nlohmann::json> linkedReports =
+        getMetricReportDefinitions(*reports);
+    if (!linkedReports)
+    {
+        BMCWEB_LOG_ERROR << "Property ReportNames is invalid in Trigger: "
+                         << id;
+        return false;
+    }
 
     if (*discrete)
     {
@@ -257,20 +277,15 @@ inline bool fillTrigger(
         json["MetricType"] = "Numeric";
     }
 
-    std::optional<std::vector<std::string>> triggerActions =
-        getTriggerActions(*actions);
-
-    if (!triggerActions)
-    {
-        BMCWEB_LOG_ERROR << "Property TriggerActions is invalid in Trigger: "
-                         << id;
-        return false;
-    }
-
+    json["@odata.type"] = "#Triggers.v1_2_0.Triggers";
+    json["@odata.id"] = crow::utility::urlFromPieces(
+                            "redfish", "v1", "TelemetryService", "Triggers", id)
+                            .string();
+    json["Id"] = id;
+    json["Name"] = *name;
     json["TriggerActions"] = *triggerActions;
     json["MetricProperties"] = getMetricProperties(*sensors);
-    json["Links"]["MetricReportDefinitions"] =
-        getMetricReportDefinitions(*reports);
+    json["Links"]["MetricReportDefinitions"] = *linkedReports;
 
     return true;
 }
-- 
2.25.1