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