blob: 8a5c30b769dbd6144302c4460cdae7e32e8a0dd1 [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
Willy Tu13451e32023-05-24 16:08:18 -070017#include "bmcweb_config.h"
James Feist46229572020-02-19 15:11:58 -080018
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "app.hpp"
20#include "dbus_utility.hpp"
21#include "event_service_manager.hpp"
Ed Tanousd093c992023-01-19 19:01:49 -080022#include "health.hpp"
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010023#include "http/parsing.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
25#include "registries/privilege_registry.hpp"
26#include "task_messages.hpp"
27
Ed Tanousd43cd0c2020-09-30 20:46:53 -070028#include <boost/asio/post.hpp>
29#include <boost/asio/steady_timer.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070030#include <boost/url/format.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include <sdbusplus/bus/match.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050032
33#include <chrono>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080034#include <memory>
James Feist46229572020-02-19 15:11:58 -080035#include <variant>
36
37namespace redfish
38{
39
40namespace task
41{
42constexpr size_t maxTaskCount = 100; // arbitrary limit
43
Ed Tanouscf9e4172022-12-21 09:30:16 -080044// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
James Feist46229572020-02-19 15:11:58 -080045static std::deque<std::shared_ptr<struct TaskData>> tasks;
46
James Feist32898ce2020-03-10 16:16:52 -070047constexpr bool completed = true;
48
James Feistfe306722020-03-12 16:32:08 -070049struct Payload
50{
Ed Tanous4e23a442022-06-06 09:57:26 -070051 explicit Payload(const crow::Request& req) :
Ed Tanous39662a32023-02-06 15:09:46 -080052 targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010053 httpHeaders(nlohmann::json::array())
James Feistfe306722020-03-12 16:32:08 -070054 {
55 using field_ns = boost::beast::http::field;
56 constexpr const std::array<boost::beast::http::field, 7>
57 headerWhitelist = {field_ns::accept, field_ns::accept_encoding,
58 field_ns::user_agent, field_ns::host,
59 field_ns::connection, field_ns::content_length,
60 field_ns::upgrade};
61
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010062 JsonParseResult ret = parseRequestAsJson(req, jsonBody);
63 if (ret != JsonParseResult::Success)
James Feistfe306722020-03-12 16:32:08 -070064 {
Ed Tanous1aa0c2b2022-02-08 12:24:30 +010065 return;
James Feistfe306722020-03-12 16:32:08 -070066 }
67
Ed Tanous98fe7402023-02-14 14:50:33 -080068 for (const auto& field : req.fields())
James Feistfe306722020-03-12 16:32:08 -070069 {
70 if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
71 field.name()) == headerWhitelist.end())
72 {
73 continue;
74 }
75 std::string header;
76 header.reserve(field.name_string().size() + 2 +
77 field.value().size());
78 header += field.name_string();
79 header += ": ";
80 header += field.value();
81 httpHeaders.emplace_back(std::move(header));
82 }
83 }
84 Payload() = delete;
85
86 std::string targetUri;
87 std::string httpOperation;
88 nlohmann::json httpHeaders;
89 nlohmann::json jsonBody;
90};
91
James Feist46229572020-02-19 15:11:58 -080092struct TaskData : std::enable_shared_from_this<TaskData>
93{
94 private:
Patrick Williams59d494e2022-07-22 19:26:55 -050095 TaskData(
96 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
97 const std::shared_ptr<TaskData>&)>&& handler,
98 const std::string& matchIn, size_t idx) :
James Feist46229572020-02-19 15:11:58 -080099 callback(std::move(handler)),
Ed Tanous23a21a12020-07-25 04:45:05 +0000100 matchStr(matchIn), index(idx),
James Feist46229572020-02-19 15:11:58 -0800101 startTime(std::chrono::system_clock::to_time_t(
102 std::chrono::system_clock::now())),
103 status("OK"), state("Running"), messages(nlohmann::json::array()),
104 timer(crow::connections::systemBus->get_io_context())
105
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500106 {}
James Feist46229572020-02-19 15:11:58 -0800107
108 public:
Ed Tanousd609fd62020-09-28 19:08:03 -0700109 TaskData() = delete;
110
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500111 static std::shared_ptr<TaskData>& createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -0500112 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113 const std::shared_ptr<TaskData>&)>&& handler,
114 const std::string& match)
James Feist46229572020-02-19 15:11:58 -0800115 {
116 static size_t lastTask = 0;
117 struct MakeSharedHelper : public TaskData
118 {
119 MakeSharedHelper(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500120 std::function<bool(boost::system::error_code,
Patrick Williams59d494e2022-07-22 19:26:55 -0500121 sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122 const std::shared_ptr<TaskData>&)>&& handler,
Ed Tanous23a21a12020-07-25 04:45:05 +0000123 const std::string& match2, size_t idx) :
124 TaskData(std::move(handler), match2, idx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500125 {}
James Feist46229572020-02-19 15:11:58 -0800126 };
127
128 if (tasks.size() >= maxTaskCount)
129 {
Ed Tanous02cad962022-06-30 16:50:15 -0700130 const auto& last = tasks.front();
James Feist46229572020-02-19 15:11:58 -0800131
132 // destroy all references
133 last->timer.cancel();
134 last->match.reset();
135 tasks.pop_front();
136 }
137
138 return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
139 std::move(handler), match, lastTask++));
140 }
141
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500142 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800143 {
144 if (!endTime)
145 {
146 res.result(boost::beast::http::status::accepted);
147 std::string strIdx = std::to_string(index);
148 std::string uri = "/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
James Feist46229572020-02-19 15:11:58 -0800156 res.addHeader(boost::beast::http::field::location,
157 uri + "/Monitor");
158 res.addHeader(boost::beast::http::field::retry_after,
159 std::to_string(retryAfterSeconds));
160 }
161 else if (!gave204)
162 {
163 res.result(boost::beast::http::status::no_content);
164 gave204 = true;
165 }
166 }
167
Ed Tanousd609fd62020-09-28 19:08:03 -0700168 void finishTask()
James Feist46229572020-02-19 15:11:58 -0800169 {
170 endTime = std::chrono::system_clock::to_time_t(
171 std::chrono::system_clock::now());
172 }
173
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500174 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800175 {
James Feist46229572020-02-19 15:11:58 -0800176 timer.expires_after(timeout);
177 timer.async_wait(
178 [self = shared_from_this()](boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700179 if (ec == boost::asio::error::operation_aborted)
180 {
181 return; // completed successfully
182 }
183 if (!ec)
184 {
185 // change ec to error as timer expired
186 ec = boost::asio::error::operation_aborted;
187 }
188 self->match.reset();
Patrick Williams59d494e2022-07-22 19:26:55 -0500189 sdbusplus::message_t msg;
Ed Tanous002d39b2022-05-31 08:59:27 -0700190 self->finishTask();
191 self->state = "Cancelled";
192 self->status = "Warning";
193 self->messages.emplace_back(
194 messages::taskAborted(std::to_string(self->index)));
195 // Send event :TaskAborted
196 self->sendTaskEvent(self->state, self->index);
197 self->callback(ec, msg, self);
198 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700199 }
200
Ed Tanous26ccae32023-02-16 10:28:44 -0800201 static void sendTaskEvent(std::string_view state, size_t index)
Sunitha Harishe7686572020-07-15 02:32:44 -0500202 {
Patrick Williams89492a12023-05-10 07:51:34 -0500203 std::string origin = "/redfish/v1/TaskService/Tasks/" +
204 std::to_string(index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500205 std::string resType = "Task";
206 // 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
217 if (state == "Starting")
218 {
219 redfish::EventServiceManager::getInstance().sendEvent(
220 redfish::messages::taskResumed(std::to_string(index)), origin,
221 resType);
222 }
223 else if (state == "Running")
224 {
225 redfish::EventServiceManager::getInstance().sendEvent(
226 redfish::messages::taskStarted(std::to_string(index)), origin,
227 resType);
228 }
229 else if ((state == "Suspended") || (state == "Interrupted") ||
230 (state == "Pending"))
231 {
232 redfish::EventServiceManager::getInstance().sendEvent(
233 redfish::messages::taskPaused(std::to_string(index)), origin,
234 resType);
235 }
236 else if (state == "Stopping")
237 {
238 redfish::EventServiceManager::getInstance().sendEvent(
239 redfish::messages::taskAborted(std::to_string(index)), origin,
240 resType);
241 }
242 else if (state == "Completed")
243 {
244 redfish::EventServiceManager::getInstance().sendEvent(
245 redfish::messages::taskCompletedOK(std::to_string(index)),
246 origin, resType);
247 }
248 else if (state == "Killed")
249 {
250 redfish::EventServiceManager::getInstance().sendEvent(
251 redfish::messages::taskRemoved(std::to_string(index)), origin,
252 resType);
253 }
254 else if (state == "Exception")
255 {
256 redfish::EventServiceManager::getInstance().sendEvent(
257 redfish::messages::taskCompletedWarning(std::to_string(index)),
258 origin, resType);
259 }
260 else if (state == "Cancelled")
261 {
262 redfish::EventServiceManager::getInstance().sendEvent(
263 redfish::messages::taskCancelled(std::to_string(index)), origin,
264 resType);
265 }
266 else
267 {
Ed Tanous62598e32023-07-17 17:06:25 -0700268 BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
Sunitha Harishe7686572020-07-15 02:32:44 -0500269 }
270 }
271
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700273 {
274 if (match)
275 {
276 return;
277 }
Patrick Williams59d494e2022-07-22 19:26:55 -0500278 match = std::make_unique<sdbusplus::bus::match_t>(
279 static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700280 matchStr,
Patrick Williams59d494e2022-07-22 19:26:55 -0500281 [self = shared_from_this()](sdbusplus::message_t& message) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700282 boost::system::error_code ec;
James Feistfd9ab9e2020-05-19 13:48:07 -0700283
Ed Tanous002d39b2022-05-31 08:59:27 -0700284 // callback to return True if callback is done, callback needs
285 // to update status itself if needed
286 if (self->callback(ec, message, self) == task::completed)
287 {
288 self->timer.cancel();
289 self->finishTask();
James Feistfd9ab9e2020-05-19 13:48:07 -0700290
Ed Tanous002d39b2022-05-31 08:59:27 -0700291 // Send event
292 self->sendTaskEvent(self->state, self->index);
Sunitha Harishe7686572020-07-15 02:32:44 -0500293
Ed Tanous002d39b2022-05-31 08:59:27 -0700294 // reset the match after the callback was successful
295 boost::asio::post(
296 crow::connections::systemBus->get_io_context(),
297 [self] { self->match.reset(); });
298 return;
299 }
James Feistfd9ab9e2020-05-19 13:48:07 -0700300 });
301
302 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700303 messages.emplace_back(messages::taskStarted(std::to_string(index)));
Sunitha Harishe7686572020-07-15 02:32:44 -0500304 // Send event : TaskStarted
305 sendTaskEvent(state, index);
James Feist46229572020-02-19 15:11:58 -0800306 }
307
Patrick Williams59d494e2022-07-22 19:26:55 -0500308 std::function<bool(boost::system::error_code, sdbusplus::message_t&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500309 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800310 callback;
311 std::string matchStr;
312 size_t index;
313 time_t startTime;
314 std::string status;
315 std::string state;
316 nlohmann::json messages;
317 boost::asio::steady_timer timer;
Patrick Williams59d494e2022-07-22 19:26:55 -0500318 std::unique_ptr<sdbusplus::bus::match_t> match;
James Feist46229572020-02-19 15:11:58 -0800319 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700320 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800321 bool gave204 = false;
George Liu6868ff52021-01-02 11:37:41 +0800322 int percentComplete = 0;
James Feist46229572020-02-19 15:11:58 -0800323};
324
325} // namespace task
326
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700327inline void requestRoutesTaskMonitor(App& app)
James Feist46229572020-02-19 15:11:58 -0800328{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700329 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
Ed Tanoused398212021-06-09 17:05:54 -0700330 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700331 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700332 [&app](const crow::Request& req,
333 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000335 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700336 {
337 return;
338 }
339 auto find = std::find_if(
340 task::tasks.begin(), task::tasks.end(),
341 [&strParam](const std::shared_ptr<task::TaskData>& task) {
342 if (!task)
343 {
344 return false;
345 }
James Feist46229572020-02-19 15:11:58 -0800346
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 // we compare against the string version as on failure
348 // strtoul returns 0
349 return std::to_string(task->index) == strParam;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700350 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700351
352 if (find == task::tasks.end())
353 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800354 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 return;
356 }
357 std::shared_ptr<task::TaskData>& ptr = *find;
358 // monitor expires after 204
359 if (ptr->gave204)
360 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800361 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 return;
363 }
364 ptr->populateResp(asyncResp->res);
365 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700366}
367
368inline void requestRoutesTask(App& app)
369{
370 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700371 .privileges(redfish::privileges::getTask)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700372 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700373 [&app](const crow::Request& req,
374 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
375 const std::string& strParam) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000376 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700377 {
378 return;
379 }
380 auto find = std::find_if(
381 task::tasks.begin(), task::tasks.end(),
382 [&strParam](const std::shared_ptr<task::TaskData>& task) {
383 if (!task)
384 {
385 return false;
386 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700387
Ed Tanous002d39b2022-05-31 08:59:27 -0700388 // we compare against the string version as on failure
389 // strtoul returns 0
390 return std::to_string(task->index) == strParam;
James Feist46229572020-02-19 15:11:58 -0800391 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700392
393 if (find == task::tasks.end())
394 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800395 messages::resourceNotFound(asyncResp->res, "Task", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700396 return;
397 }
398
Ed Tanous02cad962022-06-30 16:50:15 -0700399 const std::shared_ptr<task::TaskData>& ptr = *find;
Ed Tanous002d39b2022-05-31 08:59:27 -0700400
401 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
402 asyncResp->res.jsonValue["Id"] = strParam;
403 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
404 asyncResp->res.jsonValue["TaskState"] = ptr->state;
405 asyncResp->res.jsonValue["StartTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700406 redfish::time_utils::getDateTimeStdtime(ptr->startTime);
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 if (ptr->endTime)
408 {
409 asyncResp->res.jsonValue["EndTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700410 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
Ed Tanous002d39b2022-05-31 08:59:27 -0700411 }
412 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
413 asyncResp->res.jsonValue["Messages"] = ptr->messages;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700414 asyncResp->res.jsonValue["@odata.id"] =
415 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
Ed Tanous002d39b2022-05-31 08:59:27 -0700416 if (!ptr->gave204)
417 {
418 asyncResp->res.jsonValue["TaskMonitor"] =
419 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
420 }
Arun Thomas Baby5db7dfd2023-05-02 03:22:23 -0700421
422 asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
423
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 if (ptr->payload)
425 {
426 const task::Payload& p = *(ptr->payload);
427 asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
428 asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
429 p.httpOperation;
430 asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
431 asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
432 2, ' ', true, nlohmann::json::error_handler_t::replace);
433 }
434 asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
435 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700436}
James Feist46229572020-02-19 15:11:58 -0800437
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700438inline void requestRoutesTaskCollection(App& app)
James Feist46229572020-02-19 15:11:58 -0800439{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700440 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
Ed Tanoused398212021-06-09 17:05:54 -0700441 .privileges(redfish::privileges::getTaskCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700442 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700443 [&app](const crow::Request& req,
444 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000445 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700446 {
447 return;
448 }
449 asyncResp->res.jsonValue["@odata.type"] =
450 "#TaskCollection.TaskCollection";
451 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
452 asyncResp->res.jsonValue["Name"] = "Task Collection";
453 asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
454 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
455 members = nlohmann::json::array();
James Feist46229572020-02-19 15:11:58 -0800456
Ed Tanous002d39b2022-05-31 08:59:27 -0700457 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
458 {
459 if (task == nullptr)
460 {
461 continue; // shouldn't be possible
462 }
Ed Tanous613dabe2022-07-09 11:17:36 -0700463 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700464 member["@odata.id"] =
465 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
466 std::to_string(task->index));
Ed Tanous613dabe2022-07-09 11:17:36 -0700467 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700468 }
469 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700470}
zhanghch058d1b46d2021-04-01 11:18:24 +0800471
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700472inline void requestRoutesTaskService(App& app)
James Feist46229572020-02-19 15:11:58 -0800473{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700474 BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
Ed Tanoused398212021-06-09 17:05:54 -0700475 .privileges(redfish::privileges::getTaskService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700476 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700477 [&app](const crow::Request& req,
478 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000479 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700480 {
481 return;
482 }
483 asyncResp->res.jsonValue["@odata.type"] =
484 "#TaskService.v1_1_4.TaskService";
485 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
486 asyncResp->res.jsonValue["Name"] = "Task Service";
487 asyncResp->res.jsonValue["Id"] = "TaskService";
488 asyncResp->res.jsonValue["DateTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -0700489 redfish::time_utils::getDateTimeOffsetNow().first;
Ed Tanous002d39b2022-05-31 08:59:27 -0700490 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
James Feist46229572020-02-19 15:11:58 -0800491
Ed Tanous002d39b2022-05-31 08:59:27 -0700492 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
James Feist46229572020-02-19 15:11:58 -0800493
Willy Tu13451e32023-05-24 16:08:18 -0700494 if constexpr (bmcwebEnableHealthPopulate)
495 {
496 auto health = std::make_shared<HealthPopulate>(asyncResp);
497 health->populate();
498 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700499 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
500 asyncResp->res.jsonValue["ServiceEnabled"] = true;
501 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
502 "/redfish/v1/TaskService/Tasks";
503 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700504}
James Feist46229572020-02-19 15:11:58 -0800505
506} // namespace redfish