James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 1 | /* |
Ed Tanous | 6be832e | 2024-09-10 11:44:48 -0700 | [diff] [blame] | 2 | Copyright (c) 2020 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 15 | */ |
| 16 | #pragma once |
| 17 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "app.hpp" |
| 19 | #include "dbus_utility.hpp" |
| 20 | #include "event_service_manager.hpp" |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame] | 21 | #include "generated/enums/resource.hpp" |
| 22 | #include "generated/enums/task_service.hpp" |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 23 | #include "http/parsing.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 24 | #include "query.hpp" |
| 25 | #include "registries/privilege_registry.hpp" |
| 26 | #include "task_messages.hpp" |
| 27 | |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 28 | #include <boost/asio/post.hpp> |
| 29 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 30 | #include <boost/url/format.hpp> |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 31 | #include <sdbusplus/bus/match.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 32 | |
| 33 | #include <chrono> |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 34 | #include <memory> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 35 | #include <ranges> |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 36 | #include <variant> |
| 37 | |
| 38 | namespace redfish |
| 39 | { |
| 40 | |
| 41 | namespace task |
| 42 | { |
| 43 | constexpr size_t maxTaskCount = 100; // arbitrary limit |
| 44 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 45 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 46 | static std::deque<std::shared_ptr<struct TaskData>> tasks; |
| 47 | |
James Feist | 32898ce | 2020-03-10 16:16:52 -0700 | [diff] [blame] | 48 | constexpr bool completed = true; |
| 49 | |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 50 | struct Payload |
| 51 | { |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 52 | explicit Payload(const crow::Request& req) : |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 53 | targetUri(req.url().encoded_path()), httpOperation(req.methodString()), |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 54 | httpHeaders(nlohmann::json::array()) |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 55 | { |
| 56 | using field_ns = boost::beast::http::field; |
| 57 | constexpr const std::array<boost::beast::http::field, 7> |
| 58 | headerWhitelist = {field_ns::accept, field_ns::accept_encoding, |
| 59 | field_ns::user_agent, field_ns::host, |
| 60 | field_ns::connection, field_ns::content_length, |
| 61 | field_ns::upgrade}; |
| 62 | |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 63 | JsonParseResult ret = parseRequestAsJson(req, jsonBody); |
| 64 | if (ret != JsonParseResult::Success) |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 65 | { |
Ed Tanous | 1aa0c2b | 2022-02-08 12:24:30 +0100 | [diff] [blame] | 66 | return; |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ed Tanous | 98fe740 | 2023-02-14 14:50:33 -0800 | [diff] [blame] | 69 | for (const auto& field : req.fields()) |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 70 | { |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 71 | if (std::ranges::find(headerWhitelist, field.name()) == |
| 72 | headerWhitelist.end()) |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | std::string header; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 77 | header.reserve( |
| 78 | field.name_string().size() + 2 + field.value().size()); |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 79 | header += field.name_string(); |
| 80 | header += ": "; |
| 81 | header += field.value(); |
| 82 | httpHeaders.emplace_back(std::move(header)); |
| 83 | } |
| 84 | } |
| 85 | Payload() = delete; |
| 86 | |
| 87 | std::string targetUri; |
| 88 | std::string httpOperation; |
| 89 | nlohmann::json httpHeaders; |
| 90 | nlohmann::json jsonBody; |
| 91 | }; |
| 92 | |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 93 | struct TaskData : std::enable_shared_from_this<TaskData> |
| 94 | { |
| 95 | private: |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 96 | TaskData( |
| 97 | std::function<bool(boost::system::error_code, sdbusplus::message_t&, |
| 98 | const std::shared_ptr<TaskData>&)>&& handler, |
| 99 | const std::string& matchIn, size_t idx) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 100 | callback(std::move(handler)), matchStr(matchIn), index(idx), |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 101 | startTime(std::chrono::system_clock::to_time_t( |
| 102 | std::chrono::system_clock::now())), |
| 103 | status("OK"), state("Running"), messages(nlohmann::json::array()), |
| 104 | timer(crow::connections::systemBus->get_io_context()) |
| 105 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 106 | {} |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 107 | |
| 108 | public: |
Ed Tanous | d609fd6 | 2020-09-28 19:08:03 -0700 | [diff] [blame] | 109 | TaskData() = delete; |
| 110 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 111 | static std::shared_ptr<TaskData>& createTask( |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 112 | std::function<bool(boost::system::error_code, sdbusplus::message_t&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 113 | const std::shared_ptr<TaskData>&)>&& handler, |
| 114 | const std::string& match) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 115 | { |
| 116 | static size_t lastTask = 0; |
| 117 | struct MakeSharedHelper : public TaskData |
| 118 | { |
| 119 | MakeSharedHelper( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 120 | std::function<bool(boost::system::error_code, |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 121 | sdbusplus::message_t&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 122 | const std::shared_ptr<TaskData>&)>&& handler, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 123 | const std::string& match2, size_t idx) : |
| 124 | TaskData(std::move(handler), match2, idx) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 125 | {} |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | if (tasks.size() >= maxTaskCount) |
| 129 | { |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 130 | const auto& last = tasks.front(); |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 131 | |
| 132 | // destroy all references |
| 133 | last->timer.cancel(); |
| 134 | last->match.reset(); |
| 135 | tasks.pop_front(); |
| 136 | } |
| 137 | |
| 138 | return tasks.emplace_back(std::make_shared<MakeSharedHelper>( |
| 139 | std::move(handler), match, lastTask++)); |
| 140 | } |
| 141 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 142 | void populateResp(crow::Response& res, size_t retryAfterSeconds = 30) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 143 | { |
| 144 | if (!endTime) |
| 145 | { |
| 146 | res.result(boost::beast::http::status::accepted); |
| 147 | std::string strIdx = std::to_string(index); |
Ed Tanous | fdbce79 | 2024-06-26 14:48:46 -0700 | [diff] [blame] | 148 | boost::urls::url uri = |
| 149 | boost::urls::format("/redfish/v1/TaskService/Tasks/{}", strIdx); |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 150 | |
| 151 | res.jsonValue["@odata.id"] = uri; |
| 152 | res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task"; |
| 153 | res.jsonValue["Id"] = strIdx; |
| 154 | res.jsonValue["TaskState"] = state; |
| 155 | res.jsonValue["TaskStatus"] = status; |
| 156 | |
Ed Tanous | fdbce79 | 2024-06-26 14:48:46 -0700 | [diff] [blame] | 157 | boost::urls::url taskMonitor = boost::urls::format( |
| 158 | "/redfish/v1/TaskService/TaskMonitors/{}", strIdx); |
| 159 | |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 160 | res.addHeader(boost::beast::http::field::location, |
Ed Tanous | fdbce79 | 2024-06-26 14:48:46 -0700 | [diff] [blame] | 161 | taskMonitor.buffer()); |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 162 | res.addHeader(boost::beast::http::field::retry_after, |
| 163 | std::to_string(retryAfterSeconds)); |
| 164 | } |
| 165 | else if (!gave204) |
| 166 | { |
| 167 | res.result(boost::beast::http::status::no_content); |
| 168 | gave204 = true; |
| 169 | } |
| 170 | } |
| 171 | |
Ed Tanous | d609fd6 | 2020-09-28 19:08:03 -0700 | [diff] [blame] | 172 | void finishTask() |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 173 | { |
| 174 | endTime = std::chrono::system_clock::to_time_t( |
| 175 | std::chrono::system_clock::now()); |
| 176 | } |
| 177 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 178 | void extendTimer(const std::chrono::seconds& timeout) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 179 | { |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 180 | timer.expires_after(timeout); |
| 181 | timer.async_wait( |
| 182 | [self = shared_from_this()](boost::system::error_code ec) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 183 | if (ec == boost::asio::error::operation_aborted) |
| 184 | { |
| 185 | return; // completed successfully |
| 186 | } |
| 187 | if (!ec) |
| 188 | { |
| 189 | // change ec to error as timer expired |
| 190 | ec = boost::asio::error::operation_aborted; |
| 191 | } |
| 192 | self->match.reset(); |
| 193 | sdbusplus::message_t msg; |
| 194 | self->finishTask(); |
| 195 | self->state = "Cancelled"; |
| 196 | self->status = "Warning"; |
| 197 | self->messages.emplace_back( |
| 198 | messages::taskAborted(std::to_string(self->index))); |
| 199 | // Send event :TaskAborted |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame^] | 200 | sendTaskEvent(self->state, self->index); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 201 | self->callback(ec, msg, self); |
| 202 | }); |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 205 | static void sendTaskEvent(std::string_view state, size_t index) |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 206 | { |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 207 | // TaskState enums which should send out an event are: |
| 208 | // "Starting" = taskResumed |
| 209 | // "Running" = taskStarted |
| 210 | // "Suspended" = taskPaused |
| 211 | // "Interrupted" = taskPaused |
| 212 | // "Pending" = taskPaused |
| 213 | // "Stopping" = taskAborted |
| 214 | // "Completed" = taskCompletedOK |
| 215 | // "Killed" = taskRemoved |
| 216 | // "Exception" = taskCompletedWarning |
| 217 | // "Cancelled" = taskCancelled |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 218 | nlohmann::json event; |
| 219 | std::string indexStr = std::to_string(index); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 220 | if (state == "Starting") |
| 221 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 222 | event = redfish::messages::taskResumed(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 223 | } |
| 224 | else if (state == "Running") |
| 225 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 226 | event = redfish::messages::taskStarted(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 227 | } |
| 228 | else if ((state == "Suspended") || (state == "Interrupted") || |
| 229 | (state == "Pending")) |
| 230 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 231 | event = redfish::messages::taskPaused(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 232 | } |
| 233 | else if (state == "Stopping") |
| 234 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 235 | event = redfish::messages::taskAborted(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 236 | } |
| 237 | else if (state == "Completed") |
| 238 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 239 | event = redfish::messages::taskCompletedOK(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 240 | } |
| 241 | else if (state == "Killed") |
| 242 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 243 | event = redfish::messages::taskRemoved(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 244 | } |
| 245 | else if (state == "Exception") |
| 246 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 247 | event = redfish::messages::taskCompletedWarning(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 248 | } |
| 249 | else if (state == "Cancelled") |
| 250 | { |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 251 | event = redfish::messages::taskCancelled(indexStr); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 255 | BMCWEB_LOG_INFO("sendTaskEvent: No events to send"); |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 256 | return; |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 257 | } |
Ed Tanous | f8fe221 | 2024-06-16 14:51:23 -0700 | [diff] [blame] | 258 | boost::urls::url origin = |
| 259 | boost::urls::format("/redfish/v1/TaskService/Tasks/{}", index); |
| 260 | EventServiceManager::getInstance().sendEvent(event, origin.buffer(), |
| 261 | "Task"); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 264 | void startTimer(const std::chrono::seconds& timeout) |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 265 | { |
| 266 | if (match) |
| 267 | { |
| 268 | return; |
| 269 | } |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 270 | match = std::make_unique<sdbusplus::bus::match_t>( |
| 271 | static_cast<sdbusplus::bus_t&>(*crow::connections::systemBus), |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 272 | matchStr, |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 273 | [self = shared_from_this()](sdbusplus::message_t& message) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 274 | boost::system::error_code ec; |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 275 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 276 | // callback to return True if callback is done, callback needs |
| 277 | // to update status itself if needed |
| 278 | if (self->callback(ec, message, self) == task::completed) |
| 279 | { |
| 280 | self->timer.cancel(); |
| 281 | self->finishTask(); |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 282 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 283 | // Send event |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame^] | 284 | sendTaskEvent(self->state, self->index); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 285 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 286 | // reset the match after the callback was successful |
| 287 | boost::asio::post( |
| 288 | crow::connections::systemBus->get_io_context(), |
| 289 | [self] { self->match.reset(); }); |
| 290 | return; |
| 291 | } |
| 292 | }); |
James Feist | fd9ab9e | 2020-05-19 13:48:07 -0700 | [diff] [blame] | 293 | |
| 294 | extendTimer(timeout); |
James Feist | e5d5006 | 2020-05-11 17:29:00 -0700 | [diff] [blame] | 295 | messages.emplace_back(messages::taskStarted(std::to_string(index))); |
Sunitha Harish | e768657 | 2020-07-15 02:32:44 -0500 | [diff] [blame] | 296 | // Send event : TaskStarted |
| 297 | sendTaskEvent(state, index); |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 300 | std::function<bool(boost::system::error_code, sdbusplus::message_t&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 301 | const std::shared_ptr<TaskData>&)> |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 302 | callback; |
| 303 | std::string matchStr; |
| 304 | size_t index; |
| 305 | time_t startTime; |
| 306 | std::string status; |
| 307 | std::string state; |
| 308 | nlohmann::json messages; |
| 309 | boost::asio::steady_timer timer; |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 310 | std::unique_ptr<sdbusplus::bus::match_t> match; |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 311 | std::optional<time_t> endTime; |
James Feist | fe30672 | 2020-03-12 16:32:08 -0700 | [diff] [blame] | 312 | std::optional<Payload> payload; |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 313 | bool gave204 = false; |
George Liu | 6868ff5 | 2021-01-02 11:37:41 +0800 | [diff] [blame] | 314 | int percentComplete = 0; |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | } // namespace task |
| 318 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 319 | inline void requestRoutesTaskMonitor(App& app) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 320 | { |
Ed Tanous | fdbce79 | 2024-06-26 14:48:46 -0700 | [diff] [blame] | 321 | BMCWEB_ROUTE(app, "/redfish/v1/TaskService/TaskMonitors/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 322 | .privileges(redfish::privileges::getTask) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 323 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 324 | [&app](const crow::Request& req, |
| 325 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 326 | const std::string& strParam) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 327 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 328 | { |
| 329 | return; |
| 330 | } |
| 331 | auto find = std::ranges::find_if( |
| 332 | task::tasks, |
| 333 | [&strParam](const std::shared_ptr<task::TaskData>& task) { |
| 334 | if (!task) |
| 335 | { |
| 336 | return false; |
| 337 | } |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 338 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 339 | // we compare against the string version as on failure |
| 340 | // strtoul returns 0 |
| 341 | return std::to_string(task->index) == strParam; |
| 342 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 343 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 344 | if (find == task::tasks.end()) |
| 345 | { |
| 346 | messages::resourceNotFound(asyncResp->res, "Task", |
| 347 | strParam); |
| 348 | return; |
| 349 | } |
| 350 | std::shared_ptr<task::TaskData>& ptr = *find; |
| 351 | // monitor expires after 204 |
| 352 | if (ptr->gave204) |
| 353 | { |
| 354 | messages::resourceNotFound(asyncResp->res, "Task", |
| 355 | strParam); |
| 356 | return; |
| 357 | } |
| 358 | ptr->populateResp(asyncResp->res); |
| 359 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | inline void requestRoutesTask(App& app) |
| 363 | { |
| 364 | BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 365 | .privileges(redfish::privileges::getTask) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 366 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 367 | [&app](const crow::Request& req, |
| 368 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 369 | const std::string& strParam) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 370 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 371 | { |
| 372 | return; |
| 373 | } |
| 374 | auto find = std::ranges::find_if( |
| 375 | task::tasks, |
| 376 | [&strParam](const std::shared_ptr<task::TaskData>& task) { |
| 377 | if (!task) |
| 378 | { |
| 379 | return false; |
| 380 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 381 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 382 | // we compare against the string version as on failure |
| 383 | // strtoul returns 0 |
| 384 | return std::to_string(task->index) == strParam; |
| 385 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 386 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 387 | if (find == task::tasks.end()) |
| 388 | { |
| 389 | messages::resourceNotFound(asyncResp->res, "Task", |
| 390 | strParam); |
| 391 | return; |
| 392 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 393 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 394 | const std::shared_ptr<task::TaskData>& ptr = *find; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 395 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 396 | asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task"; |
| 397 | asyncResp->res.jsonValue["Id"] = strParam; |
| 398 | asyncResp->res.jsonValue["Name"] = "Task " + strParam; |
| 399 | asyncResp->res.jsonValue["TaskState"] = ptr->state; |
| 400 | asyncResp->res.jsonValue["StartTime"] = |
| 401 | redfish::time_utils::getDateTimeStdtime(ptr->startTime); |
| 402 | if (ptr->endTime) |
| 403 | { |
| 404 | asyncResp->res.jsonValue["EndTime"] = |
| 405 | redfish::time_utils::getDateTimeStdtime( |
| 406 | *(ptr->endTime)); |
| 407 | } |
| 408 | asyncResp->res.jsonValue["TaskStatus"] = ptr->status; |
| 409 | asyncResp->res.jsonValue["Messages"] = ptr->messages; |
| 410 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 411 | "/redfish/v1/TaskService/Tasks/{}", strParam); |
| 412 | if (!ptr->gave204) |
| 413 | { |
| 414 | asyncResp->res.jsonValue["TaskMonitor"] = |
| 415 | boost::urls::format( |
| 416 | "/redfish/v1/TaskService/TaskMonitors/{}", |
| 417 | strParam); |
| 418 | } |
Arun Thomas Baby | 5db7dfd | 2023-05-02 03:22:23 -0700 | [diff] [blame] | 419 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 420 | asyncResp->res.jsonValue["HidePayload"] = !ptr->payload; |
Arun Thomas Baby | 5db7dfd | 2023-05-02 03:22:23 -0700 | [diff] [blame] | 421 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 422 | if (ptr->payload) |
| 423 | { |
| 424 | const task::Payload& p = *(ptr->payload); |
| 425 | asyncResp->res.jsonValue["Payload"]["TargetUri"] = |
| 426 | p.targetUri; |
| 427 | asyncResp->res.jsonValue["Payload"]["HttpOperation"] = |
| 428 | p.httpOperation; |
| 429 | asyncResp->res.jsonValue["Payload"]["HttpHeaders"] = |
| 430 | p.httpHeaders; |
| 431 | asyncResp->res.jsonValue["Payload"]["JsonBody"] = |
| 432 | p.jsonBody.dump( |
| 433 | -1, ' ', true, |
| 434 | nlohmann::json::error_handler_t::replace); |
| 435 | } |
| 436 | asyncResp->res.jsonValue["PercentComplete"] = |
| 437 | ptr->percentComplete; |
| 438 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 439 | } |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 440 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 441 | inline void requestRoutesTaskCollection(App& app) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 442 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 443 | BMCWEB_ROUTE(app, "/redfish/v1/TaskService/Tasks/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 444 | .privileges(redfish::privileges::getTaskCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 445 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 446 | [&app](const crow::Request& req, |
| 447 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 448 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 449 | { |
| 450 | return; |
| 451 | } |
| 452 | asyncResp->res.jsonValue["@odata.type"] = |
| 453 | "#TaskCollection.TaskCollection"; |
| 454 | asyncResp->res.jsonValue["@odata.id"] = |
| 455 | "/redfish/v1/TaskService/Tasks"; |
| 456 | asyncResp->res.jsonValue["Name"] = "Task Collection"; |
| 457 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 458 | task::tasks.size(); |
| 459 | nlohmann::json& members = asyncResp->res.jsonValue["Members"]; |
| 460 | members = nlohmann::json::array(); |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 461 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 462 | for (const std::shared_ptr<task::TaskData>& task : task::tasks) |
| 463 | { |
| 464 | if (task == nullptr) |
| 465 | { |
| 466 | continue; // shouldn't be possible |
| 467 | } |
| 468 | nlohmann::json::object_t member; |
| 469 | member["@odata.id"] = |
| 470 | boost::urls::format("/redfish/v1/TaskService/Tasks/{}", |
| 471 | std::to_string(task->index)); |
| 472 | members.emplace_back(std::move(member)); |
| 473 | } |
| 474 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 475 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 476 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 477 | inline void requestRoutesTaskService(App& app) |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 478 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 479 | BMCWEB_ROUTE(app, "/redfish/v1/TaskService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 480 | .privileges(redfish::privileges::getTaskService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 481 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 482 | [&app](const crow::Request& req, |
| 483 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 484 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 485 | { |
| 486 | return; |
| 487 | } |
| 488 | asyncResp->res.jsonValue["@odata.type"] = |
| 489 | "#TaskService.v1_1_4.TaskService"; |
| 490 | asyncResp->res.jsonValue["@odata.id"] = |
| 491 | "/redfish/v1/TaskService"; |
| 492 | asyncResp->res.jsonValue["Name"] = "Task Service"; |
| 493 | asyncResp->res.jsonValue["Id"] = "TaskService"; |
| 494 | asyncResp->res.jsonValue["DateTime"] = |
| 495 | redfish::time_utils::getDateTimeOffsetNow().first; |
| 496 | asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = |
| 497 | task_service::OverWritePolicy::Oldest; |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 498 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 499 | asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = |
| 500 | true; |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 501 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 502 | asyncResp->res.jsonValue["Status"]["State"] = |
| 503 | resource::State::Enabled; |
| 504 | asyncResp->res.jsonValue["ServiceEnabled"] = true; |
| 505 | asyncResp->res.jsonValue["Tasks"]["@odata.id"] = |
| 506 | "/redfish/v1/TaskService/Tasks"; |
| 507 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 508 | } |
James Feist | 4622957 | 2020-02-19 15:11:58 -0800 | [diff] [blame] | 509 | |
| 510 | } // namespace redfish |