xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision 26ccae32112679c4653c1e3f8a1203c828bea05c)
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"
21d093c996SEd Tanous #include "health.hpp"
221aa0c2b8SEd Tanous #include "http/parsing.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
253ccb3adbSEd Tanous #include "task_messages.hpp"
263ccb3adbSEd Tanous 
27d43cd0caSEd Tanous #include <boost/asio/post.hpp>
28d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
293ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
301214b7e7SGunnar Mills 
311214b7e7SGunnar Mills #include <chrono>
323ccb3adbSEd Tanous #include <memory>
3346229577SJames Feist #include <variant>
3446229577SJames Feist 
3546229577SJames Feist namespace redfish
3646229577SJames Feist {
3746229577SJames Feist 
3846229577SJames Feist namespace task
3946229577SJames Feist {
4046229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
4146229577SJames Feist 
42cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
4346229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
4446229577SJames Feist 
4532898ceaSJames Feist constexpr bool completed = true;
4632898ceaSJames Feist 
47fe306728SJames Feist struct Payload
48fe306728SJames Feist {
494e23a444SEd Tanous     explicit Payload(const crow::Request& req) :
50fe306728SJames Feist         targetUri(req.url), httpOperation(req.methodString()),
511aa0c2b8SEd Tanous         httpHeaders(nlohmann::json::array())
52fe306728SJames Feist     {
53fe306728SJames Feist         using field_ns = boost::beast::http::field;
54fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
55fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
56fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
57fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
58fe306728SJames Feist                                field_ns::upgrade};
59fe306728SJames Feist 
601aa0c2b8SEd Tanous         JsonParseResult ret = parseRequestAsJson(req, jsonBody);
611aa0c2b8SEd Tanous         if (ret != JsonParseResult::Success)
62fe306728SJames Feist         {
631aa0c2b8SEd Tanous             return;
64fe306728SJames Feist         }
65fe306728SJames Feist 
66fe306728SJames Feist         for (const auto& field : req.fields)
67fe306728SJames Feist         {
68fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
69fe306728SJames Feist                           field.name()) == headerWhitelist.end())
70fe306728SJames Feist             {
71fe306728SJames Feist                 continue;
72fe306728SJames Feist             }
73fe306728SJames Feist             std::string header;
74fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
75fe306728SJames Feist                            field.value().size());
76fe306728SJames Feist             header += field.name_string();
77fe306728SJames Feist             header += ": ";
78fe306728SJames Feist             header += field.value();
79fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
80fe306728SJames Feist         }
81fe306728SJames Feist     }
82fe306728SJames Feist     Payload() = delete;
83fe306728SJames Feist 
84fe306728SJames Feist     std::string targetUri;
85fe306728SJames Feist     std::string httpOperation;
86fe306728SJames Feist     nlohmann::json httpHeaders;
87fe306728SJames Feist     nlohmann::json jsonBody;
88fe306728SJames Feist };
89fe306728SJames Feist 
9046229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
9146229577SJames Feist {
9246229577SJames Feist   private:
9359d494eeSPatrick Williams     TaskData(
9459d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
9546229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
9623a21a1cSEd Tanous         const std::string& matchIn, size_t idx) :
9746229577SJames Feist         callback(std::move(handler)),
9823a21a1cSEd Tanous         matchStr(matchIn), index(idx),
9946229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
10046229577SJames Feist             std::chrono::system_clock::now())),
10146229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
10246229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
10346229577SJames Feist 
1041214b7e7SGunnar Mills     {}
10546229577SJames Feist 
10646229577SJames Feist   public:
107d609fd6eSEd Tanous     TaskData() = delete;
108d609fd6eSEd Tanous 
10946229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
11059d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
11146229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
11246229577SJames Feist         const std::string& match)
11346229577SJames Feist     {
11446229577SJames Feist         static size_t lastTask = 0;
11546229577SJames Feist         struct MakeSharedHelper : public TaskData
11646229577SJames Feist         {
11746229577SJames Feist             MakeSharedHelper(
1181214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
11959d494eeSPatrick Williams                                    sdbusplus::message_t&,
12046229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
12123a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
12223a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1231214b7e7SGunnar Mills             {}
12446229577SJames Feist         };
12546229577SJames Feist 
12646229577SJames Feist         if (tasks.size() >= maxTaskCount)
12746229577SJames Feist         {
12802cad96eSEd Tanous             const auto& last = tasks.front();
12946229577SJames Feist 
13046229577SJames Feist             // destroy all references
13146229577SJames Feist             last->timer.cancel();
13246229577SJames Feist             last->match.reset();
13346229577SJames Feist             tasks.pop_front();
13446229577SJames Feist         }
13546229577SJames Feist 
13646229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13746229577SJames Feist             std::move(handler), match, lastTask++));
13846229577SJames Feist     }
13946229577SJames Feist 
14046229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
14146229577SJames Feist     {
14246229577SJames Feist         if (!endTime)
14346229577SJames Feist         {
14446229577SJames Feist             res.result(boost::beast::http::status::accepted);
14546229577SJames Feist             std::string strIdx = std::to_string(index);
14646229577SJames Feist             std::string uri = "/redfish/v1/TaskService/Tasks/" + strIdx;
1471476687dSEd Tanous 
1481476687dSEd Tanous             res.jsonValue["@odata.id"] = uri;
1491476687dSEd Tanous             res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
1501476687dSEd Tanous             res.jsonValue["Id"] = strIdx;
1511476687dSEd Tanous             res.jsonValue["TaskState"] = state;
1521476687dSEd Tanous             res.jsonValue["TaskStatus"] = status;
1531476687dSEd Tanous 
15446229577SJames Feist             res.addHeader(boost::beast::http::field::location,
15546229577SJames Feist                           uri + "/Monitor");
15646229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
15746229577SJames Feist                           std::to_string(retryAfterSeconds));
15846229577SJames Feist         }
15946229577SJames Feist         else if (!gave204)
16046229577SJames Feist         {
16146229577SJames Feist             res.result(boost::beast::http::status::no_content);
16246229577SJames Feist             gave204 = true;
16346229577SJames Feist         }
16446229577SJames Feist     }
16546229577SJames Feist 
166d609fd6eSEd Tanous     void finishTask()
16746229577SJames Feist     {
16846229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
16946229577SJames Feist             std::chrono::system_clock::now());
17046229577SJames Feist     }
17146229577SJames Feist 
172fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
17346229577SJames Feist     {
17446229577SJames Feist         timer.expires_after(timeout);
17546229577SJames Feist         timer.async_wait(
17646229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
17746229577SJames Feist             if (ec == boost::asio::error::operation_aborted)
17846229577SJames Feist             {
1794e0453b1SGunnar Mills                 return; // completed successfully
18046229577SJames Feist             }
18146229577SJames Feist             if (!ec)
18246229577SJames Feist             {
18346229577SJames Feist                 // change ec to error as timer expired
18446229577SJames Feist                 ec = boost::asio::error::operation_aborted;
18546229577SJames Feist             }
18646229577SJames Feist             self->match.reset();
18759d494eeSPatrick Williams             sdbusplus::message_t msg;
18846229577SJames Feist             self->finishTask();
18946229577SJames Feist             self->state = "Cancelled";
19046229577SJames Feist             self->status = "Warning";
191e5d5006bSJames Feist             self->messages.emplace_back(
192e5d5006bSJames Feist                 messages::taskAborted(std::to_string(self->index)));
193e7686576SSunitha Harish             // Send event :TaskAborted
194e7686576SSunitha Harish             self->sendTaskEvent(self->state, self->index);
19546229577SJames Feist             self->callback(ec, msg, self);
19646229577SJames Feist         });
197fd9ab9e1SJames Feist     }
198fd9ab9e1SJames Feist 
199*26ccae32SEd Tanous     static void sendTaskEvent(std::string_view state, size_t index)
200e7686576SSunitha Harish     {
201e7686576SSunitha Harish         std::string origin =
202e7686576SSunitha Harish             "/redfish/v1/TaskService/Tasks/" + std::to_string(index);
203e7686576SSunitha Harish         std::string resType = "Task";
204e7686576SSunitha Harish         // TaskState enums which should send out an event are:
205e7686576SSunitha Harish         // "Starting" = taskResumed
206e7686576SSunitha Harish         // "Running" = taskStarted
207e7686576SSunitha Harish         // "Suspended" = taskPaused
208e7686576SSunitha Harish         // "Interrupted" = taskPaused
209e7686576SSunitha Harish         // "Pending" = taskPaused
210e7686576SSunitha Harish         // "Stopping" = taskAborted
211e7686576SSunitha Harish         // "Completed" = taskCompletedOK
212e7686576SSunitha Harish         // "Killed" = taskRemoved
213e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
214e7686576SSunitha Harish         // "Cancelled" = taskCancelled
215e7686576SSunitha Harish         if (state == "Starting")
216e7686576SSunitha Harish         {
217e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
218e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
219e7686576SSunitha Harish                 resType);
220e7686576SSunitha Harish         }
221e7686576SSunitha Harish         else if (state == "Running")
222e7686576SSunitha Harish         {
223e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
224e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
225e7686576SSunitha Harish                 resType);
226e7686576SSunitha Harish         }
227e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
228e7686576SSunitha Harish                  (state == "Pending"))
229e7686576SSunitha Harish         {
230e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
231e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
232e7686576SSunitha Harish                 resType);
233e7686576SSunitha Harish         }
234e7686576SSunitha Harish         else if (state == "Stopping")
235e7686576SSunitha Harish         {
236e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
237e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
238e7686576SSunitha Harish                 resType);
239e7686576SSunitha Harish         }
240e7686576SSunitha Harish         else if (state == "Completed")
241e7686576SSunitha Harish         {
242e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
243e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
244e7686576SSunitha Harish                 origin, resType);
245e7686576SSunitha Harish         }
246e7686576SSunitha Harish         else if (state == "Killed")
247e7686576SSunitha Harish         {
248e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
249e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
250e7686576SSunitha Harish                 resType);
251e7686576SSunitha Harish         }
252e7686576SSunitha Harish         else if (state == "Exception")
253e7686576SSunitha Harish         {
254e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
255e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
256e7686576SSunitha Harish                 origin, resType);
257e7686576SSunitha Harish         }
258e7686576SSunitha Harish         else if (state == "Cancelled")
259e7686576SSunitha Harish         {
260e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
261e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
262e7686576SSunitha Harish                 resType);
263e7686576SSunitha Harish         }
264e7686576SSunitha Harish         else
265e7686576SSunitha Harish         {
266e7686576SSunitha Harish             BMCWEB_LOG_INFO << "sendTaskEvent: No events to send";
267e7686576SSunitha Harish         }
268e7686576SSunitha Harish     }
269e7686576SSunitha Harish 
270fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
271fd9ab9e1SJames Feist     {
272fd9ab9e1SJames Feist         if (match)
273fd9ab9e1SJames Feist         {
274fd9ab9e1SJames Feist             return;
275fd9ab9e1SJames Feist         }
27659d494eeSPatrick Williams         match = std::make_unique<sdbusplus::bus::match_t>(
27759d494eeSPatrick Williams             static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
278fd9ab9e1SJames Feist             matchStr,
27959d494eeSPatrick Williams             [self = shared_from_this()](sdbusplus::message_t& message) {
280fd9ab9e1SJames Feist             boost::system::error_code ec;
281fd9ab9e1SJames Feist 
282fd9ab9e1SJames Feist             // callback to return True if callback is done, callback needs
283fd9ab9e1SJames Feist             // to update status itself if needed
284fd9ab9e1SJames Feist             if (self->callback(ec, message, self) == task::completed)
285fd9ab9e1SJames Feist             {
286fd9ab9e1SJames Feist                 self->timer.cancel();
287fd9ab9e1SJames Feist                 self->finishTask();
288fd9ab9e1SJames Feist 
289e7686576SSunitha Harish                 // Send event
290e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
291e7686576SSunitha Harish 
292fd9ab9e1SJames Feist                 // reset the match after the callback was successful
293fd9ab9e1SJames Feist                 boost::asio::post(
294fd9ab9e1SJames Feist                     crow::connections::systemBus->get_io_context(),
295fd9ab9e1SJames Feist                     [self] { self->match.reset(); });
296fd9ab9e1SJames Feist                 return;
297fd9ab9e1SJames Feist             }
298fd9ab9e1SJames Feist             });
299fd9ab9e1SJames Feist 
300fd9ab9e1SJames Feist         extendTimer(timeout);
301e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
302e7686576SSunitha Harish         // Send event : TaskStarted
303e7686576SSunitha Harish         sendTaskEvent(state, index);
30446229577SJames Feist     }
30546229577SJames Feist 
30659d494eeSPatrick Williams     std::function<bool(boost::system::error_code, sdbusplus::message_t&,
30746229577SJames Feist                        const std::shared_ptr<TaskData>&)>
30846229577SJames Feist         callback;
30946229577SJames Feist     std::string matchStr;
31046229577SJames Feist     size_t index;
31146229577SJames Feist     time_t startTime;
31246229577SJames Feist     std::string status;
31346229577SJames Feist     std::string state;
31446229577SJames Feist     nlohmann::json messages;
31546229577SJames Feist     boost::asio::steady_timer timer;
31659d494eeSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> match;
31746229577SJames Feist     std::optional<time_t> endTime;
318fe306728SJames Feist     std::optional<Payload> payload;
31946229577SJames Feist     bool gave204 = false;
3206868ff50SGeorge Liu     int percentComplete = 0;
32146229577SJames Feist };
32246229577SJames Feist 
32346229577SJames Feist } // namespace task
32446229577SJames Feist 
3257e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
32646229577SJames Feist {
3277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
328ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33045ca1b86SEd Tanous             [&app](const crow::Request& req,
3317e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3327e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
33445ca1b86SEd Tanous         {
33545ca1b86SEd Tanous             return;
33645ca1b86SEd Tanous         }
33746229577SJames Feist         auto find = std::find_if(
33846229577SJames Feist             task::tasks.begin(), task::tasks.end(),
33946229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
34046229577SJames Feist             if (!task)
34146229577SJames Feist             {
34246229577SJames Feist                 return false;
34346229577SJames Feist             }
34446229577SJames Feist 
3457e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3467e860f15SJohn Edward Broadbent             // strtoul returns 0
34746229577SJames Feist             return std::to_string(task->index) == strParam;
34846229577SJames Feist             });
34946229577SJames Feist 
35046229577SJames Feist         if (find == task::tasks.end())
35146229577SJames Feist         {
352d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
35346229577SJames Feist             return;
35446229577SJames Feist         }
35546229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
35646229577SJames Feist         // monitor expires after 204
35746229577SJames Feist         if (ptr->gave204)
35846229577SJames Feist         {
359d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
36046229577SJames Feist             return;
36146229577SJames Feist         }
36246229577SJames Feist         ptr->populateResp(asyncResp->res);
3637e860f15SJohn Edward Broadbent         });
36446229577SJames Feist }
36546229577SJames Feist 
3667e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
36746229577SJames Feist {
3687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
369ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
37145ca1b86SEd Tanous             [&app](const crow::Request& req,
3727e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3737e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37545ca1b86SEd Tanous         {
37645ca1b86SEd Tanous             return;
37745ca1b86SEd Tanous         }
37846229577SJames Feist         auto find = std::find_if(
37946229577SJames Feist             task::tasks.begin(), task::tasks.end(),
38046229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
38146229577SJames Feist             if (!task)
38246229577SJames Feist             {
38346229577SJames Feist                 return false;
38446229577SJames Feist             }
38546229577SJames Feist 
3867e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3877e860f15SJohn Edward Broadbent             // strtoul returns 0
38846229577SJames Feist             return std::to_string(task->index) == strParam;
38946229577SJames Feist             });
39046229577SJames Feist 
39146229577SJames Feist         if (find == task::tasks.end())
39246229577SJames Feist         {
393d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
39446229577SJames Feist             return;
39546229577SJames Feist         }
39646229577SJames Feist 
39702cad96eSEd Tanous         const std::shared_ptr<task::TaskData>& ptr = *find;
39846229577SJames Feist 
39946229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
40046229577SJames Feist         asyncResp->res.jsonValue["Id"] = strParam;
40146229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task " + strParam;
40246229577SJames Feist         asyncResp->res.jsonValue["TaskState"] = ptr->state;
40346229577SJames Feist         asyncResp->res.jsonValue["StartTime"] =
4042b82937eSEd Tanous             redfish::time_utils::getDateTimeStdtime(ptr->startTime);
40546229577SJames Feist         if (ptr->endTime)
40646229577SJames Feist         {
40746229577SJames Feist             asyncResp->res.jsonValue["EndTime"] =
4082b82937eSEd Tanous                 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
40946229577SJames Feist         }
41046229577SJames Feist         asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
41146229577SJames Feist         asyncResp->res.jsonValue["Messages"] = ptr->messages;
412eddfc437SWilly Tu         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
413eddfc437SWilly Tu             "redfish", "v1", "TaskService", "Tasks", strParam);
41446229577SJames Feist         if (!ptr->gave204)
41546229577SJames Feist         {
41646229577SJames Feist             asyncResp->res.jsonValue["TaskMonitor"] =
417002d39b4SEd Tanous                 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
41846229577SJames Feist         }
419fe306728SJames Feist         if (ptr->payload)
420fe306728SJames Feist         {
4215fb91ba4SEd Tanous             const task::Payload& p = *(ptr->payload);
422002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
4231476687dSEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
4241476687dSEd Tanous                 p.httpOperation;
425002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
426002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
427002d39b4SEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace);
428fe306728SJames Feist         }
429002d39b4SEd Tanous         asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
4307e860f15SJohn Edward Broadbent         });
43146229577SJames Feist }
43246229577SJames Feist 
4337e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
43446229577SJames Feist {
4357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
436ed398213SEd Tanous         .privileges(redfish::privileges::getTaskCollection)
4377e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
43845ca1b86SEd Tanous             [&app](const crow::Request& req,
4397e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4403ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
44145ca1b86SEd Tanous         {
44245ca1b86SEd Tanous             return;
44345ca1b86SEd Tanous         }
44446229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
44546229577SJames Feist             "#TaskCollection.TaskCollection";
446002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
44746229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Collection";
448002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
44946229577SJames Feist         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
45046229577SJames Feist         members = nlohmann::json::array();
45146229577SJames Feist 
45246229577SJames Feist         for (const std::shared_ptr<task::TaskData>& task : task::tasks)
45346229577SJames Feist         {
45446229577SJames Feist             if (task == nullptr)
45546229577SJames Feist             {
45646229577SJames Feist                 continue; // shouldn't be possible
45746229577SJames Feist             }
458613dabeaSEd Tanous             nlohmann::json::object_t member;
459eddfc437SWilly Tu             member["@odata.id"] = crow::utility::urlFromPieces(
460eddfc437SWilly Tu                 "redfish", "v1", "TaskService", "Tasks",
461eddfc437SWilly Tu                 std::to_string(task->index));
462613dabeaSEd Tanous             members.emplace_back(std::move(member));
46346229577SJames Feist         }
4647e860f15SJohn Edward Broadbent         });
46546229577SJames Feist }
46646229577SJames Feist 
4677e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
46846229577SJames Feist {
4697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
470ed398213SEd Tanous         .privileges(redfish::privileges::getTaskService)
4717e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
47245ca1b86SEd Tanous             [&app](const crow::Request& req,
4737e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
47545ca1b86SEd Tanous         {
47645ca1b86SEd Tanous             return;
47745ca1b86SEd Tanous         }
47846229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
47946229577SJames Feist             "#TaskService.v1_1_4.TaskService";
480002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
48146229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Service";
48246229577SJames Feist         asyncResp->res.jsonValue["Id"] = "TaskService";
4837e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["DateTime"] =
4842b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow().first;
485002d39b4SEd Tanous         asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
48646229577SJames Feist 
487002d39b4SEd Tanous         asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
48846229577SJames Feist 
48946229577SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
49046229577SJames Feist         health->populate();
49146229577SJames Feist         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
49246229577SJames Feist         asyncResp->res.jsonValue["ServiceEnabled"] = true;
4931476687dSEd Tanous         asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
4941476687dSEd Tanous             "/redfish/v1/TaskService/Tasks";
4957e860f15SJohn Edward Broadbent         });
49646229577SJames Feist }
49746229577SJames Feist 
49846229577SJames Feist } // namespace redfish
499