xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision 3ccb3adb9a14783f6bef601506de9f8bcae22d51)
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 
18*3ccb3adbSEd Tanous #include "app.hpp"
19*3ccb3adbSEd Tanous #include "dbus_utility.hpp"
20*3ccb3adbSEd Tanous #include "event_service_manager.hpp"
21*3ccb3adbSEd Tanous #include "query.hpp"
22*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
23*3ccb3adbSEd Tanous #include "task_messages.hpp"
24*3ccb3adbSEd Tanous 
25d43cd0caSEd Tanous #include <boost/asio/post.hpp>
26d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
27*3ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
281214b7e7SGunnar Mills 
291214b7e7SGunnar Mills #include <chrono>
30*3ccb3adbSEd Tanous #include <memory>
3146229577SJames Feist #include <variant>
3246229577SJames Feist 
3346229577SJames Feist namespace redfish
3446229577SJames Feist {
3546229577SJames Feist 
3646229577SJames Feist namespace task
3746229577SJames Feist {
3846229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
3946229577SJames Feist 
40cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
4146229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
4246229577SJames Feist 
4332898ceaSJames Feist constexpr bool completed = true;
4432898ceaSJames Feist 
45fe306728SJames Feist struct Payload
46fe306728SJames Feist {
474e23a444SEd Tanous     explicit Payload(const crow::Request& req) :
48fe306728SJames Feist         targetUri(req.url), httpOperation(req.methodString()),
49b31cef67SEd Tanous         httpHeaders(nlohmann::json::array()),
50b31cef67SEd Tanous         jsonBody(nlohmann::json::parse(req.body, nullptr, false))
51fe306728SJames Feist     {
52fe306728SJames Feist         using field_ns = boost::beast::http::field;
53fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
54fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
55fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
56fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
57fe306728SJames Feist                                field_ns::upgrade};
58fe306728SJames Feist 
59fe306728SJames Feist         if (jsonBody.is_discarded())
60fe306728SJames Feist         {
61fe306728SJames Feist             jsonBody = nullptr;
62fe306728SJames Feist         }
63fe306728SJames Feist 
64fe306728SJames Feist         for (const auto& field : req.fields)
65fe306728SJames Feist         {
66fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
67fe306728SJames Feist                           field.name()) == headerWhitelist.end())
68fe306728SJames Feist             {
69fe306728SJames Feist                 continue;
70fe306728SJames Feist             }
71fe306728SJames Feist             std::string header;
72fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
73fe306728SJames Feist                            field.value().size());
74fe306728SJames Feist             header += field.name_string();
75fe306728SJames Feist             header += ": ";
76fe306728SJames Feist             header += field.value();
77fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
78fe306728SJames Feist         }
79fe306728SJames Feist     }
80fe306728SJames Feist     Payload() = delete;
81fe306728SJames Feist 
82fe306728SJames Feist     std::string targetUri;
83fe306728SJames Feist     std::string httpOperation;
84fe306728SJames Feist     nlohmann::json httpHeaders;
85fe306728SJames Feist     nlohmann::json jsonBody;
86fe306728SJames Feist };
87fe306728SJames Feist 
8846229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
8946229577SJames Feist {
9046229577SJames Feist   private:
9159d494eeSPatrick Williams     TaskData(
9259d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
9346229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
9423a21a1cSEd Tanous         const std::string& matchIn, size_t idx) :
9546229577SJames Feist         callback(std::move(handler)),
9623a21a1cSEd Tanous         matchStr(matchIn), index(idx),
9746229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
9846229577SJames Feist             std::chrono::system_clock::now())),
9946229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
10046229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
10146229577SJames Feist 
1021214b7e7SGunnar Mills     {}
10346229577SJames Feist 
10446229577SJames Feist   public:
105d609fd6eSEd Tanous     TaskData() = delete;
106d609fd6eSEd Tanous 
10746229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
10859d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
10946229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
11046229577SJames Feist         const std::string& match)
11146229577SJames Feist     {
11246229577SJames Feist         static size_t lastTask = 0;
11346229577SJames Feist         struct MakeSharedHelper : public TaskData
11446229577SJames Feist         {
11546229577SJames Feist             MakeSharedHelper(
1161214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
11759d494eeSPatrick Williams                                    sdbusplus::message_t&,
11846229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
11923a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
12023a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1211214b7e7SGunnar Mills             {}
12246229577SJames Feist         };
12346229577SJames Feist 
12446229577SJames Feist         if (tasks.size() >= maxTaskCount)
12546229577SJames Feist         {
12602cad96eSEd Tanous             const auto& last = tasks.front();
12746229577SJames Feist 
12846229577SJames Feist             // destroy all references
12946229577SJames Feist             last->timer.cancel();
13046229577SJames Feist             last->match.reset();
13146229577SJames Feist             tasks.pop_front();
13246229577SJames Feist         }
13346229577SJames Feist 
13446229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13546229577SJames Feist             std::move(handler), match, lastTask++));
13646229577SJames Feist     }
13746229577SJames Feist 
13846229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
13946229577SJames Feist     {
14046229577SJames Feist         if (!endTime)
14146229577SJames Feist         {
14246229577SJames Feist             res.result(boost::beast::http::status::accepted);
14346229577SJames Feist             std::string strIdx = std::to_string(index);
14446229577SJames Feist             std::string uri = "/redfish/v1/TaskService/Tasks/" + strIdx;
1451476687dSEd Tanous 
1461476687dSEd Tanous             res.jsonValue["@odata.id"] = uri;
1471476687dSEd Tanous             res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
1481476687dSEd Tanous             res.jsonValue["Id"] = strIdx;
1491476687dSEd Tanous             res.jsonValue["TaskState"] = state;
1501476687dSEd Tanous             res.jsonValue["TaskStatus"] = status;
1511476687dSEd Tanous 
15246229577SJames Feist             res.addHeader(boost::beast::http::field::location,
15346229577SJames Feist                           uri + "/Monitor");
15446229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
15546229577SJames Feist                           std::to_string(retryAfterSeconds));
15646229577SJames Feist         }
15746229577SJames Feist         else if (!gave204)
15846229577SJames Feist         {
15946229577SJames Feist             res.result(boost::beast::http::status::no_content);
16046229577SJames Feist             gave204 = true;
16146229577SJames Feist         }
16246229577SJames Feist     }
16346229577SJames Feist 
164d609fd6eSEd Tanous     void finishTask()
16546229577SJames Feist     {
16646229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
16746229577SJames Feist             std::chrono::system_clock::now());
16846229577SJames Feist     }
16946229577SJames Feist 
170fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
17146229577SJames Feist     {
17246229577SJames Feist         timer.expires_after(timeout);
17346229577SJames Feist         timer.async_wait(
17446229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
17546229577SJames Feist             if (ec == boost::asio::error::operation_aborted)
17646229577SJames Feist             {
1774e0453b1SGunnar Mills                 return; // completed successfully
17846229577SJames Feist             }
17946229577SJames Feist             if (!ec)
18046229577SJames Feist             {
18146229577SJames Feist                 // change ec to error as timer expired
18246229577SJames Feist                 ec = boost::asio::error::operation_aborted;
18346229577SJames Feist             }
18446229577SJames Feist             self->match.reset();
18559d494eeSPatrick Williams             sdbusplus::message_t msg;
18646229577SJames Feist             self->finishTask();
18746229577SJames Feist             self->state = "Cancelled";
18846229577SJames Feist             self->status = "Warning";
189e5d5006bSJames Feist             self->messages.emplace_back(
190e5d5006bSJames Feist                 messages::taskAborted(std::to_string(self->index)));
191e7686576SSunitha Harish             // Send event :TaskAborted
192e7686576SSunitha Harish             self->sendTaskEvent(self->state, self->index);
19346229577SJames Feist             self->callback(ec, msg, self);
19446229577SJames Feist         });
195fd9ab9e1SJames Feist     }
196fd9ab9e1SJames Feist 
19756d2396dSEd Tanous     static void sendTaskEvent(const std::string_view state, size_t index)
198e7686576SSunitha Harish     {
199e7686576SSunitha Harish         std::string origin =
200e7686576SSunitha Harish             "/redfish/v1/TaskService/Tasks/" + std::to_string(index);
201e7686576SSunitha Harish         std::string resType = "Task";
202e7686576SSunitha Harish         // TaskState enums which should send out an event are:
203e7686576SSunitha Harish         // "Starting" = taskResumed
204e7686576SSunitha Harish         // "Running" = taskStarted
205e7686576SSunitha Harish         // "Suspended" = taskPaused
206e7686576SSunitha Harish         // "Interrupted" = taskPaused
207e7686576SSunitha Harish         // "Pending" = taskPaused
208e7686576SSunitha Harish         // "Stopping" = taskAborted
209e7686576SSunitha Harish         // "Completed" = taskCompletedOK
210e7686576SSunitha Harish         // "Killed" = taskRemoved
211e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
212e7686576SSunitha Harish         // "Cancelled" = taskCancelled
213e7686576SSunitha Harish         if (state == "Starting")
214e7686576SSunitha Harish         {
215e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
216e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
217e7686576SSunitha Harish                 resType);
218e7686576SSunitha Harish         }
219e7686576SSunitha Harish         else if (state == "Running")
220e7686576SSunitha Harish         {
221e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
222e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
223e7686576SSunitha Harish                 resType);
224e7686576SSunitha Harish         }
225e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
226e7686576SSunitha Harish                  (state == "Pending"))
227e7686576SSunitha Harish         {
228e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
229e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
230e7686576SSunitha Harish                 resType);
231e7686576SSunitha Harish         }
232e7686576SSunitha Harish         else if (state == "Stopping")
233e7686576SSunitha Harish         {
234e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
235e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
236e7686576SSunitha Harish                 resType);
237e7686576SSunitha Harish         }
238e7686576SSunitha Harish         else if (state == "Completed")
239e7686576SSunitha Harish         {
240e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
241e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
242e7686576SSunitha Harish                 origin, resType);
243e7686576SSunitha Harish         }
244e7686576SSunitha Harish         else if (state == "Killed")
245e7686576SSunitha Harish         {
246e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
247e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
248e7686576SSunitha Harish                 resType);
249e7686576SSunitha Harish         }
250e7686576SSunitha Harish         else if (state == "Exception")
251e7686576SSunitha Harish         {
252e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
253e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
254e7686576SSunitha Harish                 origin, resType);
255e7686576SSunitha Harish         }
256e7686576SSunitha Harish         else if (state == "Cancelled")
257e7686576SSunitha Harish         {
258e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
259e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
260e7686576SSunitha Harish                 resType);
261e7686576SSunitha Harish         }
262e7686576SSunitha Harish         else
263e7686576SSunitha Harish         {
264e7686576SSunitha Harish             BMCWEB_LOG_INFO << "sendTaskEvent: No events to send";
265e7686576SSunitha Harish         }
266e7686576SSunitha Harish     }
267e7686576SSunitha Harish 
268fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
269fd9ab9e1SJames Feist     {
270fd9ab9e1SJames Feist         if (match)
271fd9ab9e1SJames Feist         {
272fd9ab9e1SJames Feist             return;
273fd9ab9e1SJames Feist         }
27459d494eeSPatrick Williams         match = std::make_unique<sdbusplus::bus::match_t>(
27559d494eeSPatrick Williams             static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
276fd9ab9e1SJames Feist             matchStr,
27759d494eeSPatrick Williams             [self = shared_from_this()](sdbusplus::message_t& message) {
278fd9ab9e1SJames Feist             boost::system::error_code ec;
279fd9ab9e1SJames Feist 
280fd9ab9e1SJames Feist             // callback to return True if callback is done, callback needs
281fd9ab9e1SJames Feist             // to update status itself if needed
282fd9ab9e1SJames Feist             if (self->callback(ec, message, self) == task::completed)
283fd9ab9e1SJames Feist             {
284fd9ab9e1SJames Feist                 self->timer.cancel();
285fd9ab9e1SJames Feist                 self->finishTask();
286fd9ab9e1SJames Feist 
287e7686576SSunitha Harish                 // Send event
288e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
289e7686576SSunitha Harish 
290fd9ab9e1SJames Feist                 // reset the match after the callback was successful
291fd9ab9e1SJames Feist                 boost::asio::post(
292fd9ab9e1SJames Feist                     crow::connections::systemBus->get_io_context(),
293fd9ab9e1SJames Feist                     [self] { self->match.reset(); });
294fd9ab9e1SJames Feist                 return;
295fd9ab9e1SJames Feist             }
296fd9ab9e1SJames Feist             });
297fd9ab9e1SJames Feist 
298fd9ab9e1SJames Feist         extendTimer(timeout);
299e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
300e7686576SSunitha Harish         // Send event : TaskStarted
301e7686576SSunitha Harish         sendTaskEvent(state, index);
30246229577SJames Feist     }
30346229577SJames Feist 
30459d494eeSPatrick Williams     std::function<bool(boost::system::error_code, sdbusplus::message_t&,
30546229577SJames Feist                        const std::shared_ptr<TaskData>&)>
30646229577SJames Feist         callback;
30746229577SJames Feist     std::string matchStr;
30846229577SJames Feist     size_t index;
30946229577SJames Feist     time_t startTime;
31046229577SJames Feist     std::string status;
31146229577SJames Feist     std::string state;
31246229577SJames Feist     nlohmann::json messages;
31346229577SJames Feist     boost::asio::steady_timer timer;
31459d494eeSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> match;
31546229577SJames Feist     std::optional<time_t> endTime;
316fe306728SJames Feist     std::optional<Payload> payload;
31746229577SJames Feist     bool gave204 = false;
3186868ff50SGeorge Liu     int percentComplete = 0;
31946229577SJames Feist };
32046229577SJames Feist 
32146229577SJames Feist } // namespace task
32246229577SJames Feist 
3237e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
32446229577SJames Feist {
3257e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
326ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3277e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
32845ca1b86SEd Tanous             [&app](const crow::Request& req,
3297e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3307e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3313ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
33245ca1b86SEd Tanous         {
33345ca1b86SEd Tanous             return;
33445ca1b86SEd Tanous         }
33546229577SJames Feist         auto find = std::find_if(
33646229577SJames Feist             task::tasks.begin(), task::tasks.end(),
33746229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
33846229577SJames Feist             if (!task)
33946229577SJames Feist             {
34046229577SJames Feist                 return false;
34146229577SJames Feist             }
34246229577SJames Feist 
3437e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3447e860f15SJohn Edward Broadbent             // strtoul returns 0
34546229577SJames Feist             return std::to_string(task->index) == strParam;
34646229577SJames Feist             });
34746229577SJames Feist 
34846229577SJames Feist         if (find == task::tasks.end())
34946229577SJames Feist         {
350d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
35146229577SJames Feist             return;
35246229577SJames Feist         }
35346229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
35446229577SJames Feist         // monitor expires after 204
35546229577SJames Feist         if (ptr->gave204)
35646229577SJames Feist         {
357d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
35846229577SJames Feist             return;
35946229577SJames Feist         }
36046229577SJames Feist         ptr->populateResp(asyncResp->res);
3617e860f15SJohn Edward Broadbent         });
36246229577SJames Feist }
36346229577SJames Feist 
3647e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
36546229577SJames Feist {
3667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
367ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
36945ca1b86SEd Tanous             [&app](const crow::Request& req,
3707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3717e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37345ca1b86SEd Tanous         {
37445ca1b86SEd Tanous             return;
37545ca1b86SEd Tanous         }
37646229577SJames Feist         auto find = std::find_if(
37746229577SJames Feist             task::tasks.begin(), task::tasks.end(),
37846229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
37946229577SJames Feist             if (!task)
38046229577SJames Feist             {
38146229577SJames Feist                 return false;
38246229577SJames Feist             }
38346229577SJames Feist 
3847e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3857e860f15SJohn Edward Broadbent             // strtoul returns 0
38646229577SJames Feist             return std::to_string(task->index) == strParam;
38746229577SJames Feist             });
38846229577SJames Feist 
38946229577SJames Feist         if (find == task::tasks.end())
39046229577SJames Feist         {
391d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
39246229577SJames Feist             return;
39346229577SJames Feist         }
39446229577SJames Feist 
39502cad96eSEd Tanous         const std::shared_ptr<task::TaskData>& ptr = *find;
39646229577SJames Feist 
39746229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
39846229577SJames Feist         asyncResp->res.jsonValue["Id"] = strParam;
39946229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task " + strParam;
40046229577SJames Feist         asyncResp->res.jsonValue["TaskState"] = ptr->state;
40146229577SJames Feist         asyncResp->res.jsonValue["StartTime"] =
4022b82937eSEd Tanous             redfish::time_utils::getDateTimeStdtime(ptr->startTime);
40346229577SJames Feist         if (ptr->endTime)
40446229577SJames Feist         {
40546229577SJames Feist             asyncResp->res.jsonValue["EndTime"] =
4062b82937eSEd Tanous                 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
40746229577SJames Feist         }
40846229577SJames Feist         asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
40946229577SJames Feist         asyncResp->res.jsonValue["Messages"] = ptr->messages;
41046229577SJames Feist         asyncResp->res.jsonValue["@odata.id"] =
41146229577SJames Feist             "/redfish/v1/TaskService/Tasks/" + strParam;
41246229577SJames Feist         if (!ptr->gave204)
41346229577SJames Feist         {
41446229577SJames Feist             asyncResp->res.jsonValue["TaskMonitor"] =
415002d39b4SEd Tanous                 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
41646229577SJames Feist         }
417fe306728SJames Feist         if (ptr->payload)
418fe306728SJames Feist         {
4195fb91ba4SEd Tanous             const task::Payload& p = *(ptr->payload);
420002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
4211476687dSEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
4221476687dSEd Tanous                 p.httpOperation;
423002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
424002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
425002d39b4SEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace);
426fe306728SJames Feist         }
427002d39b4SEd Tanous         asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
4287e860f15SJohn Edward Broadbent         });
42946229577SJames Feist }
43046229577SJames Feist 
4317e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
43246229577SJames Feist {
4337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
434ed398213SEd Tanous         .privileges(redfish::privileges::getTaskCollection)
4357e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
43645ca1b86SEd Tanous             [&app](const crow::Request& req,
4377e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4383ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
43945ca1b86SEd Tanous         {
44045ca1b86SEd Tanous             return;
44145ca1b86SEd Tanous         }
44246229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
44346229577SJames Feist             "#TaskCollection.TaskCollection";
444002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
44546229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Collection";
446002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
44746229577SJames Feist         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
44846229577SJames Feist         members = nlohmann::json::array();
44946229577SJames Feist 
45046229577SJames Feist         for (const std::shared_ptr<task::TaskData>& task : task::tasks)
45146229577SJames Feist         {
45246229577SJames Feist             if (task == nullptr)
45346229577SJames Feist             {
45446229577SJames Feist                 continue; // shouldn't be possible
45546229577SJames Feist             }
456613dabeaSEd Tanous             nlohmann::json::object_t member;
457613dabeaSEd Tanous             member["@odata.id"] =
458613dabeaSEd Tanous                 "redfish/v1/TaskService/Tasks/" + std::to_string(task->index);
459613dabeaSEd Tanous             members.emplace_back(std::move(member));
46046229577SJames Feist         }
4617e860f15SJohn Edward Broadbent         });
46246229577SJames Feist }
46346229577SJames Feist 
4647e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
46546229577SJames Feist {
4667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
467ed398213SEd Tanous         .privileges(redfish::privileges::getTaskService)
4687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
46945ca1b86SEd Tanous             [&app](const crow::Request& req,
4707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
47245ca1b86SEd Tanous         {
47345ca1b86SEd Tanous             return;
47445ca1b86SEd Tanous         }
47546229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
47646229577SJames Feist             "#TaskService.v1_1_4.TaskService";
477002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
47846229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Service";
47946229577SJames Feist         asyncResp->res.jsonValue["Id"] = "TaskService";
4807e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["DateTime"] =
4812b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow().first;
482002d39b4SEd Tanous         asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
48346229577SJames Feist 
484002d39b4SEd Tanous         asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
48546229577SJames Feist 
48646229577SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
48746229577SJames Feist         health->populate();
48846229577SJames Feist         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
48946229577SJames Feist         asyncResp->res.jsonValue["ServiceEnabled"] = true;
4901476687dSEd Tanous         asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
4911476687dSEd Tanous             "/redfish/v1/TaskService/Tasks";
4927e860f15SJohn Edward Broadbent         });
49346229577SJames Feist }
49446229577SJames Feist 
49546229577SJames Feist } // namespace redfish
496