blob: 09c92d11d34529836ea0ea13c2539ebb47653204 [file] [log] [blame]
James Feist46229572020-02-19 15:11:58 -08001/*
2// Copyright (c) 2020 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_utility.hpp"
20#include "event_service_manager.hpp"
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010021#include "http/parsing.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
24#include "task_messages.hpp"
25
Ed Tanousd43cd0c2020-09-30 20:46:53 -070026#include <boost/asio/post.hpp>
27#include <boost/asio/steady_timer.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070028#include <boost/url/format.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include <sdbusplus/bus/match.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030
31#include <chrono>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080032#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070033#include <ranges>
James Feist46229572020-02-19 15:11:58 -080034#include <variant>
35
36namespace redfish
37{
38
39namespace task
40{
41constexpr size_t maxTaskCount = 100; // arbitrary limit
42
Ed Tanouscf9e4172022-12-21 09:30:16 -080043// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
James Feist46229572020-02-19 15:11:58 -080044static std::deque<std::shared_ptr<struct TaskData>> tasks;
45
James Feist32898ce2020-03-10 16:16:52 -070046constexpr bool completed = true;
47
James Feistfe306722020-03-12 16:32:08 -070048struct Payload
49{
Ed Tanous4e23a442022-06-06 09:57:26 -070050 explicit Payload(const crow::Request& req) :
Ed Tanous39662a32023-02-06 15:09:46 -080051 targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010052 httpHeaders(nlohmann::json::array())
James Feistfe306722020-03-12 16:32:08 -070053 {
54 using field_ns = boost::beast::http::field;
55 constexpr const std::array<boost::beast::http::field, 7>
56 headerWhitelist = {field_ns::accept, field_ns::accept_encoding,
57 field_ns::user_agent, field_ns::host,
58 field_ns::connection, field_ns::content_length,
59 field_ns::upgrade};
60
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010061 JsonParseResult ret = parseRequestAsJson(req, jsonBody);
62 if (ret != JsonParseResult::Success)
James Feistfe306722020-03-12 16:32:08 -070063 {
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010064 return;
James Feistfe306722020-03-12 16:32:08 -070065 }
66
Ed Tanous98fe7402023-02-14 14:50:33 -080067 for (const auto& field : req.fields())
James Feistfe306722020-03-12 16:32:08 -070068 {
Ed Tanous3544d2a2023-08-06 18:12:20 -070069 if (std::ranges::find(headerWhitelist, field.name()) ==
70 headerWhitelist.end())
James Feistfe306722020-03-12 16:32:08 -070071 {
72 continue;
73 }
74 std::string header;
75 header.reserve(field.name_string().size() + 2 +
76 field.value().size());
77 header += field.name_string();
78 header += ": ";
79 header += field.value();
80 httpHeaders.emplace_back(std::move(header));
81 }
82 }
83 Payload() = delete;
84
85 std::string targetUri;
86 std::string httpOperation;
87 nlohmann::json httpHeaders;
88 nlohmann::json jsonBody;
89};
90
James Feist46229572020-02-19 15:11:58 -080091struct TaskData : std::enable_shared_from_this<TaskData>
92{
93 private:
Patrick Williams59d494e2022-07-22 19:26:55 -050094 TaskData(
95 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
96 const std::shared_ptr<TaskData>&)>&& handler,
97 const std::string& matchIn, size_t idx) :
James Feist46229572020-02-19 15:11:58 -080098 callback(std::move(handler)),
Ed Tanous23a21a12020-07-25 04:45:05 +000099 matchStr(matchIn), index(idx),
James Feist46229572020-02-19 15:11:58 -0800100 startTime(std::chrono::system_clock::to_time_t(
101 std::chrono::system_clock::now())),
102 status("OK"), state("Running"), messages(nlohmann::json::array()),
103 timer(crow::connections::systemBus->get_io_context())
104
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500105 {}
James Feist46229572020-02-19 15:11:58 -0800106
107 public:
Ed Tanousd609fd62020-09-28 19:08:03 -0700108 TaskData() = delete;
109
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500110 static std::shared_ptr<TaskData>& createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -0500111 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500112 const std::shared_ptr<TaskData>&)>&& handler,
113 const std::string& match)
James Feist46229572020-02-19 15:11:58 -0800114 {
115 static size_t lastTask = 0;
116 struct MakeSharedHelper : public TaskData
117 {
118 MakeSharedHelper(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500119 std::function<bool(boost::system::error_code,
Patrick Williams59d494e2022-07-22 19:26:55 -0500120 sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121 const std::shared_ptr<TaskData>&)>&& handler,
Ed Tanous23a21a12020-07-25 04:45:05 +0000122 const std::string& match2, size_t idx) :
123 TaskData(std::move(handler), match2, idx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500124 {}
James Feist46229572020-02-19 15:11:58 -0800125 };
126
127 if (tasks.size() >= maxTaskCount)
128 {
Ed Tanous02cad962022-06-30 16:50:15 -0700129 const auto& last = tasks.front();
James Feist46229572020-02-19 15:11:58 -0800130
131 // destroy all references
132 last->timer.cancel();
133 last->match.reset();
134 tasks.pop_front();
135 }
136
137 return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
138 std::move(handler), match, lastTask++));
139 }
140
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500141 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800142 {
143 if (!endTime)
144 {
145 res.result(boost::beast::http::status::accepted);
146 std::string strIdx = std::to_string(index);
147 std::string uri = "/redfish/v1/TaskService/Tasks/" + strIdx;
Ed Tanous14766872022-03-15 10:44:42 -0700148
149 res.jsonValue["@odata.id"] = uri;
150 res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
151 res.jsonValue["Id"] = strIdx;
152 res.jsonValue["TaskState"] = state;
153 res.jsonValue["TaskStatus"] = status;
154
James Feist46229572020-02-19 15:11:58 -0800155 res.addHeader(boost::beast::http::field::location,
156 uri + "/Monitor");
157 res.addHeader(boost::beast::http::field::retry_after,
158 std::to_string(retryAfterSeconds));
159 }
160 else if (!gave204)
161 {
162 res.result(boost::beast::http::status::no_content);
163 gave204 = true;
164 }
165 }
166
Ed Tanousd609fd62020-09-28 19:08:03 -0700167 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800168 {
169 endTime = std::chrono::system_clock::to_time_t(
170 std::chrono::system_clock::now());
171 }
172
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500173 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800174 {
James Feist46229572020-02-19 15:11:58 -0800175 timer.expires_after(timeout);
176 timer.async_wait(
177 [self = shared_from_this()](boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700178 if (ec == boost::asio::error::operation_aborted)
179 {
180 return; // completed successfully
181 }
182 if (!ec)
183 {
184 // change ec to error as timer expired
185 ec = boost::asio::error::operation_aborted;
186 }
187 self->match.reset();
Patrick Williams59d494e2022-07-22 19:26:55 -0500188 sdbusplus::message_t msg;
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 self->finishTask();
190 self->state = "Cancelled";
191 self->status = "Warning";
192 self->messages.emplace_back(
193 messages::taskAborted(std::to_string(self->index)));
194 // Send event :TaskAborted
195 self->sendTaskEvent(self->state, self->index);
196 self->callback(ec, msg, self);
197 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700198 }
199
Ed Tanous26ccae32023-02-16 10:28:44 -0800200 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500201 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500202 // TaskState enums which should send out an event are:
203 // "Starting" = taskResumed
204 // "Running" = taskStarted
205 // "Suspended" = taskPaused
206 // "Interrupted" = taskPaused
207 // "Pending" = taskPaused
208 // "Stopping" = taskAborted
209 // "Completed" = taskCompletedOK
210 // "Killed" = taskRemoved
211 // "Exception" = taskCompletedWarning
212 // "Cancelled" = taskCancelled
Ed Tanousf8fe2212024-06-16 14:51:23 -0700213 nlohmann::json event;
214 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500215 if (state == "Starting")
216 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700217 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500218 }
219 else if (state == "Running")
220 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700221 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500222 }
223 else if ((state == "Suspended") || (state == "Interrupted") ||
224 (state == "Pending"))
225 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700226 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500227 }
228 else if (state == "Stopping")
229 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700230 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500231 }
232 else if (state == "Completed")
233 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700234 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500235 }
236 else if (state == "Killed")
237 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700238 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500239 }
240 else if (state == "Exception")
241 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700242 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500243 }
244 else if (state == "Cancelled")
245 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700246 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500247 }
248 else
249 {
Ed Tanous62598e32023-07-17 17:06:25 -0700250 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700251 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500252 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700253 boost::urls::url origin =
254 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
255 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
256 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500257 }
258
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500259 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700260 {
261 if (match)
262 {
263 return;
264 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500265 match = std::make_unique<sdbusplus::bus::match_t>(
266 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700267 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500268 [self = shared_from_this()](sdbusplus::message_t& message) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700269 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700270
Ed Tanous002d39b2022-05-31 08:59:27 -0700271 // callback to return True if callback is done, callback needs
272 // to update status itself if needed
273 if (self->callback(ec, message, self) == task::completed)
274 {
275 self->timer.cancel();
276 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700277
Ed Tanous002d39b2022-05-31 08:59:27 -0700278 // Send event
279 self->sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500280
Ed Tanous002d39b2022-05-31 08:59:27 -0700281 // reset the match after the callback was successful
282 boost::asio::post(
283 crow::connections::systemBus->get_io_context(),
284 [self] { self->match.reset(); });
285 return;
286 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500287 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700288
289 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700290 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500291 // Send event : TaskStarted
292 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800293 }
294
Patrick Williams59d494e2022-07-22 19:26:55 -0500295 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500296 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800297 callback;
298 std::string matchStr;
299 size_t index;
300 time_t startTime;
301 std::string status;
302 std::string state;
303 nlohmann::json messages;
304 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500305 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800306 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700307 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800308 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800309 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800310};
311
312} // namespace task
313
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700314inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800315{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700316 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
Ed Tanoused398212021-06-09 17:05:54 -0700317 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700318 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700319 [&app](const crow::Request& req,
320 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
321 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000322 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700323 {
324 return;
325 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700326 auto find = std::ranges::find_if(
327 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700328 [&strParam](const std::shared_ptr<task::TaskData>& task) {
329 if (!task)
330 {
331 return false;
332 }
James Feist46229572020-02-19 15:11:58 -0800333
Ed Tanous002d39b2022-05-31 08:59:27 -0700334 // we compare against the string version as on failure
335 // strtoul returns 0
336 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500337 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700338
339 if (find == task::tasks.end())
340 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800341 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700342 return;
343 }
344 std::shared_ptr<task::TaskData>& ptr = *find;
345 // monitor expires after 204
346 if (ptr->gave204)
347 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800348 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700349 return;
350 }
351 ptr->populateResp(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500352 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700353}
354
355inline void requestRoutesTask(App& app)
356{
357 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700358 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700359 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700360 [&app](const crow::Request& req,
361 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
362 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000363 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 {
365 return;
366 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700367 auto find = std::ranges::find_if(
368 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 [&strParam](const std::shared_ptr<task::TaskData>& task) {
370 if (!task)
371 {
372 return false;
373 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700374
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 // we compare against the string version as on failure
376 // strtoul returns 0
377 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500378 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700379
380 if (find == task::tasks.end())
381 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800382 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 return;
384 }
385
Ed Tanous02cad962022-06-30 16:50:15 -0700386 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700387
388 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
389 asyncResp->res.jsonValue["Id"] = strParam;
390 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
391 asyncResp->res.jsonValue["TaskState"] = ptr->state;
392 asyncResp->res.jsonValue["StartTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700393 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
Ed Tanous002d39b2022-05-31 08:59:27 -0700394 if (ptr->endTime)
395 {
396 asyncResp->res.jsonValue["EndTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700397 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
Ed Tanous002d39b2022-05-31 08:59:27 -0700398 }
399 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
400 asyncResp->res.jsonValue["Messages"] = ptr->messages;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700401 asyncResp->res.jsonValue["@odata.id"] =
402 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700403 if (!ptr->gave204)
404 {
405 asyncResp->res.jsonValue["TaskMonitor"] =
406 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
407 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700408
409 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
410
Ed Tanous002d39b2022-05-31 08:59:27 -0700411 if (ptr->payload)
412 {
413 const task::Payload& p = *(ptr->payload);
414 asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
415 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
416 p.httpOperation;
417 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
418 asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
419 2, ' ', true, nlohmann::json::error_handler_t::replace);
420 }
421 asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
Patrick Williams5a39f772023-10-20 11:20:21 -0500422 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700423}
James Feist46229572020-02-19 15:11:58 -0800424
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700425inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800426{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700427 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700428 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700429 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700430 [&app](const crow::Request& req,
431 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000432 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700433 {
434 return;
435 }
436 asyncResp->res.jsonValue["@odata.type"] =
437 "#TaskCollection.TaskCollection";
438 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
439 asyncResp->res.jsonValue["Name"] = "Task Collection";
440 asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
441 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
442 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800443
Ed Tanous002d39b2022-05-31 08:59:27 -0700444 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
445 {
446 if (task == nullptr)
447 {
448 continue; // shouldn't be possible
449 }
Ed Tanous613dabe2022-07-09 11:17:36 -0700450 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700451 member["@odata.id"] =
452 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
453 std::to_string(task->index));
Ed Tanous613dabe2022-07-09 11:17:36 -0700454 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700455 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500456 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700457}
zhanghch058d1b46d2021-04-01 11:18:24 +0800458
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700459inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800460{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700461 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700462 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700463 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700464 [&app](const crow::Request& req,
465 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000466 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 {
468 return;
469 }
470 asyncResp->res.jsonValue["@odata.type"] =
471 "#TaskService.v1_1_4.TaskService";
472 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
473 asyncResp->res.jsonValue["Name"] = "Task Service";
474 asyncResp->res.jsonValue["Id"] = "TaskService";
475 asyncResp->res.jsonValue["DateTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700476 redfish::time_utils::getDateTimeOffsetNow().first;
Ed Tanous002d39b2022-05-31 08:59:27 -0700477 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
James Feist46229572020-02-19 15:11:58 -0800478
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
James Feist46229572020-02-19 15:11:58 -0800480
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
482 asyncResp->res.jsonValue["ServiceEnabled"] = true;
483 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
484 "/redfish/v1/TaskService/Tasks";
Patrick Williams5a39f772023-10-20 11:20:21 -0500485 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700486}
James Feist46229572020-02-19 15:11:58 -0800487
488} // namespace redfish