xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision 62598e31d0988d589506d5091bd38f72d61faf5e)
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
1713451e39SWilly Tu #include "bmcweb_config.h"
1846229577SJames Feist 
193ccb3adbSEd Tanous #include "app.hpp"
203ccb3adbSEd Tanous #include "dbus_utility.hpp"
213ccb3adbSEd Tanous #include "event_service_manager.hpp"
22d093c996SEd Tanous #include "health.hpp"
231aa0c2b8SEd Tanous #include "http/parsing.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "task_messages.hpp"
273ccb3adbSEd Tanous 
28d43cd0caSEd Tanous #include <boost/asio/post.hpp>
29d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
30ef4c65b7SEd Tanous #include <boost/url/format.hpp>
313ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
321214b7e7SGunnar Mills 
331214b7e7SGunnar Mills #include <chrono>
343ccb3adbSEd Tanous #include <memory>
3546229577SJames Feist #include <variant>
3646229577SJames Feist 
3746229577SJames Feist namespace redfish
3846229577SJames Feist {
3946229577SJames Feist 
4046229577SJames Feist namespace task
4146229577SJames Feist {
4246229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
4346229577SJames Feist 
44cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
4546229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
4646229577SJames Feist 
4732898ceaSJames Feist constexpr bool completed = true;
4832898ceaSJames Feist 
49fe306728SJames Feist struct Payload
50fe306728SJames Feist {
514e23a444SEd Tanous     explicit Payload(const crow::Request& req) :
5239662a3bSEd Tanous         targetUri(req.url().encoded_path()), httpOperation(req.methodString()),
531aa0c2b8SEd Tanous         httpHeaders(nlohmann::json::array())
54fe306728SJames Feist     {
55fe306728SJames Feist         using field_ns = boost::beast::http::field;
56fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
57fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
58fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
59fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
60fe306728SJames Feist                                field_ns::upgrade};
61fe306728SJames Feist 
621aa0c2b8SEd Tanous         JsonParseResult ret = parseRequestAsJson(req, jsonBody);
631aa0c2b8SEd Tanous         if (ret != JsonParseResult::Success)
64fe306728SJames Feist         {
651aa0c2b8SEd Tanous             return;
66fe306728SJames Feist         }
67fe306728SJames Feist 
6898fe740bSEd Tanous         for (const auto& field : req.fields())
69fe306728SJames Feist         {
70fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
71fe306728SJames Feist                           field.name()) == headerWhitelist.end())
72fe306728SJames Feist             {
73fe306728SJames Feist                 continue;
74fe306728SJames Feist             }
75fe306728SJames Feist             std::string header;
76fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
77fe306728SJames Feist                            field.value().size());
78fe306728SJames Feist             header += field.name_string();
79fe306728SJames Feist             header += ": ";
80fe306728SJames Feist             header += field.value();
81fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
82fe306728SJames Feist         }
83fe306728SJames Feist     }
84fe306728SJames Feist     Payload() = delete;
85fe306728SJames Feist 
86fe306728SJames Feist     std::string targetUri;
87fe306728SJames Feist     std::string httpOperation;
88fe306728SJames Feist     nlohmann::json httpHeaders;
89fe306728SJames Feist     nlohmann::json jsonBody;
90fe306728SJames Feist };
91fe306728SJames Feist 
9246229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
9346229577SJames Feist {
9446229577SJames Feist   private:
9559d494eeSPatrick Williams     TaskData(
9659d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
9746229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
9823a21a1cSEd Tanous         const std::string& matchIn, size_t idx) :
9946229577SJames Feist         callback(std::move(handler)),
10023a21a1cSEd Tanous         matchStr(matchIn), index(idx),
10146229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
10246229577SJames Feist             std::chrono::system_clock::now())),
10346229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
10446229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
10546229577SJames Feist 
1061214b7e7SGunnar Mills     {}
10746229577SJames Feist 
10846229577SJames Feist   public:
109d609fd6eSEd Tanous     TaskData() = delete;
110d609fd6eSEd Tanous 
11146229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
11259d494eeSPatrick Williams         std::function<bool(boost::system::error_code, sdbusplus::message_t&,
11346229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
11446229577SJames Feist         const std::string& match)
11546229577SJames Feist     {
11646229577SJames Feist         static size_t lastTask = 0;
11746229577SJames Feist         struct MakeSharedHelper : public TaskData
11846229577SJames Feist         {
11946229577SJames Feist             MakeSharedHelper(
1201214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
12159d494eeSPatrick Williams                                    sdbusplus::message_t&,
12246229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
12323a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
12423a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1251214b7e7SGunnar Mills             {}
12646229577SJames Feist         };
12746229577SJames Feist 
12846229577SJames Feist         if (tasks.size() >= maxTaskCount)
12946229577SJames Feist         {
13002cad96eSEd Tanous             const auto& last = tasks.front();
13146229577SJames Feist 
13246229577SJames Feist             // destroy all references
13346229577SJames Feist             last->timer.cancel();
13446229577SJames Feist             last->match.reset();
13546229577SJames Feist             tasks.pop_front();
13646229577SJames Feist         }
13746229577SJames Feist 
13846229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13946229577SJames Feist             std::move(handler), match, lastTask++));
14046229577SJames Feist     }
14146229577SJames Feist 
14246229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
14346229577SJames Feist     {
14446229577SJames Feist         if (!endTime)
14546229577SJames Feist         {
14646229577SJames Feist             res.result(boost::beast::http::status::accepted);
14746229577SJames Feist             std::string strIdx = std::to_string(index);
14846229577SJames Feist             std::string uri = "/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 
15646229577SJames Feist             res.addHeader(boost::beast::http::field::location,
15746229577SJames Feist                           uri + "/Monitor");
15846229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
15946229577SJames Feist                           std::to_string(retryAfterSeconds));
16046229577SJames Feist         }
16146229577SJames Feist         else if (!gave204)
16246229577SJames Feist         {
16346229577SJames Feist             res.result(boost::beast::http::status::no_content);
16446229577SJames Feist             gave204 = true;
16546229577SJames Feist         }
16646229577SJames Feist     }
16746229577SJames Feist 
168d609fd6eSEd Tanous     void finishTask()
16946229577SJames Feist     {
17046229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
17146229577SJames Feist             std::chrono::system_clock::now());
17246229577SJames Feist     }
17346229577SJames Feist 
174fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
17546229577SJames Feist     {
17646229577SJames Feist         timer.expires_after(timeout);
17746229577SJames Feist         timer.async_wait(
17846229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
17946229577SJames Feist             if (ec == boost::asio::error::operation_aborted)
18046229577SJames Feist             {
1814e0453b1SGunnar Mills                 return; // completed successfully
18246229577SJames Feist             }
18346229577SJames Feist             if (!ec)
18446229577SJames Feist             {
18546229577SJames Feist                 // change ec to error as timer expired
18646229577SJames Feist                 ec = boost::asio::error::operation_aborted;
18746229577SJames Feist             }
18846229577SJames Feist             self->match.reset();
18959d494eeSPatrick Williams             sdbusplus::message_t msg;
19046229577SJames Feist             self->finishTask();
19146229577SJames Feist             self->state = "Cancelled";
19246229577SJames Feist             self->status = "Warning";
193e5d5006bSJames Feist             self->messages.emplace_back(
194e5d5006bSJames Feist                 messages::taskAborted(std::to_string(self->index)));
195e7686576SSunitha Harish             // Send event :TaskAborted
196e7686576SSunitha Harish             self->sendTaskEvent(self->state, self->index);
19746229577SJames Feist             self->callback(ec, msg, self);
19846229577SJames Feist         });
199fd9ab9e1SJames Feist     }
200fd9ab9e1SJames Feist 
20126ccae32SEd Tanous     static void sendTaskEvent(std::string_view state, size_t index)
202e7686576SSunitha Harish     {
20389492a15SPatrick Williams         std::string origin = "/redfish/v1/TaskService/Tasks/" +
20489492a15SPatrick Williams                              std::to_string(index);
205e7686576SSunitha Harish         std::string resType = "Task";
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
217e7686576SSunitha Harish         if (state == "Starting")
218e7686576SSunitha Harish         {
219e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
220e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
221e7686576SSunitha Harish                 resType);
222e7686576SSunitha Harish         }
223e7686576SSunitha Harish         else if (state == "Running")
224e7686576SSunitha Harish         {
225e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
226e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
227e7686576SSunitha Harish                 resType);
228e7686576SSunitha Harish         }
229e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
230e7686576SSunitha Harish                  (state == "Pending"))
231e7686576SSunitha Harish         {
232e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
233e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
234e7686576SSunitha Harish                 resType);
235e7686576SSunitha Harish         }
236e7686576SSunitha Harish         else if (state == "Stopping")
237e7686576SSunitha Harish         {
238e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
239e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
240e7686576SSunitha Harish                 resType);
241e7686576SSunitha Harish         }
242e7686576SSunitha Harish         else if (state == "Completed")
243e7686576SSunitha Harish         {
244e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
245e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
246e7686576SSunitha Harish                 origin, resType);
247e7686576SSunitha Harish         }
248e7686576SSunitha Harish         else if (state == "Killed")
249e7686576SSunitha Harish         {
250e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
251e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
252e7686576SSunitha Harish                 resType);
253e7686576SSunitha Harish         }
254e7686576SSunitha Harish         else if (state == "Exception")
255e7686576SSunitha Harish         {
256e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
257e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
258e7686576SSunitha Harish                 origin, resType);
259e7686576SSunitha Harish         }
260e7686576SSunitha Harish         else if (state == "Cancelled")
261e7686576SSunitha Harish         {
262e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
263e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
264e7686576SSunitha Harish                 resType);
265e7686576SSunitha Harish         }
266e7686576SSunitha Harish         else
267e7686576SSunitha Harish         {
268*62598e31SEd Tanous             BMCWEB_LOG_INFO("sendTaskEvent: No events to send");
269e7686576SSunitha Harish         }
270e7686576SSunitha Harish     }
271e7686576SSunitha Harish 
272fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
273fd9ab9e1SJames Feist     {
274fd9ab9e1SJames Feist         if (match)
275fd9ab9e1SJames Feist         {
276fd9ab9e1SJames Feist             return;
277fd9ab9e1SJames Feist         }
27859d494eeSPatrick Williams         match = std::make_unique<sdbusplus::bus::match_t>(
27959d494eeSPatrick Williams             static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus),
280fd9ab9e1SJames Feist             matchStr,
28159d494eeSPatrick Williams             [self = shared_from_this()](sdbusplus::message_t& message) {
282fd9ab9e1SJames Feist             boost::system::error_code ec;
283fd9ab9e1SJames Feist 
284fd9ab9e1SJames Feist             // callback to return True if callback is done, callback needs
285fd9ab9e1SJames Feist             // to update status itself if needed
286fd9ab9e1SJames Feist             if (self->callback(ec, message, self) == task::completed)
287fd9ab9e1SJames Feist             {
288fd9ab9e1SJames Feist                 self->timer.cancel();
289fd9ab9e1SJames Feist                 self->finishTask();
290fd9ab9e1SJames Feist 
291e7686576SSunitha Harish                 // Send event
292e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
293e7686576SSunitha Harish 
294fd9ab9e1SJames Feist                 // reset the match after the callback was successful
295fd9ab9e1SJames Feist                 boost::asio::post(
296fd9ab9e1SJames Feist                     crow::connections::systemBus->get_io_context(),
297fd9ab9e1SJames Feist                     [self] { self->match.reset(); });
298fd9ab9e1SJames Feist                 return;
299fd9ab9e1SJames Feist             }
300fd9ab9e1SJames Feist             });
301fd9ab9e1SJames Feist 
302fd9ab9e1SJames Feist         extendTimer(timeout);
303e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
304e7686576SSunitha Harish         // Send event : TaskStarted
305e7686576SSunitha Harish         sendTaskEvent(state, index);
30646229577SJames Feist     }
30746229577SJames Feist 
30859d494eeSPatrick Williams     std::function<bool(boost::system::error_code, sdbusplus::message_t&,
30946229577SJames Feist                        const std::shared_ptr<TaskData>&)>
31046229577SJames Feist         callback;
31146229577SJames Feist     std::string matchStr;
31246229577SJames Feist     size_t index;
31346229577SJames Feist     time_t startTime;
31446229577SJames Feist     std::string status;
31546229577SJames Feist     std::string state;
31646229577SJames Feist     nlohmann::json messages;
31746229577SJames Feist     boost::asio::steady_timer timer;
31859d494eeSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> match;
31946229577SJames Feist     std::optional<time_t> endTime;
320fe306728SJames Feist     std::optional<Payload> payload;
32146229577SJames Feist     bool gave204 = false;
3226868ff50SGeorge Liu     int percentComplete = 0;
32346229577SJames Feist };
32446229577SJames Feist 
32546229577SJames Feist } // namespace task
32646229577SJames Feist 
3277e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
32846229577SJames Feist {
3297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
330ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33245ca1b86SEd Tanous             [&app](const crow::Request& req,
3337e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3347e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3353ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
33645ca1b86SEd Tanous         {
33745ca1b86SEd Tanous             return;
33845ca1b86SEd Tanous         }
33946229577SJames Feist         auto find = std::find_if(
34046229577SJames Feist             task::tasks.begin(), task::tasks.end(),
34146229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
34246229577SJames Feist             if (!task)
34346229577SJames Feist             {
34446229577SJames Feist                 return false;
34546229577SJames Feist             }
34646229577SJames Feist 
3477e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3487e860f15SJohn Edward Broadbent             // strtoul returns 0
34946229577SJames Feist             return std::to_string(task->index) == strParam;
35046229577SJames Feist             });
35146229577SJames Feist 
35246229577SJames Feist         if (find == task::tasks.end())
35346229577SJames Feist         {
354d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
35546229577SJames Feist             return;
35646229577SJames Feist         }
35746229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
35846229577SJames Feist         // monitor expires after 204
35946229577SJames Feist         if (ptr->gave204)
36046229577SJames Feist         {
361d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
36246229577SJames Feist             return;
36346229577SJames Feist         }
36446229577SJames Feist         ptr->populateResp(asyncResp->res);
3657e860f15SJohn Edward Broadbent         });
36646229577SJames Feist }
36746229577SJames Feist 
3687e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
36946229577SJames Feist {
3707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
371ed398213SEd Tanous         .privileges(redfish::privileges::getTask)
3727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
37345ca1b86SEd Tanous             [&app](const crow::Request& req,
3747e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3757e860f15SJohn Edward Broadbent                    const std::string& strParam) {
3763ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37745ca1b86SEd Tanous         {
37845ca1b86SEd Tanous             return;
37945ca1b86SEd Tanous         }
38046229577SJames Feist         auto find = std::find_if(
38146229577SJames Feist             task::tasks.begin(), task::tasks.end(),
38246229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
38346229577SJames Feist             if (!task)
38446229577SJames Feist             {
38546229577SJames Feist                 return false;
38646229577SJames Feist             }
38746229577SJames Feist 
3887e860f15SJohn Edward Broadbent             // we compare against the string version as on failure
3897e860f15SJohn Edward Broadbent             // strtoul returns 0
39046229577SJames Feist             return std::to_string(task->index) == strParam;
39146229577SJames Feist             });
39246229577SJames Feist 
39346229577SJames Feist         if (find == task::tasks.end())
39446229577SJames Feist         {
395d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "Task", strParam);
39646229577SJames Feist             return;
39746229577SJames Feist         }
39846229577SJames Feist 
39902cad96eSEd Tanous         const std::shared_ptr<task::TaskData>& ptr = *find;
40046229577SJames Feist 
40146229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
40246229577SJames Feist         asyncResp->res.jsonValue["Id"] = strParam;
40346229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task " + strParam;
40446229577SJames Feist         asyncResp->res.jsonValue["TaskState"] = ptr->state;
40546229577SJames Feist         asyncResp->res.jsonValue["StartTime"] =
4062b82937eSEd Tanous             redfish::time_utils::getDateTimeStdtime(ptr->startTime);
40746229577SJames Feist         if (ptr->endTime)
40846229577SJames Feist         {
40946229577SJames Feist             asyncResp->res.jsonValue["EndTime"] =
4102b82937eSEd Tanous                 redfish::time_utils::getDateTimeStdtime(*(ptr->endTime));
41146229577SJames Feist         }
41246229577SJames Feist         asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
41346229577SJames Feist         asyncResp->res.jsonValue["Messages"] = ptr->messages;
414ef4c65b7SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
415ef4c65b7SEd Tanous             boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strParam);
41646229577SJames Feist         if (!ptr->gave204)
41746229577SJames Feist         {
41846229577SJames Feist             asyncResp->res.jsonValue["TaskMonitor"] =
419002d39b4SEd Tanous                 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
42046229577SJames Feist         }
4215db7dfd6SArun Thomas Baby 
4225db7dfd6SArun Thomas Baby         asyncResp->res.jsonValue["HidePayload"] = !ptr->payload;
4235db7dfd6SArun Thomas Baby 
424fe306728SJames Feist         if (ptr->payload)
425fe306728SJames Feist         {
4265fb91ba4SEd Tanous             const task::Payload& p = *(ptr->payload);
427002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["TargetUri"] = p.targetUri;
4281476687dSEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpOperation"] =
4291476687dSEd Tanous                 p.httpOperation;
430002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = p.httpHeaders;
431002d39b4SEd Tanous             asyncResp->res.jsonValue["Payload"]["JsonBody"] = p.jsonBody.dump(
432002d39b4SEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace);
433fe306728SJames Feist         }
434002d39b4SEd Tanous         asyncResp->res.jsonValue["PercentComplete"] = ptr->percentComplete;
4357e860f15SJohn Edward Broadbent         });
43646229577SJames Feist }
43746229577SJames Feist 
4387e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
43946229577SJames Feist {
4407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
441ed398213SEd Tanous         .privileges(redfish::privileges::getTaskCollection)
4427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
44345ca1b86SEd Tanous             [&app](const crow::Request& req,
4447e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
44645ca1b86SEd Tanous         {
44745ca1b86SEd Tanous             return;
44845ca1b86SEd Tanous         }
44946229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
45046229577SJames Feist             "#TaskCollection.TaskCollection";
451002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
45246229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Collection";
453002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
45446229577SJames Feist         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
45546229577SJames Feist         members = nlohmann::json::array();
45646229577SJames Feist 
45746229577SJames Feist         for (const std::shared_ptr<task::TaskData>& task : task::tasks)
45846229577SJames Feist         {
45946229577SJames Feist             if (task == nullptr)
46046229577SJames Feist             {
46146229577SJames Feist                 continue; // shouldn't be possible
46246229577SJames Feist             }
463613dabeaSEd Tanous             nlohmann::json::object_t member;
464ef4c65b7SEd Tanous             member["@odata.id"] =
465ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/TaskService/Tasks/{}",
466eddfc437SWilly Tu                                     std::to_string(task->index));
467613dabeaSEd Tanous             members.emplace_back(std::move(member));
46846229577SJames Feist         }
4697e860f15SJohn Edward Broadbent         });
47046229577SJames Feist }
47146229577SJames Feist 
4727e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
47346229577SJames Feist {
4747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
475ed398213SEd Tanous         .privileges(redfish::privileges::getTaskService)
4767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
47745ca1b86SEd Tanous             [&app](const crow::Request& req,
4787e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
48045ca1b86SEd Tanous         {
48145ca1b86SEd Tanous             return;
48245ca1b86SEd Tanous         }
48346229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
48446229577SJames Feist             "#TaskService.v1_1_4.TaskService";
485002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = "/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"] =
4892b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow().first;
490002d39b4SEd Tanous         asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
49146229577SJames Feist 
492002d39b4SEd Tanous         asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
49346229577SJames Feist 
49413451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
49513451e39SWilly Tu         {
49646229577SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
49746229577SJames Feist             health->populate();
49813451e39SWilly Tu         }
49946229577SJames Feist         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
50046229577SJames Feist         asyncResp->res.jsonValue["ServiceEnabled"] = true;
5011476687dSEd Tanous         asyncResp->res.jsonValue["Tasks"]["@odata.id"] =
5021476687dSEd Tanous             "/redfish/v1/TaskService/Tasks";
5037e860f15SJohn Edward Broadbent         });
50446229577SJames Feist }
50546229577SJames Feist 
50646229577SJames Feist } // namespace redfish
507