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