blob: d0f2b07a9d42921beb567161b2f8fa69c719a1a4 [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 {
Rohit PAI9f038942025-03-10 15:29:56 +0530141 const auto last = getTaskToRemove();
James Feist46229572020-02-19 15:11:58 -0800142
143 // destroy all references
Rohit PAI9f038942025-03-10 15:29:56 +0530144 (*last)->timer.cancel();
145 (*last)->match.reset();
146 tasks.erase(last);
James Feist46229572020-02-19 15:11:58 -0800147 }
148
149 return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
150 std::move(handler), match, lastTask++));
151 }
152
Rohit PAI9f038942025-03-10 15:29:56 +0530153 /**
154 * @brief Get the first completed/aborted task or oldest running task to
155 * remove
156 */
157 static std::deque<std::shared_ptr<TaskData>>::iterator getTaskToRemove()
158 {
159 static constexpr std::array<std::string_view, 5> activeStates = {
160 "Running", "Pending", "Starting", "Suspended", "Interrupted"};
161
162 auto it =
163 std::find_if(tasks.begin(), tasks.end(), [](const auto& task) {
164 return std::ranges::find(activeStates, task->state) ==
165 activeStates.end();
166 });
167
168 return (it != tasks.end()) ? it : tasks.begin();
169 }
170
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500171 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800172 {
173 if (!endTime)
174 {
175 res.result(boost::beast::http::status::accepted);
176 std::string strIdx = std::to_string(index);
Ed Tanousfdbce792024-06-26 14:48:46 -0700177 boost::urls::url uri =
178 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx);
Ed Tanous14766872022-03-15 10:44:42 -0700179
180 res.jsonValue["@odata.id"] = uri;
181 res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
182 res.jsonValue["Id"] = strIdx;
183 res.jsonValue["TaskState"] = state;
184 res.jsonValue["TaskStatus"] = status;
185
Ed Tanousfdbce792024-06-26 14:48:46 -0700186 boost::urls::url taskMonitor = boost::urls::format(
187 "/redfish/v1/TaskService/TaskMonitors/{}", strIdx);
188
James Feist46229572020-02-19 15:11:58 -0800189 res.addHeader(boost::beast::http::field::location,
Ed Tanousfdbce792024-06-26 14:48:46 -0700190 taskMonitor.buffer());
James Feist46229572020-02-19 15:11:58 -0800191 res.addHeader(boost::beast::http::field::retry_after,
192 std::to_string(retryAfterSeconds));
193 }
194 else if (!gave204)
195 {
196 res.result(boost::beast::http::status::no_content);
197 gave204 = true;
198 }
199 }
200
Ed Tanousd609fd62020-09-28 19:08:03 -0700201 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800202 {
203 endTime = std::chrono::system_clock::to_time_t(
204 std::chrono::system_clock::now());
205 }
206
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500207 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800208 {
James Feist46229572020-02-19 15:11:58 -0800209 timer.expires_after(timeout);
210 timer.async_wait(
211 [self = shared_from_this()](boost::system::error_code ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400212 if (ec == boost::asio::error::operation_aborted)
213 {
214 return; // completed successfully
215 }
216 if (!ec)
217 {
218 // change ec to error as timer expired
219 ec = boost::asio::error::operation_aborted;
220 }
221 self->match.reset();
222 sdbusplus::message_t msg;
223 self->finishTask();
224 self->state = "Cancelled";
225 self->status = "Warning";
226 self->messages.emplace_back(
227 messages::taskAborted(std::to_string(self->index)));
228 // Send event :TaskAborted
Ed Tanousdaadfb22024-12-20 09:25:54 -0800229 sendTaskEvent(self->state, self->index);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400230 self->callback(ec, msg, self);
231 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700232 }
233
Ed Tanous26ccae32023-02-16 10:28:44 -0800234 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500235 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500236 // TaskState enums which should send out an event are:
237 // "Starting" = taskResumed
238 // "Running" = taskStarted
239 // "Suspended" = taskPaused
240 // "Interrupted" = taskPaused
241 // "Pending" = taskPaused
242 // "Stopping" = taskAborted
243 // "Completed" = taskCompletedOK
244 // "Killed" = taskRemoved
245 // "Exception" = taskCompletedWarning
246 // "Cancelled" = taskCancelled
Ed Tanousf8fe2212024-06-16 14:51:23 -0700247 nlohmann::json event;
248 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500249 if (state == "Starting")
250 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700251 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500252 }
253 else if (state == "Running")
254 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700255 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500256 }
257 else if ((state == "Suspended") || (state == "Interrupted") ||
258 (state == "Pending"))
259 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700260 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500261 }
262 else if (state == "Stopping")
263 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700264 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500265 }
266 else if (state == "Completed")
267 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700268 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500269 }
270 else if (state == "Killed")
271 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700272 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500273 }
274 else if (state == "Exception")
275 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700276 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500277 }
278 else if (state == "Cancelled")
279 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700280 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500281 }
282 else
283 {
Ed Tanous62598e32023-07-17 17:06:25 -0700284 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700285 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500286 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700287 boost::urls::url origin =
288 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
289 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
290 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500291 }
292
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700294 {
295 if (match)
296 {
297 return;
298 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500299 match = std::make_unique<sdbusplus::bus::match_t>(
300 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700301 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500302 [self = shared_from_this()](sdbusplus::message_t& message) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400303 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700304
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400305 // callback to return True if callback is done, callback needs
306 // to update status itself if needed
307 if (self->callback(ec, message, self) == task::completed)
308 {
309 self->timer.cancel();
310 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700311
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400312 // Send event
Ed Tanousdaadfb22024-12-20 09:25:54 -0800313 sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500314
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400315 // reset the match after the callback was successful
316 boost::asio::post(
317 crow::connections::systemBus->get_io_context(),
318 [self] { self->match.reset(); });
319 return;
320 }
321 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700322
323 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700324 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500325 // Send event : TaskStarted
326 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800327 }
328
Patrick Williams59d494e2022-07-22 19:26:55 -0500329 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500330 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800331 callback;
332 std::string matchStr;
333 size_t index;
334 time_t startTime;
335 std::string status;
336 std::string state;
337 nlohmann::json messages;
338 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500339 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800340 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700341 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800342 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800343 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800344};
345
346} // namespace task
347
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700348inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800349{
Ed Tanousfdbce792024-06-26 14:48:46 -0700350 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700351 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700352 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700353 [&app](const crow::Request& req,
354 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
355 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400356 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
357 {
358 return;
359 }
360 auto find = std::ranges::find_if(
361 task::tasks,
362 [&strParam](const std::shared_ptr<task::TaskData>& task) {
363 if (!task)
364 {
365 return false;
366 }
James Feist46229572020-02-19 15:11:58 -0800367
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400368 // we compare against the string version as on failure
369 // strtoul returns 0
370 return std::to_string(task->index) == strParam;
371 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700372
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400373 if (find == task::tasks.end())
374 {
375 messages::resourceNotFound(asyncResp->res, "Task",
376 strParam);
377 return;
378 }
379 std::shared_ptr<task::TaskData>& ptr = *find;
380 // monitor expires after 204
381 if (ptr->gave204)
382 {
383 messages::resourceNotFound(asyncResp->res, "Task",
384 strParam);
385 return;
386 }
387 ptr->populateResp(asyncResp->res);
388 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700389}
390
391inline void requestRoutesTask(App& app)
392{
393 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700394 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700395 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700396 [&app](const crow::Request& req,
397 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
398 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400399 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
400 {
401 return;
402 }
403 auto find = std::ranges::find_if(
404 task::tasks,
405 [&strParam](const std::shared_ptr<task::TaskData>& task) {
406 if (!task)
407 {
408 return false;
409 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700410
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400411 // we compare against the string version as on failure
412 // strtoul returns 0
413 return std::to_string(task->index) == strParam;
414 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700415
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400416 if (find == task::tasks.end())
417 {
418 messages::resourceNotFound(asyncResp->res, "Task",
419 strParam);
420 return;
421 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700422
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400423 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700424
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400425 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
426 asyncResp->res.jsonValue["Id"] = strParam;
427 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
428 asyncResp->res.jsonValue["TaskState"] = ptr->state;
429 asyncResp->res.jsonValue["StartTime"] =
430 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
431 if (ptr->endTime)
432 {
433 asyncResp->res.jsonValue["EndTime"] =
434 redfish::time_utils::getDateTimeStdtime(
435 *(ptr->endTime));
436 }
437 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
438 asyncResp->res.jsonValue["Messages"] = ptr->messages;
439 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
440 "/redfish/v1/TaskService/Tasks/{}", strParam);
441 if (!ptr->gave204)
442 {
443 asyncResp->res.jsonValue["TaskMonitor"] =
444 boost::urls::format(
445 "/redfish/v1/TaskService/TaskMonitors/{}",
446 strParam);
447 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700448
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400449 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700450
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400451 if (ptr->payload)
452 {
453 const task::Payload& p = *(ptr->payload);
454 asyncResp->res.jsonValue["Payload"]["TargetUri"] =
455 p.targetUri;
456 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
457 p.httpOperation;
458 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] =
459 p.httpHeaders;
460 asyncResp->res.jsonValue["Payload"]["JsonBody"] =
461 p.jsonBody.dump(
462 -1, ' ', true,
463 nlohmann::json::error_handler_t::replace);
464 }
465 asyncResp->res.jsonValue["PercentComplete"] =
466 ptr->percentComplete;
467 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700468}
James Feist46229572020-02-19 15:11:58 -0800469
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700470inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800471{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700472 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700473 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700474 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700475 [&app](const crow::Request& req,
476 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400477 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
478 {
479 return;
480 }
481 asyncResp->res.jsonValue["@odata.type"] =
482 "#TaskCollection.TaskCollection";
483 asyncResp->res.jsonValue["@odata.id"] =
484 "/redfish/v1/TaskService/Tasks";
485 asyncResp->res.jsonValue["Name"] = "Task Collection";
486 asyncResp->res.jsonValue["Members@odata.count"] =
487 task::tasks.size();
488 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
489 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800490
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400491 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
492 {
493 if (task == nullptr)
494 {
495 continue; // shouldn't be possible
496 }
497 nlohmann::json::object_t member;
498 member["@odata.id"] =
499 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
500 std::to_string(task->index));
501 members.emplace_back(std::move(member));
502 }
503 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700504}
zhanghch058d1b46d2021-04-01 11:18:24 +0800505
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700506inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800507{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700508 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700509 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700510 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700511 [&app](const crow::Request& req,
512 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400513 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
514 {
515 return;
516 }
517 asyncResp->res.jsonValue["@odata.type"] =
518 "#TaskService.v1_1_4.TaskService";
519 asyncResp->res.jsonValue["@odata.id"] =
520 "/redfish/v1/TaskService";
521 asyncResp->res.jsonValue["Name"] = "Task Service";
522 asyncResp->res.jsonValue["Id"] = "TaskService";
523 asyncResp->res.jsonValue["DateTime"] =
524 redfish::time_utils::getDateTimeOffsetNow().first;
525 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
526 task_service::OverWritePolicy::Oldest;
James Feist46229572020-02-19 15:11:58 -0800527
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400528 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
529 true;
James Feist46229572020-02-19 15:11:58 -0800530
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400531 asyncResp->res.jsonValue["Status"]["State"] =
532 resource::State::Enabled;
533 asyncResp->res.jsonValue["ServiceEnabled"] = true;
534 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
535 "/redfish/v1/TaskService/Tasks";
536 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700537}
James Feist46229572020-02-19 15:11:58 -0800538
539} // namespace redfish