blob: a80d03bd37dcab9e7482658ced1e7b149ed6d602 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
James Feist46229572020-02-19 15:11:58 -08004#pragma once
5
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08006#include "app.hpp"
7#include "dbus_utility.hpp"
8#include "event_service_manager.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -07009#include "generated/enums/resource.hpp"
10#include "generated/enums/task_service.hpp"
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010011#include "http/parsing.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "query.hpp"
13#include "registries/privilege_registry.hpp"
14#include "task_messages.hpp"
15
Ed Tanousd43cd0c2020-09-30 20:46:53 -070016#include <boost/asio/post.hpp>
17#include <boost/asio/steady_timer.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070018#include <boost/url/format.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include <sdbusplus/bus/match.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050020
21#include <chrono>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070023#include <ranges>
James Feist46229572020-02-19 15:11:58 -080024#include <variant>
25
26namespace redfish
27{
28
29namespace task
30{
31constexpr size_t maxTaskCount = 100; // arbitrary limit
32
Ed Tanouscf9e4172022-12-21 09:30:16 -080033// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
James Feist46229572020-02-19 15:11:58 -080034static std::deque<std::shared_ptr<struct TaskData>> tasks;
35
James Feist32898ce2020-03-10 16:16:52 -070036constexpr bool completed = true;
37
James Feistfe306722020-03-12 16:32:08 -070038struct Payload
39{
Ed Tanous4e23a442022-06-06 09:57:26 -070040 explicit Payload(const crow::Request& req) :
Ed Tanous39662a32023-02-06 15:09:46 -080041 targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010042 httpHeaders(nlohmann::json::array())
James Feistfe306722020-03-12 16:32:08 -070043 {
44 using field_ns = boost::beast::http::field;
45 constexpr const std::array<boost::beast::http::field, 7>
46 headerWhitelist = {field_ns::accept, field_ns::accept_encoding,
47 field_ns::user_agent, field_ns::host,
48 field_ns::connection, field_ns::content_length,
49 field_ns::upgrade};
50
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010051 JsonParseResult ret = parseRequestAsJson(req, jsonBody);
52 if (ret != JsonParseResult::Success)
James Feistfe306722020-03-12 16:32:08 -070053 {
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010054 return;
James Feistfe306722020-03-12 16:32:08 -070055 }
56
Ed Tanous98fe7402023-02-14 14:50:33 -080057 for (const auto& field : req.fields())
James Feistfe306722020-03-12 16:32:08 -070058 {
Ed Tanous3544d2a2023-08-06 18:12:20 -070059 if (std::ranges::find(headerWhitelist, field.name()) ==
60 headerWhitelist.end())
James Feistfe306722020-03-12 16:32:08 -070061 {
62 continue;
63 }
64 std::string header;
Patrick Williamsbd79bce2024-08-16 15:22:20 -040065 header.reserve(
66 field.name_string().size() + 2 + field.value().size());
James Feistfe306722020-03-12 16:32:08 -070067 header += field.name_string();
68 header += ": ";
69 header += field.value();
70 httpHeaders.emplace_back(std::move(header));
71 }
72 }
73 Payload() = delete;
74
75 std::string targetUri;
76 std::string httpOperation;
77 nlohmann::json httpHeaders;
78 nlohmann::json jsonBody;
79};
80
James Feist46229572020-02-19 15:11:58 -080081struct TaskData : std::enable_shared_from_this<TaskData>
82{
83 private:
Patrick Williams59d494e2022-07-22 19:26:55 -050084 TaskData(
85 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
86 const std::shared_ptr<TaskData>&)>&& handler,
87 const std::string& matchIn, size_t idx) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -040088 callback(std::move(handler)), matchStr(matchIn), index(idx),
James Feist46229572020-02-19 15:11:58 -080089 startTime(std::chrono::system_clock::to_time_t(
90 std::chrono::system_clock::now())),
91 status("OK"), state("Running"), messages(nlohmann::json::array()),
92 timer(crow::connections::systemBus->get_io_context())
93
Gunnar Mills1214b7e2020-06-04 10:11:30 -050094 {}
James Feist46229572020-02-19 15:11:58 -080095
96 public:
Ed Tanousd609fd62020-09-28 19:08:03 -070097 TaskData() = delete;
98
Gunnar Mills1214b7e2020-06-04 10:11:30 -050099 static std::shared_ptr<TaskData>& createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -0500100 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500101 const std::shared_ptr<TaskData>&)>&& handler,
102 const std::string& match)
James Feist46229572020-02-19 15:11:58 -0800103 {
104 static size_t lastTask = 0;
105 struct MakeSharedHelper : public TaskData
106 {
107 MakeSharedHelper(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500108 std::function<bool(boost::system::error_code,
Patrick Williams59d494e2022-07-22 19:26:55 -0500109 sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500110 const std::shared_ptr<TaskData>&)>&& handler,
Ed Tanous23a21a12020-07-25 04:45:05 +0000111 const std::string& match2, size_t idx) :
112 TaskData(std::move(handler), match2, idx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113 {}
James Feist46229572020-02-19 15:11:58 -0800114 };
115
116 if (tasks.size() >= maxTaskCount)
117 {
Ed Tanous02cad962022-06-30 16:50:15 -0700118 const auto& last = tasks.front();
James Feist46229572020-02-19 15:11:58 -0800119
120 // destroy all references
121 last->timer.cancel();
122 last->match.reset();
123 tasks.pop_front();
124 }
125
126 return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
127 std::move(handler), match, lastTask++));
128 }
129
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500130 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800131 {
132 if (!endTime)
133 {
134 res.result(boost::beast::http::status::accepted);
135 std::string strIdx = std::to_string(index);
Ed Tanousfdbce792024-06-26 14:48:46 -0700136 boost::urls::url uri =
137 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx);
Ed Tanous14766872022-03-15 10:44:42 -0700138
139 res.jsonValue["@odata.id"] = uri;
140 res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
141 res.jsonValue["Id"] = strIdx;
142 res.jsonValue["TaskState"] = state;
143 res.jsonValue["TaskStatus"] = status;
144
Ed Tanousfdbce792024-06-26 14:48:46 -0700145 boost::urls::url taskMonitor = boost::urls::format(
146 "/redfish/v1/TaskService/TaskMonitors/{}", strIdx);
147
James Feist46229572020-02-19 15:11:58 -0800148 res.addHeader(boost::beast::http::field::location,
Ed Tanousfdbce792024-06-26 14:48:46 -0700149 taskMonitor.buffer());
James Feist46229572020-02-19 15:11:58 -0800150 res.addHeader(boost::beast::http::field::retry_after,
151 std::to_string(retryAfterSeconds));
152 }
153 else if (!gave204)
154 {
155 res.result(boost::beast::http::status::no_content);
156 gave204 = true;
157 }
158 }
159
Ed Tanousd609fd62020-09-28 19:08:03 -0700160 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800161 {
162 endTime = std::chrono::system_clock::to_time_t(
163 std::chrono::system_clock::now());
164 }
165
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500166 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800167 {
James Feist46229572020-02-19 15:11:58 -0800168 timer.expires_after(timeout);
169 timer.async_wait(
170 [self = shared_from_this()](boost::system::error_code ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400171 if (ec == boost::asio::error::operation_aborted)
172 {
173 return; // completed successfully
174 }
175 if (!ec)
176 {
177 // change ec to error as timer expired
178 ec = boost::asio::error::operation_aborted;
179 }
180 self->match.reset();
181 sdbusplus::message_t msg;
182 self->finishTask();
183 self->state = "Cancelled";
184 self->status = "Warning";
185 self->messages.emplace_back(
186 messages::taskAborted(std::to_string(self->index)));
187 // Send event :TaskAborted
Ed Tanousdaadfb22024-12-20 09:25:54 -0800188 sendTaskEvent(self->state, self->index);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400189 self->callback(ec, msg, self);
190 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700191 }
192
Ed Tanous26ccae32023-02-16 10:28:44 -0800193 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500194 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500195 // TaskState enums which should send out an event are:
196 // "Starting" = taskResumed
197 // "Running" = taskStarted
198 // "Suspended" = taskPaused
199 // "Interrupted" = taskPaused
200 // "Pending" = taskPaused
201 // "Stopping" = taskAborted
202 // "Completed" = taskCompletedOK
203 // "Killed" = taskRemoved
204 // "Exception" = taskCompletedWarning
205 // "Cancelled" = taskCancelled
Ed Tanousf8fe2212024-06-16 14:51:23 -0700206 nlohmann::json event;
207 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500208 if (state == "Starting")
209 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700210 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500211 }
212 else if (state == "Running")
213 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700214 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500215 }
216 else if ((state == "Suspended") || (state == "Interrupted") ||
217 (state == "Pending"))
218 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700219 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500220 }
221 else if (state == "Stopping")
222 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700223 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500224 }
225 else if (state == "Completed")
226 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700227 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500228 }
229 else if (state == "Killed")
230 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700231 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500232 }
233 else if (state == "Exception")
234 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700235 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500236 }
237 else if (state == "Cancelled")
238 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700239 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500240 }
241 else
242 {
Ed Tanous62598e32023-07-17 17:06:25 -0700243 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700244 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500245 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700246 boost::urls::url origin =
247 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
248 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
249 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500250 }
251
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700253 {
254 if (match)
255 {
256 return;
257 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500258 match = std::make_unique<sdbusplus::bus::match_t>(
259 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700260 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500261 [self = shared_from_this()](sdbusplus::message_t& message) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400262 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700263
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400264 // callback to return True if callback is done, callback needs
265 // to update status itself if needed
266 if (self->callback(ec, message, self) == task::completed)
267 {
268 self->timer.cancel();
269 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700270
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400271 // Send event
Ed Tanousdaadfb22024-12-20 09:25:54 -0800272 sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500273
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400274 // reset the match after the callback was successful
275 boost::asio::post(
276 crow::connections::systemBus->get_io_context(),
277 [self] { self->match.reset(); });
278 return;
279 }
280 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700281
282 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700283 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500284 // Send event : TaskStarted
285 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800286 }
287
Patrick Williams59d494e2022-07-22 19:26:55 -0500288 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500289 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800290 callback;
291 std::string matchStr;
292 size_t index;
293 time_t startTime;
294 std::string status;
295 std::string state;
296 nlohmann::json messages;
297 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500298 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800299 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700300 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800301 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800302 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800303};
304
305} // namespace task
306
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700307inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800308{
Ed Tanousfdbce792024-06-26 14:48:46 -0700309 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700310 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700311 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700312 [&app](const crow::Request& req,
313 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
314 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400315 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
316 {
317 return;
318 }
319 auto find = std::ranges::find_if(
320 task::tasks,
321 [&strParam](const std::shared_ptr<task::TaskData>& task) {
322 if (!task)
323 {
324 return false;
325 }
James Feist46229572020-02-19 15:11:58 -0800326
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400327 // we compare against the string version as on failure
328 // strtoul returns 0
329 return std::to_string(task->index) == strParam;
330 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700331
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400332 if (find == task::tasks.end())
333 {
334 messages::resourceNotFound(asyncResp->res, "Task",
335 strParam);
336 return;
337 }
338 std::shared_ptr<task::TaskData>& ptr = *find;
339 // monitor expires after 204
340 if (ptr->gave204)
341 {
342 messages::resourceNotFound(asyncResp->res, "Task",
343 strParam);
344 return;
345 }
346 ptr->populateResp(asyncResp->res);
347 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700348}
349
350inline void requestRoutesTask(App& app)
351{
352 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700353 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700354 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700355 [&app](const crow::Request& req,
356 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400358 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
359 {
360 return;
361 }
362 auto find = std::ranges::find_if(
363 task::tasks,
364 [&strParam](const std::shared_ptr<task::TaskData>& task) {
365 if (!task)
366 {
367 return false;
368 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700369
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400370 // we compare against the string version as on failure
371 // strtoul returns 0
372 return std::to_string(task->index) == strParam;
373 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700374
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400375 if (find == task::tasks.end())
376 {
377 messages::resourceNotFound(asyncResp->res, "Task",
378 strParam);
379 return;
380 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700381
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400382 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700383
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400384 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
385 asyncResp->res.jsonValue["Id"] = strParam;
386 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
387 asyncResp->res.jsonValue["TaskState"] = ptr->state;
388 asyncResp->res.jsonValue["StartTime"] =
389 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
390 if (ptr->endTime)
391 {
392 asyncResp->res.jsonValue["EndTime"] =
393 redfish::time_utils::getDateTimeStdtime(
394 *(ptr->endTime));
395 }
396 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
397 asyncResp->res.jsonValue["Messages"] = ptr->messages;
398 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
399 "/redfish/v1/TaskService/Tasks/{}", strParam);
400 if (!ptr->gave204)
401 {
402 asyncResp->res.jsonValue["TaskMonitor"] =
403 boost::urls::format(
404 "/redfish/v1/TaskService/TaskMonitors/{}",
405 strParam);
406 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700407
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400408 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700409
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400410 if (ptr->payload)
411 {
412 const task::Payload& p = *(ptr->payload);
413 asyncResp->res.jsonValue["Payload"]["TargetUri"] =
414 p.targetUri;
415 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
416 p.httpOperation;
417 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] =
418 p.httpHeaders;
419 asyncResp->res.jsonValue["Payload"]["JsonBody"] =
420 p.jsonBody.dump(
421 -1, ' ', true,
422 nlohmann::json::error_handler_t::replace);
423 }
424 asyncResp->res.jsonValue["PercentComplete"] =
425 ptr->percentComplete;
426 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700427}
James Feist46229572020-02-19 15:11:58 -0800428
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700429inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800430{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700431 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700432 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700433 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700434 [&app](const crow::Request& req,
435 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
437 {
438 return;
439 }
440 asyncResp->res.jsonValue["@odata.type"] =
441 "#TaskCollection.TaskCollection";
442 asyncResp->res.jsonValue["@odata.id"] =
443 "/redfish/v1/TaskService/Tasks";
444 asyncResp->res.jsonValue["Name"] = "Task Collection";
445 asyncResp->res.jsonValue["Members@odata.count"] =
446 task::tasks.size();
447 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
448 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800449
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400450 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
451 {
452 if (task == nullptr)
453 {
454 continue; // shouldn't be possible
455 }
456 nlohmann::json::object_t member;
457 member["@odata.id"] =
458 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
459 std::to_string(task->index));
460 members.emplace_back(std::move(member));
461 }
462 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700463}
zhanghch058d1b46d2021-04-01 11:18:24 +0800464
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700465inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800466{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700467 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700468 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700469 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700470 [&app](const crow::Request& req,
471 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400472 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
473 {
474 return;
475 }
476 asyncResp->res.jsonValue["@odata.type"] =
477 "#TaskService.v1_1_4.TaskService";
478 asyncResp->res.jsonValue["@odata.id"] =
479 "/redfish/v1/TaskService";
480 asyncResp->res.jsonValue["Name"] = "Task Service";
481 asyncResp->res.jsonValue["Id"] = "TaskService";
482 asyncResp->res.jsonValue["DateTime"] =
483 redfish::time_utils::getDateTimeOffsetNow().first;
484 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
485 task_service::OverWritePolicy::Oldest;
James Feist46229572020-02-19 15:11:58 -0800486
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400487 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
488 true;
James Feist46229572020-02-19 15:11:58 -0800489
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400490 asyncResp->res.jsonValue["Status"]["State"] =
491 resource::State::Enabled;
492 asyncResp->res.jsonValue["ServiceEnabled"] = true;
493 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
494 "/redfish/v1/TaskService/Tasks";
495 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700496}
James Feist46229572020-02-19 15:11:58 -0800497
498} // namespace redfish