xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision fdbce79b9a238aefd52f70b2a3b3d119b595d7ca)
146229577SJames Feist /*
246229577SJames Feist // Copyright (c) 2020 Intel Corporation
346229577SJames Feist //
446229577SJames Feist // Licensed under the Apache License, Version 2.0 (the "License");
546229577SJames Feist // you may not use this file except in compliance with the License.
646229577SJames Feist // You may obtain a copy of the License at
746229577SJames Feist //
846229577SJames Feist //      http://www.apache.org/licenses/LICENSE-2.0
946229577SJames Feist //
1046229577SJames Feist // Unless required by applicable law or agreed to in writing, software
1146229577SJames Feist // distributed under the License is distributed on an "AS IS" BASIS,
1246229577SJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346229577SJames Feist // See the License for the specific language governing permissions and
1446229577SJames Feist // limitations under the License.
1546229577SJames Feist */
1646229577SJames Feist #pragma once
1746229577SJames Feist 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "event_service_manager.hpp"
211aa0c2b8SEd Tanous #include "http/parsing.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd Tanous #include "task_messages.hpp"
253ccb3adbSEd Tanous 
26d43cd0caSEd Tanous #include <boost/asio/post.hpp>
27d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
28ef4c65b7SEd Tanous #include <boost/url/format.hpp>
293ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
301214b7e7SGunnar Mills 
311214b7e7SGunnar Mills #include <chrono>
323ccb3adbSEd Tanous #include <memory>
333544d2a7SEd Tanous #include <ranges>
3446229577SJames Feist #include <variant>
3546229577SJames Feist 
3646229577SJames Feist namespace redfish
3746229577SJames Feist {
3846229577SJames Feist 
3946229577SJames Feist namespace task
4046229577SJames Feist {
4146229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
4246229577SJames Feist 
43cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
4446229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
4546229577SJames Feist 
4632898ceaSJames Feist constexpr bool completed = true;
4732898ceaSJames Feist 
48fe306728SJames Feist struct Payload
49fe306728SJames Feist {
504e23a444SEd Tanous     explicit Payload(const crow::Request& req) :
5139662a3bSEd Tanous         targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
521aa0c2b8SEd Tanous         httpHeaders(nlohmann::json::array())
53fe306728SJames Feist     {
54fe306728SJames Feist         using field_ns = boost::beast::http::field;
55fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
56fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
57fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
58fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
59fe306728SJames Feist                                field_ns::upgrade};
60fe306728SJames Feist 
611aa0c2b8SEd Tanous         JsonParseResult ret = parseRequestAsJson(req, jsonBody);
621aa0c2b8SEd Tanous         if (ret != JsonParseResult::Success)
63fe306728SJames Feist         {
641aa0c2b8SEd Tanous             return;
65fe306728SJames Feist         }
66fe306728SJames Feist 
6798fe740bSEd Tanous         for (const auto& field : req.fields())
68fe306728SJames Feist         {
693544d2a7SEd Tanous             if (std::ranges::find(headerWhitelist, field.name()) ==
703544d2a7SEd Tanous                 headerWhitelist.end())
71fe306728SJames Feist             {
72fe306728SJames Feist                 continue;
73fe306728SJames Feist             }
74fe306728SJames Feist             std::string header;
75fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
76fe306728SJames Feist                            field.value().size());
77fe306728SJames Feist             header += field.name_string();
78fe306728SJames Feist             header += ": ";
79fe306728SJames Feist             header += field.value();
80fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
81fe306728SJames Feist         }
82fe306728SJames Feist     }
83fe306728SJames Feist     Payload() = delete;
84fe306728SJames Feist 
85fe306728SJames Feist     std::string targetUri;
86fe306728SJames Feist     std::string httpOperation;
87fe306728SJames Feist     nlohmann::json httpHeaders;
88fe306728SJames Feist     nlohmann::json jsonBody;
89fe306728SJames Feist };
90fe306728SJames Feist 
9146229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
9246229577SJames Feist {
9346229577SJames Feist   private:
9459d494eeSPatrick Williams     TaskData(
9559d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
9646229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
9723a21a1cSEd Tanous         const std::string& matchIn, size_t idx) :
9846229577SJames Feist         callback(std::move(handler)),
9923a21a1cSEd Tanous         matchStr(matchIn), index(idx),
10046229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
10146229577SJames Feist             std::chrono::system_clock::now())),
10246229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
10346229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
10446229577SJames Feist 
1051214b7e7SGunnar Mills     {}
10646229577SJames Feist 
10746229577SJames Feist   public:
108d609fd6eSEd Tanous     TaskData() = delete;
109d609fd6eSEd Tanous 
11046229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
11159d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
11246229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
11346229577SJames Feist         const std::string& match)
11446229577SJames Feist     {
11546229577SJames Feist         static size_t lastTask = 0;
11646229577SJames Feist         struct MakeSharedHelper : public TaskData
11746229577SJames Feist         {
11846229577SJames Feist             MakeSharedHelper(
1191214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
12059d494eeSPatrick Williams                                    sdbusplus::message_t&,
12146229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
12223a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
12323a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1241214b7e7SGunnar Mills             {}
12546229577SJames Feist         };
12646229577SJames Feist 
12746229577SJames Feist         if (tasks.size() >= maxTaskCount)
12846229577SJames Feist         {
12902cad96eSEd Tanous             const auto& last = tasks.front();
13046229577SJames Feist 
13146229577SJames Feist             // destroy all references
13246229577SJames Feist             last->timer.cancel();
13346229577SJames Feist             last->match.reset();
13446229577SJames Feist             tasks.pop_front();
13546229577SJames Feist         }
13646229577SJames Feist 
13746229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13846229577SJames Feist             std::move(handler), match, lastTask++));
13946229577SJames Feist     }
14046229577SJames Feist 
14146229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
14246229577SJames Feist     {
14346229577SJames Feist         if (!endTime)
14446229577SJames Feist         {
14546229577SJames Feist             res.result(boost::beast::http::status::accepted);
14646229577SJames Feist             std::string strIdx = std::to_string(index);
147*fdbce79bSEd Tanous             boost::urls::url uri =
148*fdbce79bSEd Tanous                 boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx);
1491476687dSEd Tanous 
1501476687dSEd Tanous             res.jsonValue["@odata.id"] = uri;
1511476687dSEd Tanous             res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
1521476687dSEd Tanous             res.jsonValue["Id"] = strIdx;
1531476687dSEd Tanous             res.jsonValue["TaskState"] = state;
1541476687dSEd Tanous             res.jsonValue["TaskStatus"] = status;
1551476687dSEd Tanous 
156*fdbce79bSEd Tanous             boost::urls::url taskMonitor = boost::urls::format(
157*fdbce79bSEd Tanous                 "/redfish/v1/TaskService/TaskMonitors/{}", strIdx);
158*fdbce79bSEd Tanous 
15946229577SJames Feist             res.addHeader(boost::beast::http::field::location,
160*fdbce79bSEd Tanous                           taskMonitor.buffer());
16146229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
16246229577SJames Feist                           std::to_string(retryAfterSeconds));
16346229577SJames Feist         }
16446229577SJames Feist         else if (!gave204)
16546229577SJames Feist         {
16646229577SJames Feist             res.result(boost::beast::http::status::no_content);
16746229577SJames Feist             gave204 = true;
16846229577SJames Feist         }
16946229577SJames Feist     }
17046229577SJames Feist 
171d609fd6eSEd Tanous     void finishTask()
17246229577SJames Feist     {
17346229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
17446229577SJames Feist             std::chrono::system_clock::now());
17546229577SJames Feist     }
17646229577SJames Feist 
177fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
17846229577SJames Feist     {
17946229577SJames Feist         timer.expires_after(timeout);
18046229577SJames Feist         timer.async_wait(
18146229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
18246229577SJames Feist             if (ec == boost::asio::error::operation_aborted)
18346229577SJames Feist             {
1844e0453b1SGunnar Mills                 return; // completed successfully
18546229577SJames Feist             }
18646229577SJames Feist             if (!ec)
18746229577SJames Feist             {
18846229577SJames Feist                 // change ec to error as timer expired
18946229577SJames Feist                 ec = boost::asio::error::operation_aborted;
19046229577SJames Feist             }
19146229577SJames Feist             self->match.reset();
19259d494eeSPatrick Williams             sdbusplus::message_t msg;
19346229577SJames Feist             self->finishTask();
19446229577SJames Feist             self->state = "Cancelled";
19546229577SJames Feist             self->status = "Warning";
196e5d5006bSJames Feist             self->messages.emplace_back(
197e5d5006bSJames Feist                 messages::taskAborted(std::to_string(self->index)));
198e7686576SSunitha Harish             // Send event :TaskAborted
199e7686576SSunitha Harish             self->sendTaskEvent(self->state, self->index);
20046229577SJames Feist             self->callback(ec, msg, self);
20146229577SJames Feist         });
202fd9ab9e1SJames Feist     }
203fd9ab9e1SJames Feist 
20426ccae32SEd Tanous     static void sendTaskEvent(std::string_view state, size_t index)
205e7686576SSunitha Harish     {
206e7686576SSunitha Harish         // TaskState enums which should send out an event are:
207e7686576SSunitha Harish         // "Starting" = taskResumed
208e7686576SSunitha Harish         // "Running" = taskStarted
209e7686576SSunitha Harish         // "Suspended" = taskPaused
210e7686576SSunitha Harish         // "Interrupted" = taskPaused
211e7686576SSunitha Harish         // "Pending" = taskPaused
212e7686576SSunitha Harish         // "Stopping" = taskAborted
213e7686576SSunitha Harish         // "Completed" = taskCompletedOK
214e7686576SSunitha Harish         // "Killed" = taskRemoved
215e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
216e7686576SSunitha Harish         // "Cancelled" = taskCancelled
217f8fe2211SEd Tanous         nlohmann::json event;
218f8fe2211SEd Tanous         std::string indexStr = std::to_string(index);
219e7686576SSunitha Harish         if (state == "Starting")
220e7686576SSunitha Harish         {
221f8fe2211SEd Tanous             event = redfish::messages::taskResumed(indexStr);
222e7686576SSunitha Harish         }
223e7686576SSunitha Harish         else if (state == "Running")
224e7686576SSunitha Harish         {
225f8fe2211SEd Tanous             event = redfish::messages::taskStarted(indexStr);
226e7686576SSunitha Harish         }
227e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
228e7686576SSunitha Harish                  (state == "Pending"))
229e7686576SSunitha Harish         {
230f8fe2211SEd Tanous             event = redfish::messages::taskPaused(indexStr);
231e7686576SSunitha Harish         }
232e7686576SSunitha Harish         else if (state == "Stopping")
233e7686576SSunitha Harish         {
234f8fe2211SEd Tanous             event = redfish::messages::taskAborted(indexStr);
235e7686576SSunitha Harish         }
236e7686576SSunitha Harish         else if (state == "Completed")
237e7686576SSunitha Harish         {
238f8fe2211SEd Tanous             event = redfish::messages::taskCompletedOK(indexStr);
239e7686576SSunitha Harish         }
240e7686576SSunitha Harish         else if (state == "Killed")
241e7686576SSunitha Harish         {
242f8fe2211SEd Tanous             event = redfish::messages::taskRemoved(indexStr);
243e7686576SSunitha Harish         }
244e7686576SSunitha Harish         else if (state == "Exception")
245e7686576SSunitha Harish         {
246f8fe2211SEd Tanous             event = redfish::messages::taskCompletedWarning(indexStr);
247e7686576SSunitha Harish         }
248e7686576SSunitha Harish         else if (state == "Cancelled")
249e7686576SSunitha Harish         {
250f8fe2211SEd Tanous             event = redfish::messages::taskCancelled(indexStr);
251e7686576SSunitha Harish         }
252e7686576SSunitha Harish         else
253e7686576SSunitha Harish         {
25462598e31SEd Tanous             BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
255f8fe2211SEd Tanous             return;
256e7686576SSunitha Harish         }
257f8fe2211SEd Tanous         boost::urls::url origin =
258f8fe2211SEd Tanous             boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index);
259f8fe2211SEd Tanous         EventServiceManager::getInstance().sendEvent(event, origin.buffer(),
260f8fe2211SEd Tanous                                                      "Task");
261e7686576SSunitha Harish     }
262e7686576SSunitha Harish 
263fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
264fd9ab9e1SJames Feist     {
265fd9ab9e1SJames Feist         if (match)
266fd9ab9e1SJames Feist         {
267fd9ab9e1SJames Feist             return;
268fd9ab9e1SJames Feist         }
26959d494eeSPatrick Williams         match = std::make_unique<sdbusplus::bus::match_t>(
27059d494eeSPatrick Williams             static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
271fd9ab9e1SJames Feist             matchStr,
27259d494eeSPatrick Williams             [self = shared_from_this()](sdbusplus::message_t& message) {
273fd9ab9e1SJames Feist             boost::system::error_code ec;
274fd9ab9e1SJames Feist 
275fd9ab9e1SJames Feist             // callback to return True if callback is done, callback needs
276fd9ab9e1SJames Feist             // to update status itself if needed
277fd9ab9e1SJames Feist             if (self->callback(ec, message, self) == task::completed)
278fd9ab9e1SJames Feist             {
279fd9ab9e1SJames Feist                 self->timer.cancel();
280fd9ab9e1SJames Feist                 self->finishTask();
281fd9ab9e1SJames Feist 
282e7686576SSunitha Harish                 // Send event
283e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
284e7686576SSunitha Harish 
285fd9ab9e1SJames Feist                 // reset the match after the callback was successful
286fd9ab9e1SJames Feist                 boost::asio::post(
287fd9ab9e1SJames Feist                     crow::connections::systemBus->get_io_context(),
288fd9ab9e1SJames Feist                     [self] { self->match.reset(); });
289fd9ab9e1SJames Feist                 return;
290fd9ab9e1SJames Feist             }
291fd9ab9e1SJames Feist         });
292fd9ab9e1SJames Feist 
293fd9ab9e1SJames Feist         extendTimer(timeout);
294e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
295e7686576SSunitha Harish         // Send event : TaskStarted
296e7686576SSunitha Harish         sendTaskEvent(state, index);
29746229577SJames Feist     }
29846229577SJames Feist 
29959d494eeSPatrick Williams     std::function<bool(boost::system::error_code, sdbusplus::message_t&,
30046229577SJames Feist                        const std::shared_ptr<TaskData>&)>
30146229577SJames Feist         callback;
30246229577SJames Feist     std::string matchStr;
30346229577SJames Feist     size_t index;
30446229577SJames Feist     time_t startTime;
30546229577SJames Feist     std::string status;
30646229577SJames Feist     std::string state;
30746229577SJames Feist     nlohmann::json messages;
30846229577SJames Feist     boost::asio::steady_timer timer;
30959d494eeSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> match;
31046229577SJames Feist     std::optional<time_t> endTime;
311fe306728SJames Feist     std::optional<Payload> payload;
31246229577SJames Feist     bool gave204 = false;
3136868ff50SGeorge Liu     int percentComplete = 0;
31446229577SJames Feist };
31546229577SJames Feist 
31646229577SJames Feist } // namespace task
31746229577SJames Feist 
3187e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
31946229577SJames Feist {
320*fdbce79bSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/")
321ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3227e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
32345ca1b86SEd Tanous             [&app](const crow::Request& req,
3247e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3257e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
32745ca1b86SEd Tanous         {
32845ca1b86SEd Tanous             return;
32945ca1b86SEd Tanous         }
3303544d2a7SEd Tanous         auto find = std::ranges::find_if(
3313544d2a7SEd Tanous             task::tasks,
33246229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
33346229577SJames Feist             if (!task)
33446229577SJames Feist             {
33546229577SJames Feist                 return false;
33646229577SJames Feist             }
33746229577SJames Feist 
3387e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3397e860f15SJohn Edward Broadbent             // strtoul returns 0
34046229577SJames Feist             return std::to_string(task->index) == strParam;
34146229577SJames Feist         });
34246229577SJames Feist 
34346229577SJames Feist         if (find == task::tasks.end())
34446229577SJames Feist         {
345d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
34646229577SJames Feist             return;
34746229577SJames Feist         }
34846229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
34946229577SJames Feist         // monitor expires after 204
35046229577SJames Feist         if (ptr->gave204)
35146229577SJames Feist         {
352d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
35346229577SJames Feist             return;
35446229577SJames Feist         }
35546229577SJames Feist         ptr->populateResp(asyncResp->res);
3567e860f15SJohn Edward Broadbent     });
35746229577SJames Feist }
35846229577SJames Feist 
3597e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
36046229577SJames Feist {
3617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
362ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3637e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
36445ca1b86SEd Tanous             [&app](const crow::Request& req,
3657e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3667e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3673ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
36845ca1b86SEd Tanous         {
36945ca1b86SEd Tanous             return;
37045ca1b86SEd Tanous         }
3713544d2a7SEd Tanous         auto find = std::ranges::find_if(
3723544d2a7SEd Tanous             task::tasks,
37346229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
37446229577SJames Feist             if (!task)
37546229577SJames Feist             {
37646229577SJames Feist                 return false;
37746229577SJames Feist             }
37846229577SJames Feist 
3797e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3807e860f15SJohn Edward Broadbent             // strtoul returns 0
38146229577SJames Feist             return std::to_string(task->index) == strParam;
38246229577SJames Feist         });
38346229577SJames Feist 
38446229577SJames Feist         if (find == task::tasks.end())
38546229577SJames Feist         {
386d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
38746229577SJames Feist             return;
38846229577SJames Feist         }
38946229577SJames Feist 
39002cad96eSEd Tanous         const std::shared_ptr<task::TaskData>& ptr = *find;
39146229577SJames Feist 
39246229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
39346229577SJames Feist         asyncResp->res.jsonValue["Id"] = strParam;
39446229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task " + strParam;
39546229577SJames Feist         asyncResp->res.jsonValue["TaskState"] = ptr->state;
39646229577SJames Feist         asyncResp->res.jsonValue["StartTime"] =
3972b82937eSEd Tanous             redfish::time_utils::getDateTimeStdtime(ptr->startTime);
39846229577SJames Feist         if (ptr->endTime)
39946229577SJames Feist         {
40046229577SJames Feist             asyncResp->res.jsonValue["EndTime"] =
4012b82937eSEd Tanous                 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
40246229577SJames Feist         }
40346229577SJames Feist         asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
40446229577SJames Feist         asyncResp->res.jsonValue["Messages"] = ptr->messages;
405ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
406ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
40746229577SJames Feist         if (!ptr->gave204)
40846229577SJames Feist         {
409*fdbce79bSEd Tanous             asyncResp->res.jsonValue["TaskMonitor"] = boost::urls::format(
410*fdbce79bSEd Tanous                 "/redfish/v1/TaskService/TaskMonitors/{}", strParam);
41146229577SJames Feist         }
4125db7dfd6SArun Thomas Baby 
4135db7dfd6SArun Thomas Baby         asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
4145db7dfd6SArun Thomas Baby 
415fe306728SJames Feist         if (ptr->payload)
416fe306728SJames Feist         {
4175fb91ba4SEd Tanous             const task::Payload& p = *(ptr->payload);
418002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
4191476687dSEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
4201476687dSEd Tanous                 p.httpOperation;
421002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
422002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
42383e37257SEd Tanous                 -1, ' ', true, nlohmann::json::error_handler_t::replace);
424fe306728SJames Feist         }
425002d39b4SEd Tanous         asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
4267e860f15SJohn Edward Broadbent     });
42746229577SJames Feist }
42846229577SJames Feist 
4297e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
43046229577SJames Feist {
4317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
432ed398213SEd Tanous         .privileges(redfish::privileges::getTaskCollection)
4337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
43445ca1b86SEd Tanous             [&app](const crow::Request& req,
4357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
43745ca1b86SEd Tanous         {
43845ca1b86SEd Tanous             return;
43945ca1b86SEd Tanous         }
44046229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
44146229577SJames Feist             "#TaskCollection.TaskCollection";
442002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
44346229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Collection";
444002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
44546229577SJames Feist         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
44646229577SJames Feist         members = nlohmann::json::array();
44746229577SJames Feist 
44846229577SJames Feist         for (const std::shared_ptr<task::TaskData>& task : task::tasks)
44946229577SJames Feist         {
45046229577SJames Feist             if (task == nullptr)
45146229577SJames Feist             {
45246229577SJames Feist                 continue; // shouldn't be possible
45346229577SJames Feist             }
454613dabeaSEd Tanous             nlohmann::json::object_t member;
455ef4c65b7SEd Tanous             member["@odata.id"] =
456ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
457eddfc437SWilly Tu                                     std::to_string(task->index));
458613dabeaSEd Tanous             members.emplace_back(std::move(member));
45946229577SJames Feist         }
4607e860f15SJohn Edward Broadbent     });
46146229577SJames Feist }
46246229577SJames Feist 
4637e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
46446229577SJames Feist {
4657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
466ed398213SEd Tanous         .privileges(redfish::privileges::getTaskService)
4677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
46845ca1b86SEd Tanous             [&app](const crow::Request& req,
4697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
47145ca1b86SEd Tanous         {
47245ca1b86SEd Tanous             return;
47345ca1b86SEd Tanous         }
47446229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
47546229577SJames Feist             "#TaskService.v1_1_4.TaskService";
476002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
47746229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Service";
47846229577SJames Feist         asyncResp->res.jsonValue["Id"] = "TaskService";
4797e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["DateTime"] =
4802b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow().first;
481002d39b4SEd Tanous         asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
48246229577SJames Feist 
483002d39b4SEd Tanous         asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
48446229577SJames Feist 
48546229577SJames Feist         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
48646229577SJames Feist         asyncResp->res.jsonValue["ServiceEnabled"] = true;
4871476687dSEd Tanous         asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
4881476687dSEd Tanous             "/redfish/v1/TaskService/Tasks";
4897e860f15SJohn Edward Broadbent     });
49046229577SJames Feist }
49146229577SJames Feist 
49246229577SJames Feist } // namespace redfish
493