140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation 446229577SJames Feist #pragma once 546229577SJames Feist 63ccb3adbSEd Tanous #include "app.hpp" 7d7857201SEd Tanous #include "async_resp.hpp" 8d7857201SEd Tanous #include "dbus_singleton.hpp" 9d7857201SEd Tanous #include "error_messages.hpp" 103ccb3adbSEd Tanous #include "event_service_manager.hpp" 11539d8c6bSEd Tanous #include "generated/enums/resource.hpp" 12539d8c6bSEd Tanous #include "generated/enums/task_service.hpp" 131aa0c2b8SEd Tanous #include "http/parsing.hpp" 14d7857201SEd Tanous #include "http_request.hpp" 15d7857201SEd Tanous #include "http_response.hpp" 16d7857201SEd Tanous #include "logging.hpp" 173ccb3adbSEd Tanous #include "query.hpp" 183ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 193ccb3adbSEd Tanous #include "task_messages.hpp" 20d7857201SEd Tanous #include "utils/time_utils.hpp" 213ccb3adbSEd Tanous 22d7857201SEd Tanous #include <boost/asio/error.hpp> 23d43cd0caSEd Tanous #include <boost/asio/post.hpp> 24d43cd0caSEd Tanous #include <boost/asio/steady_timer.hpp> 25d7857201SEd Tanous #include <boost/beast/http/field.hpp> 26d7857201SEd Tanous #include <boost/beast/http/status.hpp> 27d7857201SEd Tanous #include <boost/beast/http/verb.hpp> 28ef4c65b7SEd Tanous #include <boost/url/format.hpp> 29d7857201SEd Tanous #include <boost/url/url.hpp> 30d7857201SEd Tanous #include <nlohmann/json.hpp> 31d7857201SEd Tanous #include <sdbusplus/bus.hpp> 323ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 33d7857201SEd Tanous #include <sdbusplus/message.hpp> 341214b7e7SGunnar Mills 35d7857201SEd Tanous #include <algorithm> 36d7857201SEd Tanous #include <array> 371214b7e7SGunnar Mills #include <chrono> 38d7857201SEd Tanous #include <cstddef> 39d7857201SEd Tanous #include <ctime> 40d7857201SEd Tanous #include <deque> 41d7857201SEd Tanous #include <functional> 423ccb3adbSEd Tanous #include <memory> 43d7857201SEd Tanous #include <optional> 443544d2a7SEd Tanous #include <ranges> 45d7857201SEd Tanous #include <string> 46d7857201SEd Tanous #include <string_view> 47d7857201SEd Tanous #include <utility> 4846229577SJames Feist 4946229577SJames Feist namespace redfish 5046229577SJames Feist { 5146229577SJames Feist 5246229577SJames Feist namespace task 5346229577SJames Feist { 5446229577SJames Feist constexpr size_t maxTaskCount = 100; // arbitrary limit 5546229577SJames Feist 56cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 5746229577SJames Feist static std::deque<std::shared_ptr<struct TaskData>> tasks; 5846229577SJames Feist 5932898ceaSJames Feist constexpr bool completed = true; 6032898ceaSJames Feist 61fe306728SJames Feist struct Payload 62fe306728SJames Feist { 634e23a444SEd Tanous explicit Payload(const crow::Request& req) : 6439662a3bSEd Tanous targetUri(req.url().encoded_path()), httpOperation(req.methodString()), 651aa0c2b8SEd Tanous httpHeaders(nlohmann::json::array()) 66fe306728SJames Feist { 67fe306728SJames Feist using field_ns = boost::beast::http::field; 68fe306728SJames Feist constexpr const std::array<boost::beast::http::field, 7> 69fe306728SJames Feist headerWhitelist = {field_ns::accept, field_ns::accept_encoding, 70fe306728SJames Feist field_ns::user_agent, field_ns::host, 71fe306728SJames Feist field_ns::connection, field_ns::content_length, 72fe306728SJames Feist field_ns::upgrade}; 73fe306728SJames Feist 741aa0c2b8SEd Tanous JsonParseResult ret = parseRequestAsJson(req, jsonBody); 751aa0c2b8SEd Tanous if (ret != JsonParseResult::Success) 76fe306728SJames Feist { 771aa0c2b8SEd Tanous return; 78fe306728SJames Feist } 79fe306728SJames Feist 8098fe740bSEd Tanous for (const auto& field : req.fields()) 81fe306728SJames Feist { 823544d2a7SEd Tanous if (std::ranges::find(headerWhitelist, field.name()) == 833544d2a7SEd Tanous headerWhitelist.end()) 84fe306728SJames Feist { 85fe306728SJames Feist continue; 86fe306728SJames Feist } 87fe306728SJames Feist std::string header; 88bd79bce8SPatrick Williams header.reserve( 89bd79bce8SPatrick Williams field.name_string().size() + 2 + field.value().size()); 90fe306728SJames Feist header += field.name_string(); 91fe306728SJames Feist header += ": "; 92fe306728SJames Feist header += field.value(); 93fe306728SJames Feist httpHeaders.emplace_back(std::move(header)); 94fe306728SJames Feist } 95fe306728SJames Feist } 96fe306728SJames Feist Payload() = delete; 97fe306728SJames Feist 98fe306728SJames Feist std::string targetUri; 99fe306728SJames Feist std::string httpOperation; 100fe306728SJames Feist nlohmann::json httpHeaders; 101fe306728SJames Feist nlohmann::json jsonBody; 102fe306728SJames Feist }; 103fe306728SJames Feist 10446229577SJames Feist struct TaskData : std::enable_shared_from_this<TaskData> 10546229577SJames Feist { 10646229577SJames Feist private: 10759d494eeSPatrick Williams TaskData( 10859d494eeSPatrick Williams std::function<bool(boost::system::error_code, sdbusplus::message_t&, 10946229577SJames Feist const std::shared_ptr<TaskData>&)>&& handler, 11023a21a1cSEd Tanous const std::string& matchIn, size_t idx) : 111bd79bce8SPatrick Williams callback(std::move(handler)), matchStr(matchIn), index(idx), 11246229577SJames Feist startTime(std::chrono::system_clock::to_time_t( 11346229577SJames Feist std::chrono::system_clock::now())), 11446229577SJames Feist status("OK"), state("Running"), messages(nlohmann::json::array()), 11546229577SJames Feist timer(crow::connections::systemBus->get_io_context()) 11646229577SJames Feist 1171214b7e7SGunnar Mills {} 11846229577SJames Feist 11946229577SJames Feist public: 120d609fd6eSEd Tanous TaskData() = delete; 121d609fd6eSEd Tanous 12246229577SJames Feist static std::shared_ptr<TaskData>& createTask( 12359d494eeSPatrick Williams std::function<bool(boost::system::error_code, sdbusplus::message_t&, 12446229577SJames Feist const std::shared_ptr<TaskData>&)>&& handler, 12546229577SJames Feist const std::string& match) 12646229577SJames Feist { 12746229577SJames Feist static size_t lastTask = 0; 12846229577SJames Feist struct MakeSharedHelper : public TaskData 12946229577SJames Feist { 13046229577SJames Feist MakeSharedHelper( 1311214b7e7SGunnar Mills std::function<bool(boost::system::error_code, 13259d494eeSPatrick Williams sdbusplus::message_t&, 13346229577SJames Feist const std::shared_ptr<TaskData>&)>&& handler, 13423a21a1cSEd Tanous const std::string& match2, size_t idx) : 13523a21a1cSEd Tanous TaskData(std::move(handler), match2, idx) 1361214b7e7SGunnar Mills {} 13746229577SJames Feist }; 13846229577SJames Feist 13946229577SJames Feist if (tasks.size() >= maxTaskCount) 14046229577SJames Feist { 141*9f03894eSRohit PAI const auto last = getTaskToRemove(); 14246229577SJames Feist 14346229577SJames Feist // destroy all references 144*9f03894eSRohit PAI (*last)->timer.cancel(); 145*9f03894eSRohit PAI (*last)->match.reset(); 146*9f03894eSRohit PAI tasks.erase(last); 14746229577SJames Feist } 14846229577SJames Feist 14946229577SJames Feist return tasks.emplace_back(std::make_shared<MakeSharedHelper>( 15046229577SJames Feist std::move(handler), match, lastTask++)); 15146229577SJames Feist } 15246229577SJames Feist 153*9f03894eSRohit PAI /** 154*9f03894eSRohit PAI * @brief Get the first completed/aborted task or oldest running task to 155*9f03894eSRohit PAI * remove 156*9f03894eSRohit PAI */ 157*9f03894eSRohit PAI static std::deque<std::shared_ptr<TaskData>>::iterator getTaskToRemove() 158*9f03894eSRohit PAI { 159*9f03894eSRohit PAI static constexpr std::array<std::string_view, 5> activeStates = { 160*9f03894eSRohit PAI "Running", "Pending", "Starting", "Suspended", "Interrupted"}; 161*9f03894eSRohit PAI 162*9f03894eSRohit PAI auto it = 163*9f03894eSRohit PAI std::find_if(tasks.begin(), tasks.end(), [](const auto& task) { 164*9f03894eSRohit PAI return std::ranges::find(activeStates, task->state) == 165*9f03894eSRohit PAI activeStates.end(); 166*9f03894eSRohit PAI }); 167*9f03894eSRohit PAI 168*9f03894eSRohit PAI return (it != tasks.end()) ? it : tasks.begin(); 169*9f03894eSRohit PAI } 170*9f03894eSRohit PAI 17146229577SJames Feist void populateResp(crow::Response& res, size_t retryAfterSeconds = 30) 17246229577SJames Feist { 17346229577SJames Feist if (!endTime) 17446229577SJames Feist { 17546229577SJames Feist res.result(boost::beast::http::status::accepted); 17646229577SJames Feist std::string strIdx = std::to_string(index); 177fdbce79bSEd Tanous boost::urls::url uri = 178fdbce79bSEd Tanous boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx); 1791476687dSEd Tanous 1801476687dSEd Tanous res.jsonValue["@odata.id"] = uri; 1811476687dSEd Tanous res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task"; 1821476687dSEd Tanous res.jsonValue["Id"] = strIdx; 1831476687dSEd Tanous res.jsonValue["TaskState"] = state; 1841476687dSEd Tanous res.jsonValue["TaskStatus"] = status; 1851476687dSEd Tanous 186fdbce79bSEd Tanous boost::urls::url taskMonitor = boost::urls::format( 187fdbce79bSEd Tanous "/redfish/v1/TaskService/TaskMonitors/{}", strIdx); 188fdbce79bSEd Tanous 18946229577SJames Feist res.addHeader(boost::beast::http::field::location, 190fdbce79bSEd Tanous taskMonitor.buffer()); 19146229577SJames Feist res.addHeader(boost::beast::http::field::retry_after, 19246229577SJames Feist std::to_string(retryAfterSeconds)); 19346229577SJames Feist } 19446229577SJames Feist else if (!gave204) 19546229577SJames Feist { 19646229577SJames Feist res.result(boost::beast::http::status::no_content); 19746229577SJames Feist gave204 = true; 19846229577SJames Feist } 19946229577SJames Feist } 20046229577SJames Feist 201d609fd6eSEd Tanous void finishTask() 20246229577SJames Feist { 20346229577SJames Feist endTime = std::chrono::system_clock::to_time_t( 20446229577SJames Feist std::chrono::system_clock::now()); 20546229577SJames Feist } 20646229577SJames Feist 207fd9ab9e1SJames Feist void extendTimer(const std::chrono::seconds& timeout) 20846229577SJames Feist { 20946229577SJames Feist timer.expires_after(timeout); 21046229577SJames Feist timer.async_wait( 21146229577SJames Feist [self = shared_from_this()](boost::system::error_code ec) { 21246229577SJames Feist if (ec == boost::asio::error::operation_aborted) 21346229577SJames Feist { 2144e0453b1SGunnar Mills return; // completed successfully 21546229577SJames Feist } 21646229577SJames Feist if (!ec) 21746229577SJames Feist { 21846229577SJames Feist // change ec to error as timer expired 21946229577SJames Feist ec = boost::asio::error::operation_aborted; 22046229577SJames Feist } 22146229577SJames Feist self->match.reset(); 22259d494eeSPatrick Williams sdbusplus::message_t msg; 22346229577SJames Feist self->finishTask(); 22446229577SJames Feist self->state = "Cancelled"; 22546229577SJames Feist self->status = "Warning"; 226e5d5006bSJames Feist self->messages.emplace_back( 227e5d5006bSJames Feist messages::taskAborted(std::to_string(self->index))); 228e7686576SSunitha Harish // Send event :TaskAborted 229daadfb2eSEd Tanous sendTaskEvent(self->state, self->index); 23046229577SJames Feist self->callback(ec, msg, self); 23146229577SJames Feist }); 232fd9ab9e1SJames Feist } 233fd9ab9e1SJames Feist 23426ccae32SEd Tanous static void sendTaskEvent(std::string_view state, size_t index) 235e7686576SSunitha Harish { 236e7686576SSunitha Harish // TaskState enums which should send out an event are: 237e7686576SSunitha Harish // "Starting" = taskResumed 238e7686576SSunitha Harish // "Running" = taskStarted 239e7686576SSunitha Harish // "Suspended" = taskPaused 240e7686576SSunitha Harish // "Interrupted" = taskPaused 241e7686576SSunitha Harish // "Pending" = taskPaused 242e7686576SSunitha Harish // "Stopping" = taskAborted 243e7686576SSunitha Harish // "Completed" = taskCompletedOK 244e7686576SSunitha Harish // "Killed" = taskRemoved 245e7686576SSunitha Harish // "Exception" = taskCompletedWarning 246e7686576SSunitha Harish // "Cancelled" = taskCancelled 247f8fe2211SEd Tanous nlohmann::json event; 248f8fe2211SEd Tanous std::string indexStr = std::to_string(index); 249e7686576SSunitha Harish if (state == "Starting") 250e7686576SSunitha Harish { 251f8fe2211SEd Tanous event = redfish::messages::taskResumed(indexStr); 252e7686576SSunitha Harish } 253e7686576SSunitha Harish else if (state == "Running") 254e7686576SSunitha Harish { 255f8fe2211SEd Tanous event = redfish::messages::taskStarted(indexStr); 256e7686576SSunitha Harish } 257e7686576SSunitha Harish else if ((state == "Suspended") || (state == "Interrupted") || 258e7686576SSunitha Harish (state == "Pending")) 259e7686576SSunitha Harish { 260f8fe2211SEd Tanous event = redfish::messages::taskPaused(indexStr); 261e7686576SSunitha Harish } 262e7686576SSunitha Harish else if (state == "Stopping") 263e7686576SSunitha Harish { 264f8fe2211SEd Tanous event = redfish::messages::taskAborted(indexStr); 265e7686576SSunitha Harish } 266e7686576SSunitha Harish else if (state == "Completed") 267e7686576SSunitha Harish { 268f8fe2211SEd Tanous event = redfish::messages::taskCompletedOK(indexStr); 269e7686576SSunitha Harish } 270e7686576SSunitha Harish else if (state == "Killed") 271e7686576SSunitha Harish { 272f8fe2211SEd Tanous event = redfish::messages::taskRemoved(indexStr); 273e7686576SSunitha Harish } 274e7686576SSunitha Harish else if (state == "Exception") 275e7686576SSunitha Harish { 276f8fe2211SEd Tanous event = redfish::messages::taskCompletedWarning(indexStr); 277e7686576SSunitha Harish } 278e7686576SSunitha Harish else if (state == "Cancelled") 279e7686576SSunitha Harish { 280f8fe2211SEd Tanous event = redfish::messages::taskCancelled(indexStr); 281e7686576SSunitha Harish } 282e7686576SSunitha Harish else 283e7686576SSunitha Harish { 28462598e31SEd Tanous BMCWEB_LOG_INFO("sendTaskEvent: No events to send"); 285f8fe2211SEd Tanous return; 286e7686576SSunitha Harish } 287f8fe2211SEd Tanous boost::urls::url origin = 288f8fe2211SEd Tanous boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index); 289f8fe2211SEd Tanous EventServiceManager::getInstance().sendEvent(event, origin.buffer(), 290f8fe2211SEd Tanous "Task"); 291e7686576SSunitha Harish } 292e7686576SSunitha Harish 293fd9ab9e1SJames Feist void startTimer(const std::chrono::seconds& timeout) 294fd9ab9e1SJames Feist { 295fd9ab9e1SJames Feist if (match) 296fd9ab9e1SJames Feist { 297fd9ab9e1SJames Feist return; 298fd9ab9e1SJames Feist } 29959d494eeSPatrick Williams match = std::make_unique<sdbusplus::bus::match_t>( 30059d494eeSPatrick Williams static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus), 301fd9ab9e1SJames Feist matchStr, 30259d494eeSPatrick Williams [self = shared_from_this()](sdbusplus::message_t& message) { 303fd9ab9e1SJames Feist boost::system::error_code ec; 304fd9ab9e1SJames Feist 305fd9ab9e1SJames Feist // callback to return True if callback is done, callback needs 306fd9ab9e1SJames Feist // to update status itself if needed 307fd9ab9e1SJames Feist if (self->callback(ec, message, self) == task::completed) 308fd9ab9e1SJames Feist { 309fd9ab9e1SJames Feist self->timer.cancel(); 310fd9ab9e1SJames Feist self->finishTask(); 311fd9ab9e1SJames Feist 312e7686576SSunitha Harish // Send event 313daadfb2eSEd Tanous sendTaskEvent(self->state, self->index); 314e7686576SSunitha Harish 315fd9ab9e1SJames Feist // reset the match after the callback was successful 316fd9ab9e1SJames Feist boost::asio::post( 317fd9ab9e1SJames Feist crow::connections::systemBus->get_io_context(), 318fd9ab9e1SJames Feist [self] { self->match.reset(); }); 319fd9ab9e1SJames Feist return; 320fd9ab9e1SJames Feist } 321fd9ab9e1SJames Feist }); 322fd9ab9e1SJames Feist 323fd9ab9e1SJames Feist extendTimer(timeout); 324e5d5006bSJames Feist messages.emplace_back(messages::taskStarted(std::to_string(index))); 325e7686576SSunitha Harish // Send event : TaskStarted 326e7686576SSunitha Harish sendTaskEvent(state, index); 32746229577SJames Feist } 32846229577SJames Feist 32959d494eeSPatrick Williams std::function<bool(boost::system::error_code, sdbusplus::message_t&, 33046229577SJames Feist const std::shared_ptr<TaskData>&)> 33146229577SJames Feist callback; 33246229577SJames Feist std::string matchStr; 33346229577SJames Feist size_t index; 33446229577SJames Feist time_t startTime; 33546229577SJames Feist std::string status; 33646229577SJames Feist std::string state; 33746229577SJames Feist nlohmann::json messages; 33846229577SJames Feist boost::asio::steady_timer timer; 33959d494eeSPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> match; 34046229577SJames Feist std::optional<time_t> endTime; 341fe306728SJames Feist std::optional<Payload> payload; 34246229577SJames Feist bool gave204 = false; 3436868ff50SGeorge Liu int percentComplete = 0; 34446229577SJames Feist }; 34546229577SJames Feist 34646229577SJames Feist } // namespace task 34746229577SJames Feist 3487e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app) 34946229577SJames Feist { 350fdbce79bSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/") 351ed398213SEd Tanous .privileges(redfish::privileges::getTask) 3527e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 35345ca1b86SEd Tanous [&app](const crow::Request& req, 3547e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3557e860f15SJohn Edward Broadbent const std::string& strParam) { 3563ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 35745ca1b86SEd Tanous { 35845ca1b86SEd Tanous return; 35945ca1b86SEd Tanous } 3603544d2a7SEd Tanous auto find = std::ranges::find_if( 3613544d2a7SEd Tanous task::tasks, 36246229577SJames Feist [&strParam](const std::shared_ptr<task::TaskData>& task) { 36346229577SJames Feist if (!task) 36446229577SJames Feist { 36546229577SJames Feist return false; 36646229577SJames Feist } 36746229577SJames Feist 3687e860f15SJohn Edward Broadbent // we compare against the string version as on failure 3697e860f15SJohn Edward Broadbent // strtoul returns 0 37046229577SJames Feist return std::to_string(task->index) == strParam; 37146229577SJames Feist }); 37246229577SJames Feist 37346229577SJames Feist if (find == task::tasks.end()) 37446229577SJames Feist { 375bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 376bd79bce8SPatrick Williams strParam); 37746229577SJames Feist return; 37846229577SJames Feist } 37946229577SJames Feist std::shared_ptr<task::TaskData>& ptr = *find; 38046229577SJames Feist // monitor expires after 204 38146229577SJames Feist if (ptr->gave204) 38246229577SJames Feist { 383bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 384bd79bce8SPatrick Williams strParam); 38546229577SJames Feist return; 38646229577SJames Feist } 38746229577SJames Feist ptr->populateResp(asyncResp->res); 3887e860f15SJohn Edward Broadbent }); 38946229577SJames Feist } 39046229577SJames Feist 3917e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app) 39246229577SJames Feist { 3937e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/") 394ed398213SEd Tanous .privileges(redfish::privileges::getTask) 3957e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 39645ca1b86SEd Tanous [&app](const crow::Request& req, 3977e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3987e860f15SJohn Edward Broadbent const std::string& strParam) { 3993ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 40045ca1b86SEd Tanous { 40145ca1b86SEd Tanous return; 40245ca1b86SEd Tanous } 4033544d2a7SEd Tanous auto find = std::ranges::find_if( 4043544d2a7SEd Tanous task::tasks, 40546229577SJames Feist [&strParam](const std::shared_ptr<task::TaskData>& task) { 40646229577SJames Feist if (!task) 40746229577SJames Feist { 40846229577SJames Feist return false; 40946229577SJames Feist } 41046229577SJames Feist 4117e860f15SJohn Edward Broadbent // we compare against the string version as on failure 4127e860f15SJohn Edward Broadbent // strtoul returns 0 41346229577SJames Feist return std::to_string(task->index) == strParam; 41446229577SJames Feist }); 41546229577SJames Feist 41646229577SJames Feist if (find == task::tasks.end()) 41746229577SJames Feist { 418bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 419bd79bce8SPatrick Williams strParam); 42046229577SJames Feist return; 42146229577SJames Feist } 42246229577SJames Feist 42302cad96eSEd Tanous const std::shared_ptr<task::TaskData>& ptr = *find; 42446229577SJames Feist 42546229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task"; 42646229577SJames Feist asyncResp->res.jsonValue["Id"] = strParam; 42746229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task " + strParam; 42846229577SJames Feist asyncResp->res.jsonValue["TaskState"] = ptr->state; 42946229577SJames Feist asyncResp->res.jsonValue["StartTime"] = 4302b82937eSEd Tanous redfish::time_utils::getDateTimeStdtime(ptr->startTime); 43146229577SJames Feist if (ptr->endTime) 43246229577SJames Feist { 43346229577SJames Feist asyncResp->res.jsonValue["EndTime"] = 434bd79bce8SPatrick Williams redfish::time_utils::getDateTimeStdtime( 435bd79bce8SPatrick Williams *(ptr->endTime)); 43646229577SJames Feist } 43746229577SJames Feist asyncResp->res.jsonValue["TaskStatus"] = ptr->status; 43846229577SJames Feist asyncResp->res.jsonValue["Messages"] = ptr->messages; 439bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 440bd79bce8SPatrick Williams "/redfish/v1/TaskService/Tasks/{}", strParam); 44146229577SJames Feist if (!ptr->gave204) 44246229577SJames Feist { 443bd79bce8SPatrick Williams asyncResp->res.jsonValue["TaskMonitor"] = 444bd79bce8SPatrick Williams boost::urls::format( 445bd79bce8SPatrick Williams "/redfish/v1/TaskService/TaskMonitors/{}", 446bd79bce8SPatrick Williams strParam); 44746229577SJames Feist } 4485db7dfd6SArun Thomas Baby 4495db7dfd6SArun Thomas Baby asyncResp->res.jsonValue["HidePayload"] = !ptr->payload; 4505db7dfd6SArun Thomas Baby 451fe306728SJames Feist if (ptr->payload) 452fe306728SJames Feist { 4535fb91ba4SEd Tanous const task::Payload& p = *(ptr->payload); 454bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["TargetUri"] = 455bd79bce8SPatrick Williams p.targetUri; 4561476687dSEd Tanous asyncResp->res.jsonValue["Payload"]["HttpOperation"] = 4571476687dSEd Tanous p.httpOperation; 458bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = 459bd79bce8SPatrick Williams p.httpHeaders; 460bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["JsonBody"] = 461bd79bce8SPatrick Williams p.jsonBody.dump( 462bd79bce8SPatrick Williams -1, ' ', true, 463bd79bce8SPatrick Williams nlohmann::json::error_handler_t::replace); 464fe306728SJames Feist } 465bd79bce8SPatrick Williams asyncResp->res.jsonValue["PercentComplete"] = 466bd79bce8SPatrick Williams ptr->percentComplete; 4677e860f15SJohn Edward Broadbent }); 46846229577SJames Feist } 46946229577SJames Feist 4707e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app) 47146229577SJames Feist { 4727e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/") 473ed398213SEd Tanous .privileges(redfish::privileges::getTaskCollection) 4747e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 47545ca1b86SEd Tanous [&app](const crow::Request& req, 4767e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 4773ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 47845ca1b86SEd Tanous { 47945ca1b86SEd Tanous return; 48045ca1b86SEd Tanous } 48146229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = 48246229577SJames Feist "#TaskCollection.TaskCollection"; 483bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = 484bd79bce8SPatrick Williams "/redfish/v1/TaskService/Tasks"; 48546229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task Collection"; 486bd79bce8SPatrick Williams asyncResp->res.jsonValue["Members@odata.count"] = 487bd79bce8SPatrick Williams task::tasks.size(); 48846229577SJames Feist nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 48946229577SJames Feist members = nlohmann::json::array(); 49046229577SJames Feist 49146229577SJames Feist for (const std::shared_ptr<task::TaskData>& task : task::tasks) 49246229577SJames Feist { 49346229577SJames Feist if (task == nullptr) 49446229577SJames Feist { 49546229577SJames Feist continue; // shouldn't be possible 49646229577SJames Feist } 497613dabeaSEd Tanous nlohmann::json::object_t member; 498ef4c65b7SEd Tanous member["@odata.id"] = 499ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/TaskService/Tasks/{}", 500eddfc437SWilly Tu std::to_string(task->index)); 501613dabeaSEd Tanous members.emplace_back(std::move(member)); 50246229577SJames Feist } 5037e860f15SJohn Edward Broadbent }); 50446229577SJames Feist } 50546229577SJames Feist 5067e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app) 50746229577SJames Feist { 5087e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/") 509ed398213SEd Tanous .privileges(redfish::privileges::getTaskService) 5107e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 51145ca1b86SEd Tanous [&app](const crow::Request& req, 5127e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 5133ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 51445ca1b86SEd Tanous { 51545ca1b86SEd Tanous return; 51645ca1b86SEd Tanous } 51746229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = 51846229577SJames Feist "#TaskService.v1_1_4.TaskService"; 519bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = 520bd79bce8SPatrick Williams "/redfish/v1/TaskService"; 52146229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task Service"; 52246229577SJames Feist asyncResp->res.jsonValue["Id"] = "TaskService"; 5237e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["DateTime"] = 5242b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 525539d8c6bSEd Tanous asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = 526539d8c6bSEd Tanous task_service::OverWritePolicy::Oldest; 52746229577SJames Feist 528bd79bce8SPatrick Williams asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = 529bd79bce8SPatrick Williams true; 53046229577SJames Feist 531bd79bce8SPatrick Williams asyncResp->res.jsonValue["Status"]["State"] = 532bd79bce8SPatrick Williams resource::State::Enabled; 53346229577SJames Feist asyncResp->res.jsonValue["ServiceEnabled"] = true; 5341476687dSEd Tanous asyncResp->res.jsonValue["Tasks"]["@odata.id"] = 5351476687dSEd Tanous "/redfish/v1/TaskService/Tasks"; 5367e860f15SJohn Edward Broadbent }); 53746229577SJames Feist } 53846229577SJames Feist 53946229577SJames Feist } // namespace redfish 540