xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
19d43cd0caSEd Tanous #include <boost/asio/post.hpp>
20d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
21b9d36b47SEd Tanous #include <dbus_utility.hpp>
2245ca1b86SEd Tanous #include <query.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
24e5d5006bSJames Feist #include <task_messages.hpp>
251214b7e7SGunnar Mills 
261214b7e7SGunnar Mills #include <chrono>
2746229577SJames Feist #include <variant>
2846229577SJames Feist 
2946229577SJames Feist namespace redfish
3046229577SJames Feist {
3146229577SJames Feist 
3246229577SJames Feist namespace task
3346229577SJames Feist {
3446229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
3546229577SJames Feist 
3646229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
3746229577SJames Feist 
3832898ceaSJames Feist constexpr bool completed = true;
3932898ceaSJames Feist 
40fe306728SJames Feist struct Payload
41fe306728SJames Feist {
42fe306728SJames Feist     Payload(const crow::Request& req) :
43fe306728SJames Feist         targetUri(req.url), httpOperation(req.methodString()),
44fe306728SJames Feist         httpHeaders(nlohmann::json::array())
45fe306728SJames Feist 
46fe306728SJames Feist     {
47fe306728SJames Feist         using field_ns = boost::beast::http::field;
48fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
49fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
50fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
51fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
52fe306728SJames Feist                                field_ns::upgrade};
53fe306728SJames Feist 
54fe306728SJames Feist         jsonBody = nlohmann::json::parse(req.body, nullptr, false);
55fe306728SJames Feist         if (jsonBody.is_discarded())
56fe306728SJames Feist         {
57fe306728SJames Feist             jsonBody = nullptr;
58fe306728SJames Feist         }
59fe306728SJames Feist 
60fe306728SJames Feist         for (const auto& field : req.fields)
61fe306728SJames Feist         {
62fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
63fe306728SJames Feist                           field.name()) == headerWhitelist.end())
64fe306728SJames Feist             {
65fe306728SJames Feist                 continue;
66fe306728SJames Feist             }
67fe306728SJames Feist             std::string header;
68fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
69fe306728SJames Feist                            field.value().size());
70fe306728SJames Feist             header += field.name_string();
71fe306728SJames Feist             header += ": ";
72fe306728SJames Feist             header += field.value();
73fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
74fe306728SJames Feist         }
75fe306728SJames Feist     }
76fe306728SJames Feist     Payload() = delete;
77fe306728SJames Feist 
78fe306728SJames Feist     std::string targetUri;
79fe306728SJames Feist     std::string httpOperation;
80fe306728SJames Feist     nlohmann::json httpHeaders;
81fe306728SJames Feist     nlohmann::json jsonBody;
82fe306728SJames Feist };
83fe306728SJames Feist 
8446229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
8546229577SJames Feist {
8646229577SJames Feist   private:
8746229577SJames Feist     TaskData(std::function<bool(boost::system::error_code,
8846229577SJames Feist                                 sdbusplus::message::message&,
8946229577SJames Feist                                 const std::shared_ptr<TaskData>&)>&& handler,
9023a21a1cSEd Tanous              const std::string& matchIn, size_t idx) :
9146229577SJames Feist         callback(std::move(handler)),
9223a21a1cSEd Tanous         matchStr(matchIn), index(idx),
9346229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
9446229577SJames Feist             std::chrono::system_clock::now())),
9546229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
9646229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
9746229577SJames Feist 
981214b7e7SGunnar Mills     {}
9946229577SJames Feist 
10046229577SJames Feist   public:
101d609fd6eSEd Tanous     TaskData() = delete;
102d609fd6eSEd Tanous 
10346229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
10446229577SJames Feist         std::function<bool(boost::system::error_code,
10546229577SJames Feist                            sdbusplus::message::message&,
10646229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
10746229577SJames Feist         const std::string& match)
10846229577SJames Feist     {
10946229577SJames Feist         static size_t lastTask = 0;
11046229577SJames Feist         struct MakeSharedHelper : public TaskData
11146229577SJames Feist         {
11246229577SJames Feist             MakeSharedHelper(
1131214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
1141214b7e7SGunnar Mills                                    sdbusplus::message::message&,
11546229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
11623a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
11723a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1181214b7e7SGunnar Mills             {}
11946229577SJames Feist         };
12046229577SJames Feist 
12146229577SJames Feist         if (tasks.size() >= maxTaskCount)
12246229577SJames Feist         {
12346229577SJames Feist             auto& last = tasks.front();
12446229577SJames Feist 
12546229577SJames Feist             // destroy all references
12646229577SJames Feist             last->timer.cancel();
12746229577SJames Feist             last->match.reset();
12846229577SJames Feist             tasks.pop_front();
12946229577SJames Feist         }
13046229577SJames Feist 
13146229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13246229577SJames Feist             std::move(handler), match, lastTask++));
13346229577SJames Feist     }
13446229577SJames Feist 
13546229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
13646229577SJames Feist     {
13746229577SJames Feist         if (!endTime)
13846229577SJames Feist         {
13946229577SJames Feist             res.result(boost::beast::http::status::accepted);
14046229577SJames Feist             std::string strIdx = std::to_string(index);
14146229577SJames Feist             std::string uri = "/redfish/v1/TaskService/Tasks/" + strIdx;
142*1476687dSEd Tanous 
143*1476687dSEd Tanous             res.jsonValue["@odata.id"] = uri;
144*1476687dSEd Tanous             res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
145*1476687dSEd Tanous             res.jsonValue["Id"] = strIdx;
146*1476687dSEd Tanous             res.jsonValue["TaskState"] = state;
147*1476687dSEd Tanous             res.jsonValue["TaskStatus"] = status;
148*1476687dSEd Tanous 
14946229577SJames Feist             res.addHeader(boost::beast::http::field::location,
15046229577SJames Feist                           uri + "/Monitor");
15146229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
15246229577SJames Feist                           std::to_string(retryAfterSeconds));
15346229577SJames Feist         }
15446229577SJames Feist         else if (!gave204)
15546229577SJames Feist         {
15646229577SJames Feist             res.result(boost::beast::http::status::no_content);
15746229577SJames Feist             gave204 = true;
15846229577SJames Feist         }
15946229577SJames Feist     }
16046229577SJames Feist 
161d609fd6eSEd Tanous     void finishTask()
16246229577SJames Feist     {
16346229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
16446229577SJames Feist             std::chrono::system_clock::now());
16546229577SJames Feist     }
16646229577SJames Feist 
167fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
16846229577SJames Feist     {
16946229577SJames Feist         timer.expires_after(timeout);
17046229577SJames Feist         timer.async_wait(
17146229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
17246229577SJames Feist                 if (ec == boost::asio::error::operation_aborted)
17346229577SJames Feist                 {
1744e0453b1SGunnar Mills                     return; // completed successfully
17546229577SJames Feist                 }
17646229577SJames Feist                 if (!ec)
17746229577SJames Feist                 {
17846229577SJames Feist                     // change ec to error as timer expired
17946229577SJames Feist                     ec = boost::asio::error::operation_aborted;
18046229577SJames Feist                 }
18146229577SJames Feist                 self->match.reset();
18246229577SJames Feist                 sdbusplus::message::message msg;
18346229577SJames Feist                 self->finishTask();
18446229577SJames Feist                 self->state = "Cancelled";
18546229577SJames Feist                 self->status = "Warning";
186e5d5006bSJames Feist                 self->messages.emplace_back(
187e5d5006bSJames Feist                     messages::taskAborted(std::to_string(self->index)));
188e7686576SSunitha Harish                 // Send event :TaskAborted
189e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
19046229577SJames Feist                 self->callback(ec, msg, self);
19146229577SJames Feist             });
192fd9ab9e1SJames Feist     }
193fd9ab9e1SJames Feist 
19456d2396dSEd Tanous     static void sendTaskEvent(const std::string_view state, size_t index)
195e7686576SSunitha Harish     {
196e7686576SSunitha Harish         std::string origin =
197e7686576SSunitha Harish             "/redfish/v1/TaskService/Tasks/" + std::to_string(index);
198e7686576SSunitha Harish         std::string resType = "Task";
199e7686576SSunitha Harish         // TaskState enums which should send out an event are:
200e7686576SSunitha Harish         // "Starting" = taskResumed
201e7686576SSunitha Harish         // "Running" = taskStarted
202e7686576SSunitha Harish         // "Suspended" = taskPaused
203e7686576SSunitha Harish         // "Interrupted" = taskPaused
204e7686576SSunitha Harish         // "Pending" = taskPaused
205e7686576SSunitha Harish         // "Stopping" = taskAborted
206e7686576SSunitha Harish         // "Completed" = taskCompletedOK
207e7686576SSunitha Harish         // "Killed" = taskRemoved
208e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
209e7686576SSunitha Harish         // "Cancelled" = taskCancelled
210e7686576SSunitha Harish         if (state == "Starting")
211e7686576SSunitha Harish         {
212e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
213e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
214e7686576SSunitha Harish                 resType);
215e7686576SSunitha Harish         }
216e7686576SSunitha Harish         else if (state == "Running")
217e7686576SSunitha Harish         {
218e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
219e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
220e7686576SSunitha Harish                 resType);
221e7686576SSunitha Harish         }
222e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
223e7686576SSunitha Harish                  (state == "Pending"))
224e7686576SSunitha Harish         {
225e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
226e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
227e7686576SSunitha Harish                 resType);
228e7686576SSunitha Harish         }
229e7686576SSunitha Harish         else if (state == "Stopping")
230e7686576SSunitha Harish         {
231e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
232e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
233e7686576SSunitha Harish                 resType);
234e7686576SSunitha Harish         }
235e7686576SSunitha Harish         else if (state == "Completed")
236e7686576SSunitha Harish         {
237e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
238e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
239e7686576SSunitha Harish                 origin, resType);
240e7686576SSunitha Harish         }
241e7686576SSunitha Harish         else if (state == "Killed")
242e7686576SSunitha Harish         {
243e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
244e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
245e7686576SSunitha Harish                 resType);
246e7686576SSunitha Harish         }
247e7686576SSunitha Harish         else if (state == "Exception")
248e7686576SSunitha Harish         {
249e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
250e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
251e7686576SSunitha Harish                 origin, resType);
252e7686576SSunitha Harish         }
253e7686576SSunitha Harish         else if (state == "Cancelled")
254e7686576SSunitha Harish         {
255e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
256e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
257e7686576SSunitha Harish                 resType);
258e7686576SSunitha Harish         }
259e7686576SSunitha Harish         else
260e7686576SSunitha Harish         {
261e7686576SSunitha Harish             BMCWEB_LOG_INFO << "sendTaskEvent: No events to send";
262e7686576SSunitha Harish         }
263e7686576SSunitha Harish     }
264e7686576SSunitha Harish 
265fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
266fd9ab9e1SJames Feist     {
267fd9ab9e1SJames Feist         if (match)
268fd9ab9e1SJames Feist         {
269fd9ab9e1SJames Feist             return;
270fd9ab9e1SJames Feist         }
271fd9ab9e1SJames Feist         match = std::make_unique<sdbusplus::bus::match::match>(
272fd9ab9e1SJames Feist             static_cast<sdbusplus::bus::bus&>(*crow::connections::systemBus),
273fd9ab9e1SJames Feist             matchStr,
274fd9ab9e1SJames Feist             [self = shared_from_this()](sdbusplus::message::message& message) {
275fd9ab9e1SJames Feist                 boost::system::error_code ec;
276fd9ab9e1SJames Feist 
277fd9ab9e1SJames Feist                 // callback to return True if callback is done, callback needs
278fd9ab9e1SJames Feist                 // to update status itself if needed
279fd9ab9e1SJames Feist                 if (self->callback(ec, message, self) == task::completed)
280fd9ab9e1SJames Feist                 {
281fd9ab9e1SJames Feist                     self->timer.cancel();
282fd9ab9e1SJames Feist                     self->finishTask();
283fd9ab9e1SJames Feist 
284e7686576SSunitha Harish                     // Send event
285e7686576SSunitha Harish                     self->sendTaskEvent(self->state, self->index);
286e7686576SSunitha Harish 
287fd9ab9e1SJames Feist                     // reset the match after the callback was successful
288fd9ab9e1SJames Feist                     boost::asio::post(
289fd9ab9e1SJames Feist                         crow::connections::systemBus->get_io_context(),
290fd9ab9e1SJames Feist                         [self] { self->match.reset(); });
291fd9ab9e1SJames Feist                     return;
292fd9ab9e1SJames Feist                 }
293fd9ab9e1SJames Feist             });
294fd9ab9e1SJames Feist 
295fd9ab9e1SJames Feist         extendTimer(timeout);
296e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
297e7686576SSunitha Harish         // Send event : TaskStarted
298e7686576SSunitha Harish         sendTaskEvent(state, index);
29946229577SJames Feist     }
30046229577SJames Feist 
30146229577SJames Feist     std::function<bool(boost::system::error_code, sdbusplus::message::message&,
30246229577SJames Feist                        const std::shared_ptr<TaskData>&)>
30346229577SJames Feist         callback;
30446229577SJames Feist     std::string matchStr;
30546229577SJames Feist     size_t index;
30646229577SJames Feist     time_t startTime;
30746229577SJames Feist     std::string status;
30846229577SJames Feist     std::string state;
30946229577SJames Feist     nlohmann::json messages;
31046229577SJames Feist     boost::asio::steady_timer timer;
31146229577SJames Feist     std::unique_ptr<sdbusplus::bus::match::match> match;
31246229577SJames Feist     std::optional<time_t> endTime;
313fe306728SJames Feist     std::optional<Payload> payload;
31446229577SJames Feist     bool gave204 = false;
3156868ff50SGeorge Liu     int percentComplete = 0;
31646229577SJames Feist };
31746229577SJames Feist 
31846229577SJames Feist } // namespace task
31946229577SJames Feist 
3207e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
32146229577SJames Feist {
3227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
323ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
32545ca1b86SEd Tanous             [&app](const crow::Request& req,
3267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3277e860f15SJohn Edward Broadbent                    const std::string& strParam) {
32845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
32945ca1b86SEd Tanous                 {
33045ca1b86SEd Tanous                     return;
33145ca1b86SEd Tanous                 }
33246229577SJames Feist                 auto find = std::find_if(
33346229577SJames Feist                     task::tasks.begin(), task::tasks.end(),
33446229577SJames Feist                     [&strParam](const std::shared_ptr<task::TaskData>& task) {
33546229577SJames Feist                         if (!task)
33646229577SJames Feist                         {
33746229577SJames Feist                             return false;
33846229577SJames Feist                         }
33946229577SJames Feist 
3407e860f15SJohn Edward Broadbent                         // we compare against the string version as on failure
3417e860f15SJohn Edward Broadbent                         // strtoul returns 0
34246229577SJames Feist                         return std::to_string(task->index) == strParam;
34346229577SJames Feist                     });
34446229577SJames Feist 
34546229577SJames Feist                 if (find == task::tasks.end())
34646229577SJames Feist                 {
3477e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Monitor",
3487e860f15SJohn Edward Broadbent                                                strParam);
34946229577SJames Feist                     return;
35046229577SJames Feist                 }
35146229577SJames Feist                 std::shared_ptr<task::TaskData>& ptr = *find;
35246229577SJames Feist                 // monitor expires after 204
35346229577SJames Feist                 if (ptr->gave204)
35446229577SJames Feist                 {
3557e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Monitor",
3567e860f15SJohn Edward Broadbent                                                strParam);
35746229577SJames Feist                     return;
35846229577SJames Feist                 }
35946229577SJames Feist                 ptr->populateResp(asyncResp->res);
3607e860f15SJohn Edward Broadbent             });
36146229577SJames Feist }
36246229577SJames Feist 
3637e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
36446229577SJames Feist {
3657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
366ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
36845ca1b86SEd Tanous             [&app](const crow::Request& req,
3697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3707e860f15SJohn Edward Broadbent                    const std::string& strParam) {
37145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
37245ca1b86SEd Tanous                 {
37345ca1b86SEd Tanous                     return;
37445ca1b86SEd Tanous                 }
37546229577SJames Feist                 auto find = std::find_if(
37646229577SJames Feist                     task::tasks.begin(), task::tasks.end(),
37746229577SJames Feist                     [&strParam](const std::shared_ptr<task::TaskData>& task) {
37846229577SJames Feist                         if (!task)
37946229577SJames Feist                         {
38046229577SJames Feist                             return false;
38146229577SJames Feist                         }
38246229577SJames Feist 
3837e860f15SJohn Edward Broadbent                         // we compare against the string version as on failure
3847e860f15SJohn Edward Broadbent                         // strtoul returns 0
38546229577SJames Feist                         return std::to_string(task->index) == strParam;
38646229577SJames Feist                     });
38746229577SJames Feist 
38846229577SJames Feist                 if (find == task::tasks.end())
38946229577SJames Feist                 {
3907e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Tasks",
3917e860f15SJohn Edward Broadbent                                                strParam);
39246229577SJames Feist                     return;
39346229577SJames Feist                 }
39446229577SJames Feist 
39546229577SJames Feist                 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"] =
4021d8782e7SNan Zhou                     crow::utility::getDateTimeStdtime(ptr->startTime);
40346229577SJames Feist                 if (ptr->endTime)
40446229577SJames Feist                 {
40546229577SJames Feist                     asyncResp->res.jsonValue["EndTime"] =
4061d8782e7SNan Zhou                         crow::utility::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"] =
4157e860f15SJohn Edward Broadbent                         "/redfish/v1/TaskService/Tasks/" + strParam +
4167e860f15SJohn Edward Broadbent                         "/Monitor";
41746229577SJames Feist                 }
418fe306728SJames Feist                 if (ptr->payload)
419fe306728SJames Feist                 {
4205fb91ba4SEd Tanous                     const task::Payload& p = *(ptr->payload);
421*1476687dSEd Tanous                     asyncResp->res.jsonValue["Payload"]["TargetUri"] =
422*1476687dSEd Tanous                         p.targetUri;
423*1476687dSEd Tanous                     asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
424*1476687dSEd Tanous                         p.httpOperation;
425*1476687dSEd Tanous                     asyncResp->res.jsonValue["Payload"]["HttpHeaders"] =
426*1476687dSEd Tanous                         p.httpHeaders;
427*1476687dSEd Tanous                     asyncResp->res.jsonValue["Payload"]["JsonBody"] =
4287e860f15SJohn Edward Broadbent                         p.jsonBody.dump(
4297e860f15SJohn Edward Broadbent                             2, ' ', true,
430*1476687dSEd Tanous                             nlohmann::json::error_handler_t::replace);
431fe306728SJames Feist                 }
4327e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["PercentComplete"] =
4337e860f15SJohn Edward Broadbent                     ptr->percentComplete;
4347e860f15SJohn Edward Broadbent             });
43546229577SJames Feist }
43646229577SJames Feist 
4377e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
43846229577SJames Feist {
4397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
440ed398213SEd Tanous         .privileges(redfish::privileges::getTaskCollection)
4417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
44245ca1b86SEd Tanous             [&app](const crow::Request& req,
4437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
44445ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
44545ca1b86SEd Tanous                 {
44645ca1b86SEd Tanous                     return;
44745ca1b86SEd Tanous                 }
44846229577SJames Feist                 asyncResp->res.jsonValue["@odata.type"] =
44946229577SJames Feist                     "#TaskCollection.TaskCollection";
4507e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.id"] =
4517e860f15SJohn Edward Broadbent                     "/redfish/v1/TaskService/Tasks";
45246229577SJames Feist                 asyncResp->res.jsonValue["Name"] = "Task Collection";
4537e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] =
4547e860f15SJohn Edward Broadbent                     task::tasks.size();
45546229577SJames Feist                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
45646229577SJames Feist                 members = nlohmann::json::array();
45746229577SJames Feist 
45846229577SJames Feist                 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
45946229577SJames Feist                 {
46046229577SJames Feist                     if (task == nullptr)
46146229577SJames Feist                     {
46246229577SJames Feist                         continue; // shouldn't be possible
46346229577SJames Feist                     }
4647e860f15SJohn Edward Broadbent                     members.emplace_back(nlohmann::json{
4657e860f15SJohn Edward Broadbent                         {"@odata.id", "/redfish/v1/TaskService/Tasks/" +
46646229577SJames Feist                                           std::to_string(task->index)}});
46746229577SJames Feist                 }
4687e860f15SJohn Edward Broadbent             });
46946229577SJames Feist }
47046229577SJames Feist 
4717e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
47246229577SJames Feist {
4737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
474ed398213SEd Tanous         .privileges(redfish::privileges::getTaskService)
4757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
47645ca1b86SEd Tanous             [&app](const crow::Request& req,
4777e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
47845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
47945ca1b86SEd Tanous                 {
48045ca1b86SEd Tanous                     return;
48145ca1b86SEd Tanous                 }
48246229577SJames Feist                 asyncResp->res.jsonValue["@odata.type"] =
48346229577SJames Feist                     "#TaskService.v1_1_4.TaskService";
4847e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.id"] =
4857e860f15SJohn Edward Broadbent                     "/redfish/v1/TaskService";
48646229577SJames Feist                 asyncResp->res.jsonValue["Name"] = "Task Service";
48746229577SJames Feist                 asyncResp->res.jsonValue["Id"] = "TaskService";
4887e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["DateTime"] =
4897c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow().first;
4907e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
4917e860f15SJohn Edward Broadbent                     "Oldest";
49246229577SJames Feist 
4937e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
4947e860f15SJohn Edward Broadbent                     true;
49546229577SJames Feist 
49646229577SJames Feist                 auto health = std::make_shared<HealthPopulate>(asyncResp);
49746229577SJames Feist                 health->populate();
49846229577SJames Feist                 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
49946229577SJames Feist                 asyncResp->res.jsonValue["ServiceEnabled"] = true;
500*1476687dSEd Tanous                 asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
501*1476687dSEd Tanous                     "/redfish/v1/TaskService/Tasks";
5027e860f15SJohn Edward Broadbent             });
50346229577SJames Feist }
50446229577SJames Feist 
50546229577SJames Feist } // namespace redfish
506