blob: 0e29cae78e83282118e1ab6b57399750b2fabdba [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));
Chinmay Shripad Hegde29e2bdd2025-06-06 16:23:47 +0530193 res.jsonValue["Name"] = "Task " + strIdx;
194 res.jsonValue["StartTime"] =
195 redfish::time_utils::getDateTimeStdtime(startTime);
196 res.jsonValue["Messages"] = messages;
197 res.jsonValue["TaskMonitor"] = taskMonitor;
198 res.jsonValue["HidePayload"] = !payload;
199 if (payload)
200 {
201 const task::Payload& p = *payload;
202 nlohmann::json::object_t payloadObj;
203 payloadObj["TargetUri"] = p.targetUri;
204 payloadObj["HttpOperation"] = p.httpOperation;
205 payloadObj["HttpHeaders"] = p.httpHeaders;
206 if (p.jsonBody.is_object())
207 {
208 payloadObj["JsonBody"] = p.jsonBody.dump(
209 2, ' ', true, nlohmann::json::error_handler_t::replace);
210 }
211 res.jsonValue["Payload"] = std::move(payloadObj);
212 }
213 res.jsonValue["PercentComplete"] = percentComplete;
James Feist46229572020-02-19 15:11:58 -0800214 }
215 else if (!gave204)
216 {
217 res.result(boost::beast::http::status::no_content);
218 gave204 = true;
219 }
220 }
221
Ed Tanousd609fd62020-09-28 19:08:03 -0700222 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800223 {
224 endTime = std::chrono::system_clock::to_time_t(
225 std::chrono::system_clock::now());
226 }
227
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500228 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800229 {
James Feist46229572020-02-19 15:11:58 -0800230 timer.expires_after(timeout);
231 timer.async_wait(
232 [self = shared_from_this()](boost::system::error_code ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400233 if (ec == boost::asio::error::operation_aborted)
234 {
235 return; // completed successfully
236 }
237 if (!ec)
238 {
239 // change ec to error as timer expired
240 ec = boost::asio::error::operation_aborted;
241 }
242 self->match.reset();
243 sdbusplus::message_t msg;
244 self->finishTask();
245 self->state = "Cancelled";
246 self->status = "Warning";
247 self->messages.emplace_back(
248 messages::taskAborted(std::to_string(self->index)));
249 // Send event :TaskAborted
Ed Tanousdaadfb22024-12-20 09:25:54 -0800250 sendTaskEvent(self->state, self->index);
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400251 self->callback(ec, msg, self);
252 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700253 }
254
Ed Tanous26ccae32023-02-16 10:28:44 -0800255 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500256 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500257 // TaskState enums which should send out an event are:
258 // "Starting" = taskResumed
259 // "Running" = taskStarted
260 // "Suspended" = taskPaused
261 // "Interrupted" = taskPaused
262 // "Pending" = taskPaused
263 // "Stopping" = taskAborted
264 // "Completed" = taskCompletedOK
265 // "Killed" = taskRemoved
266 // "Exception" = taskCompletedWarning
267 // "Cancelled" = taskCancelled
Ed Tanous82b286f2025-05-06 13:29:48 -0700268 nlohmann::json::object_t event;
Ed Tanousf8fe2212024-06-16 14:51:23 -0700269 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500270 if (state == "Starting")
271 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700272 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500273 }
274 else if (state == "Running")
275 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700276 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500277 }
278 else if ((state == "Suspended") || (state == "Interrupted") ||
279 (state == "Pending"))
280 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700281 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500282 }
283 else if (state == "Stopping")
284 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700285 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500286 }
287 else if (state == "Completed")
288 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700289 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500290 }
291 else if (state == "Killed")
292 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700293 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500294 }
295 else if (state == "Exception")
296 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700297 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500298 }
299 else if (state == "Cancelled")
300 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700301 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500302 }
303 else
304 {
Ed Tanous62598e32023-07-17 17:06:25 -0700305 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700306 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500307 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700308 boost::urls::url origin =
309 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
310 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
311 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500312 }
313
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500314 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700315 {
316 if (match)
317 {
318 return;
319 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500320 match = std::make_unique<sdbusplus::bus::match_t>(
321 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700322 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500323 [self = shared_from_this()](sdbusplus::message_t& message) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400324 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700325
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400326 // callback to return True if callback is done, callback needs
327 // to update status itself if needed
328 if (self->callback(ec, message, self) == task::completed)
329 {
330 self->timer.cancel();
331 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700332
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400333 // Send event
Ed Tanousdaadfb22024-12-20 09:25:54 -0800334 sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500335
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400336 // reset the match after the callback was successful
337 boost::asio::post(
338 crow::connections::systemBus->get_io_context(),
339 [self] { self->match.reset(); });
340 return;
341 }
342 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700343
344 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700345 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500346 // Send event : TaskStarted
347 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800348 }
349
Patrick Williams59d494e2022-07-22 19:26:55 -0500350 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500351 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800352 callback;
353 std::string matchStr;
354 size_t index;
355 time_t startTime;
356 std::string status;
357 std::string state;
358 nlohmann::json messages;
359 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500360 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800361 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700362 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800363 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800364 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800365};
366
367} // namespace task
368
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700369inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800370{
Ed Tanousfdbce792024-06-26 14:48:46 -0700371 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700372 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700373 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700374 [&app](const crow::Request& req,
375 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
376 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400377 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
378 {
379 return;
380 }
381 auto find = std::ranges::find_if(
382 task::tasks,
383 [&strParam](const std::shared_ptr<task::TaskData>& task) {
384 if (!task)
385 {
386 return false;
387 }
James Feist46229572020-02-19 15:11:58 -0800388
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400389 // we compare against the string version as on failure
390 // strtoul returns 0
391 return std::to_string(task->index) == strParam;
392 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700393
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400394 if (find == task::tasks.end())
395 {
396 messages::resourceNotFound(asyncResp->res, "Task",
397 strParam);
398 return;
399 }
400 std::shared_ptr<task::TaskData>& ptr = *find;
401 // monitor expires after 204
402 if (ptr->gave204)
403 {
404 messages::resourceNotFound(asyncResp->res, "Task",
405 strParam);
406 return;
407 }
408 ptr->populateResp(asyncResp->res);
409 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700410}
411
412inline void requestRoutesTask(App& app)
413{
414 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700415 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700416 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700417 [&app](const crow::Request& req,
418 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
419 const std::string& strParam) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400420 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
421 {
422 return;
423 }
424 auto find = std::ranges::find_if(
425 task::tasks,
426 [&strParam](const std::shared_ptr<task::TaskData>& task) {
427 if (!task)
428 {
429 return false;
430 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700431
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400432 // we compare against the string version as on failure
433 // strtoul returns 0
434 return std::to_string(task->index) == strParam;
435 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700436
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400437 if (find == task::tasks.end())
438 {
439 messages::resourceNotFound(asyncResp->res, "Task",
440 strParam);
441 return;
442 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700443
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400444 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700445
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400446 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
447 asyncResp->res.jsonValue["Id"] = strParam;
448 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
449 asyncResp->res.jsonValue["TaskState"] = ptr->state;
450 asyncResp->res.jsonValue["StartTime"] =
451 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
452 if (ptr->endTime)
453 {
454 asyncResp->res.jsonValue["EndTime"] =
455 redfish::time_utils::getDateTimeStdtime(
456 *(ptr->endTime));
457 }
458 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
459 asyncResp->res.jsonValue["Messages"] = ptr->messages;
460 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
461 "/redfish/v1/TaskService/Tasks/{}", strParam);
462 if (!ptr->gave204)
463 {
464 asyncResp->res.jsonValue["TaskMonitor"] =
465 boost::urls::format(
466 "/redfish/v1/TaskService/TaskMonitors/{}",
467 strParam);
468 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700469
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400470 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700471
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400472 if (ptr->payload)
473 {
474 const task::Payload& p = *(ptr->payload);
475 asyncResp->res.jsonValue["Payload"]["TargetUri"] =
476 p.targetUri;
477 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
478 p.httpOperation;
479 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] =
480 p.httpHeaders;
481 asyncResp->res.jsonValue["Payload"]["JsonBody"] =
482 p.jsonBody.dump(
483 -1, ' ', true,
484 nlohmann::json::error_handler_t::replace);
485 }
486 asyncResp->res.jsonValue["PercentComplete"] =
487 ptr->percentComplete;
488 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700489}
James Feist46229572020-02-19 15:11:58 -0800490
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700491inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800492{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700493 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700494 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700495 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700496 [&app](const crow::Request& req,
497 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400498 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
499 {
500 return;
501 }
502 asyncResp->res.jsonValue["@odata.type"] =
503 "#TaskCollection.TaskCollection";
504 asyncResp->res.jsonValue["@odata.id"] =
505 "/redfish/v1/TaskService/Tasks";
506 asyncResp->res.jsonValue["Name"] = "Task Collection";
507 asyncResp->res.jsonValue["Members@odata.count"] =
508 task::tasks.size();
509 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
510 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800511
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400512 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
513 {
514 if (task == nullptr)
515 {
516 continue; // shouldn't be possible
517 }
518 nlohmann::json::object_t member;
519 member["@odata.id"] =
520 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
521 std::to_string(task->index));
522 members.emplace_back(std::move(member));
523 }
524 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700525}
zhanghch058d1b46d2021-04-01 11:18:24 +0800526
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700527inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800528{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700529 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700530 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700531 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700532 [&app](const crow::Request& req,
533 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400534 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
535 {
536 return;
537 }
538 asyncResp->res.jsonValue["@odata.type"] =
539 "#TaskService.v1_1_4.TaskService";
540 asyncResp->res.jsonValue["@odata.id"] =
541 "/redfish/v1/TaskService";
542 asyncResp->res.jsonValue["Name"] = "Task Service";
543 asyncResp->res.jsonValue["Id"] = "TaskService";
544 asyncResp->res.jsonValue["DateTime"] =
545 redfish::time_utils::getDateTimeOffsetNow().first;
546 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
547 task_service::OverWritePolicy::Oldest;
James Feist46229572020-02-19 15:11:58 -0800548
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400549 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
550 true;
James Feist46229572020-02-19 15:11:58 -0800551
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400552 asyncResp->res.jsonValue["Status"]["State"] =
553 resource::State::Enabled;
554 asyncResp->res.jsonValue["ServiceEnabled"] = true;
555 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
556 "/redfish/v1/TaskService/Tasks";
557 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700558}
James Feist46229572020-02-19 15:11:58 -0800559
560} // namespace redfish