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 { 1419f03894eSRohit PAI const auto last = getTaskToRemove(); 14246229577SJames Feist 14346229577SJames Feist // destroy all references 1449f03894eSRohit PAI (*last)->timer.cancel(); 1459f03894eSRohit PAI (*last)->match.reset(); 1469f03894eSRohit 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 1539f03894eSRohit PAI /** 1549f03894eSRohit PAI * @brief Get the first completed/aborted task or oldest running task to 1559f03894eSRohit PAI * remove 1569f03894eSRohit PAI */ 1579f03894eSRohit PAI static std::deque<std::shared_ptr<TaskData>>::iterator getTaskToRemove() 1589f03894eSRohit PAI { 1599f03894eSRohit PAI static constexpr std::array<std::string_view, 5> activeStates = { 1609f03894eSRohit PAI "Running", "Pending", "Starting", "Suspended", "Interrupted"}; 1619f03894eSRohit PAI 1629f03894eSRohit PAI auto it = 1639f03894eSRohit PAI std::find_if(tasks.begin(), tasks.end(), [](const auto& task) { 1649f03894eSRohit PAI return std::ranges::find(activeStates, task->state) == 1659f03894eSRohit PAI activeStates.end(); 1669f03894eSRohit PAI }); 1679f03894eSRohit PAI 1689f03894eSRohit PAI return (it != tasks.end()) ? it : tasks.begin(); 1699f03894eSRohit PAI } 1709f03894eSRohit 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)); 193*29e2bdd7SChinmay Shripad Hegde res.jsonValue["Name"] = "Task " + strIdx; 194*29e2bdd7SChinmay Shripad Hegde res.jsonValue["StartTime"] = 195*29e2bdd7SChinmay Shripad Hegde redfish::time_utils::getDateTimeStdtime(startTime); 196*29e2bdd7SChinmay Shripad Hegde res.jsonValue["Messages"] = messages; 197*29e2bdd7SChinmay Shripad Hegde res.jsonValue["TaskMonitor"] = taskMonitor; 198*29e2bdd7SChinmay Shripad Hegde res.jsonValue["HidePayload"] = !payload; 199*29e2bdd7SChinmay Shripad Hegde if (payload) 200*29e2bdd7SChinmay Shripad Hegde { 201*29e2bdd7SChinmay Shripad Hegde const task::Payload& p = *payload; 202*29e2bdd7SChinmay Shripad Hegde nlohmann::json::object_t payloadObj; 203*29e2bdd7SChinmay Shripad Hegde payloadObj["TargetUri"] = p.targetUri; 204*29e2bdd7SChinmay Shripad Hegde payloadObj["HttpOperation"] = p.httpOperation; 205*29e2bdd7SChinmay Shripad Hegde payloadObj["HttpHeaders"] = p.httpHeaders; 206*29e2bdd7SChinmay Shripad Hegde if (p.jsonBody.is_object()) 207*29e2bdd7SChinmay Shripad Hegde { 208*29e2bdd7SChinmay Shripad Hegde payloadObj["JsonBody"] = p.jsonBody.dump( 209*29e2bdd7SChinmay Shripad Hegde 2, ' ', true, nlohmann::json::error_handler_t::replace); 210*29e2bdd7SChinmay Shripad Hegde } 211*29e2bdd7SChinmay Shripad Hegde res.jsonValue["Payload"] = std::move(payloadObj); 212*29e2bdd7SChinmay Shripad Hegde } 213*29e2bdd7SChinmay Shripad Hegde res.jsonValue["PercentComplete"] = percentComplete; 21446229577SJames Feist } 21546229577SJames Feist else if (!gave204) 21646229577SJames Feist { 21746229577SJames Feist res.result(boost::beast::http::status::no_content); 21846229577SJames Feist gave204 = true; 21946229577SJames Feist } 22046229577SJames Feist } 22146229577SJames Feist 222d609fd6eSEd Tanous void finishTask() 22346229577SJames Feist { 22446229577SJames Feist endTime = std::chrono::system_clock::to_time_t( 22546229577SJames Feist std::chrono::system_clock::now()); 22646229577SJames Feist } 22746229577SJames Feist 228fd9ab9e1SJames Feist void extendTimer(const std::chrono::seconds& timeout) 22946229577SJames Feist { 23046229577SJames Feist timer.expires_after(timeout); 23146229577SJames Feist timer.async_wait( 23246229577SJames Feist [self = shared_from_this()](boost::system::error_code ec) { 23346229577SJames Feist if (ec == boost::asio::error::operation_aborted) 23446229577SJames Feist { 2354e0453b1SGunnar Mills return; // completed successfully 23646229577SJames Feist } 23746229577SJames Feist if (!ec) 23846229577SJames Feist { 23946229577SJames Feist // change ec to error as timer expired 24046229577SJames Feist ec = boost::asio::error::operation_aborted; 24146229577SJames Feist } 24246229577SJames Feist self->match.reset(); 24359d494eeSPatrick Williams sdbusplus::message_t msg; 24446229577SJames Feist self->finishTask(); 24546229577SJames Feist self->state = "Cancelled"; 24646229577SJames Feist self->status = "Warning"; 247e5d5006bSJames Feist self->messages.emplace_back( 248e5d5006bSJames Feist messages::taskAborted(std::to_string(self->index))); 249e7686576SSunitha Harish // Send event :TaskAborted 250daadfb2eSEd Tanous sendTaskEvent(self->state, self->index); 25146229577SJames Feist self->callback(ec, msg, self); 25246229577SJames Feist }); 253fd9ab9e1SJames Feist } 254fd9ab9e1SJames Feist 25526ccae32SEd Tanous static void sendTaskEvent(std::string_view state, size_t index) 256e7686576SSunitha Harish { 257e7686576SSunitha Harish // TaskState enums which should send out an event are: 258e7686576SSunitha Harish // "Starting" = taskResumed 259e7686576SSunitha Harish // "Running" = taskStarted 260e7686576SSunitha Harish // "Suspended" = taskPaused 261e7686576SSunitha Harish // "Interrupted" = taskPaused 262e7686576SSunitha Harish // "Pending" = taskPaused 263e7686576SSunitha Harish // "Stopping" = taskAborted 264e7686576SSunitha Harish // "Completed" = taskCompletedOK 265e7686576SSunitha Harish // "Killed" = taskRemoved 266e7686576SSunitha Harish // "Exception" = taskCompletedWarning 267e7686576SSunitha Harish // "Cancelled" = taskCancelled 26882b286fbSEd Tanous nlohmann::json::object_t event; 269f8fe2211SEd Tanous std::string indexStr = std::to_string(index); 270e7686576SSunitha Harish if (state == "Starting") 271e7686576SSunitha Harish { 272f8fe2211SEd Tanous event = redfish::messages::taskResumed(indexStr); 273e7686576SSunitha Harish } 274e7686576SSunitha Harish else if (state == "Running") 275e7686576SSunitha Harish { 276f8fe2211SEd Tanous event = redfish::messages::taskStarted(indexStr); 277e7686576SSunitha Harish } 278e7686576SSunitha Harish else if ((state == "Suspended") || (state == "Interrupted") || 279e7686576SSunitha Harish (state == "Pending")) 280e7686576SSunitha Harish { 281f8fe2211SEd Tanous event = redfish::messages::taskPaused(indexStr); 282e7686576SSunitha Harish } 283e7686576SSunitha Harish else if (state == "Stopping") 284e7686576SSunitha Harish { 285f8fe2211SEd Tanous event = redfish::messages::taskAborted(indexStr); 286e7686576SSunitha Harish } 287e7686576SSunitha Harish else if (state == "Completed") 288e7686576SSunitha Harish { 289f8fe2211SEd Tanous event = redfish::messages::taskCompletedOK(indexStr); 290e7686576SSunitha Harish } 291e7686576SSunitha Harish else if (state == "Killed") 292e7686576SSunitha Harish { 293f8fe2211SEd Tanous event = redfish::messages::taskRemoved(indexStr); 294e7686576SSunitha Harish } 295e7686576SSunitha Harish else if (state == "Exception") 296e7686576SSunitha Harish { 297f8fe2211SEd Tanous event = redfish::messages::taskCompletedWarning(indexStr); 298e7686576SSunitha Harish } 299e7686576SSunitha Harish else if (state == "Cancelled") 300e7686576SSunitha Harish { 301f8fe2211SEd Tanous event = redfish::messages::taskCancelled(indexStr); 302e7686576SSunitha Harish } 303e7686576SSunitha Harish else 304e7686576SSunitha Harish { 30562598e31SEd Tanous BMCWEB_LOG_INFO("sendTaskEvent: No events to send"); 306f8fe2211SEd Tanous return; 307e7686576SSunitha Harish } 308f8fe2211SEd Tanous boost::urls::url origin = 309f8fe2211SEd Tanous boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index); 310f8fe2211SEd Tanous EventServiceManager::getInstance().sendEvent(event, origin.buffer(), 311f8fe2211SEd Tanous "Task"); 312e7686576SSunitha Harish } 313e7686576SSunitha Harish 314fd9ab9e1SJames Feist void startTimer(const std::chrono::seconds& timeout) 315fd9ab9e1SJames Feist { 316fd9ab9e1SJames Feist if (match) 317fd9ab9e1SJames Feist { 318fd9ab9e1SJames Feist return; 319fd9ab9e1SJames Feist } 32059d494eeSPatrick Williams match = std::make_unique<sdbusplus::bus::match_t>( 32159d494eeSPatrick Williams static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus), 322fd9ab9e1SJames Feist matchStr, 32359d494eeSPatrick Williams [self = shared_from_this()](sdbusplus::message_t& message) { 324fd9ab9e1SJames Feist boost::system::error_code ec; 325fd9ab9e1SJames Feist 326fd9ab9e1SJames Feist // callback to return True if callback is done, callback needs 327fd9ab9e1SJames Feist // to update status itself if needed 328fd9ab9e1SJames Feist if (self->callback(ec, message, self) == task::completed) 329fd9ab9e1SJames Feist { 330fd9ab9e1SJames Feist self->timer.cancel(); 331fd9ab9e1SJames Feist self->finishTask(); 332fd9ab9e1SJames Feist 333e7686576SSunitha Harish // Send event 334daadfb2eSEd Tanous sendTaskEvent(self->state, self->index); 335e7686576SSunitha Harish 336fd9ab9e1SJames Feist // reset the match after the callback was successful 337fd9ab9e1SJames Feist boost::asio::post( 338fd9ab9e1SJames Feist crow::connections::systemBus->get_io_context(), 339fd9ab9e1SJames Feist [self] { self->match.reset(); }); 340fd9ab9e1SJames Feist return; 341fd9ab9e1SJames Feist } 342fd9ab9e1SJames Feist }); 343fd9ab9e1SJames Feist 344fd9ab9e1SJames Feist extendTimer(timeout); 345e5d5006bSJames Feist messages.emplace_back(messages::taskStarted(std::to_string(index))); 346e7686576SSunitha Harish // Send event : TaskStarted 347e7686576SSunitha Harish sendTaskEvent(state, index); 34846229577SJames Feist } 34946229577SJames Feist 35059d494eeSPatrick Williams std::function<bool(boost::system::error_code, sdbusplus::message_t&, 35146229577SJames Feist const std::shared_ptr<TaskData>&)> 35246229577SJames Feist callback; 35346229577SJames Feist std::string matchStr; 35446229577SJames Feist size_t index; 35546229577SJames Feist time_t startTime; 35646229577SJames Feist std::string status; 35746229577SJames Feist std::string state; 35846229577SJames Feist nlohmann::json messages; 35946229577SJames Feist boost::asio::steady_timer timer; 36059d494eeSPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> match; 36146229577SJames Feist std::optional<time_t> endTime; 362fe306728SJames Feist std::optional<Payload> payload; 36346229577SJames Feist bool gave204 = false; 3646868ff50SGeorge Liu int percentComplete = 0; 36546229577SJames Feist }; 36646229577SJames Feist 36746229577SJames Feist } // namespace task 36846229577SJames Feist 3697e860f15SJohn Edward Broadbent inline void requestRoutesTaskMonitor(App& app) 37046229577SJames Feist { 371fdbce79bSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/") 372ed398213SEd Tanous .privileges(redfish::privileges::getTask) 3737e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 37445ca1b86SEd Tanous [&app](const crow::Request& req, 3757e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3767e860f15SJohn Edward Broadbent const std::string& strParam) { 3773ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 37845ca1b86SEd Tanous { 37945ca1b86SEd Tanous return; 38045ca1b86SEd Tanous } 3813544d2a7SEd Tanous auto find = std::ranges::find_if( 3823544d2a7SEd Tanous task::tasks, 38346229577SJames Feist [&strParam](const std::shared_ptr<task::TaskData>& task) { 38446229577SJames Feist if (!task) 38546229577SJames Feist { 38646229577SJames Feist return false; 38746229577SJames Feist } 38846229577SJames Feist 3897e860f15SJohn Edward Broadbent // we compare against the string version as on failure 3907e860f15SJohn Edward Broadbent // strtoul returns 0 39146229577SJames Feist return std::to_string(task->index) == strParam; 39246229577SJames Feist }); 39346229577SJames Feist 39446229577SJames Feist if (find == task::tasks.end()) 39546229577SJames Feist { 396bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 397bd79bce8SPatrick Williams strParam); 39846229577SJames Feist return; 39946229577SJames Feist } 40046229577SJames Feist std::shared_ptr<task::TaskData>& ptr = *find; 40146229577SJames Feist // monitor expires after 204 40246229577SJames Feist if (ptr->gave204) 40346229577SJames Feist { 404bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 405bd79bce8SPatrick Williams strParam); 40646229577SJames Feist return; 40746229577SJames Feist } 40846229577SJames Feist ptr->populateResp(asyncResp->res); 4097e860f15SJohn Edward Broadbent }); 41046229577SJames Feist } 41146229577SJames Feist 4127e860f15SJohn Edward Broadbent inline void requestRoutesTask(App& app) 41346229577SJames Feist { 4147e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/") 415ed398213SEd Tanous .privileges(redfish::privileges::getTask) 4167e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 41745ca1b86SEd Tanous [&app](const crow::Request& req, 4187e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4197e860f15SJohn Edward Broadbent const std::string& strParam) { 4203ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 42145ca1b86SEd Tanous { 42245ca1b86SEd Tanous return; 42345ca1b86SEd Tanous } 4243544d2a7SEd Tanous auto find = std::ranges::find_if( 4253544d2a7SEd Tanous task::tasks, 42646229577SJames Feist [&strParam](const std::shared_ptr<task::TaskData>& task) { 42746229577SJames Feist if (!task) 42846229577SJames Feist { 42946229577SJames Feist return false; 43046229577SJames Feist } 43146229577SJames Feist 4327e860f15SJohn Edward Broadbent // we compare against the string version as on failure 4337e860f15SJohn Edward Broadbent // strtoul returns 0 43446229577SJames Feist return std::to_string(task->index) == strParam; 43546229577SJames Feist }); 43646229577SJames Feist 43746229577SJames Feist if (find == task::tasks.end()) 43846229577SJames Feist { 439bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Task", 440bd79bce8SPatrick Williams strParam); 44146229577SJames Feist return; 44246229577SJames Feist } 44346229577SJames Feist 44402cad96eSEd Tanous const std::shared_ptr<task::TaskData>& ptr = *find; 44546229577SJames Feist 44646229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task"; 44746229577SJames Feist asyncResp->res.jsonValue["Id"] = strParam; 44846229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task " + strParam; 44946229577SJames Feist asyncResp->res.jsonValue["TaskState"] = ptr->state; 45046229577SJames Feist asyncResp->res.jsonValue["StartTime"] = 4512b82937eSEd Tanous redfish::time_utils::getDateTimeStdtime(ptr->startTime); 45246229577SJames Feist if (ptr->endTime) 45346229577SJames Feist { 45446229577SJames Feist asyncResp->res.jsonValue["EndTime"] = 455bd79bce8SPatrick Williams redfish::time_utils::getDateTimeStdtime( 456bd79bce8SPatrick Williams *(ptr->endTime)); 45746229577SJames Feist } 45846229577SJames Feist asyncResp->res.jsonValue["TaskStatus"] = ptr->status; 45946229577SJames Feist asyncResp->res.jsonValue["Messages"] = ptr->messages; 460bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 461bd79bce8SPatrick Williams "/redfish/v1/TaskService/Tasks/{}", strParam); 46246229577SJames Feist if (!ptr->gave204) 46346229577SJames Feist { 464bd79bce8SPatrick Williams asyncResp->res.jsonValue["TaskMonitor"] = 465bd79bce8SPatrick Williams boost::urls::format( 466bd79bce8SPatrick Williams "/redfish/v1/TaskService/TaskMonitors/{}", 467bd79bce8SPatrick Williams strParam); 46846229577SJames Feist } 4695db7dfd6SArun Thomas Baby 4705db7dfd6SArun Thomas Baby asyncResp->res.jsonValue["HidePayload"] = !ptr->payload; 4715db7dfd6SArun Thomas Baby 472fe306728SJames Feist if (ptr->payload) 473fe306728SJames Feist { 4745fb91ba4SEd Tanous const task::Payload& p = *(ptr->payload); 475bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["TargetUri"] = 476bd79bce8SPatrick Williams p.targetUri; 4771476687dSEd Tanous asyncResp->res.jsonValue["Payload"]["HttpOperation"] = 4781476687dSEd Tanous p.httpOperation; 479bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = 480bd79bce8SPatrick Williams p.httpHeaders; 481bd79bce8SPatrick Williams asyncResp->res.jsonValue["Payload"]["JsonBody"] = 482bd79bce8SPatrick Williams p.jsonBody.dump( 483bd79bce8SPatrick Williams -1, ' ', true, 484bd79bce8SPatrick Williams nlohmann::json::error_handler_t::replace); 485fe306728SJames Feist } 486bd79bce8SPatrick Williams asyncResp->res.jsonValue["PercentComplete"] = 487bd79bce8SPatrick Williams ptr->percentComplete; 4887e860f15SJohn Edward Broadbent }); 48946229577SJames Feist } 49046229577SJames Feist 4917e860f15SJohn Edward Broadbent inline void requestRoutesTaskCollection(App& app) 49246229577SJames Feist { 4937e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/") 494ed398213SEd Tanous .privileges(redfish::privileges::getTaskCollection) 4957e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 49645ca1b86SEd Tanous [&app](const crow::Request& req, 4977e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 4983ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 49945ca1b86SEd Tanous { 50045ca1b86SEd Tanous return; 50145ca1b86SEd Tanous } 50246229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = 50346229577SJames Feist "#TaskCollection.TaskCollection"; 504bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = 505bd79bce8SPatrick Williams "/redfish/v1/TaskService/Tasks"; 50646229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task Collection"; 507bd79bce8SPatrick Williams asyncResp->res.jsonValue["Members@odata.count"] = 508bd79bce8SPatrick Williams task::tasks.size(); 50946229577SJames Feist nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 51046229577SJames Feist members = nlohmann::json::array(); 51146229577SJames Feist 51246229577SJames Feist for (const std::shared_ptr<task::TaskData>& task : task::tasks) 51346229577SJames Feist { 51446229577SJames Feist if (task == nullptr) 51546229577SJames Feist { 51646229577SJames Feist continue; // shouldn't be possible 51746229577SJames Feist } 518613dabeaSEd Tanous nlohmann::json::object_t member; 519ef4c65b7SEd Tanous member["@odata.id"] = 520ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/TaskService/Tasks/{}", 521eddfc437SWilly Tu std::to_string(task->index)); 522613dabeaSEd Tanous members.emplace_back(std::move(member)); 52346229577SJames Feist } 5247e860f15SJohn Edward Broadbent }); 52546229577SJames Feist } 52646229577SJames Feist 5277e860f15SJohn Edward Broadbent inline void requestRoutesTaskService(App& app) 52846229577SJames Feist { 5297e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/TaskService/") 530ed398213SEd Tanous .privileges(redfish::privileges::getTaskService) 5317e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 53245ca1b86SEd Tanous [&app](const crow::Request& req, 5337e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 5343ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 53545ca1b86SEd Tanous { 53645ca1b86SEd Tanous return; 53745ca1b86SEd Tanous } 53846229577SJames Feist asyncResp->res.jsonValue["@odata.type"] = 53946229577SJames Feist "#TaskService.v1_1_4.TaskService"; 540bd79bce8SPatrick Williams asyncResp->res.jsonValue["@odata.id"] = 541bd79bce8SPatrick Williams "/redfish/v1/TaskService"; 54246229577SJames Feist asyncResp->res.jsonValue["Name"] = "Task Service"; 54346229577SJames Feist asyncResp->res.jsonValue["Id"] = "TaskService"; 5447e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["DateTime"] = 5452b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 546539d8c6bSEd Tanous asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = 547539d8c6bSEd Tanous task_service::OverWritePolicy::Oldest; 54846229577SJames Feist 549bd79bce8SPatrick Williams asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = 550bd79bce8SPatrick Williams true; 55146229577SJames Feist 552bd79bce8SPatrick Williams asyncResp->res.jsonValue["Status"]["State"] = 553bd79bce8SPatrick Williams resource::State::Enabled; 55446229577SJames Feist asyncResp->res.jsonValue["ServiceEnabled"] = true; 5551476687dSEd Tanous asyncResp->res.jsonValue["Tasks"]["@odata.id"] = 5561476687dSEd Tanous "/redfish/v1/TaskService/Tasks"; 5577e860f15SJohn Edward Broadbent }); 55846229577SJames Feist } 55946229577SJames Feist 56046229577SJames Feist } // namespace redfish 561