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