blob: af87b7668489eed05445dce081b60941336dc4fc [file] [log] [blame]
Ratan Gupta453fed02019-12-14 09:39:47 +05301#pragma once
2#include <app.h>
3#include <tinyxml2.h>
4
5#include <async_resp.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -06006#include <boost/algorithm/string.hpp>
7#include <boost/container/flat_set.hpp>
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +05308#include <error_messages.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -05009#include <event_service_manager.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053010#include <ibm/locks.hpp>
11#include <nlohmann/json.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -050012#include <resource_messages.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060013#include <sdbusplus/message/types.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053014#include <utils/json_utils.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060015
Gunnar Mills1214b7e2020-06-04 10:11:30 -050016#include <filesystem>
17#include <fstream>
18#include <regex>
19
Ratan Guptae46946a2020-05-11 13:22:59 +053020// Allow save area file size to 500KB
21#define MAX_SAVE_AREA_FILESIZE 500000
22
manojkiraneda0b631ae2019-12-03 17:54:28 +053023using SType = std::string;
24using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>;
25using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
26using LockRequests = std::vector<LockRequest>;
27using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053028using RcGetLockList =
29 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
30using ListOfSessionIds = std::vector<std::string>;
Ratan Gupta453fed02019-12-14 09:39:47 +053031namespace crow
32{
33namespace ibm_mc
34{
Sunitha Harish96330b92020-06-26 05:42:14 -050035using namespace redfish;
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036constexpr const char* methodNotAllowedMsg = "Method Not Allowed";
37constexpr const char* resourceNotFoundMsg = "Resource Not Found";
38constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable";
39constexpr const char* internalServerError = "Internal Server Error";
Sunitha Harish97b0e432019-11-21 04:59:29 -060040
Gunnar Mills1214b7e2020-06-04 10:11:30 -050041bool createSaveAreaPath(crow::Response& res)
Sunitha Harish97b0e432019-11-21 04:59:29 -060042{
43 // The path /var/lib/obmc will be created by initrdscripts
44 // Create the directories for the save-area files, when we get
45 // first file upload request
46 std::error_code ec;
47 if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec))
48 {
49 std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec);
50 }
51 if (ec)
52 {
53 res.result(boost::beast::http::status::internal_server_error);
54 res.jsonValue["Description"] = internalServerError;
55 BMCWEB_LOG_DEBUG
56 << "handleIbmPost: Failed to prepare save-area directory. ec : "
57 << ec;
58 return false;
59 }
60
61 if (!std::filesystem::is_directory(
62 "/var/lib/obmc/bmc-console-mgmt/save-area", ec))
63 {
64 std::filesystem::create_directory(
65 "/var/lib/obmc/bmc-console-mgmt/save-area", ec);
66 }
67 if (ec)
68 {
69 res.result(boost::beast::http::status::internal_server_error);
70 res.jsonValue["Description"] = internalServerError;
71 BMCWEB_LOG_DEBUG
72 << "handleIbmPost: Failed to prepare save-area directory. ec : "
73 << ec;
74 return false;
75 }
76 return true;
77}
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078void handleFilePut(const crow::Request& req, crow::Response& res,
79 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060080{
81 // Check the content-type of the request
82 std::string_view contentType = req.getHeaderValue("content-type");
83 if (boost::starts_with(contentType, "multipart/form-data"))
84 {
85 BMCWEB_LOG_DEBUG
86 << "This is multipart/form-data. Invalid content for PUT";
87
88 res.result(boost::beast::http::status::not_acceptable);
89 res.jsonValue["Description"] = contentNotAcceptableMsg;
90 return;
91 }
92 else
93 {
94 BMCWEB_LOG_DEBUG << "Not a multipart/form-data. Continue..";
95 }
asmithakarun1c7b07c2019-09-09 03:42:59 -050096
97 BMCWEB_LOG_DEBUG
98 << "handleIbmPut: Request to create/update the save-area file";
99 if (!createSaveAreaPath(res))
Sunitha Harish97b0e432019-11-21 04:59:29 -0600100 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500101 res.result(boost::beast::http::status::not_found);
102 res.jsonValue["Description"] = resourceNotFoundMsg;
103 return;
104 }
105 // Create the file
106 std::ofstream file;
107 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
108 loc /= fileID;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600109
asmithakarun1c7b07c2019-09-09 03:42:59 -0500110 std::string data = std::move(req.body);
111 BMCWEB_LOG_DEBUG << "data capaticty : " << data.capacity();
112 if (data.capacity() > MAX_SAVE_AREA_FILESIZE)
113 {
114 res.result(boost::beast::http::status::bad_request);
115 res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530116 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500117 return;
118 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500119 BMCWEB_LOG_DEBUG << "Creating file " << loc;
120 file.open(loc, std::ofstream::out);
121 if (file.fail())
122 {
123 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
124 res.result(boost::beast::http::status::internal_server_error);
125 res.jsonValue["Description"] = "Error while creating the file";
126 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600127 }
128 else
129 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500130 file << data;
131 BMCWEB_LOG_DEBUG << "save-area file is created";
132 res.jsonValue["Description"] = "File Created";
Sunitha Harish96330b92020-06-26 05:42:14 -0500133 // Push an event
134 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
135 redfish::EventServiceManager::getInstance().sendEvent(
136 redfish::messages::ResourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500137 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530138}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500139
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500140void handleConfigFileList(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530141{
142 std::vector<std::string> pathObjList;
143 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
144 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
145 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500146 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530147 {
148 std::filesystem::path pathObj(file.path());
149 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
150 pathObj.filename().string());
151 }
152 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500153 res.jsonValue["@odata.type"] = "#IBMConfigFile.v1_0_0.IBMConfigFile";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530154 res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
155 res.jsonValue["Id"] = "ConfigFiles";
156 res.jsonValue["Name"] = "ConfigFiles";
157
158 res.jsonValue["Members"] = std::move(pathObjList);
Sunitha Harishe56f2542020-07-22 02:38:59 -0500159 res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530160 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500161 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Ratan Guptad3630cb2019-12-14 11:21:35 +0530162 res.end();
163}
164
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500165void deleteConfigFiles(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530166{
167 std::vector<std::string> pathObjList;
168 std::error_code ec;
169 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
170 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
171 {
172 std::filesystem::remove_all(loc, ec);
173 if (ec)
174 {
175 res.result(boost::beast::http::status::internal_server_error);
176 res.jsonValue["Description"] = internalServerError;
177 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
178 "config files directory. ec : "
179 << ec;
180 }
181 }
182 res.end();
asmithakarun1c7b07c2019-09-09 03:42:59 -0500183}
184
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500185void getLockServiceData(crow::Response& res)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530186{
187 res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
188 res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
189 res.jsonValue["Id"] = "LockService";
190 res.jsonValue["Name"] = "LockService";
191
192 res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
193 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
194 res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
195 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
196 res.jsonValue["Actions"]["#LockService.GetLockList"] = {
197 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
198 res.end();
199}
200
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500201void handleFileGet(crow::Response& res, const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500202{
203 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
204 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" +
205 fileID);
206 if (!std::filesystem::exists(loc))
207 {
208 BMCWEB_LOG_ERROR << loc << "Not found";
209 res.result(boost::beast::http::status::not_found);
210 res.jsonValue["Description"] = resourceNotFoundMsg;
211 return;
212 }
213
214 std::ifstream readfile(loc.string());
215 if (!readfile)
216 {
217 BMCWEB_LOG_ERROR << loc.string() << "Not found";
218 res.result(boost::beast::http::status::not_found);
219 res.jsonValue["Description"] = resourceNotFoundMsg;
220 return;
221 }
222
223 std::string contentDispositionParam =
224 "attachment; filename=\"" + fileID + "\"";
225 res.addHeader("Content-Disposition", contentDispositionParam);
226 std::string fileData;
227 fileData = {std::istreambuf_iterator<char>(readfile),
228 std::istreambuf_iterator<char>()};
229 res.jsonValue["Data"] = fileData;
230 return;
231}
232
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500233void handleFileDelete(crow::Response& res, const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500234{
235 std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID);
236 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
237
238 std::ifstream file_open(filePath.c_str());
239 if (static_cast<bool>(file_open))
240 if (remove(filePath.c_str()) == 0)
241 {
242 BMCWEB_LOG_DEBUG << "File removed!\n";
243 res.jsonValue["Description"] = "File Deleted";
244 }
245 else
246 {
247 BMCWEB_LOG_ERROR << "File not removed!\n";
248 res.result(boost::beast::http::status::internal_server_error);
249 res.jsonValue["Description"] = internalServerError;
250 }
251 else
252 {
253 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harish97b0e432019-11-21 04:59:29 -0600254 res.result(boost::beast::http::status::not_found);
255 res.jsonValue["Description"] = resourceNotFoundMsg;
256 }
257 return;
258}
259
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500260inline void handleFileUrl(const crow::Request& req, crow::Response& res,
261 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600262{
Ed Tanousb41187f2019-10-24 16:30:02 -0700263 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600264 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500265 handleFilePut(req, res, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600266 res.end();
267 return;
268 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700269 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500270 {
271 handleFileGet(res, fileID);
272 res.end();
273 return;
274 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700275 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500276 {
277 handleFileDelete(res, fileID);
278 res.end();
279 return;
280 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600281}
Ratan Gupta453fed02019-12-14 09:39:47 +0530282
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500283void handleAcquireLockAPI(const crow::Request& req, crow::Response& res,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530284 std::vector<nlohmann::json> body)
285{
286 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530288 {
289 std::string lockType;
290 uint64_t resourceId;
291
292 SegmentFlags segInfo;
293 std::vector<nlohmann::json> segmentFlags;
294
295 if (!redfish::json_util::readJson(element, res, "LockType", lockType,
296 "ResourceID", resourceId,
297 "SegmentFlags", segmentFlags))
298 {
299 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
300 res.result(boost::beast::http::status::bad_request);
301 res.end();
302 return;
303 }
304 BMCWEB_LOG_DEBUG << lockType;
305 BMCWEB_LOG_DEBUG << resourceId;
306
307 BMCWEB_LOG_DEBUG << "Segment Flags are present";
308
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500309 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530310 {
311 std::string lockFlags;
312 uint32_t segmentLength;
313
314 if (!redfish::json_util::readJson(e, res, "LockFlag", lockFlags,
315 "SegmentLength", segmentLength))
316 {
317 res.result(boost::beast::http::status::bad_request);
318 res.end();
319 return;
320 }
321
322 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
323 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
324
325 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
326 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530327 lockRequestStructure.push_back(
328 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
329 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530330 }
331
332 // print lock request into journal
333
334 for (uint32_t i = 0; i < lockRequestStructure.size(); i++)
335 {
336 BMCWEB_LOG_DEBUG << std::get<0>(lockRequestStructure[i]);
337 BMCWEB_LOG_DEBUG << std::get<1>(lockRequestStructure[i]);
338 BMCWEB_LOG_DEBUG << std::get<2>(lockRequestStructure[i]);
339 BMCWEB_LOG_DEBUG << std::get<3>(lockRequestStructure[i]);
340
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500341 for (const auto& p : std::get<4>(lockRequestStructure[i]))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530342 {
343 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
344 }
345 }
346
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500347 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530348
Ratan Gupta07386c62019-12-14 14:06:09 +0530349 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530350
351 if (varAcquireLock.first)
352 {
353 // Either validity failure of there is a conflict with itself
354
355 auto validityStatus =
356 std::get<std::pair<bool, int>>(varAcquireLock.second);
357
358 if ((!validityStatus.first) && (validityStatus.second == 0))
359 {
360 BMCWEB_LOG_DEBUG << "Not a Valid record";
361 BMCWEB_LOG_DEBUG << "Bad json in request";
362 res.result(boost::beast::http::status::bad_request);
363 res.end();
364 return;
365 }
366 if (validityStatus.first && (validityStatus.second == 1))
367 {
368 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
369 res.result(boost::beast::http::status::bad_request);
370 res.end();
371 return;
372 }
373 }
374 else
375 {
376 auto conflictStatus =
377 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
378 if (!conflictStatus.first)
379 {
380 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
381 res.result(boost::beast::http::status::ok);
382
383 auto var = std::get<uint32_t>(conflictStatus.second);
384 nlohmann::json returnJson;
385 returnJson["id"] = var;
386 res.jsonValue["TransactionID"] = var;
387 res.end();
388 return;
389 }
390 else
391 {
392 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
393 res.result(boost::beast::http::status::conflict);
394 auto var = std::get<std::pair<uint32_t, LockRequest>>(
395 conflictStatus.second);
396 nlohmann::json returnJson, segments;
397 nlohmann::json myarray = nlohmann::json::array();
398 returnJson["TransactionID"] = var.first;
399 returnJson["SessionID"] = std::get<0>(var.second);
400 returnJson["HMCID"] = std::get<1>(var.second);
401 returnJson["LockType"] = std::get<2>(var.second);
402 returnJson["ResourceID"] = std::get<3>(var.second);
403
404 for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++)
405 {
406 segments["LockFlag"] = std::get<4>(var.second)[i].first;
407 segments["SegmentLength"] = std::get<4>(var.second)[i].second;
408 myarray.push_back(segments);
409 }
410
411 returnJson["SegmentFlags"] = myarray;
412
413 res.jsonValue["Record"] = returnJson;
414 res.end();
415 return;
416 }
417 }
418}
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500419void handleRelaseAllAPI(const crow::Request& req, crow::Response& res)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530420{
421 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
422 res.result(boost::beast::http::status::ok);
423 res.end();
424 return;
425}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530426
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500427void handleReleaseLockAPI(const crow::Request& req, crow::Response& res,
428 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530429{
430 BMCWEB_LOG_DEBUG << listTransactionIds.size();
431 BMCWEB_LOG_DEBUG << "Data is present";
432 for (uint32_t i = 0; i < listTransactionIds.size(); i++)
433 {
434 BMCWEB_LOG_DEBUG << listTransactionIds[i];
435 }
436
manojkiraneda3b6dea62019-12-13 17:05:36 +0530437 // validate the request ids
438
Ratan Gupta07386c62019-12-14 14:06:09 +0530439 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530440 listTransactionIds,
441 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530442
443 if (!varReleaselock.first)
444 {
445 // validation Failed
446 res.result(boost::beast::http::status::bad_request);
447 res.end();
448 return;
449 }
450 else
451 {
452 auto statusRelease =
453 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
454 if (statusRelease.first)
455 {
456 // The current hmc owns all the locks, so we already released
457 // them
458 res.result(boost::beast::http::status::ok);
459 res.end();
460 return;
461 }
462
463 else
464 {
465 // valid rid, but the current hmc does not own all the locks
466 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
467 res.result(boost::beast::http::status::unauthorized);
468
469 auto var = statusRelease.second;
470 nlohmann::json returnJson, segments;
471 nlohmann::json myArray = nlohmann::json::array();
472 returnJson["TransactionID"] = var.first;
473 returnJson["SessionID"] = std::get<0>(var.second);
474 returnJson["HMCID"] = std::get<1>(var.second);
475 returnJson["LockType"] = std::get<2>(var.second);
476 returnJson["ResourceID"] = std::get<3>(var.second);
477
478 for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++)
479 {
480 segments["LockFlag"] = std::get<4>(var.second)[i].first;
481 segments["SegmentLength"] = std::get<4>(var.second)[i].second;
482 myArray.push_back(segments);
483 }
484
485 returnJson["SegmentFlags"] = myArray;
486 res.jsonValue["Record"] = returnJson;
487 res.end();
488 return;
489 }
490 }
491}
492
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500493void handleGetLockListAPI(const crow::Request& req, crow::Response& res,
494 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530495{
496 BMCWEB_LOG_DEBUG << listSessionIds.size();
497
Ratan Gupta07386c62019-12-14 14:06:09 +0530498 auto status =
499 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530500 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
501
502 nlohmann::json lockRecords = nlohmann::json::array();
503
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500504 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530505 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500506 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530507 {
508 nlohmann::json returnJson;
509
510 returnJson["TransactionID"] = transactionId.first;
511 returnJson["SessionID"] = std::get<0>(lockRecord);
512 returnJson["HMCID"] = std::get<1>(lockRecord);
513 returnJson["LockType"] = std::get<2>(lockRecord);
514 returnJson["ResourceID"] = std::get<3>(lockRecord);
515
516 nlohmann::json segments;
517 nlohmann::json segmentInfoArray = nlohmann::json::array();
518
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500519 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530520 {
521 segments["LockFlag"] = segment.first;
522 segments["SegmentLength"] = segment.second;
523 segmentInfoArray.push_back(segments);
524 }
525
526 returnJson["SegmentFlags"] = segmentInfoArray;
527 lockRecords.push_back(returnJson);
528 }
529 }
530 res.result(boost::beast::http::status::ok);
531 res.jsonValue["Records"] = lockRecords;
532 res.end();
533}
534
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500535template <typename... Middlewares>
536void requestRoutes(Crow<Middlewares...>& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530537{
538
539 // allowed only for admin
540 BMCWEB_ROUTE(app, "/ibm/v1/")
541 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700542 .methods(boost::beast::http::verb::get)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500543 [](const crow::Request& req, crow::Response& res) {
Ratan Gupta453fed02019-12-14 09:39:47 +0530544 res.jsonValue["@odata.type"] =
545 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
546 res.jsonValue["@odata.id"] = "/ibm/v1/";
547 res.jsonValue["Id"] = "IBM Rest RootService";
548 res.jsonValue["Name"] = "IBM Service Root";
549 res.jsonValue["ConfigFiles"] = {
550 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
551 res.jsonValue["LockService"] = {
552 {"@odata.id", "/ibm/v1/HMC/LockService"}};
553 res.end();
554 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600555
Ratan Guptad3630cb2019-12-14 11:21:35 +0530556 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
557 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700558 .methods(boost::beast::http::verb::get)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500559 [](const crow::Request& req, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530560 handleConfigFileList(res);
561 });
562
563 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500564 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ratan Guptad3630cb2019-12-14 11:21:35 +0530565 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700566 .methods(boost::beast::http::verb::post)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500567 [](const crow::Request& req, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530568 deleteConfigFiles(res);
569 });
570
asmithakarun1c7b07c2019-09-09 03:42:59 -0500571 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<path>")
Sunitha Harish97b0e432019-11-21 04:59:29 -0600572 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700573 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
574 boost::beast::http::verb::delete_)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500575 [](const crow::Request& req, crow::Response& res,
576 const std::string& path) { handleFileUrl(req, res, path); });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530577
578 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
579 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700580 .methods(boost::beast::http::verb::get)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500581 [](const crow::Request& req, crow::Response& res) {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530582 getLockServiceData(res);
583 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530584
585 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
586 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700587 .methods(boost::beast::http::verb::post)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500588 [](const crow::Request& req, crow::Response& res) {
manojkiraneda0b631ae2019-12-03 17:54:28 +0530589 std::vector<nlohmann::json> body;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530590 if (!redfish::json_util::readJson(req, res, "Request", body))
591 {
592 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
593 res.result(boost::beast::http::status::bad_request);
594 res.end();
595 return;
596 }
597 handleAcquireLockAPI(req, res, body);
598 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530599 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
600 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700601 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
602 crow::Response& res) {
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530603 std::string type;
604 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530605
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530606 if (!redfish::json_util::readJson(req, res, "Type", type,
607 "TransactionIDs",
608 listTransactionIds))
609 {
610 res.result(boost::beast::http::status::bad_request);
611 res.end();
612 return;
613 }
614 if (type == "Transaction")
615 {
manojkiraneda3b6dea62019-12-13 17:05:36 +0530616 handleReleaseLockAPI(req, res, listTransactionIds);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530617 }
618 else if (type == "Session")
619 {
620 handleRelaseAllAPI(req, res);
621 }
622 else
623 {
624 BMCWEB_LOG_DEBUG << " Value of Type : " << type
625 << "is Not a Valid key";
626 redfish::messages::propertyValueNotInList(res, type, "Type");
627 }
628 });
manojkiraneda402b5712019-12-13 17:07:09 +0530629 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
630 .requires({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700631 .methods(boost::beast::http::verb::post)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500632 [](const crow::Request& req, crow::Response& res) {
manojkiraneda402b5712019-12-13 17:07:09 +0530633 ListOfSessionIds listSessionIds;
634
635 if (!redfish::json_util::readJson(req, res, "SessionIDs",
636 listSessionIds))
637 {
638 res.result(boost::beast::http::status::bad_request);
639 res.end();
640 return;
641 }
manojkiraneda402b5712019-12-13 17:07:09 +0530642 handleGetLockListAPI(req, res, listSessionIds);
643 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530644}
645
646} // namespace ibm_mc
647} // namespace crow