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