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