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