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