blob: fe1468feebd4508aae6cf7e20cb58dfbd4d98060 [file] [log] [blame]
James Feist46229572020-02-19 15:11:58 -08001/*
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
18#include "node.hpp"
19
Zhenfei Taidecde9e2020-05-05 13:39:07 -070020#include <boost/asio.hpp>
James Feist46229572020-02-19 15:11:58 -080021#include <boost/container/flat_map.hpp>
James Feiste5d50062020-05-11 17:29:00 -070022#include <task_messages.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023
24#include <chrono>
James Feist46229572020-02-19 15:11:58 -080025#include <variant>
26
27namespace redfish
28{
29
30namespace task
31{
32constexpr size_t maxTaskCount = 100; // arbitrary limit
33
34static std::deque<std::shared_ptr<struct TaskData>> tasks;
35
James Feist32898ce2020-03-10 16:16:52 -070036constexpr bool completed = true;
37
James Feistfe306722020-03-12 16:32:08 -070038struct Payload
39{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040 Payload(const crow::Request& req) :
James Feistfe306722020-03-12 16:32:08 -070041 targetUri(req.url), httpOperation(req.methodString()),
42 httpHeaders(nlohmann::json::array())
43
44 {
45 using field_ns = boost::beast::http::field;
46 constexpr const std::array<boost::beast::http::field, 7>
47 headerWhitelist = {field_ns::accept, field_ns::accept_encoding,
48 field_ns::user_agent, field_ns::host,
49 field_ns::connection, field_ns::content_length,
50 field_ns::upgrade};
51
52 jsonBody = nlohmann::json::parse(req.body, nullptr, false);
53 if (jsonBody.is_discarded())
54 {
55 jsonBody = nullptr;
56 }
57
Gunnar Mills1214b7e2020-06-04 10:11:30 -050058 for (const auto& field : req.fields)
James Feistfe306722020-03-12 16:32:08 -070059 {
60 if (std::find(headerWhitelist.begin(), headerWhitelist.end(),
61 field.name()) == headerWhitelist.end())
62 {
63 continue;
64 }
65 std::string header;
66 header.reserve(field.name_string().size() + 2 +
67 field.value().size());
68 header += field.name_string();
69 header += ": ";
70 header += field.value();
71 httpHeaders.emplace_back(std::move(header));
72 }
73 }
74 Payload() = delete;
75
76 std::string targetUri;
77 std::string httpOperation;
78 nlohmann::json httpHeaders;
79 nlohmann::json jsonBody;
80};
81
Gunnar Mills1214b7e2020-06-04 10:11:30 -050082inline void to_json(nlohmann::json& j, const Payload& p)
James Feistfe306722020-03-12 16:32:08 -070083{
84 j = {{"TargetUri", p.targetUri},
85 {"HttpOperation", p.httpOperation},
86 {"HttpHeaders", p.httpHeaders},
87 {"JsonBody", p.jsonBody.dump()}};
88}
89
James Feist46229572020-02-19 15:11:58 -080090struct TaskData : std::enable_shared_from_this<TaskData>
91{
92 private:
93 TaskData(std::function<bool(boost::system::error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -050094 sdbusplus::message::message&,
95 const std::shared_ptr<TaskData>&)>&& handler,
96 const std::string& match, size_t idx) :
James Feist46229572020-02-19 15:11:58 -080097 callback(std::move(handler)),
98 matchStr(match), index(idx),
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 Mills1214b7e2020-06-04 10:11:30 -0500104 {}
James Feist46229572020-02-19 15:11:58 -0800105 TaskData() = delete;
106
107 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500108 static std::shared_ptr<TaskData>& createTask(
James Feist46229572020-02-19 15:11:58 -0800109 std::function<bool(boost::system::error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500110 sdbusplus::message::message&,
111 const std::shared_ptr<TaskData>&)>&& handler,
112 const std::string& match)
James Feist46229572020-02-19 15:11:58 -0800113 {
114 static size_t lastTask = 0;
115 struct MakeSharedHelper : public TaskData
116 {
117 MakeSharedHelper(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500118 std::function<bool(boost::system::error_code,
119 sdbusplus::message::message&,
120 const std::shared_ptr<TaskData>&)>&& handler,
121 const std::string& match, size_t idx) :
James Feist46229572020-02-19 15:11:58 -0800122 TaskData(std::move(handler), match, idx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500123 {}
James Feist46229572020-02-19 15:11:58 -0800124 };
125
126 if (tasks.size() >= maxTaskCount)
127 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500128 auto& last = tasks.front();
James Feist46229572020-02-19 15:11:58 -0800129
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 Mills1214b7e2020-06-04 10:11:30 -0500140 void populateResp(crow::Response& res, size_t retryAfterSeconds = 30)
James Feist46229572020-02-19 15:11:58 -0800141 {
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;
147 res.jsonValue = {{"@odata.id", uri},
148 {"@odata.type", "#Task.v1_4_3.Task"},
149 {"Id", strIdx},
150 {"TaskState", state},
151 {"TaskStatus", status}};
152 res.addHeader(boost::beast::http::field::location,
153 uri + "/Monitor");
154 res.addHeader(boost::beast::http::field::retry_after,
155 std::to_string(retryAfterSeconds));
156 }
157 else if (!gave204)
158 {
159 res.result(boost::beast::http::status::no_content);
160 gave204 = true;
161 }
162 }
163
164 void finishTask(void)
165 {
166 endTime = std::chrono::system_clock::to_time_t(
167 std::chrono::system_clock::now());
168 }
169
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500170 void extendTimer(const std::chrono::seconds& timeout)
James Feist46229572020-02-19 15:11:58 -0800171 {
James Feist46229572020-02-19 15:11:58 -0800172 timer.expires_after(timeout);
173 timer.async_wait(
174 [self = shared_from_this()](boost::system::error_code ec) {
175 if (ec == boost::asio::error::operation_aborted)
176 {
177 return; // completed succesfully
178 }
179 if (!ec)
180 {
181 // change ec to error as timer expired
182 ec = boost::asio::error::operation_aborted;
183 }
184 self->match.reset();
185 sdbusplus::message::message msg;
186 self->finishTask();
187 self->state = "Cancelled";
188 self->status = "Warning";
James Feiste5d50062020-05-11 17:29:00 -0700189 self->messages.emplace_back(
190 messages::taskAborted(std::to_string(self->index)));
James Feist46229572020-02-19 15:11:58 -0800191 self->callback(ec, msg, self);
192 });
James Feistfd9ab9e2020-05-19 13:48:07 -0700193 }
194
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500195 void startTimer(const std::chrono::seconds& timeout)
James Feistfd9ab9e2020-05-19 13:48:07 -0700196 {
197 if (match)
198 {
199 return;
200 }
201 match = std::make_unique<sdbusplus::bus::match::match>(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500202 static_cast<sdbusplus::bus::bus&>(*crow::connections::systemBus),
James Feistfd9ab9e2020-05-19 13:48:07 -0700203 matchStr,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500204 [self = shared_from_this()](sdbusplus::message::message& message) {
James Feistfd9ab9e2020-05-19 13:48:07 -0700205 boost::system::error_code ec;
206
207 // callback to return True if callback is done, callback needs
208 // to update status itself if needed
209 if (self->callback(ec, message, self) == task::completed)
210 {
211 self->timer.cancel();
212 self->finishTask();
213
214 // reset the match after the callback was successful
215 boost::asio::post(
216 crow::connections::systemBus->get_io_context(),
217 [self] { self->match.reset(); });
218 return;
219 }
220 });
221
222 extendTimer(timeout);
James Feiste5d50062020-05-11 17:29:00 -0700223 messages.emplace_back(messages::taskStarted(std::to_string(index)));
James Feist46229572020-02-19 15:11:58 -0800224 }
225
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500226 std::function<bool(boost::system::error_code, sdbusplus::message::message&,
227 const std::shared_ptr<TaskData>&)>
James Feist46229572020-02-19 15:11:58 -0800228 callback;
229 std::string matchStr;
230 size_t index;
231 time_t startTime;
232 std::string status;
233 std::string state;
234 nlohmann::json messages;
235 boost::asio::steady_timer timer;
236 std::unique_ptr<sdbusplus::bus::match::match> match;
237 std::optional<time_t> endTime;
James Feistfe306722020-03-12 16:32:08 -0700238 std::optional<Payload> payload;
James Feist46229572020-02-19 15:11:58 -0800239 bool gave204 = false;
240};
241
242} // namespace task
243
244class TaskMonitor : public Node
245{
246 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500247 TaskMonitor(CrowApp& app) :
Gunnar Mills7af91512020-04-14 22:16:57 -0500248 Node((app), "/redfish/v1/TaskService/Tasks/<str>/Monitor/",
James Feist46229572020-02-19 15:11:58 -0800249 std::string())
250 {
251 entityPrivileges = {
252 {boost::beast::http::verb::get, {{"Login"}}},
253 {boost::beast::http::verb::head, {{"Login"}}},
254 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
255 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
256 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
257 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
258 }
259
260 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500261 void doGet(crow::Response& res, const crow::Request& req,
262 const std::vector<std::string>& params) override
James Feist46229572020-02-19 15:11:58 -0800263 {
264 auto asyncResp = std::make_shared<AsyncResp>(res);
265 if (params.size() != 1)
266 {
267 messages::internalError(asyncResp->res);
268 return;
269 }
270
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500271 const std::string& strParam = params[0];
James Feist46229572020-02-19 15:11:58 -0800272 auto find = std::find_if(
273 task::tasks.begin(), task::tasks.end(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500274 [&strParam](const std::shared_ptr<task::TaskData>& task) {
James Feist46229572020-02-19 15:11:58 -0800275 if (!task)
276 {
277 return false;
278 }
279
280 // we compare against the string version as on failure strtoul
281 // returns 0
282 return std::to_string(task->index) == strParam;
283 });
284
285 if (find == task::tasks.end())
286 {
287 messages::resourceNotFound(asyncResp->res, "Monitor", strParam);
288 return;
289 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500290 std::shared_ptr<task::TaskData>& ptr = *find;
James Feist46229572020-02-19 15:11:58 -0800291 // monitor expires after 204
292 if (ptr->gave204)
293 {
294 messages::resourceNotFound(asyncResp->res, "Monitor", strParam);
295 return;
296 }
297 ptr->populateResp(asyncResp->res);
298 }
299};
300
301class Task : public Node
302{
303 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500304 Task(CrowApp& app) :
Gunnar Mills7af91512020-04-14 22:16:57 -0500305 Node((app), "/redfish/v1/TaskService/Tasks/<str>/", std::string())
James Feist46229572020-02-19 15:11:58 -0800306 {
307 entityPrivileges = {
308 {boost::beast::http::verb::get, {{"Login"}}},
309 {boost::beast::http::verb::head, {{"Login"}}},
310 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
311 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
312 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
313 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
314 }
315
316 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 void doGet(crow::Response& res, const crow::Request& req,
318 const std::vector<std::string>& params) override
James Feist46229572020-02-19 15:11:58 -0800319 {
320 auto asyncResp = std::make_shared<AsyncResp>(res);
321 if (params.size() != 1)
322 {
323 messages::internalError(asyncResp->res);
324 return;
325 }
326
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500327 const std::string& strParam = params[0];
James Feist46229572020-02-19 15:11:58 -0800328 auto find = std::find_if(
329 task::tasks.begin(), task::tasks.end(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500330 [&strParam](const std::shared_ptr<task::TaskData>& task) {
James Feist46229572020-02-19 15:11:58 -0800331 if (!task)
332 {
333 return false;
334 }
335
336 // we compare against the string version as on failure strtoul
337 // returns 0
338 return std::to_string(task->index) == strParam;
339 });
340
341 if (find == task::tasks.end())
342 {
343 messages::resourceNotFound(asyncResp->res, "Tasks", strParam);
344 return;
345 }
346
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500347 std::shared_ptr<task::TaskData>& ptr = *find;
James Feist46229572020-02-19 15:11:58 -0800348
349 asyncResp->res.jsonValue["@odata.type"] = "#Task.v1_4_3.Task";
350 asyncResp->res.jsonValue["Id"] = strParam;
351 asyncResp->res.jsonValue["Name"] = "Task " + strParam;
352 asyncResp->res.jsonValue["TaskState"] = ptr->state;
353 asyncResp->res.jsonValue["StartTime"] =
354 crow::utility::getDateTime(ptr->startTime);
355 if (ptr->endTime)
356 {
357 asyncResp->res.jsonValue["EndTime"] =
358 crow::utility::getDateTime(*(ptr->endTime));
359 }
360 asyncResp->res.jsonValue["TaskStatus"] = ptr->status;
361 asyncResp->res.jsonValue["Messages"] = ptr->messages;
362 asyncResp->res.jsonValue["@odata.id"] =
363 "/redfish/v1/TaskService/Tasks/" + strParam;
364 if (!ptr->gave204)
365 {
366 asyncResp->res.jsonValue["TaskMonitor"] =
367 "/redfish/v1/TaskService/Tasks/" + strParam + "/Monitor";
368 }
James Feistfe306722020-03-12 16:32:08 -0700369 if (ptr->payload)
370 {
371 asyncResp->res.jsonValue["Payload"] = *(ptr->payload);
372 }
James Feist46229572020-02-19 15:11:58 -0800373 }
374};
375
376class TaskCollection : public Node
377{
378 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500379 TaskCollection(CrowApp& app) : Node(app, "/redfish/v1/TaskService/Tasks/")
James Feist46229572020-02-19 15:11:58 -0800380 {
381 entityPrivileges = {
382 {boost::beast::http::verb::get, {{"Login"}}},
383 {boost::beast::http::verb::head, {{"Login"}}},
384 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
385 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
386 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
387 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
388 }
389
390 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500391 void doGet(crow::Response& res, const crow::Request& req,
392 const std::vector<std::string>& params) override
James Feist46229572020-02-19 15:11:58 -0800393 {
394 auto asyncResp = std::make_shared<AsyncResp>(res);
395 asyncResp->res.jsonValue["@odata.type"] =
396 "#TaskCollection.TaskCollection";
397 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService/Tasks";
398 asyncResp->res.jsonValue["Name"] = "Task Collection";
399 asyncResp->res.jsonValue["Members@odata.count"] = task::tasks.size();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500400 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
James Feist46229572020-02-19 15:11:58 -0800401 members = nlohmann::json::array();
402
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500403 for (const std::shared_ptr<task::TaskData>& task : task::tasks)
James Feist46229572020-02-19 15:11:58 -0800404 {
405 if (task == nullptr)
406 {
407 continue; // shouldn't be possible
408 }
409 members.emplace_back(
410 nlohmann::json{{"@odata.id", "/redfish/v1/TaskService/Tasks/" +
411 std::to_string(task->index)}});
412 }
413 }
414};
415
416class TaskService : public Node
417{
418 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500419 TaskService(CrowApp& app) : Node(app, "/redfish/v1/TaskService/")
James Feist46229572020-02-19 15:11:58 -0800420 {
421 entityPrivileges = {
422 {boost::beast::http::verb::get, {{"Login"}}},
423 {boost::beast::http::verb::head, {{"Login"}}},
424 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
425 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
426 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
427 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
428 }
429
430 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500431 void doGet(crow::Response& res, const crow::Request& req,
432 const std::vector<std::string>& params) override
James Feist46229572020-02-19 15:11:58 -0800433 {
434 auto asyncResp = std::make_shared<AsyncResp>(res);
435 asyncResp->res.jsonValue["@odata.type"] =
436 "#TaskService.v1_1_4.TaskService";
437 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TaskService";
438 asyncResp->res.jsonValue["Name"] = "Task Service";
439 asyncResp->res.jsonValue["Id"] = "TaskService";
440 asyncResp->res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
441 asyncResp->res.jsonValue["CompletedTaskOverWritePolicy"] = "Oldest";
442
443 // todo: if we enable events, change this to true
444 asyncResp->res.jsonValue["LifeCycleEventOnTaskStateChange"] = false;
445
446 auto health = std::make_shared<HealthPopulate>(asyncResp);
447 health->populate();
448 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
449 asyncResp->res.jsonValue["ServiceEnabled"] = true;
450 asyncResp->res.jsonValue["Tasks"] = {
451 {"@odata.id", "/redfish/v1/TaskService/Tasks"}};
452 }
453};
454
455} // namespace redfish