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