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