xref: /openbmc/bmcweb/features/redfish/lib/task.hpp (revision d43cd0ca0ff6ea11e6dd0ccedb83147bb0b2d207)
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 
1846229577SJames Feist #include "node.hpp"
1946229577SJames Feist 
20*d43cd0caSEd Tanous #include <boost/asio/post.hpp>
21*d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp>
2246229577SJames Feist #include <boost/container/flat_map.hpp>
23e5d5006bSJames Feist #include <task_messages.hpp>
241214b7e7SGunnar Mills 
251214b7e7SGunnar Mills #include <chrono>
2646229577SJames Feist #include <variant>
2746229577SJames Feist 
2846229577SJames Feist namespace redfish
2946229577SJames Feist {
3046229577SJames Feist 
3146229577SJames Feist namespace task
3246229577SJames Feist {
3346229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit
3446229577SJames Feist 
3546229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks;
3646229577SJames Feist 
3732898ceaSJames Feist constexpr bool completed = true;
3832898ceaSJames Feist 
39fe306728SJames Feist struct Payload
40fe306728SJames Feist {
41fe306728SJames Feist     Payload(const crow::Request& req) :
42fe306728SJames Feist         targetUri(req.url), httpOperation(req.methodString()),
43fe306728SJames Feist         httpHeaders(nlohmann::json::array())
44fe306728SJames Feist 
45fe306728SJames Feist     {
46fe306728SJames Feist         using field_ns = boost::beast::http::field;
47fe306728SJames Feist         constexpr const std::array<boost::beast::http::field, 7>
48fe306728SJames Feist             headerWhitelist = {field_ns::accept,     field_ns::accept_encoding,
49fe306728SJames Feist                                field_ns::user_agent, field_ns::host,
50fe306728SJames Feist                                field_ns::connection, field_ns::content_length,
51fe306728SJames Feist                                field_ns::upgrade};
52fe306728SJames Feist 
53fe306728SJames Feist         jsonBody = nlohmann::json::parse(req.body, nullptr, false);
54fe306728SJames Feist         if (jsonBody.is_discarded())
55fe306728SJames Feist         {
56fe306728SJames Feist             jsonBody = nullptr;
57fe306728SJames Feist         }
58fe306728SJames Feist 
59fe306728SJames Feist         for (const auto& field : req.fields)
60fe306728SJames Feist         {
61fe306728SJames Feist             if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
62fe306728SJames Feist                           field.name()) == headerWhitelist.end())
63fe306728SJames Feist             {
64fe306728SJames Feist                 continue;
65fe306728SJames Feist             }
66fe306728SJames Feist             std::string header;
67fe306728SJames Feist             header.reserve(field.name_string().size() + 2 +
68fe306728SJames Feist                            field.value().size());
69fe306728SJames Feist             header += field.name_string();
70fe306728SJames Feist             header += ": ";
71fe306728SJames Feist             header += field.value();
72fe306728SJames Feist             httpHeaders.emplace_back(std::move(header));
73fe306728SJames Feist         }
74fe306728SJames Feist     }
75fe306728SJames Feist     Payload() = delete;
76fe306728SJames Feist 
77fe306728SJames Feist     std::string targetUri;
78fe306728SJames Feist     std::string httpOperation;
79fe306728SJames Feist     nlohmann::json httpHeaders;
80fe306728SJames Feist     nlohmann::json jsonBody;
81fe306728SJames Feist };
82fe306728SJames Feist 
83fe306728SJames Feist inline void to_json(nlohmann::json& j, const Payload& p)
84fe306728SJames Feist {
85fe306728SJames Feist     j = {{"TargetUri", p.targetUri},
86fe306728SJames Feist          {"HttpOperation", p.httpOperation},
87fe306728SJames Feist          {"HttpHeaders", p.httpHeaders},
88fe306728SJames Feist          {"JsonBody", p.jsonBody.dump()}};
89fe306728SJames Feist }
90fe306728SJames Feist 
9146229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData>
9246229577SJames Feist {
9346229577SJames Feist   private:
9446229577SJames Feist     TaskData(std::function<bool(boost::system::error_code,
9546229577SJames Feist                                 sdbusplus::message::message&,
9646229577SJames Feist                                 const std::shared_ptr<TaskData>&)>&& handler,
9723a21a1cSEd Tanous              const std::string& matchIn, size_t idx) :
9846229577SJames Feist         callback(std::move(handler)),
9923a21a1cSEd Tanous         matchStr(matchIn), index(idx),
10046229577SJames Feist         startTime(std::chrono::system_clock::to_time_t(
10146229577SJames Feist             std::chrono::system_clock::now())),
10246229577SJames Feist         status("OK"), state("Running"), messages(nlohmann::json::array()),
10346229577SJames Feist         timer(crow::connections::systemBus->get_io_context())
10446229577SJames Feist 
1051214b7e7SGunnar Mills     {}
10646229577SJames Feist 
10746229577SJames Feist   public:
108d609fd6eSEd Tanous     TaskData() = delete;
109d609fd6eSEd Tanous 
11046229577SJames Feist     static std::shared_ptr<TaskData>& createTask(
11146229577SJames Feist         std::function<bool(boost::system::error_code,
11246229577SJames Feist                            sdbusplus::message::message&,
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,
1211214b7e7SGunnar Mills                                    sdbusplus::message::message&,
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         {
13046229577SJames Feist             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;
14946229577SJames Feist             res.jsonValue = {{"@odata.id", uri},
15046229577SJames Feist                              {"@odata.type", "#Task.v1_4_3.Task"},
15146229577SJames Feist                              {"Id", strIdx},
15246229577SJames Feist                              {"TaskState", state},
15346229577SJames Feist                              {"TaskStatus", status}};
15446229577SJames Feist             res.addHeader(boost::beast::http::field::location,
15546229577SJames Feist                           uri + "/Monitor");
15646229577SJames Feist             res.addHeader(boost::beast::http::field::retry_after,
15746229577SJames Feist                           std::to_string(retryAfterSeconds));
15846229577SJames Feist         }
15946229577SJames Feist         else if (!gave204)
16046229577SJames Feist         {
16146229577SJames Feist             res.result(boost::beast::http::status::no_content);
16246229577SJames Feist             gave204 = true;
16346229577SJames Feist         }
16446229577SJames Feist     }
16546229577SJames Feist 
166d609fd6eSEd Tanous     void finishTask()
16746229577SJames Feist     {
16846229577SJames Feist         endTime = std::chrono::system_clock::to_time_t(
16946229577SJames Feist             std::chrono::system_clock::now());
17046229577SJames Feist     }
17146229577SJames Feist 
172fd9ab9e1SJames Feist     void extendTimer(const std::chrono::seconds& timeout)
17346229577SJames Feist     {
17446229577SJames Feist         timer.expires_after(timeout);
17546229577SJames Feist         timer.async_wait(
17646229577SJames Feist             [self = shared_from_this()](boost::system::error_code ec) {
17746229577SJames Feist                 if (ec == boost::asio::error::operation_aborted)
17846229577SJames Feist                 {
1794e0453b1SGunnar Mills                     return; // completed successfully
18046229577SJames Feist                 }
18146229577SJames Feist                 if (!ec)
18246229577SJames Feist                 {
18346229577SJames Feist                     // change ec to error as timer expired
18446229577SJames Feist                     ec = boost::asio::error::operation_aborted;
18546229577SJames Feist                 }
18646229577SJames Feist                 self->match.reset();
18746229577SJames Feist                 sdbusplus::message::message msg;
18846229577SJames Feist                 self->finishTask();
18946229577SJames Feist                 self->state = "Cancelled";
19046229577SJames Feist                 self->status = "Warning";
191e5d5006bSJames Feist                 self->messages.emplace_back(
192e5d5006bSJames Feist                     messages::taskAborted(std::to_string(self->index)));
193e7686576SSunitha Harish                 // Send event :TaskAborted
194e7686576SSunitha Harish                 self->sendTaskEvent(self->state, self->index);
19546229577SJames Feist                 self->callback(ec, msg, self);
19646229577SJames Feist             });
197fd9ab9e1SJames Feist     }
198fd9ab9e1SJames Feist 
199e7686576SSunitha Harish     void sendTaskEvent(const std::string_view state, size_t index)
200e7686576SSunitha Harish     {
201e7686576SSunitha Harish         std::string origin =
202e7686576SSunitha Harish             "/redfish/v1/TaskService/Tasks/" + std::to_string(index);
203e7686576SSunitha Harish         std::string resType = "Task";
204e7686576SSunitha Harish         // TaskState enums which should send out an event are:
205e7686576SSunitha Harish         // "Starting" = taskResumed
206e7686576SSunitha Harish         // "Running" = taskStarted
207e7686576SSunitha Harish         // "Suspended" = taskPaused
208e7686576SSunitha Harish         // "Interrupted" = taskPaused
209e7686576SSunitha Harish         // "Pending" = taskPaused
210e7686576SSunitha Harish         // "Stopping" = taskAborted
211e7686576SSunitha Harish         // "Completed" = taskCompletedOK
212e7686576SSunitha Harish         // "Killed" = taskRemoved
213e7686576SSunitha Harish         // "Exception" = taskCompletedWarning
214e7686576SSunitha Harish         // "Cancelled" = taskCancelled
215e7686576SSunitha Harish         if (state == "Starting")
216e7686576SSunitha Harish         {
217e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
218e7686576SSunitha Harish                 redfish::messages::taskResumed(std::to_string(index)), origin,
219e7686576SSunitha Harish                 resType);
220e7686576SSunitha Harish         }
221e7686576SSunitha Harish         else if (state == "Running")
222e7686576SSunitha Harish         {
223e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
224e7686576SSunitha Harish                 redfish::messages::taskStarted(std::to_string(index)), origin,
225e7686576SSunitha Harish                 resType);
226e7686576SSunitha Harish         }
227e7686576SSunitha Harish         else if ((state == "Suspended") || (state == "Interrupted") ||
228e7686576SSunitha Harish                  (state == "Pending"))
229e7686576SSunitha Harish         {
230e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
231e7686576SSunitha Harish                 redfish::messages::taskPaused(std::to_string(index)), origin,
232e7686576SSunitha Harish                 resType);
233e7686576SSunitha Harish         }
234e7686576SSunitha Harish         else if (state == "Stopping")
235e7686576SSunitha Harish         {
236e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
237e7686576SSunitha Harish                 redfish::messages::taskAborted(std::to_string(index)), origin,
238e7686576SSunitha Harish                 resType);
239e7686576SSunitha Harish         }
240e7686576SSunitha Harish         else if (state == "Completed")
241e7686576SSunitha Harish         {
242e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
243e7686576SSunitha Harish                 redfish::messages::taskCompletedOK(std::to_string(index)),
244e7686576SSunitha Harish                 origin, resType);
245e7686576SSunitha Harish         }
246e7686576SSunitha Harish         else if (state == "Killed")
247e7686576SSunitha Harish         {
248e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
249e7686576SSunitha Harish                 redfish::messages::taskRemoved(std::to_string(index)), origin,
250e7686576SSunitha Harish                 resType);
251e7686576SSunitha Harish         }
252e7686576SSunitha Harish         else if (state == "Exception")
253e7686576SSunitha Harish         {
254e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
255e7686576SSunitha Harish                 redfish::messages::taskCompletedWarning(std::to_string(index)),
256e7686576SSunitha Harish                 origin, resType);
257e7686576SSunitha Harish         }
258e7686576SSunitha Harish         else if (state == "Cancelled")
259e7686576SSunitha Harish         {
260e7686576SSunitha Harish             redfish::EventServiceManager::getInstance().sendEvent(
261e7686576SSunitha Harish                 redfish::messages::taskCancelled(std::to_string(index)), origin,
262e7686576SSunitha Harish                 resType);
263e7686576SSunitha Harish         }
264e7686576SSunitha Harish         else
265e7686576SSunitha Harish         {
266e7686576SSunitha Harish             BMCWEB_LOG_INFO << "sendTaskEvent: No events to send";
267e7686576SSunitha Harish         }
268e7686576SSunitha Harish     }
269e7686576SSunitha Harish 
270fd9ab9e1SJames Feist     void startTimer(const std::chrono::seconds& timeout)
271fd9ab9e1SJames Feist     {
272fd9ab9e1SJames Feist         if (match)
273fd9ab9e1SJames Feist         {
274fd9ab9e1SJames Feist             return;
275fd9ab9e1SJames Feist         }
276fd9ab9e1SJames Feist         match = std::make_unique<sdbusplus::bus::match::match>(
277fd9ab9e1SJames Feist             static_cast<sdbusplus::bus::bus&>(*crow::connections::systemBus),
278fd9ab9e1SJames Feist             matchStr,
279fd9ab9e1SJames Feist             [self = shared_from_this()](sdbusplus::message::message& message) {
280fd9ab9e1SJames Feist                 boost::system::error_code ec;
281fd9ab9e1SJames Feist 
282fd9ab9e1SJames Feist                 // callback to return True if callback is done, callback needs
283fd9ab9e1SJames Feist                 // to update status itself if needed
284fd9ab9e1SJames Feist                 if (self->callback(ec, message, self) == task::completed)
285fd9ab9e1SJames Feist                 {
286fd9ab9e1SJames Feist                     self->timer.cancel();
287fd9ab9e1SJames Feist                     self->finishTask();
288fd9ab9e1SJames Feist 
289e7686576SSunitha Harish                     // Send event
290e7686576SSunitha Harish                     self->sendTaskEvent(self->state, self->index);
291e7686576SSunitha Harish 
292fd9ab9e1SJames Feist                     // reset the match after the callback was successful
293fd9ab9e1SJames Feist                     boost::asio::post(
294fd9ab9e1SJames Feist                         crow::connections::systemBus->get_io_context(),
295fd9ab9e1SJames Feist                         [self] { self->match.reset(); });
296fd9ab9e1SJames Feist                     return;
297fd9ab9e1SJames Feist                 }
298fd9ab9e1SJames Feist             });
299fd9ab9e1SJames Feist 
300fd9ab9e1SJames Feist         extendTimer(timeout);
301e5d5006bSJames Feist         messages.emplace_back(messages::taskStarted(std::to_string(index)));
302e7686576SSunitha Harish         // Send event : TaskStarted
303e7686576SSunitha Harish         sendTaskEvent(state, index);
30446229577SJames Feist     }
30546229577SJames Feist 
30646229577SJames Feist     std::function<bool(boost::system::error_code, sdbusplus::message::message&,
30746229577SJames Feist                        const std::shared_ptr<TaskData>&)>
30846229577SJames Feist         callback;
30946229577SJames Feist     std::string matchStr;
31046229577SJames Feist     size_t index;
31146229577SJames Feist     time_t startTime;
31246229577SJames Feist     std::string status;
31346229577SJames Feist     std::string state;
31446229577SJames Feist     nlohmann::json messages;
31546229577SJames Feist     boost::asio::steady_timer timer;
31646229577SJames Feist     std::unique_ptr<sdbusplus::bus::match::match> match;
31746229577SJames Feist     std::optional<time_t> endTime;
318fe306728SJames Feist     std::optional<Payload> payload;
31946229577SJames Feist     bool gave204 = false;
32046229577SJames Feist };
32146229577SJames Feist 
32246229577SJames Feist } // namespace task
32346229577SJames Feist 
32446229577SJames Feist class TaskMonitor : public Node
32546229577SJames Feist {
32646229577SJames Feist   public:
32752cc112dSEd Tanous     TaskMonitor(App& app) :
3287af91514SGunnar Mills         Node((app), "/redfish/v1/TaskService/Tasks/<str>/Monitor/",
32946229577SJames Feist              std::string())
33046229577SJames Feist     {
33146229577SJames Feist         entityPrivileges = {
33246229577SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
33346229577SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
33446229577SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
33546229577SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
33646229577SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
33746229577SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
33846229577SJames Feist     }
33946229577SJames Feist 
34046229577SJames Feist   private:
341cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
34246229577SJames Feist                const std::vector<std::string>& params) override
34346229577SJames Feist     {
34446229577SJames Feist         auto asyncResp = std::make_shared<AsyncResp>(res);
34546229577SJames Feist         if (params.size() != 1)
34646229577SJames Feist         {
34746229577SJames Feist             messages::internalError(asyncResp->res);
34846229577SJames Feist             return;
34946229577SJames Feist         }
35046229577SJames Feist 
35146229577SJames Feist         const std::string& strParam = params[0];
35246229577SJames Feist         auto find = std::find_if(
35346229577SJames Feist             task::tasks.begin(), task::tasks.end(),
35446229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
35546229577SJames Feist                 if (!task)
35646229577SJames Feist                 {
35746229577SJames Feist                     return false;
35846229577SJames Feist                 }
35946229577SJames Feist 
36046229577SJames Feist                 // we compare against the string version as on failure strtoul
36146229577SJames Feist                 // returns 0
36246229577SJames Feist                 return std::to_string(task->index) == strParam;
36346229577SJames Feist             });
36446229577SJames Feist 
36546229577SJames Feist         if (find == task::tasks.end())
36646229577SJames Feist         {
36746229577SJames Feist             messages::resourceNotFound(asyncResp->res, "Monitor", strParam);
36846229577SJames Feist             return;
36946229577SJames Feist         }
37046229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
37146229577SJames Feist         // monitor expires after 204
37246229577SJames Feist         if (ptr->gave204)
37346229577SJames Feist         {
37446229577SJames Feist             messages::resourceNotFound(asyncResp->res, "Monitor", strParam);
37546229577SJames Feist             return;
37646229577SJames Feist         }
37746229577SJames Feist         ptr->populateResp(asyncResp->res);
37846229577SJames Feist     }
37946229577SJames Feist };
38046229577SJames Feist 
38146229577SJames Feist class Task : public Node
38246229577SJames Feist {
38346229577SJames Feist   public:
38452cc112dSEd Tanous     Task(App& app) :
3857af91514SGunnar Mills         Node((app), "/redfish/v1/TaskService/Tasks/<str>/", std::string())
38646229577SJames Feist     {
38746229577SJames Feist         entityPrivileges = {
38846229577SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
38946229577SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
39046229577SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
39146229577SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
39246229577SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
39346229577SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
39446229577SJames Feist     }
39546229577SJames Feist 
39646229577SJames Feist   private:
397cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
39846229577SJames Feist                const std::vector<std::string>& params) override
39946229577SJames Feist     {
40046229577SJames Feist         auto asyncResp = std::make_shared<AsyncResp>(res);
40146229577SJames Feist         if (params.size() != 1)
40246229577SJames Feist         {
40346229577SJames Feist             messages::internalError(asyncResp->res);
40446229577SJames Feist             return;
40546229577SJames Feist         }
40646229577SJames Feist 
40746229577SJames Feist         const std::string& strParam = params[0];
40846229577SJames Feist         auto find = std::find_if(
40946229577SJames Feist             task::tasks.begin(), task::tasks.end(),
41046229577SJames Feist             [&strParam](const std::shared_ptr<task::TaskData>& task) {
41146229577SJames Feist                 if (!task)
41246229577SJames Feist                 {
41346229577SJames Feist                     return false;
41446229577SJames Feist                 }
41546229577SJames Feist 
41646229577SJames Feist                 // we compare against the string version as on failure strtoul
41746229577SJames Feist                 // returns 0
41846229577SJames Feist                 return std::to_string(task->index) == strParam;
41946229577SJames Feist             });
42046229577SJames Feist 
42146229577SJames Feist         if (find == task::tasks.end())
42246229577SJames Feist         {
42346229577SJames Feist             messages::resourceNotFound(asyncResp->res, "Tasks", strParam);
42446229577SJames Feist             return;
42546229577SJames Feist         }
42646229577SJames Feist 
42746229577SJames Feist         std::shared_ptr<task::TaskData>& ptr = *find;
42846229577SJames Feist 
42946229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
43046229577SJames Feist         asyncResp->res.jsonValue["Id"] = strParam;
43146229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task " + strParam;
43246229577SJames Feist         asyncResp->res.jsonValue["TaskState"] = ptr->state;
43346229577SJames Feist         asyncResp->res.jsonValue["StartTime"] =
43446229577SJames Feist             crow::utility::getDateTime(ptr->startTime);
43546229577SJames Feist         if (ptr->endTime)
43646229577SJames Feist         {
43746229577SJames Feist             asyncResp->res.jsonValue["EndTime"] =
43846229577SJames Feist                 crow::utility::getDateTime(*(ptr->endTime));
43946229577SJames Feist         }
44046229577SJames Feist         asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
44146229577SJames Feist         asyncResp->res.jsonValue["Messages"] = ptr->messages;
44246229577SJames Feist         asyncResp->res.jsonValue["@odata.id"] =
44346229577SJames Feist             "/redfish/v1/TaskService/Tasks/" + strParam;
44446229577SJames Feist         if (!ptr->gave204)
44546229577SJames Feist         {
44646229577SJames Feist             asyncResp->res.jsonValue["TaskMonitor"] =
44746229577SJames Feist                 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
44846229577SJames Feist         }
449fe306728SJames Feist         if (ptr->payload)
450fe306728SJames Feist         {
451fe306728SJames Feist             asyncResp->res.jsonValue["Payload"] = *(ptr->payload);
452fe306728SJames Feist         }
45346229577SJames Feist     }
45446229577SJames Feist };
45546229577SJames Feist 
45646229577SJames Feist class TaskCollection : public Node
45746229577SJames Feist {
45846229577SJames Feist   public:
45952cc112dSEd Tanous     TaskCollection(App& app) : Node(app, "/redfish/v1/TaskService/Tasks/")
46046229577SJames Feist     {
46146229577SJames Feist         entityPrivileges = {
46246229577SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
46346229577SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
46446229577SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
46546229577SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
46646229577SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
46746229577SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
46846229577SJames Feist     }
46946229577SJames Feist 
47046229577SJames Feist   private:
471cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
472cb13a392SEd Tanous                const std::vector<std::string>&) override
47346229577SJames Feist     {
47446229577SJames Feist         auto asyncResp = std::make_shared<AsyncResp>(res);
47546229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
47646229577SJames Feist             "#TaskCollection.TaskCollection";
47746229577SJames Feist         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
47846229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Collection";
47946229577SJames Feist         asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
48046229577SJames Feist         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
48146229577SJames Feist         members = nlohmann::json::array();
48246229577SJames Feist 
48346229577SJames Feist         for (const std::shared_ptr<task::TaskData>& task : task::tasks)
48446229577SJames Feist         {
48546229577SJames Feist             if (task == nullptr)
48646229577SJames Feist             {
48746229577SJames Feist                 continue; // shouldn't be possible
48846229577SJames Feist             }
48946229577SJames Feist             members.emplace_back(
49046229577SJames Feist                 nlohmann::json{{"@odata.id", "/redfish/v1/TaskService/Tasks/" +
49146229577SJames Feist                                                  std::to_string(task->index)}});
49246229577SJames Feist         }
49346229577SJames Feist     }
49446229577SJames Feist };
49546229577SJames Feist 
49646229577SJames Feist class TaskService : public Node
49746229577SJames Feist {
49846229577SJames Feist   public:
49952cc112dSEd Tanous     TaskService(App& app) : Node(app, "/redfish/v1/TaskService/")
50046229577SJames Feist     {
50146229577SJames Feist         entityPrivileges = {
50246229577SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
50346229577SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
50446229577SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
50546229577SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
50646229577SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
50746229577SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
50846229577SJames Feist     }
50946229577SJames Feist 
51046229577SJames Feist   private:
511cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
512cb13a392SEd Tanous                const std::vector<std::string>&) override
51346229577SJames Feist     {
51446229577SJames Feist         auto asyncResp = std::make_shared<AsyncResp>(res);
51546229577SJames Feist         asyncResp->res.jsonValue["@odata.type"] =
51646229577SJames Feist             "#TaskService.v1_1_4.TaskService";
51746229577SJames Feist         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
51846229577SJames Feist         asyncResp->res.jsonValue["Name"] = "Task Service";
51946229577SJames Feist         asyncResp->res.jsonValue["Id"] = "TaskService";
52046229577SJames Feist         asyncResp->res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
52146229577SJames Feist         asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
52246229577SJames Feist 
523e7686576SSunitha Harish         asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = true;
52446229577SJames Feist 
52546229577SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
52646229577SJames Feist         health->populate();
52746229577SJames Feist         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
52846229577SJames Feist         asyncResp->res.jsonValue["ServiceEnabled"] = true;
52946229577SJames Feist         asyncResp->res.jsonValue["Tasks"] = {
53046229577SJames Feist             {"@odata.id", "/redfish/v1/TaskService/Tasks"}};
53146229577SJames Feist     }
53246229577SJames Feist };
53346229577SJames Feist 
53446229577SJames Feist } // namespace redfish
535