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