blob: a2959b8455d4c775b606bbb12f3d294da7919a05 [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 {
Patrick Williams89492a12023-05-10 07:51:34 -0500202 std::string origin = "/redfish/v1/TaskService/Tasks/" +
203 std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500204 std::string resType = "Task";
205 // TaskState enums which should send out an event are:
206 // "Starting" = taskResumed
207 // "Running" = taskStarted
208 // "Suspended" = taskPaused
209 // "Interrupted" = taskPaused
210 // "Pending" = taskPaused
211 // "Stopping" = taskAborted
212 // "Completed" = taskCompletedOK
213 // "Killed" = taskRemoved
214 // "Exception" = taskCompletedWarning
215 // "Cancelled" = taskCancelled
216 if (state == "Starting")
217 {
218 redfish::EventServiceManager::getInstance().sendEvent(
219 redfish::messages::taskResumed(std::to_string(index)), origin,
220 resType);
221 }
222 else if (state == "Running")
223 {
224 redfish::EventServiceManager::getInstance().sendEvent(
225 redfish::messages::taskStarted(std::to_string(index)), origin,
226 resType);
227 }
228 else if ((state == "Suspended") || (state == "Interrupted") ||
229 (state == "Pending"))
230 {
231 redfish::EventServiceManager::getInstance().sendEvent(
232 redfish::messages::taskPaused(std::to_string(index)), origin,
233 resType);
234 }
235 else if (state == "Stopping")
236 {
237 redfish::EventServiceManager::getInstance().sendEvent(
238 redfish::messages::taskAborted(std::to_string(index)), origin,
239 resType);
240 }
241 else if (state == "Completed")
242 {
243 redfish::EventServiceManager::getInstance().sendEvent(
244 redfish::messages::taskCompletedOK(std::to_string(index)),
245 origin, resType);
246 }
247 else if (state == "Killed")
248 {
249 redfish::EventServiceManager::getInstance().sendEvent(
250 redfish::messages::taskRemoved(std::to_string(index)), origin,
251 resType);
252 }
253 else if (state == "Exception")
254 {
255 redfish::EventServiceManager::getInstance().sendEvent(
256 redfish::messages::taskCompletedWarning(std::to_string(index)),
257 origin, resType);
258 }
259 else if (state == "Cancelled")
260 {
261 redfish::EventServiceManager::getInstance().sendEvent(
262 redfish::messages::taskCancelled(std::to_string(index)), origin,
263 resType);
264 }
265 else
266 {
Ed Tanous62598e32023-07-17 17:06:25 -0700267 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Sunitha Harishe7686572020-07-15 02:32:44 -0500268 }
269 }
270
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500271 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700272 {
273 if (match)
274 {
275 return;
276 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500277 match = std::make_unique<sdbusplus::bus::match_t>(
278 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700279 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500280 [self = shared_from_this()](sdbusplus::message_t& message) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700281 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700282
Ed Tanous002d39b2022-05-31 08:59:27 -0700283 // callback to return True if callback is done, callback needs
284 // to update status itself if needed
285 if (self->callback(ec, message, self) == task::completed)
286 {
287 self->timer.cancel();
288 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700289
Ed Tanous002d39b2022-05-31 08:59:27 -0700290 // Send event
291 self->sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500292
Ed Tanous002d39b2022-05-31 08:59:27 -0700293 // reset the match after the callback was successful
294 boost::asio::post(
295 crow::connections::systemBus->get_io_context(),
296 [self] { self->match.reset(); });
297 return;
298 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500299 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700300
301 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700302 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500303 // Send event : TaskStarted
304 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800305 }
306
Patrick Williams59d494e2022-07-22 19:26:55 -0500307 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500308 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800309 callback;
310 std::string matchStr;
311 size_t index;
312 time_t startTime;
313 std::string status;
314 std::string state;
315 nlohmann::json messages;
316 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500317 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800318 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700319 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800320 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800321 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800322};
323
324} // namespace task
325
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700326inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800327{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700328 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
Ed Tanoused398212021-06-09 17:05:54 -0700329 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700330 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700331 [&app](const crow::Request& req,
332 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
333 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000334 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700335 {
336 return;
337 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700338 auto find = std::ranges::find_if(
339 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 [&strParam](const std::shared_ptr<task::TaskData>& task) {
341 if (!task)
342 {
343 return false;
344 }
James Feist46229572020-02-19 15:11:58 -0800345
Ed Tanous002d39b2022-05-31 08:59:27 -0700346 // we compare against the string version as on failure
347 // strtoul returns 0
348 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500349 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700350
351 if (find == task::tasks.end())
352 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800353 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700354 return;
355 }
356 std::shared_ptr<task::TaskData>& ptr = *find;
357 // monitor expires after 204
358 if (ptr->gave204)
359 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800360 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700361 return;
362 }
363 ptr->populateResp(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500364 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700365}
366
367inline void requestRoutesTask(App& app)
368{
369 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700370 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700371 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700372 [&app](const crow::Request& req,
373 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
374 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000375 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 {
377 return;
378 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700379 auto find = std::ranges::find_if(
380 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700381 [&strParam](const std::shared_ptr<task::TaskData>& task) {
382 if (!task)
383 {
384 return false;
385 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700386
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 // we compare against the string version as on failure
388 // strtoul returns 0
389 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500390 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700391
392 if (find == task::tasks.end())
393 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800394 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700395 return;
396 }
397
Ed Tanous02cad962022-06-30 16:50:15 -0700398 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700399
400 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
401 asyncResp->res.jsonValue["Id"] = strParam;
402 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
403 asyncResp->res.jsonValue["TaskState"] = ptr->state;
404 asyncResp->res.jsonValue["StartTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700405 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
Ed Tanous002d39b2022-05-31 08:59:27 -0700406 if (ptr->endTime)
407 {
408 asyncResp->res.jsonValue["EndTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700409 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
Ed Tanous002d39b2022-05-31 08:59:27 -0700410 }
411 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
412 asyncResp->res.jsonValue["Messages"] = ptr->messages;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700413 asyncResp->res.jsonValue["@odata.id"] =
414 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700415 if (!ptr->gave204)
416 {
417 asyncResp->res.jsonValue["TaskMonitor"] =
418 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
419 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700420
421 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
422
Ed Tanous002d39b2022-05-31 08:59:27 -0700423 if (ptr->payload)
424 {
425 const task::Payload& p = *(ptr->payload);
426 asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
427 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
428 p.httpOperation;
429 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
430 asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
431 2, ' ', true, nlohmann::json::error_handler_t::replace);
432 }
433 asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
Patrick Williams5a39f772023-10-20 11:20:21 -0500434 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700435}
James Feist46229572020-02-19 15:11:58 -0800436
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700437inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800438{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700439 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700440 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700441 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700442 [&app](const crow::Request& req,
443 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000444 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700445 {
446 return;
447 }
448 asyncResp->res.jsonValue["@odata.type"] =
449 "#TaskCollection.TaskCollection";
450 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
451 asyncResp->res.jsonValue["Name"] = "Task Collection";
452 asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
453 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
454 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800455
Ed Tanous002d39b2022-05-31 08:59:27 -0700456 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
457 {
458 if (task == nullptr)
459 {
460 continue; // shouldn't be possible
461 }
Ed Tanous613dabe2022-07-09 11:17:36 -0700462 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700463 member["@odata.id"] =
464 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
465 std::to_string(task->index));
Ed Tanous613dabe2022-07-09 11:17:36 -0700466 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500468 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700469}
zhanghch058d1b46d2021-04-01 11:18:24 +0800470
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700471inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800472{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700473 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700474 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700475 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700476 [&app](const crow::Request& req,
477 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000478 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 {
480 return;
481 }
482 asyncResp->res.jsonValue["@odata.type"] =
483 "#TaskService.v1_1_4.TaskService";
484 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
485 asyncResp->res.jsonValue["Name"] = "Task Service";
486 asyncResp->res.jsonValue["Id"] = "TaskService";
487 asyncResp->res.jsonValue["DateTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700488 redfish::time_utils::getDateTimeOffsetNow().first;
Ed Tanous002d39b2022-05-31 08:59:27 -0700489 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
James Feist46229572020-02-19 15:11:58 -0800490
Ed Tanous002d39b2022-05-31 08:59:27 -0700491 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
James Feist46229572020-02-19 15:11:58 -0800492
Ed Tanous002d39b2022-05-31 08:59:27 -0700493 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
494 asyncResp->res.jsonValue["ServiceEnabled"] = true;
495 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
496 "/redfish/v1/TaskService/Tasks";
Patrick Williams5a39f772023-10-20 11:20:21 -0500497 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700498}
James Feist46229572020-02-19 15:11:58 -0800499
500} // namespace redfish