xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision 7e860f1550c8686eec42f7a75bc5f2ef51e756ad)
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*7e860f15SJohn Edward Broadbent #include <app.hpp>
19d43cd0caSEd Tanous #include <boost/asio/post.hpp>
20d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
2146229577SJames Feist #include <boost/container/flat_map.hpp>
22e5d5006bSJames Feist #include <task_messages.hpp>
231214b7e7SGunnar Mills 
241214b7e7SGunnar Mills #include <chrono>
2546229577SJames Feist #include <variant>
2646229577SJames Feist 
2746229577SJames Feist namespace redfish
2846229577SJames Feist {
2946229577SJames Feist 
3046229577SJames Feist namespace task
3146229577SJames Feist {
3246229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
3346229577SJames Feist 
3446229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
3546229577SJames Feist 
3632898ceaSJames Feist constexpr bool completed = true;
3732898ceaSJames Feist 
38fe306728SJames Feist struct Payload
39fe306728SJames Feist {
40fe306728SJames Feist     Payload(const crow::Request& req) :
41fe306728SJames Feist         targetUri(req.url), httpOperation(req.methodString()),
42fe306728SJames Feist         httpHeaders(nlohmann::json::array())
43fe306728SJames Feist 
44fe306728SJames Feist     {
45fe306728SJames Feist         using field_ns = boost::beast::http::field;
46fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
47fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
48fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
49fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
50fe306728SJames Feist                                field_ns::upgrade};
51fe306728SJames Feist 
52fe306728SJames Feist         jsonBody = nlohmann::json::parse(req.body, nullptr, false);
53fe306728SJames Feist         if (jsonBody.is_discarded())
54fe306728SJames Feist         {
55fe306728SJames Feist             jsonBody = nullptr;
56fe306728SJames Feist         }
57fe306728SJames Feist 
58fe306728SJames Feist         for (const auto& field : req.fields)
59fe306728SJames Feist         {
60fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
61fe306728SJames Feist                           field.name()) == headerWhitelist.end())
62fe306728SJames Feist             {
63fe306728SJames Feist                 continue;
64fe306728SJames Feist             }
65fe306728SJames Feist             std::string header;
66fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
67fe306728SJames Feist                            field.value().size());
68fe306728SJames Feist             header += field.name_string();
69fe306728SJames Feist             header += ": ";
70fe306728SJames Feist             header += field.value();
71fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
72fe306728SJames Feist         }
73fe306728SJames Feist     }
74fe306728SJames Feist     Payload() = delete;
75fe306728SJames Feist 
76fe306728SJames Feist     std::string targetUri;
77fe306728SJames Feist     std::string httpOperation;
78fe306728SJames Feist     nlohmann::json httpHeaders;
79fe306728SJames Feist     nlohmann::json jsonBody;
80fe306728SJames Feist };
81fe306728SJames Feist 
8246229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
8346229577SJames Feist {
8446229577SJames Feist   private:
8546229577SJames Feist     TaskData(std::function<bool(boost::system::error_code,
8646229577SJames Feist                                 sdbusplus::message::message&,
8746229577SJames Feist                                 const std::shared_ptr<TaskData>&)>&& handler,
8823a21a1cSEd Tanous              const std::string& matchIn, size_t idx) :
8946229577SJames Feist         callback(std::move(handler)),
9023a21a1cSEd Tanous         matchStr(matchIn), index(idx),
9146229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
9246229577SJames Feist             std::chrono::system_clock::now())),
9346229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
9446229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
9546229577SJames Feist 
961214b7e7SGunnar Mills     {}
9746229577SJames Feist 
9846229577SJames Feist   public:
99d609fd6eSEd Tanous     TaskData() = delete;
100d609fd6eSEd Tanous 
10146229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
10246229577SJames Feist         std::function<bool(boost::system::error_code,
10346229577SJames Feist                            sdbusplus::message::message&,
10446229577SJames Feist                            const std::shared_ptr<TaskData>&)>&& handler,
10546229577SJames Feist         const std::string& match)
10646229577SJames Feist     {
10746229577SJames Feist         static size_t lastTask = 0;
10846229577SJames Feist         struct MakeSharedHelper : public TaskData
10946229577SJames Feist         {
11046229577SJames Feist             MakeSharedHelper(
1111214b7e7SGunnar Mills                 std::function<bool(boost::system::error_code,
1121214b7e7SGunnar Mills                                    sdbusplus::message::message&,
11346229577SJames Feist                                    const std::shared_ptr<TaskData>&)>&& handler,
11423a21a1cSEd Tanous                 const std::string& match2, size_t idx) :
11523a21a1cSEd Tanous                 TaskData(std::move(handler), match2, idx)
1161214b7e7SGunnar Mills             {}
11746229577SJames Feist         };
11846229577SJames Feist 
11946229577SJames Feist         if (tasks.size() >= maxTaskCount)
12046229577SJames Feist         {
12146229577SJames Feist             auto& last = tasks.front();
12246229577SJames Feist 
12346229577SJames Feist             // destroy all references
12446229577SJames Feist             last->timer.cancel();
12546229577SJames Feist             last->match.reset();
12646229577SJames Feist             tasks.pop_front();
12746229577SJames Feist         }
12846229577SJames Feist 
12946229577SJames Feist         return tasks.emplace_back(std::make_shared<MakeSharedHelper>(
13046229577SJames Feist             std::move(handler), match, lastTask++));
13146229577SJames Feist     }
13246229577SJames Feist 
13346229577SJames Feist     void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
13446229577SJames Feist     {
13546229577SJames Feist         if (!endTime)
13646229577SJames Feist         {
13746229577SJames Feist             res.result(boost::beast::http::status::accepted);
13846229577SJames Feist             std::string strIdx = std::to_string(index);
13946229577SJames Feist             std::string uri = "/redfish/v1/TaskService/Tasks/" + strIdx;
14046229577SJames Feist             res.jsonValue = {{"@odata.id", uri},
14146229577SJames Feist                              {"@odata.type", "#Task.v1_4_3.Task"},
14246229577SJames Feist                              {"Id", strIdx},
14346229577SJames Feist                              {"TaskState", state},
14446229577SJames Feist                              {"TaskStatus", status}};
14546229577SJames Feist             res.addHeader(boost::beast::http::field::location,
14646229577SJames Feist                           uri + "/Monitor");
14746229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
14846229577SJames Feist                           std::to_string(retryAfterSeconds));
14946229577SJames Feist         }
15046229577SJames Feist         else if (!gave204)
15146229577SJames Feist         {
15246229577SJames Feist             res.result(boost::beast::http::status::no_content);
15346229577SJames Feist             gave204 = true;
15446229577SJames Feist         }
15546229577SJames Feist     }
15646229577SJames Feist 
157d609fd6eSEd Tanous     void finishTask()
15846229577SJames Feist     {
15946229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
16046229577SJames Feist             std::chrono::system_clock::now());
16146229577SJames Feist     }
16246229577SJames Feist 
163fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
16446229577SJames Feist     {
16546229577SJames Feist         timer.expires_after(timeout);
16646229577SJames Feist         timer.async_wait(
16746229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
16846229577SJames Feist                 if (ec == boost::asio::error::operation_aborted)
16946229577SJames Feist                 {
1704e0453b1SGunnar Mills                     return; // completed successfully
17146229577SJames Feist                 }
17246229577SJames Feist                 if (!ec)
17346229577SJames Feist                 {
17446229577SJames Feist                     // change ec to error as timer expired
17546229577SJames Feist                     ec = boost::asio::error::operation_aborted;
17646229577SJames Feist                 }
17746229577SJames Feist                 self->match.reset();
17846229577SJames Feist                 sdbusplus::message::message msg;
17946229577SJames Feist                 self->finishTask();
18046229577SJames Feist                 self->state = "Cancelled";
18146229577SJames Feist                 self->status = "Warning";
182e5d5006bSJames Feist                 self->messages.emplace_back(
183e5d5006bSJames Feist                     messages::taskAborted(std::to_string(self->index)));
184e7686576SSunitha Harish                 // Send event :TaskAborted
185e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
18646229577SJames Feist                 self->callback(ec, msg, self);
18746229577SJames Feist             });
188fd9ab9e1SJames Feist     }
189fd9ab9e1SJames Feist 
190e7686576SSunitha Harish     void sendTaskEvent(const std::string_view state, size_t index)
191e7686576SSunitha Harish     {
192e7686576SSunitha Harish         std::string origin =
193e7686576SSunitha Harish             "/redfish/v1/TaskService/Tasks/" + std::to_string(index);
194e7686576SSunitha Harish         std::string resType = "Task";
195e7686576SSunitha Harish         // TaskState enums which should send out an event are:
196e7686576SSunitha Harish         // "Starting" = taskResumed
197e7686576SSunitha Harish         // "Running" = taskStarted
198e7686576SSunitha Harish         // "Suspended" = taskPaused
199e7686576SSunitha Harish         // "Interrupted" = taskPaused
200e7686576SSunitha Harish         // "Pending" = taskPaused
201e7686576SSunitha Harish         // "Stopping" = taskAborted
202e7686576SSunitha Harish         // "Completed" = taskCompletedOK
203e7686576SSunitha Harish         // "Killed" = taskRemoved
204e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
205e7686576SSunitha Harish         // "Cancelled" = taskCancelled
206e7686576SSunitha Harish         if (state == "Starting")
207e7686576SSunitha Harish         {
208e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
209e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
210e7686576SSunitha Harish                 resType);
211e7686576SSunitha Harish         }
212e7686576SSunitha Harish         else if (state == "Running")
213e7686576SSunitha Harish         {
214e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
215e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
216e7686576SSunitha Harish                 resType);
217e7686576SSunitha Harish         }
218e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
219e7686576SSunitha Harish                  (state == "Pending"))
220e7686576SSunitha Harish         {
221e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
222e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
223e7686576SSunitha Harish                 resType);
224e7686576SSunitha Harish         }
225e7686576SSunitha Harish         else if (state == "Stopping")
226e7686576SSunitha Harish         {
227e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
228e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
229e7686576SSunitha Harish                 resType);
230e7686576SSunitha Harish         }
231e7686576SSunitha Harish         else if (state == "Completed")
232e7686576SSunitha Harish         {
233e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
234e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
235e7686576SSunitha Harish                 origin, resType);
236e7686576SSunitha Harish         }
237e7686576SSunitha Harish         else if (state == "Killed")
238e7686576SSunitha Harish         {
239e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
240e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
241e7686576SSunitha Harish                 resType);
242e7686576SSunitha Harish         }
243e7686576SSunitha Harish         else if (state == "Exception")
244e7686576SSunitha Harish         {
245e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
246e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
247e7686576SSunitha Harish                 origin, resType);
248e7686576SSunitha Harish         }
249e7686576SSunitha Harish         else if (state == "Cancelled")
250e7686576SSunitha Harish         {
251e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
252e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
253e7686576SSunitha Harish                 resType);
254e7686576SSunitha Harish         }
255e7686576SSunitha Harish         else
256e7686576SSunitha Harish         {
257e7686576SSunitha Harish             BMCWEB_LOG_INFO << "sendTaskEvent: No events to send";
258e7686576SSunitha Harish         }
259e7686576SSunitha Harish     }
260e7686576SSunitha Harish 
261fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
262fd9ab9e1SJames Feist     {
263fd9ab9e1SJames Feist         if (match)
264fd9ab9e1SJames Feist         {
265fd9ab9e1SJames Feist             return;
266fd9ab9e1SJames Feist         }
267fd9ab9e1SJames Feist         match = std::make_unique<sdbusplus::bus::match::match>(
268fd9ab9e1SJames Feist             static_cast<sdbusplus::bus::bus&>(*crow::connections::systemBus),
269fd9ab9e1SJames Feist             matchStr,
270fd9ab9e1SJames Feist             [self = shared_from_this()](sdbusplus::message::message& message) {
271fd9ab9e1SJames Feist                 boost::system::error_code ec;
272fd9ab9e1SJames Feist 
273fd9ab9e1SJames Feist                 // callback to return True if callback is done, callback needs
274fd9ab9e1SJames Feist                 // to update status itself if needed
275fd9ab9e1SJames Feist                 if (self->callback(ec, message, self) == task::completed)
276fd9ab9e1SJames Feist                 {
277fd9ab9e1SJames Feist                     self->timer.cancel();
278fd9ab9e1SJames Feist                     self->finishTask();
279fd9ab9e1SJames Feist 
280e7686576SSunitha Harish                     // Send event
281e7686576SSunitha Harish                     self->sendTaskEvent(self->state, self->index);
282e7686576SSunitha Harish 
283fd9ab9e1SJames Feist                     // reset the match after the callback was successful
284fd9ab9e1SJames Feist                     boost::asio::post(
285fd9ab9e1SJames Feist                         crow::connections::systemBus->get_io_context(),
286fd9ab9e1SJames Feist                         [self] { self->match.reset(); });
287fd9ab9e1SJames Feist                     return;
288fd9ab9e1SJames Feist                 }
289fd9ab9e1SJames Feist             });
290fd9ab9e1SJames Feist 
291fd9ab9e1SJames Feist         extendTimer(timeout);
292e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
293e7686576SSunitha Harish         // Send event : TaskStarted
294e7686576SSunitha Harish         sendTaskEvent(state, index);
29546229577SJames Feist     }
29646229577SJames Feist 
29746229577SJames Feist     std::function<bool(boost::system::error_code, sdbusplus::message::message&,
29846229577SJames Feist                        const std::shared_ptr<TaskData>&)>
29946229577SJames Feist         callback;
30046229577SJames Feist     std::string matchStr;
30146229577SJames Feist     size_t index;
30246229577SJames Feist     time_t startTime;
30346229577SJames Feist     std::string status;
30446229577SJames Feist     std::string state;
30546229577SJames Feist     nlohmann::json messages;
30646229577SJames Feist     boost::asio::steady_timer timer;
30746229577SJames Feist     std::unique_ptr<sdbusplus::bus::match::match> match;
30846229577SJames Feist     std::optional<time_t> endTime;
309fe306728SJames Feist     std::optional<Payload> payload;
31046229577SJames Feist     bool gave204 = false;
3116868ff50SGeorge Liu     int percentComplete = 0;
31246229577SJames Feist };
31346229577SJames Feist 
31446229577SJames Feist } // namespace task
31546229577SJames Feist 
316*7e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app)
31746229577SJames Feist {
318*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/Monitor/")
319*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
320*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
321*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
322*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
323*7e860f15SJohn Edward Broadbent                const std::string& strParam) {
32446229577SJames Feist                 auto find = std::find_if(
32546229577SJames Feist                     task::tasks.begin(), task::tasks.end(),
32646229577SJames Feist                     [&strParam](const std::shared_ptr<task::TaskData>& task) {
32746229577SJames Feist                         if (!task)
32846229577SJames Feist                         {
32946229577SJames Feist                             return false;
33046229577SJames Feist                         }
33146229577SJames Feist 
332*7e860f15SJohn Edward Broadbent                         // we compare against the string version as on failure
333*7e860f15SJohn Edward Broadbent                         // strtoul returns 0
33446229577SJames Feist                         return std::to_string(task->index) == strParam;
33546229577SJames Feist                     });
33646229577SJames Feist 
33746229577SJames Feist                 if (find == task::tasks.end())
33846229577SJames Feist                 {
339*7e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Monitor",
340*7e860f15SJohn Edward Broadbent                                                strParam);
34146229577SJames Feist                     return;
34246229577SJames Feist                 }
34346229577SJames Feist                 std::shared_ptr<task::TaskData>& ptr = *find;
34446229577SJames Feist                 // monitor expires after 204
34546229577SJames Feist                 if (ptr->gave204)
34646229577SJames Feist                 {
347*7e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Monitor",
348*7e860f15SJohn Edward Broadbent                                                strParam);
34946229577SJames Feist                     return;
35046229577SJames Feist                 }
35146229577SJames Feist                 ptr->populateResp(asyncResp->res);
352*7e860f15SJohn Edward Broadbent             });
35346229577SJames Feist }
35446229577SJames Feist 
355*7e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app)
35646229577SJames Feist {
357*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/")
358*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
359*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
360*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
361*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
362*7e860f15SJohn Edward Broadbent                const std::string& strParam) {
36346229577SJames Feist                 auto find = std::find_if(
36446229577SJames Feist                     task::tasks.begin(), task::tasks.end(),
36546229577SJames Feist                     [&strParam](const std::shared_ptr<task::TaskData>& task) {
36646229577SJames Feist                         if (!task)
36746229577SJames Feist                         {
36846229577SJames Feist                             return false;
36946229577SJames Feist                         }
37046229577SJames Feist 
371*7e860f15SJohn Edward Broadbent                         // we compare against the string version as on failure
372*7e860f15SJohn Edward Broadbent                         // strtoul returns 0
37346229577SJames Feist                         return std::to_string(task->index) == strParam;
37446229577SJames Feist                     });
37546229577SJames Feist 
37646229577SJames Feist                 if (find == task::tasks.end())
37746229577SJames Feist                 {
378*7e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res, "Tasks",
379*7e860f15SJohn Edward Broadbent                                                strParam);
38046229577SJames Feist                     return;
38146229577SJames Feist                 }
38246229577SJames Feist 
38346229577SJames Feist                 std::shared_ptr<task::TaskData>& ptr = *find;
38446229577SJames Feist 
38546229577SJames Feist                 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
38646229577SJames Feist                 asyncResp->res.jsonValue["Id"] = strParam;
38746229577SJames Feist                 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
38846229577SJames Feist                 asyncResp->res.jsonValue["TaskState"] = ptr->state;
38946229577SJames Feist                 asyncResp->res.jsonValue["StartTime"] =
39046229577SJames Feist                     crow::utility::getDateTime(ptr->startTime);
39146229577SJames Feist                 if (ptr->endTime)
39246229577SJames Feist                 {
39346229577SJames Feist                     asyncResp->res.jsonValue["EndTime"] =
39446229577SJames Feist                         crow::utility::getDateTime(*(ptr->endTime));
39546229577SJames Feist                 }
39646229577SJames Feist                 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
39746229577SJames Feist                 asyncResp->res.jsonValue["Messages"] = ptr->messages;
39846229577SJames Feist                 asyncResp->res.jsonValue["@odata.id"] =
39946229577SJames Feist                     "/redfish/v1/TaskService/Tasks/" + strParam;
40046229577SJames Feist                 if (!ptr->gave204)
40146229577SJames Feist                 {
40246229577SJames Feist                     asyncResp->res.jsonValue["TaskMonitor"] =
403*7e860f15SJohn Edward Broadbent                         "/redfish/v1/TaskService/Tasks/" + strParam +
404*7e860f15SJohn Edward Broadbent                         "/Monitor";
40546229577SJames Feist                 }
406fe306728SJames Feist                 if (ptr->payload)
407fe306728SJames Feist                 {
4085fb91ba4SEd Tanous                     const task::Payload& p = *(ptr->payload);
4095fb91ba4SEd Tanous                     asyncResp->res.jsonValue["Payload"] = {
4105fb91ba4SEd Tanous                         {"TargetUri", p.targetUri},
4115fb91ba4SEd Tanous                         {"HttpOperation", p.httpOperation},
4125fb91ba4SEd Tanous                         {"HttpHeaders", p.httpHeaders},
41371f52d96SEd Tanous                         {"JsonBody",
414*7e860f15SJohn Edward Broadbent                          p.jsonBody.dump(
415*7e860f15SJohn Edward Broadbent                              2, ' ', true,
41671f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace)}};
417fe306728SJames Feist                 }
418*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["PercentComplete"] =
419*7e860f15SJohn Edward Broadbent                     ptr->percentComplete;
420*7e860f15SJohn Edward Broadbent             });
42146229577SJames Feist }
42246229577SJames Feist 
423*7e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app)
42446229577SJames Feist {
425*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
426*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
427*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
428*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
429*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
43046229577SJames Feist                 asyncResp->res.jsonValue["@odata.type"] =
43146229577SJames Feist                     "#TaskCollection.TaskCollection";
432*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.id"] =
433*7e860f15SJohn Edward Broadbent                     "/redfish/v1/TaskService/Tasks";
43446229577SJames Feist                 asyncResp->res.jsonValue["Name"] = "Task Collection";
435*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] =
436*7e860f15SJohn Edward Broadbent                     task::tasks.size();
43746229577SJames Feist                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
43846229577SJames Feist                 members = nlohmann::json::array();
43946229577SJames Feist 
44046229577SJames Feist                 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
44146229577SJames Feist                 {
44246229577SJames Feist                     if (task == nullptr)
44346229577SJames Feist                     {
44446229577SJames Feist                         continue; // shouldn't be possible
44546229577SJames Feist                     }
446*7e860f15SJohn Edward Broadbent                     members.emplace_back(nlohmann::json{
447*7e860f15SJohn Edward Broadbent                         {"@odata.id", "/redfish/v1/TaskService/Tasks/" +
44846229577SJames Feist                                           std::to_string(task->index)}});
44946229577SJames Feist                 }
450*7e860f15SJohn Edward Broadbent             });
45146229577SJames Feist }
45246229577SJames Feist 
453*7e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app)
45446229577SJames Feist {
455*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TaskService/")
456*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
457*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
458*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
459*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
46046229577SJames Feist                 asyncResp->res.jsonValue["@odata.type"] =
46146229577SJames Feist                     "#TaskService.v1_1_4.TaskService";
462*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.id"] =
463*7e860f15SJohn Edward Broadbent                     "/redfish/v1/TaskService";
46446229577SJames Feist                 asyncResp->res.jsonValue["Name"] = "Task Service";
46546229577SJames Feist                 asyncResp->res.jsonValue["Id"] = "TaskService";
466*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["DateTime"] =
467*7e860f15SJohn Edward Broadbent                     crow::utility::dateTimeNow();
468*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] =
469*7e860f15SJohn Edward Broadbent                     "Oldest";
47046229577SJames Feist 
471*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] =
472*7e860f15SJohn Edward Broadbent                     true;
47346229577SJames Feist 
47446229577SJames Feist                 auto health = std::make_shared<HealthPopulate>(asyncResp);
47546229577SJames Feist                 health->populate();
47646229577SJames Feist                 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
47746229577SJames Feist                 asyncResp->res.jsonValue["ServiceEnabled"] = true;
47846229577SJames Feist                 asyncResp->res.jsonValue["Tasks"] = {
47946229577SJames Feist                     {"@odata.id", "/redfish/v1/TaskService/Tasks"}};
480*7e860f15SJohn Edward Broadbent             });
48146229577SJames Feist }
48246229577SJames Feist 
48346229577SJames Feist } // namespace redfish
484