blob: 61a4b84d75808e22bdde2516f30efaf97916d43a [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
manojkiraneda0b631ae2019-12-03 17:54:28 +053020using SType = std::string;
21using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>;
22using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
23using LockRequests = std::vector<LockRequest>;
24using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053025using RcGetLockList =
26 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
27using ListOfSessionIds = std::vector<std::string>;
Ratan Gupta453fed02019-12-14 09:39:47 +053028namespace crow
29{
30namespace ibm_mc
31{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050032constexpr const char* methodNotAllowedMsg = "Method Not Allowed";
33constexpr const char* resourceNotFoundMsg = "Resource Not Found";
34constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable";
35constexpr const char* internalServerError = "Internal Server Error";
Sunitha Harish97b0e432019-11-21 04:59:29 -060036
Asmitha Karunanithi5738de52020-07-17 02:03:31 -050037constexpr size_t maxSaveareaFileSize =
38 500000; // Allow save area file size upto 500KB
39constexpr size_t maxBroadcastMsgSize =
40 1000; // Allow Broadcast message size upto 1KB
41
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042bool createSaveAreaPath(crow::Response& res)
Sunitha Harish97b0e432019-11-21 04:59:29 -060043{
44 // The path /var/lib/obmc will be created by initrdscripts
45 // Create the directories for the save-area files, when we get
46 // first file upload request
47 std::error_code ec;
48 if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec))
49 {
50 std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec);
51 }
52 if (ec)
53 {
54 res.result(boost::beast::http::status::internal_server_error);
55 res.jsonValue["Description"] = internalServerError;
56 BMCWEB_LOG_DEBUG
57 << "handleIbmPost: Failed to prepare save-area directory. ec : "
58 << ec;
59 return false;
60 }
61
62 if (!std::filesystem::is_directory(
63 "/var/lib/obmc/bmc-console-mgmt/save-area", ec))
64 {
65 std::filesystem::create_directory(
66 "/var/lib/obmc/bmc-console-mgmt/save-area", ec);
67 }
68 if (ec)
69 {
70 res.result(boost::beast::http::status::internal_server_error);
71 res.jsonValue["Description"] = internalServerError;
72 BMCWEB_LOG_DEBUG
73 << "handleIbmPost: Failed to prepare save-area directory. ec : "
74 << ec;
75 return false;
76 }
77 return true;
78}
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079void handleFilePut(const crow::Request& req, crow::Response& res,
80 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060081{
82 // Check the content-type of the request
83 std::string_view contentType = req.getHeaderValue("content-type");
84 if (boost::starts_with(contentType, "multipart/form-data"))
85 {
86 BMCWEB_LOG_DEBUG
87 << "This is multipart/form-data. Invalid content for PUT";
88
89 res.result(boost::beast::http::status::not_acceptable);
90 res.jsonValue["Description"] = contentNotAcceptableMsg;
91 return;
92 }
93 else
94 {
95 BMCWEB_LOG_DEBUG << "Not a multipart/form-data. Continue..";
96 }
asmithakarun1c7b07c2019-09-09 03:42:59 -050097
98 BMCWEB_LOG_DEBUG
99 << "handleIbmPut: Request to create/update the save-area file";
100 if (!createSaveAreaPath(res))
Sunitha Harish97b0e432019-11-21 04:59:29 -0600101 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500102 res.result(boost::beast::http::status::not_found);
103 res.jsonValue["Description"] = resourceNotFoundMsg;
104 return;
105 }
106 // Create the file
107 std::ofstream file;
108 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
109 loc /= fileID;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600110
asmithakarun1c7b07c2019-09-09 03:42:59 -0500111 std::string data = std::move(req.body);
112 BMCWEB_LOG_DEBUG << "data capaticty : " << data.capacity();
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500113 if (data.capacity() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500114 {
115 res.result(boost::beast::http::status::bad_request);
116 res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530117 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500118 return;
119 }
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500120 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc;
121
122 bool fileExists = false;
123 if (std::filesystem::exists(loc))
124 {
125 fileExists = true;
126 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500127 file.open(loc, std::ofstream::out);
128 if (file.fail())
129 {
130 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
131 res.result(boost::beast::http::status::internal_server_error);
132 res.jsonValue["Description"] = "Error while creating the file";
133 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600134 }
135 else
136 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500137 file << data;
Sunitha Harish96330b92020-06-26 05:42:14 -0500138 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500139 // Push an event
140 if (fileExists)
141 {
142 BMCWEB_LOG_DEBUG << "config file is updated";
143 res.jsonValue["Description"] = "File Updated";
144
145 redfish::EventServiceManager::getInstance().sendEvent(
146 redfish::messages::ResourceChanged(), origin, "IBMConfigFile");
147 }
148 else
149 {
150 BMCWEB_LOG_DEBUG << "config file is created";
151 res.jsonValue["Description"] = "File Created";
152
153 redfish::EventServiceManager::getInstance().sendEvent(
154 redfish::messages::ResourceCreated(), origin, "IBMConfigFile");
155 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500156 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530157}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500158
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500159void handleConfigFileList(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530160{
161 std::vector<std::string> pathObjList;
162 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
163 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
164 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500165 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530166 {
167 std::filesystem::path pathObj(file.path());
168 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
169 pathObj.filename().string());
170 }
171 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500172 res.jsonValue["@odata.type"] = "#IBMConfigFile.v1_0_0.IBMConfigFile";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530173 res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
174 res.jsonValue["Id"] = "ConfigFiles";
175 res.jsonValue["Name"] = "ConfigFiles";
176
177 res.jsonValue["Members"] = std::move(pathObjList);
Sunitha Harishe56f2542020-07-22 02:38:59 -0500178 res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530179 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500180 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Ratan Guptad3630cb2019-12-14 11:21:35 +0530181 res.end();
182}
183
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500184void deleteConfigFiles(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530185{
186 std::vector<std::string> pathObjList;
187 std::error_code ec;
188 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
189 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
190 {
191 std::filesystem::remove_all(loc, ec);
192 if (ec)
193 {
194 res.result(boost::beast::http::status::internal_server_error);
195 res.jsonValue["Description"] = internalServerError;
196 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
197 "config files directory. ec : "
198 << ec;
199 }
200 }
201 res.end();
asmithakarun1c7b07c2019-09-09 03:42:59 -0500202}
203
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500204void getLockServiceData(crow::Response& res)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530205{
206 res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
207 res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
208 res.jsonValue["Id"] = "LockService";
209 res.jsonValue["Name"] = "LockService";
210
211 res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
212 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
213 res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
214 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
215 res.jsonValue["Actions"]["#LockService.GetLockList"] = {
216 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
217 res.end();
218}
219
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500220void handleFileGet(crow::Response& res, const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500221{
222 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
223 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" +
224 fileID);
225 if (!std::filesystem::exists(loc))
226 {
227 BMCWEB_LOG_ERROR << loc << "Not found";
228 res.result(boost::beast::http::status::not_found);
229 res.jsonValue["Description"] = resourceNotFoundMsg;
230 return;
231 }
232
233 std::ifstream readfile(loc.string());
234 if (!readfile)
235 {
236 BMCWEB_LOG_ERROR << loc.string() << "Not found";
237 res.result(boost::beast::http::status::not_found);
238 res.jsonValue["Description"] = resourceNotFoundMsg;
239 return;
240 }
241
242 std::string contentDispositionParam =
243 "attachment; filename=\"" + fileID + "\"";
244 res.addHeader("Content-Disposition", contentDispositionParam);
245 std::string fileData;
246 fileData = {std::istreambuf_iterator<char>(readfile),
247 std::istreambuf_iterator<char>()};
248 res.jsonValue["Data"] = fileData;
249 return;
250}
251
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252void handleFileDelete(crow::Response& res, const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500253{
254 std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID);
255 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
256
257 std::ifstream file_open(filePath.c_str());
258 if (static_cast<bool>(file_open))
259 if (remove(filePath.c_str()) == 0)
260 {
261 BMCWEB_LOG_DEBUG << "File removed!\n";
262 res.jsonValue["Description"] = "File Deleted";
263 }
264 else
265 {
266 BMCWEB_LOG_ERROR << "File not removed!\n";
267 res.result(boost::beast::http::status::internal_server_error);
268 res.jsonValue["Description"] = internalServerError;
269 }
270 else
271 {
272 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harish97b0e432019-11-21 04:59:29 -0600273 res.result(boost::beast::http::status::not_found);
274 res.jsonValue["Description"] = resourceNotFoundMsg;
275 }
276 return;
277}
278
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500279inline void handleBroadcastService(const crow::Request& req,
280 crow::Response& res)
281{
282 std::string broadcastMsg;
283
284 if (!redfish::json_util::readJson(req, res, "Message", broadcastMsg))
285 {
286 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
287 res.result(boost::beast::http::status::bad_request);
288 return;
289 }
290 if (broadcastMsg.size() > maxBroadcastMsgSize)
291 {
292 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
293 res.result(boost::beast::http::status::bad_request);
294 return;
295 }
296 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
297 res.end();
298 return;
299}
300
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500301inline void handleFileUrl(const crow::Request& req, crow::Response& res,
302 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600303{
Ed Tanousb41187f2019-10-24 16:30:02 -0700304 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600305 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500306 handleFilePut(req, res, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600307 res.end();
308 return;
309 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700310 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500311 {
312 handleFileGet(res, fileID);
313 res.end();
314 return;
315 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700316 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500317 {
318 handleFileDelete(res, fileID);
319 res.end();
320 return;
321 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600322}
Ratan Gupta453fed02019-12-14 09:39:47 +0530323
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500324void handleAcquireLockAPI(const crow::Request& req, crow::Response& res,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530325 std::vector<nlohmann::json> body)
326{
327 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500328 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530329 {
330 std::string lockType;
331 uint64_t resourceId;
332
333 SegmentFlags segInfo;
334 std::vector<nlohmann::json> segmentFlags;
335
336 if (!redfish::json_util::readJson(element, res, "LockType", lockType,
337 "ResourceID", resourceId,
338 "SegmentFlags", segmentFlags))
339 {
340 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
341 res.result(boost::beast::http::status::bad_request);
342 res.end();
343 return;
344 }
345 BMCWEB_LOG_DEBUG << lockType;
346 BMCWEB_LOG_DEBUG << resourceId;
347
348 BMCWEB_LOG_DEBUG << "Segment Flags are present";
349
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500350 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530351 {
352 std::string lockFlags;
353 uint32_t segmentLength;
354
355 if (!redfish::json_util::readJson(e, res, "LockFlag", lockFlags,
356 "SegmentLength", segmentLength))
357 {
358 res.result(boost::beast::http::status::bad_request);
359 res.end();
360 return;
361 }
362
363 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
364 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
365
366 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
367 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530368 lockRequestStructure.push_back(
369 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
370 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530371 }
372
373 // print lock request into journal
374
375 for (uint32_t i = 0; i < lockRequestStructure.size(); i++)
376 {
377 BMCWEB_LOG_DEBUG << std::get<0>(lockRequestStructure[i]);
378 BMCWEB_LOG_DEBUG << std::get<1>(lockRequestStructure[i]);
379 BMCWEB_LOG_DEBUG << std::get<2>(lockRequestStructure[i]);
380 BMCWEB_LOG_DEBUG << std::get<3>(lockRequestStructure[i]);
381
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500382 for (const auto& p : std::get<4>(lockRequestStructure[i]))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530383 {
384 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
385 }
386 }
387
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500388 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530389
Ratan Gupta07386c62019-12-14 14:06:09 +0530390 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530391
392 if (varAcquireLock.first)
393 {
394 // Either validity failure of there is a conflict with itself
395
396 auto validityStatus =
397 std::get<std::pair<bool, int>>(varAcquireLock.second);
398
399 if ((!validityStatus.first) && (validityStatus.second == 0))
400 {
401 BMCWEB_LOG_DEBUG << "Not a Valid record";
402 BMCWEB_LOG_DEBUG << "Bad json in request";
403 res.result(boost::beast::http::status::bad_request);
404 res.end();
405 return;
406 }
407 if (validityStatus.first && (validityStatus.second == 1))
408 {
409 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
410 res.result(boost::beast::http::status::bad_request);
411 res.end();
412 return;
413 }
414 }
415 else
416 {
417 auto conflictStatus =
418 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
419 if (!conflictStatus.first)
420 {
421 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
422 res.result(boost::beast::http::status::ok);
423
424 auto var = std::get<uint32_t>(conflictStatus.second);
425 nlohmann::json returnJson;
426 returnJson["id"] = var;
427 res.jsonValue["TransactionID"] = var;
428 res.end();
429 return;
430 }
431 else
432 {
433 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
434 res.result(boost::beast::http::status::conflict);
435 auto var = std::get<std::pair<uint32_t, LockRequest>>(
436 conflictStatus.second);
437 nlohmann::json returnJson, segments;
438 nlohmann::json myarray = nlohmann::json::array();
439 returnJson["TransactionID"] = var.first;
440 returnJson["SessionID"] = std::get<0>(var.second);
441 returnJson["HMCID"] = std::get<1>(var.second);
442 returnJson["LockType"] = std::get<2>(var.second);
443 returnJson["ResourceID"] = std::get<3>(var.second);
444
445 for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++)
446 {
447 segments["LockFlag"] = std::get<4>(var.second)[i].first;
448 segments["SegmentLength"] = std::get<4>(var.second)[i].second;
449 myarray.push_back(segments);
450 }
451
452 returnJson["SegmentFlags"] = myarray;
453
454 res.jsonValue["Record"] = returnJson;
455 res.end();
456 return;
457 }
458 }
459}
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500460void handleRelaseAllAPI(const crow::Request& req, crow::Response& res)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530461{
462 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
463 res.result(boost::beast::http::status::ok);
464 res.end();
465 return;
466}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530467
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500468void handleReleaseLockAPI(const crow::Request& req, crow::Response& res,
469 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530470{
471 BMCWEB_LOG_DEBUG << listTransactionIds.size();
472 BMCWEB_LOG_DEBUG << "Data is present";
473 for (uint32_t i = 0; i < listTransactionIds.size(); i++)
474 {
475 BMCWEB_LOG_DEBUG << listTransactionIds[i];
476 }
477
manojkiraneda3b6dea62019-12-13 17:05:36 +0530478 // validate the request ids
479
Ratan Gupta07386c62019-12-14 14:06:09 +0530480 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530481 listTransactionIds,
482 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530483
484 if (!varReleaselock.first)
485 {
486 // validation Failed
487 res.result(boost::beast::http::status::bad_request);
488 res.end();
489 return;
490 }
491 else
492 {
493 auto statusRelease =
494 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
495 if (statusRelease.first)
496 {
497 // The current hmc owns all the locks, so we already released
498 // them
499 res.result(boost::beast::http::status::ok);
500 res.end();
501 return;
502 }
503
504 else
505 {
506 // valid rid, but the current hmc does not own all the locks
507 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
508 res.result(boost::beast::http::status::unauthorized);
509
510 auto var = statusRelease.second;
511 nlohmann::json returnJson, segments;
512 nlohmann::json myArray = nlohmann::json::array();
513 returnJson["TransactionID"] = var.first;
514 returnJson["SessionID"] = std::get<0>(var.second);
515 returnJson["HMCID"] = std::get<1>(var.second);
516 returnJson["LockType"] = std::get<2>(var.second);
517 returnJson["ResourceID"] = std::get<3>(var.second);
518
519 for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++)
520 {
521 segments["LockFlag"] = std::get<4>(var.second)[i].first;
522 segments["SegmentLength"] = std::get<4>(var.second)[i].second;
523 myArray.push_back(segments);
524 }
525
526 returnJson["SegmentFlags"] = myArray;
527 res.jsonValue["Record"] = returnJson;
528 res.end();
529 return;
530 }
531 }
532}
533
Ed Tanouscb13a392020-07-25 19:02:03 +0000534void handleGetLockListAPI(crow::Response& res,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500535 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530536{
537 BMCWEB_LOG_DEBUG << listSessionIds.size();
538
Ratan Gupta07386c62019-12-14 14:06:09 +0530539 auto status =
540 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530541 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
542
543 nlohmann::json lockRecords = nlohmann::json::array();
544
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500545 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530546 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500547 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530548 {
549 nlohmann::json returnJson;
550
551 returnJson["TransactionID"] = transactionId.first;
552 returnJson["SessionID"] = std::get<0>(lockRecord);
553 returnJson["HMCID"] = std::get<1>(lockRecord);
554 returnJson["LockType"] = std::get<2>(lockRecord);
555 returnJson["ResourceID"] = std::get<3>(lockRecord);
556
557 nlohmann::json segments;
558 nlohmann::json segmentInfoArray = nlohmann::json::array();
559
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500560 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530561 {
562 segments["LockFlag"] = segment.first;
563 segments["SegmentLength"] = segment.second;
564 segmentInfoArray.push_back(segments);
565 }
566
567 returnJson["SegmentFlags"] = segmentInfoArray;
568 lockRecords.push_back(returnJson);
569 }
570 }
571 res.result(boost::beast::http::status::ok);
572 res.jsonValue["Records"] = lockRecords;
573 res.end();
574}
575
Ed Tanous52cc1122020-07-18 13:51:21 -0700576void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530577{
578
579 // allowed only for admin
580 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous23a21a12020-07-25 04:45:05 +0000581 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700582 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000583 [](const crow::Request&, crow::Response& res) {
Ratan Gupta453fed02019-12-14 09:39:47 +0530584 res.jsonValue["@odata.type"] =
585 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
586 res.jsonValue["@odata.id"] = "/ibm/v1/";
587 res.jsonValue["Id"] = "IBM Rest RootService";
588 res.jsonValue["Name"] = "IBM Service Root";
589 res.jsonValue["ConfigFiles"] = {
590 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
591 res.jsonValue["LockService"] = {
592 {"@odata.id", "/ibm/v1/HMC/LockService"}};
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500593 res.jsonValue["BroadcastService"] = {
594 {"@odata.id", "/ibm/v1/HMC/BroadcastService"}};
Ratan Gupta453fed02019-12-14 09:39:47 +0530595 res.end();
596 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600597
Ratan Guptad3630cb2019-12-14 11:21:35 +0530598 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous23a21a12020-07-25 04:45:05 +0000599 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700600 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000601 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530602 handleConfigFileList(res);
603 });
604
605 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500606 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous23a21a12020-07-25 04:45:05 +0000607 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700608 .methods(boost::beast::http::verb::post)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000609 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530610 deleteConfigFiles(res);
611 });
612
asmithakarun1c7b07c2019-09-09 03:42:59 -0500613 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<path>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000614 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700615 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
616 boost::beast::http::verb::delete_)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500617 [](const crow::Request& req, crow::Response& res,
618 const std::string& path) { handleFileUrl(req, res, path); });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530619
620 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000621 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700622 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000623 [](const crow::Request&, crow::Response& res) {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530624 getLockServiceData(res);
625 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530626
627 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000628 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700629 .methods(boost::beast::http::verb::post)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500630 [](const crow::Request& req, crow::Response& res) {
manojkiraneda0b631ae2019-12-03 17:54:28 +0530631 std::vector<nlohmann::json> body;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530632 if (!redfish::json_util::readJson(req, res, "Request", body))
633 {
634 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
635 res.result(boost::beast::http::status::bad_request);
636 res.end();
637 return;
638 }
639 handleAcquireLockAPI(req, res, body);
640 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530641 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000642 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700643 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
644 crow::Response& res) {
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530645 std::string type;
646 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530647
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530648 if (!redfish::json_util::readJson(req, res, "Type", type,
649 "TransactionIDs",
650 listTransactionIds))
651 {
652 res.result(boost::beast::http::status::bad_request);
653 res.end();
654 return;
655 }
656 if (type == "Transaction")
657 {
manojkiraneda3b6dea62019-12-13 17:05:36 +0530658 handleReleaseLockAPI(req, res, listTransactionIds);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530659 }
660 else if (type == "Session")
661 {
662 handleRelaseAllAPI(req, res);
663 }
664 else
665 {
666 BMCWEB_LOG_DEBUG << " Value of Type : " << type
667 << "is Not a Valid key";
668 redfish::messages::propertyValueNotInList(res, type, "Type");
669 }
670 });
manojkiraneda402b5712019-12-13 17:07:09 +0530671 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous23a21a12020-07-25 04:45:05 +0000672 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700673 .methods(boost::beast::http::verb::post)(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500674 [](const crow::Request& req, crow::Response& res) {
manojkiraneda402b5712019-12-13 17:07:09 +0530675 ListOfSessionIds listSessionIds;
676
677 if (!redfish::json_util::readJson(req, res, "SessionIDs",
678 listSessionIds))
679 {
680 res.result(boost::beast::http::status::bad_request);
681 res.end();
682 return;
683 }
Ed Tanouscb13a392020-07-25 19:02:03 +0000684 handleGetLockListAPI(res, listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530685 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500686
687 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000688 .privileges({"ConfigureComponents", "ConfigureManager"})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500689 .methods(boost::beast::http::verb::post)(
690 [](const crow::Request& req, crow::Response& res) {
691 handleBroadcastService(req, res);
692 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530693}
694
695} // namespace ibm_mc
696} // namespace crow