summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/telemetry/0004-Add-support-for-MetricDefinition-property-in-MetricReport.patch
blob: bf5a09d9d1cc26b5f0033b6e7851a3b45f309153 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
From dab3c96f9e39a89d7c359e22655650c7c16952ec Mon Sep 17 00:00:00 2001
From: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Date: Tue, 12 Oct 2021 08:06:13 +0000
Subject: [PATCH] Add support for MetricDefinition property in MetricReport

Added MetricDefinition as part of MetricValues array returned by
MetricReport. It contains single @odata.id with URI to proper
MetricDefinition resource - depending on MetricProperty.

Testing done:
- GET request on redfish/v1/TelemetryService/MetricReports
  got response with MetricDefinition and proper id inside
  MetricValues array.

Testing steps:
1. POST on redfish/v1/TelemetryService/MetricReportDefinitions
   with body:
{
  "Id": "PeriodicReport_1",
  "MetricReportDefinitionType": "Periodic",
  "ReportActions": [
    "LogToMetricReportsCollection",
    "RedfishEvent"
  ],
  "Metrics": [
    {
      "MetricId": "sensor_1",
      "MetricProperties": [
        "/redfish/v1/Chassis/AC_Baseboard/Thermal#/Fans/1/Reading"
      ]
    }
  ],
  "Schedule": {
    "RecurrenceInterval": "PT10S"
  }
}

2. GET on redfish/v1/TelemetryService/MetricReports/PeriodicReport_1
   should return:
{
  "@odata.id":
  "/redfish/v1/TelemetryService/MetricReports/PeriodicReport_1",
  "@odata.type": "#MetricReport.v1_3_0.MetricReport",
  "Id": "PeriodicReport_1",
  "MetricReportDefinition": {
    "@odata.id":
    "/redfish/v1/TelemetryService/MetricReportDefinitions/PeriodicReport_1"
  },
  "MetricValues": [
    {
      "MetricDefinition": {
        "@odata.id":
        "/redfish/v1/TelemetryService/MetricDefinitions/Rotational"
      },
      "MetricId": "sensor_1",
      "MetricProperty":
      "/redfish/v1/Chassis/AC_Baseboard/Thermal#/Fans/1/Reading",
      "MetricValue": "nan",
      "Timestamp": "1970-01-01T00:03:21+00:00"
    }
  ],
  "Name": "PeriodicReport_1",
  "Timestamp": "1970-01-01T00:03:21+00:00"
}

Change-Id: I7181c612f9b443015d551259bae25303aa436822
Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
---
 meson.build                                   |  4 +-
 .../include/utils/telemetry_utils.hpp         | 40 ++++++++++++
 redfish-core/lib/metric_report.hpp            | 64 +++++++++++++++----
 redfish-core/lib/sensors.hpp                  |  2 +
 4 files changed, 95 insertions(+), 15 deletions(-)

diff --git a/meson.build b/meson.build
index 6b6a8ab..218ea49 100644
--- a/meson.build
+++ b/meson.build
@@ -377,6 +377,8 @@ srcfiles_unittest = [
   'http/ut/utility_test.cpp'
 ]
 
+srcfiles_unittest_dependencies = ['redfish-core/src/error_messages.cpp', 'src/boost_url.cpp']
+
 # Gather the Configuration data
 
 conf_data = configuration_data()
@@ -434,7 +436,7 @@ executable('bmcweb',srcfiles_bmcweb,
 if(get_option('tests').enabled())
   foreach src_test : srcfiles_unittest
     testname = src_test.split('/')[-1].split('.')[0]
-    test(testname,executable(testname,src_test,
+    test(testname,executable(testname,[src_test] + srcfiles_unittest_dependencies,
                 include_directories : incdir,
                 install_dir: bindir,
                 dependencies: [
diff --git a/redfish-core/include/utils/telemetry_utils.hpp b/redfish-core/include/utils/telemetry_utils.hpp
index 1b4f75d..c0c5ba3 100644
--- a/redfish-core/include/utils/telemetry_utils.hpp
+++ b/redfish-core/include/utils/telemetry_utils.hpp
@@ -17,6 +17,46 @@ constexpr const char* metricReportDefinitionUri =
 constexpr const char* metricReportUri =
     "/redfish/v1/TelemetryService/MetricReports/";
 
+inline std::optional<nlohmann::json>
+    getMetadataJson(const std::string& metadataStr)
+{
+    std::optional<nlohmann::json> res =
+        nlohmann::json::parse(metadataStr, nullptr, false);
+    if (res->is_discarded())
+    {
+        BMCWEB_LOG_ERROR << "Malformed reading metatadata JSON provided by "
+                            "telemetry service.";
+        return std::nullopt;
+    }
+    return res;
+}
+
+inline std::optional<std::string>
+    readStringFromMetadata(const nlohmann::json& metadataJson, const char* key)
+{
+    std::optional<std::string> res;
+    if (auto it = metadataJson.find(key); it != metadataJson.end())
+    {
+        if (const std::string* value = it->get_ptr<const std::string*>())
+        {
+            res = *value;
+        }
+        else
+        {
+            BMCWEB_LOG_ERROR << "Incorrect reading metatadata JSON provided by "
+                                "telemetry service. Missing key '"
+                             << key << "'.";
+        }
+    }
+    else
+    {
+        BMCWEB_LOG_ERROR << "Incorrect reading metatadata JSON provided by "
+                            "telemetry service. Key '"
+                         << key << "' has a wrong type.";
+    }
+    return res;
+}
+
 inline void
     getReportCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                         const std::string& uri)
diff --git a/redfish-core/lib/metric_report.hpp b/redfish-core/lib/metric_report.hpp
index 7fe281d..13bf792 100644
--- a/redfish-core/lib/metric_report.hpp
+++ b/redfish-core/lib/metric_report.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "sensors.hpp"
 #include "utils/telemetry_utils.hpp"
 
 #include <app.hpp>
@@ -15,34 +16,56 @@ using Readings =
     std::vector<std::tuple<std::string, std::string, double, uint64_t>>;
 using TimestampReadings = std::tuple<uint64_t, Readings>;
 
-inline nlohmann::json toMetricValues(const Readings& readings)
+inline bool fillMetricValues(nlohmann::json& metricValues,
+                             const Readings& readings)
 {
-    nlohmann::json metricValues = nlohmann::json::array_t();
-
-    for (auto& [id, metadata, sensorValue, timestamp] : readings)
+    for (auto& [id, metadataStr, sensorValue, timestamp] : readings)
     {
+        std::optional<nlohmann::json> readingMetadataJson =
+            getMetadataJson(metadataStr);
+        if (!readingMetadataJson)
+        {
+            return false;
+        }
+
+        std::optional<std::string> sensorDbusPath =
+            readStringFromMetadata(*readingMetadataJson, "SensorDbusPath");
+        if (!sensorDbusPath)
+        {
+            return false;
+        }
+
+        std::optional<std::string> sensorRedfishUri =
+            readStringFromMetadata(*readingMetadataJson, "SensorRedfishUri");
+        if (!sensorRedfishUri)
+        {
+            return false;
+        }
+
+        std::string metricDefinition =
+            std::string(metricDefinitionUri) +
+            sensors::toReadingType(
+                sdbusplus::message::object_path(*sensorDbusPath)
+                    .parent_path()
+                    .filename());
+
         metricValues.push_back({
+            {"MetricDefinition",
+             nlohmann::json{{"@odata.id", metricDefinition}}},
             {"MetricId", id},
-            {"MetricProperty", metadata},
+            {"MetricProperty", *sensorRedfishUri},
             {"MetricValue", std::to_string(sensorValue)},
             {"Timestamp",
              crow::utility::getDateTime(static_cast<time_t>(timestamp))},
         });
     }
 
-    return metricValues;
+    return true;
 }
 
 inline bool fillReport(nlohmann::json& json, const std::string& id,
                        const std::variant<TimestampReadings>& var)
 {
-    json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
-    json["@odata.id"] = telemetry::metricReportUri + id;
-    json["Id"] = id;
-    json["Name"] = id;
-    json["MetricReportDefinition"]["@odata.id"] =
-        telemetry::metricReportDefinitionUri + id;
-
     const TimestampReadings* timestampReadings =
         std::get_if<TimestampReadings>(&var);
     if (!timestampReadings)
@@ -52,9 +75,22 @@ inline bool fillReport(nlohmann::json& json, const std::string& id,
     }
 
     const auto& [timestamp, readings] = *timestampReadings;
+    nlohmann::json metricValues = nlohmann::json::array();
+    if (!fillMetricValues(metricValues, readings))
+    {
+        return false;
+    }
+
+    json["@odata.type"] = "#MetricReport.v1_3_0.MetricReport";
+    json["@odata.id"] = telemetry::metricReportUri + id;
+    json["Id"] = id;
+    json["Name"] = id;
+    json["MetricReportDefinition"]["@odata.id"] =
+        telemetry::metricReportDefinitionUri + id;
     json["Timestamp"] =
         crow::utility::getDateTime(static_cast<time_t>(timestamp));
-    json["MetricValues"] = toMetricValues(readings);
+    json["MetricValues"] = metricValues;
+
     return true;
 }
 } // namespace telemetry
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 7405e5a..9850b24 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -21,6 +21,8 @@
 #include <boost/container/flat_map.hpp>
 #include <boost/range/algorithm/replace_copy_if.hpp>
 #include <dbus_singleton.hpp>
+#include <dbus_utility.hpp>
+#include <error_messages.hpp>
 #include <registries/privilege_registry.hpp>
 #include <utils/json_utils.hpp>
 
-- 
2.25.1