blob: ec92acaa6b8796b18e1acc43fa2bb50ba34f30e0 [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);
Ed Tanousfdbce792024-06-26 14:48:46 -0700147 boost::urls::url uri =
148 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx);
Ed Tanous14766872022-03-15 10:44:42 -0700149
150 res.jsonValue["@odata.id"] = uri;
151 res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
152 res.jsonValue["Id"] = strIdx;
153 res.jsonValue["TaskState"] = state;
154 res.jsonValue["TaskStatus"] = status;
155
Ed Tanousfdbce792024-06-26 14:48:46 -0700156 boost::urls::url taskMonitor = boost::urls::format(
157 "/redfish/v1/TaskService/TaskMonitors/{}", strIdx);
158
James Feist46229572020-02-19 15:11:58 -0800159 res.addHeader(boost::beast::http::field::location,
Ed Tanousfdbce792024-06-26 14:48:46 -0700160 taskMonitor.buffer());
James Feist46229572020-02-19 15:11:58 -0800161 res.addHeader(boost::beast::http::field::retry_after,
162 std::to_string(retryAfterSeconds));
163 }
164 else if (!gave204)
165 {
166 res.result(boost::beast::http::status::no_content);
167 gave204 = true;
168 }
169 }
170
Ed Tanousd609fd62020-09-28 19:08:03 -0700171 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800172 {
173 endTime = std::chrono::system_clock::to_time_t(
174 std::chrono::system_clock::now());
175 }
176
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500177 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800178 {
James Feist46229572020-02-19 15:11:58 -0800179 timer.expires_after(timeout);
180 timer.async_wait(
181 [self = shared_from_this()](boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700182 if (ec == boost::asio::error::operation_aborted)
183 {
184 return; // completed successfully
185 }
186 if (!ec)
187 {
188 // change ec to error as timer expired
189 ec = boost::asio::error::operation_aborted;
190 }
191 self->match.reset();
Patrick Williams59d494e2022-07-22 19:26:55 -0500192 sdbusplus::message_t msg;
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 self->finishTask();
194 self->state = "Cancelled";
195 self->status = "Warning";
196 self->messages.emplace_back(
197 messages::taskAborted(std::to_string(self->index)));
198 // Send event :TaskAborted
199 self->sendTaskEvent(self->state, self->index);
200 self->callback(ec, msg, self);
201 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700202 }
203
Ed Tanous26ccae32023-02-16 10:28:44 -0800204 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500205 {
Sunitha Harishe7686572020-07-15 02:32:44 -0500206 // TaskState enums which should send out an event are:
207 // "Starting" = taskResumed
208 // "Running" = taskStarted
209 // "Suspended" = taskPaused
210 // "Interrupted" = taskPaused
211 // "Pending" = taskPaused
212 // "Stopping" = taskAborted
213 // "Completed" = taskCompletedOK
214 // "Killed" = taskRemoved
215 // "Exception" = taskCompletedWarning
216 // "Cancelled" = taskCancelled
Ed Tanousf8fe2212024-06-16 14:51:23 -0700217 nlohmann::json event;
218 std::string indexStr = std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500219 if (state == "Starting")
220 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700221 event = redfish::messages::taskResumed(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500222 }
223 else if (state == "Running")
224 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700225 event = redfish::messages::taskStarted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500226 }
227 else if ((state == "Suspended") || (state == "Interrupted") ||
228 (state == "Pending"))
229 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700230 event = redfish::messages::taskPaused(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500231 }
232 else if (state == "Stopping")
233 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700234 event = redfish::messages::taskAborted(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500235 }
236 else if (state == "Completed")
237 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700238 event = redfish::messages::taskCompletedOK(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500239 }
240 else if (state == "Killed")
241 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700242 event = redfish::messages::taskRemoved(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500243 }
244 else if (state == "Exception")
245 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700246 event = redfish::messages::taskCompletedWarning(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500247 }
248 else if (state == "Cancelled")
249 {
Ed Tanousf8fe2212024-06-16 14:51:23 -0700250 event = redfish::messages::taskCancelled(indexStr);
Sunitha Harishe7686572020-07-15 02:32:44 -0500251 }
252 else
253 {
Ed Tanous62598e32023-07-17 17:06:25 -0700254 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Ed Tanousf8fe2212024-06-16 14:51:23 -0700255 return;
Sunitha Harishe7686572020-07-15 02:32:44 -0500256 }
Ed Tanousf8fe2212024-06-16 14:51:23 -0700257 boost::urls::url origin =
258 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
259 EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
260 "Task");
Sunitha Harishe7686572020-07-15 02:32:44 -0500261 }
262
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500263 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700264 {
265 if (match)
266 {
267 return;
268 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500269 match = std::make_unique<sdbusplus::bus::match_t>(
270 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700271 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500272 [self = shared_from_this()](sdbusplus::message_t& message) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700273 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700274
Ed Tanous002d39b2022-05-31 08:59:27 -0700275 // callback to return True if callback is done, callback needs
276 // to update status itself if needed
277 if (self->callback(ec, message, self) == task::completed)
278 {
279 self->timer.cancel();
280 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700281
Ed Tanous002d39b2022-05-31 08:59:27 -0700282 // Send event
283 self->sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500284
Ed Tanous002d39b2022-05-31 08:59:27 -0700285 // reset the match after the callback was successful
286 boost::asio::post(
287 crow::connections::systemBus->get_io_context(),
288 [self] { self->match.reset(); });
289 return;
290 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500291 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700292
293 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700294 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500295 // Send event : TaskStarted
296 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800297 }
298
Patrick Williams59d494e2022-07-22 19:26:55 -0500299 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500300 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800301 callback;
302 std::string matchStr;
303 size_t index;
304 time_t startTime;
305 std::string status;
306 std::string state;
307 nlohmann::json messages;
308 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500309 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800310 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700311 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800312 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800313 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800314};
315
316} // namespace task
317
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700318inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800319{
Ed Tanousfdbce792024-06-26 14:48:46 -0700320 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700321 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700322 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700323 [&app](const crow::Request& req,
324 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
325 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000326 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700327 {
328 return;
329 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700330 auto find = std::ranges::find_if(
331 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700332 [&strParam](const std::shared_ptr<task::TaskData>& task) {
333 if (!task)
334 {
335 return false;
336 }
James Feist46229572020-02-19 15:11:58 -0800337
Ed Tanous002d39b2022-05-31 08:59:27 -0700338 // we compare against the string version as on failure
339 // strtoul returns 0
340 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500341 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700342
343 if (find == task::tasks.end())
344 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800345 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700346 return;
347 }
348 std::shared_ptr<task::TaskData>& ptr = *find;
349 // monitor expires after 204
350 if (ptr->gave204)
351 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800352 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700353 return;
354 }
355 ptr->populateResp(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500356 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700357}
358
359inline void requestRoutesTask(App& app)
360{
361 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700362 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700363 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700364 [&app](const crow::Request& req,
365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
366 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000367 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700368 {
369 return;
370 }
Ed Tanous3544d2a2023-08-06 18:12:20 -0700371 auto find = std::ranges::find_if(
372 task::tasks,
Ed Tanous002d39b2022-05-31 08:59:27 -0700373 [&strParam](const std::shared_ptr<task::TaskData>& task) {
374 if (!task)
375 {
376 return false;
377 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700378
Ed Tanous002d39b2022-05-31 08:59:27 -0700379 // we compare against the string version as on failure
380 // strtoul returns 0
381 return std::to_string(task->index) == strParam;
Patrick Williams5a39f772023-10-20 11:20:21 -0500382 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700383
384 if (find == task::tasks.end())
385 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800386 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 return;
388 }
389
Ed Tanous02cad962022-06-30 16:50:15 -0700390 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700391
392 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
393 asyncResp->res.jsonValue["Id"] = strParam;
394 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
395 asyncResp->res.jsonValue["TaskState"] = ptr->state;
396 asyncResp->res.jsonValue["StartTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700397 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
Ed Tanous002d39b2022-05-31 08:59:27 -0700398 if (ptr->endTime)
399 {
400 asyncResp->res.jsonValue["EndTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700401 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 }
403 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
404 asyncResp->res.jsonValue["Messages"] = ptr->messages;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700405 asyncResp->res.jsonValue["@odata.id"] =
406 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 if (!ptr->gave204)
408 {
Ed Tanousfdbce792024-06-26 14:48:46 -0700409 asyncResp->res.jsonValue["TaskMonitor"] = boost::urls::format(
410 "/redfish/v1/TaskService/TaskMonitors/{}", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700411 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700412
413 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
414
Ed Tanous002d39b2022-05-31 08:59:27 -0700415 if (ptr->payload)
416 {
417 const task::Payload& p = *(ptr->payload);
418 asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
419 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
420 p.httpOperation;
421 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
422 asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
Ed Tanous83e37252024-07-22 14:06:07 -0700423 -1, ' ', true, nlohmann::json::error_handler_t::replace);
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 }
425 asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
Patrick Williams5a39f772023-10-20 11:20:21 -0500426 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700427}
James Feist46229572020-02-19 15:11:58 -0800428
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700429inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800430{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700431 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700432 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700433 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700434 [&app](const crow::Request& req,
435 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700437 {
438 return;
439 }
440 asyncResp->res.jsonValue["@odata.type"] =
441 "#TaskCollection.TaskCollection";
442 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
443 asyncResp->res.jsonValue["Name"] = "Task Collection";
444 asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
445 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
446 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800447
Ed Tanous002d39b2022-05-31 08:59:27 -0700448 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
449 {
450 if (task == nullptr)
451 {
452 continue; // shouldn't be possible
453 }
Ed Tanous613dabe2022-07-09 11:17:36 -0700454 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700455 member["@odata.id"] =
456 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
457 std::to_string(task->index));
Ed Tanous613dabe2022-07-09 11:17:36 -0700458 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700459 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500460 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700461}
zhanghch058d1b46d2021-04-01 11:18:24 +0800462
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700463inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800464{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700465 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700466 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700467 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700468 [&app](const crow::Request& req,
469 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000470 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700471 {
472 return;
473 }
474 asyncResp->res.jsonValue["@odata.type"] =
475 "#TaskService.v1_1_4.TaskService";
476 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
477 asyncResp->res.jsonValue["Name"] = "Task Service";
478 asyncResp->res.jsonValue["Id"] = "TaskService";
479 asyncResp->res.jsonValue["DateTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700480 redfish::time_utils::getDateTimeOffsetNow().first;
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
James Feist46229572020-02-19 15:11:58 -0800482
Ed Tanous002d39b2022-05-31 08:59:27 -0700483 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
James Feist46229572020-02-19 15:11:58 -0800484
Ed Tanous002d39b2022-05-31 08:59:27 -0700485 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
486 asyncResp->res.jsonValue["ServiceEnabled"] = true;
487 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
488 "/redfish/v1/TaskService/Tasks";
Patrick Williams5a39f772023-10-20 11:20:21 -0500489 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700490}
James Feist46229572020-02-19 15:11:58 -0800491
492} // namespace redfish