blob: 40aaffd4e363237eadf59c80cbb2d55a073fec3e [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"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "async_resp.hpp"
8#include "dbus_singleton.hpp"
9#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010#include "event_service_manager.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070011#include "generated/enums/resource.hpp"
12#include "generated/enums/task_service.hpp"
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010013#include "http/parsing.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080014#include "http_request.hpp"
15#include "http_response.hpp"
16#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080017#include "query.hpp"
18#include "registries/privilege_registry.hpp"
19#include "task_messages.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080020#include "utils/time_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <boost/asio/error.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070023#include <boost/asio/post.hpp>
24#include <boost/asio/steady_timer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <boost/beast/http/field.hpp>
26#include <boost/beast/http/status.hpp>
27#include <boost/beast/http/verb.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070028#include <boost/url/format.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080029#include <boost/url/url.hpp>
30#include <nlohmann/json.hpp>
31#include <sdbusplus/bus.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080032#include <sdbusplus/bus/match.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080033#include <sdbusplus/message.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034
Ed Tanousd7857202025-01-28 15:32:26 -080035#include <algorithm>
36#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037#include <chrono>
Ed Tanousd7857202025-01-28 15:32:26 -080038#include <cstddef>
39#include <ctime>
40#include <deque>
41#include <functional>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080042#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080043#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070044#include <ranges>
Ed Tanousd7857202025-01-28 15:32:26 -080045#include <string>
46#include <string_view>
47#include <utility>
James Feist46229572020-02-19 15:11:58 -080048
49namespace redfish
50{
51
52namespace task
53{
54constexpr size_t maxTaskCount = 100; // arbitrary limit
55
Ed Tanouscf9e4172022-12-21 09:30:16 -080056// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
James Feist46229572020-02-19 15:11:58 -080057static std::deque<std::shared_ptr<struct TaskData>> tasks;
58
James Feist32898ce2020-03-10 16:16:52 -070059constexpr bool completed = true;
60
James Feistfe306722020-03-12 16:32:08 -070061struct Payload
62{
Ed Tanous4e23a442022-06-06 09:57:26 -070063 explicit Payload(const crow::Request& req) :
Ed Tanous39662a32023-02-06 15:09:46 -080064 targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010065 httpHeaders(nlohmann::json::array())
James Feistfe306722020-03-12 16:32:08 -070066 {
67 using field_ns = boost::beast::http::field;
68 constexpr const std::array<boost::beast::http::field, 7>
69 headerWhitelist = {field_ns::accept, field_ns::accept_encoding,
70 field_ns::user_agent, field_ns::host,
71 field_ns::connection, field_ns::content_length,
72 field_ns::upgrade};
73
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010074 JsonParseResult ret = parseRequestAsJson(req, jsonBody);
75 if (ret != JsonParseResult::Success)
James Feistfe306722020-03-12 16:32:08 -070076 {
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010077 return;
James Feistfe306722020-03-12 16:32:08 -070078 }
79
Ed Tanous98fe7402023-02-14 14:50:33 -080080 for (const auto& field : req.fields())
James Feistfe306722020-03-12 16:32:08 -070081 {
Ed Tanous3544d2a2023-08-06 18:12:20 -070082 if (std::ranges::find(headerWhitelist, field.name()) ==
83 headerWhitelist.end())
James Feistfe306722020-03-12 16:32:08 -070084 {
85 continue;
86 }
87 std::string header;
Patrick Williamsbd79bce2024-08-16 15:22:20 -040088 header.reserve(
89 field.name_string().size() + 2 + field.value().size());
James Feistfe306722020-03-12 16:32:08 -070090 header += field.name_string();
91 header += ": ";
92 header += field.value();
93 httpHeaders.emplace_back(std::move(header));
94 }
95 }
96 Payload() = delete;
97
98 std::string targetUri;
99 std::string httpOperation;
100 nlohmann::json httpHeaders;
101 nlohmann::json jsonBody;
102};
103
James Feist46229572020-02-19 15:11:58 -0800104struct TaskData : std::enable_shared_from_this<TaskData>
105{
106 private:
Patrick Williams59d494e2022-07-22 19:26:55 -0500107 TaskData(
108 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
109 const std::shared_ptr<TaskData>&)>&& handler,
110 const std::string& matchIn, size_t idx) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400111 callback(std::move(handler)), matchStr(matchIn), index(idx),
James Feist46229572020-02-19 15:11:58 -0800112 startTime(std::chrono::system_clock::to_time_t(
113 std::chrono::system_clock::now())),
114 status("OK"), state("Running"), messages(nlohmann::json::array()),
115 timer(crow::connections::systemBus->get_io_context())
116
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500117 {}
James Feist46229572020-02-19 15:11:58 -0800118
119 public:
Ed Tanousd609fd62020-09-28 19:08:03 -0700120 TaskData() = delete;
121
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122 static std::shared_ptr<TaskData>& createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -0500123 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500124 const std::shared_ptr<TaskData>&)>&& handler,
125 const std::string& match)
James Feist46229572020-02-19 15:11:58 -0800126 {
127 static size_t lastTask = 0;
128 struct MakeSharedHelper : public TaskData
129 {
130 MakeSharedHelper(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500131 std::function<bool(boost::system::error_code,
Patrick Williams59d494e2022-07-22 19:26:55 -0500132 sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500133 const std::shared_ptr<TaskData>&)>&& handler,
Ed Tanous23a21a12020-07-25 04:45:05 +0000134 const std::string& match2, size_t idx) :
135 TaskData(std::move(handler), match2, idx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500136 {}
James Feist46229572020-02-19 15:11:58 -0800137 };
138
139 if (tasks.size() >= maxTaskCount)
140 {
Ed Tanous02cad962022-06-30 16:50:15 -0700141 const auto& last = tasks.front();
James Feist46229572020-02-19 15:11:58 -0800142
143 // destroy all references
144 last->timer.cancel();
145 last->match.reset();
146 tasks.pop_front();
147 }
148
149 return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
150 std::move(handler), match, lastTask++));
151 }
152
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500153 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800154 {
155 if (!endTime)
156 {
157 res.result(boost::beast::http::status::accepted);
158 std::string strIdx = std::to_string(index);
Ed Tanousfdbce792024-06-26 14:48:46 -0700159 boost::urls::url uri =
160 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx);
Ed Tanous14766872022-03-15 10:44:42 -0700161
162 res.jsonValue["@odata.id"] = uri;
163 res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
164 res.jsonValue["Id"] = strIdx;
165 res.jsonValue["TaskState"] = state;
166 res.jsonValue["TaskStatus"] = status;
167
Ed Tanousfdbce792024-06-26 14:48:46 -0700168 boost::urls::url taskMonitor = boost::urls::format(
169 "/redfish/v1/TaskService/TaskMonitors/{}", strIdx);
170
James Feist46229572020-02-19 15:11:58 -0800171 res.addHeader(boost::beast::http::field::location,
Ed Tanousfdbce792024-06-26 14:48:46 -0700172 taskMonitor.buffer());
James Feist46229572020-02-19 15:11:58 -0800173 res.addHeader(boost::beast::http::field::retry_after,
174 std::to_string(retryAfterSeconds));
175 }
176 else if (!gave204)
177 {
178 res.result(boost::beast::http::status::no_content);
179 gave204 = true;
180 }
181 }
182
Ed Tanousd609fd62020-09-28 19:08:03 -0700183 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800184 {
185 endTime = std::chrono::system_clock::to_time_t(
186 std::chrono::system_clock::now());
187 }
188
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500189 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800190 {
James Feist46229572020-02-19 15:11:58 -0800191 timer.expires_after(timeout);
192 timer.async_wait(
193 [self = shared_from_this()](boost::system::error_code ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400194 if (ec == boost::asio::error::operation_aborted)
195 {
196 return; // completed successfully
197 }
198 if (!ec)
199 {
200 // change ec to error as timer expired
201 ec = boost::asio::error::operation_aborted;
202 }
203 self->match.reset();
204 sdbusplus::message_t msg;
205 self->finishTask();
206 self->state = "Cancelled";
207 self->status = "Warning";
208 self->messages.emplace_back(
209 messages::taskAborted(std::to_string(self->index)));
210 // Send event :TaskAborted
Ed Tanousdaadfb22024-12-20 09:25:54 -0800211 sendTaskEvent(self->state, self->index);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400212 self->callback(ec, msg, self);
213 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700214 }
215
Ed Tanous26ccae32023-02-16 10:28:44 -0800216 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500217 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500218 // TaskState enums which should send out an event are:
219 // "Starting" = taskResumed
220 // "Running" = taskStarted
221 // "Suspended" = taskPaused
222 // "Interrupted" = taskPaused
223 // "Pending" = taskPaused
224 // "Stopping" = taskAborted
225 // "Completed" = taskCompletedOK
226 // "Killed" = taskRemoved
227 // "Exception" = taskCompletedWarning
228 // "Cancelled" = taskCancelled
Ed Tanousf8fe2212024-06-16 14:51:23 -0700229 nlohmann::json event;
230 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500231 if (state == "Starting")
232 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700233 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500234 }
235 else if (state == "Running")
236 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700237 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500238 }
239 else if ((state == "Suspended") || (state == "Interrupted") ||
240 (state == "Pending"))
241 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700242 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500243 }
244 else if (state == "Stopping")
245 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700246 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500247 }
248 else if (state == "Completed")
249 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700250 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500251 }
252 else if (state == "Killed")
253 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700254 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500255 }
256 else if (state == "Exception")
257 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700258 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500259 }
260 else if (state == "Cancelled")
261 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700262 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500263 }
264 else
265 {
Ed Tanous62598e32023-07-17 17:06:25 -0700266 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700267 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500268 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700269 boost::urls::url origin =
270 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
271 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
272 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500273 }
274
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500275 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700276 {
277 if (match)
278 {
279 return;
280 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500281 match = std::make_unique<sdbusplus::bus::match_t>(
282 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700283 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500284 [self = shared_from_this()](sdbusplus::message_t& message) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400285 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700286
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400287 // callback to return True if callback is done, callback needs
288 // to update status itself if needed
289 if (self->callback(ec, message, self) == task::completed)
290 {
291 self->timer.cancel();
292 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700293
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400294 // Send event
Ed Tanousdaadfb22024-12-20 09:25:54 -0800295 sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500296
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400297 // reset the match after the callback was successful
298 boost::asio::post(
299 crow::connections::systemBus->get_io_context(),
300 [self] { self->match.reset(); });
301 return;
302 }
303 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700304
305 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700306 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500307 // Send event : TaskStarted
308 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800309 }
310
Patrick Williams59d494e2022-07-22 19:26:55 -0500311 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500312 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800313 callback;
314 std::string matchStr;
315 size_t index;
316 time_t startTime;
317 std::string status;
318 std::string state;
319 nlohmann::json messages;
320 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500321 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800322 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700323 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800324 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800325 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800326};
327
328} // namespace task
329
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700330inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800331{
Ed Tanousfdbce792024-06-26 14:48:46 -0700332 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700333 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700334 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700335 [&app](const crow::Request& req,
336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
337 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400338 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
339 {
340 return;
341 }
342 auto find = std::ranges::find_if(
343 task::tasks,
344 [&strParam](const std::shared_ptr<task::TaskData>& task) {
345 if (!task)
346 {
347 return false;
348 }
James Feist46229572020-02-19 15:11:58 -0800349
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400350 // we compare against the string version as on failure
351 // strtoul returns 0
352 return std::to_string(task->index) == strParam;
353 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700354
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400355 if (find == task::tasks.end())
356 {
357 messages::resourceNotFound(asyncResp->res, "Task",
358 strParam);
359 return;
360 }
361 std::shared_ptr<task::TaskData>& ptr = *find;
362 // monitor expires after 204
363 if (ptr->gave204)
364 {
365 messages::resourceNotFound(asyncResp->res, "Task",
366 strParam);
367 return;
368 }
369 ptr->populateResp(asyncResp->res);
370 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700371}
372
373inline void requestRoutesTask(App& app)
374{
375 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700376 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700377 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700378 [&app](const crow::Request& req,
379 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
380 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400381 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
382 {
383 return;
384 }
385 auto find = std::ranges::find_if(
386 task::tasks,
387 [&strParam](const std::shared_ptr<task::TaskData>& task) {
388 if (!task)
389 {
390 return false;
391 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700392
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400393 // we compare against the string version as on failure
394 // strtoul returns 0
395 return std::to_string(task->index) == strParam;
396 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700397
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400398 if (find == task::tasks.end())
399 {
400 messages::resourceNotFound(asyncResp->res, "Task",
401 strParam);
402 return;
403 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700404
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400405 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700406
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400407 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
408 asyncResp->res.jsonValue["Id"] = strParam;
409 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
410 asyncResp->res.jsonValue["TaskState"] = ptr->state;
411 asyncResp->res.jsonValue["StartTime"] =
412 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
413 if (ptr->endTime)
414 {
415 asyncResp->res.jsonValue["EndTime"] =
416 redfish::time_utils::getDateTimeStdtime(
417 *(ptr->endTime));
418 }
419 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
420 asyncResp->res.jsonValue["Messages"] = ptr->messages;
421 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
422 "/redfish/v1/TaskService/Tasks/{}", strParam);
423 if (!ptr->gave204)
424 {
425 asyncResp->res.jsonValue["TaskMonitor"] =
426 boost::urls::format(
427 "/redfish/v1/TaskService/TaskMonitors/{}",
428 strParam);
429 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700430
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400431 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700432
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400433 if (ptr->payload)
434 {
435 const task::Payload& p = *(ptr->payload);
436 asyncResp->res.jsonValue["Payload"]["TargetUri"] =
437 p.targetUri;
438 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
439 p.httpOperation;
440 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] =
441 p.httpHeaders;
442 asyncResp->res.jsonValue["Payload"]["JsonBody"] =
443 p.jsonBody.dump(
444 -1, ' ', true,
445 nlohmann::json::error_handler_t::replace);
446 }
447 asyncResp->res.jsonValue["PercentComplete"] =
448 ptr->percentComplete;
449 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700450}
James Feist46229572020-02-19 15:11:58 -0800451
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700452inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800453{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700454 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700455 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700456 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700457 [&app](const crow::Request& req,
458 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400459 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
460 {
461 return;
462 }
463 asyncResp->res.jsonValue["@odata.type"] =
464 "#TaskCollection.TaskCollection";
465 asyncResp->res.jsonValue["@odata.id"] =
466 "/redfish/v1/TaskService/Tasks";
467 asyncResp->res.jsonValue["Name"] = "Task Collection";
468 asyncResp->res.jsonValue["Members@odata.count"] =
469 task::tasks.size();
470 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
471 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800472
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400473 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
474 {
475 if (task == nullptr)
476 {
477 continue; // shouldn't be possible
478 }
479 nlohmann::json::object_t member;
480 member["@odata.id"] =
481 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
482 std::to_string(task->index));
483 members.emplace_back(std::move(member));
484 }
485 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700486}
zhanghch058d1b46d2021-04-01 11:18:24 +0800487
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700488inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800489{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700490 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700491 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700492 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700493 [&app](const crow::Request& req,
494 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400495 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
496 {
497 return;
498 }
499 asyncResp->res.jsonValue["@odata.type"] =
500 "#TaskService.v1_1_4.TaskService";
501 asyncResp->res.jsonValue["@odata.id"] =
502 "/redfish/v1/TaskService";
503 asyncResp->res.jsonValue["Name"] = "Task Service";
504 asyncResp->res.jsonValue["Id"] = "TaskService";
505 asyncResp->res.jsonValue["DateTime"] =
506 redfish::time_utils::getDateTimeOffsetNow().first;
507 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
508 task_service::OverWritePolicy::Oldest;
James Feist46229572020-02-19 15:11:58 -0800509
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400510 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
511 true;
James Feist46229572020-02-19 15:11:58 -0800512
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400513 asyncResp->res.jsonValue["Status"]["State"] =
514 resource::State::Enabled;
515 asyncResp->res.jsonValue["ServiceEnabled"] = true;
516 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
517 "/redfish/v1/TaskService/Tasks";
518 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700519}
James Feist46229572020-02-19 15:11:58 -0800520
521} // namespace redfish