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