blob: 59d4111a77fa7ebbe1058739337204fbacf960f1 [file] [log] [blame]
Ratan Gupta453fed02019-12-14 09:39:47 +05301#pragma once
Ratan Gupta453fed02019-12-14 09:39:47 +05302
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include <app.hpp>
Ratan Gupta453fed02019-12-14 09:39:47 +05304#include <async_resp.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -06005#include <boost/algorithm/string.hpp>
6#include <boost/container/flat_set.hpp>
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +05307#include <error_messages.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -05008#include <event_service_manager.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +05309#include <ibm/locks.hpp>
10#include <nlohmann/json.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -050011#include <resource_messages.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060012#include <sdbusplus/message/types.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053013#include <utils/json_utils.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060014
Gunnar Mills1214b7e2020-06-04 10:11:30 -050015#include <filesystem>
16#include <fstream>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017
manojkiraneda0b631ae2019-12-03 17:54:28 +053018using SType = std::string;
19using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>;
20using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
21using LockRequests = std::vector<LockRequest>;
22using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053023using RcGetLockList =
24 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
25using ListOfSessionIds = std::vector<std::string>;
Ratan Gupta453fed02019-12-14 09:39:47 +053026namespace crow
27{
28namespace ibm_mc
29{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030constexpr const char* methodNotAllowedMsg = "Method Not Allowed";
31constexpr const char* resourceNotFoundMsg = "Resource Not Found";
32constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable";
33constexpr const char* internalServerError = "Internal Server Error";
Sunitha Harish97b0e432019-11-21 04:59:29 -060034
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050035constexpr size_t maxSaveareaDirSize =
36 10000000; // Allow save area dir size to be max 10MB
37constexpr size_t minSaveareaFileSize =
38 100; // Allow save area file size of minimum 100B
Asmitha Karunanithi5738de52020-07-17 02:03:31 -050039constexpr size_t maxSaveareaFileSize =
40 500000; // Allow save area file size upto 500KB
41constexpr size_t maxBroadcastMsgSize =
42 1000; // Allow Broadcast message size upto 1KB
43
Sunitha Harishdb81c072021-01-21 23:33:21 -060044inline void handleFilePut(const crow::Request& req,
45 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -070046 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060047{
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050048 std::error_code ec;
Sunitha Harish97b0e432019-11-21 04:59:29 -060049 // Check the content-type of the request
Sunitha Harish086d32c2021-02-01 02:11:49 -060050 boost::beast::string_view contentType = req.getHeaderValue("content-type");
51 if (!boost::iequals(contentType, "application/octet-stream"))
Sunitha Harish97b0e432019-11-21 04:59:29 -060052 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060053 asyncResp->res.result(boost::beast::http::status::not_acceptable);
54 asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -060055 return;
56 }
Sunitha Harish086d32c2021-02-01 02:11:49 -060057 BMCWEB_LOG_DEBUG
58 << "File upload in application/octet-stream format. Continue..";
asmithakarun1c7b07c2019-09-09 03:42:59 -050059
60 BMCWEB_LOG_DEBUG
61 << "handleIbmPut: Request to create/update the save-area file";
Sunitha Harish3e919b52020-10-13 01:21:48 -050062 std::string_view path =
63 "/var/lib/bmcweb/ibm-management-console/configfiles";
64 if (!crow::ibm_utils::createDirectory(path))
Sunitha Harish97b0e432019-11-21 04:59:29 -060065 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060066 asyncResp->res.result(boost::beast::http::status::not_found);
67 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -050068 return;
69 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050070
asmithakarun1c7b07c2019-09-09 03:42:59 -050071 std::ofstream file;
Sunitha Harish3e919b52020-10-13 01:21:48 -050072 std::filesystem::path loc(
73 "/var/lib/bmcweb/ibm-management-console/configfiles");
Sunitha Harish97b0e432019-11-21 04:59:29 -060074
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050075 // Get the current size of the savearea directory
76 std::filesystem::recursive_directory_iterator iter(loc, ec);
77 if (ec)
78 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060079 asyncResp->res.result(
80 boost::beast::http::status::internal_server_error);
81 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050082 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
83 "directory iterator. ec : "
84 << ec;
85 return;
86 }
87 std::uintmax_t saveAreaDirSize = 0;
88 for (auto& it : iter)
89 {
90 if (!std::filesystem::is_directory(it, ec))
91 {
92 if (ec)
93 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060094 asyncResp->res.result(
95 boost::beast::http::status::internal_server_error);
96 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050097 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
98 "directory . ec : "
99 << ec;
100 return;
101 }
102 std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
103 if (ec)
104 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600105 asyncResp->res.result(
106 boost::beast::http::status::internal_server_error);
107 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500108 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
109 "file size inside the directory . ec : "
110 << ec;
111 return;
112 }
113 saveAreaDirSize += fileSize;
114 }
115 }
116 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
117
118 // Get the file size getting uploaded
Ed Tanous3174e4d2020-10-07 11:41:22 -0700119 const std::string& data = req.body;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500120 BMCWEB_LOG_DEBUG << "data length: " << data.length();
121
122 if (data.length() < minSaveareaFileSize)
123 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600124 asyncResp->res.result(boost::beast::http::status::bad_request);
125 asyncResp->res.jsonValue["Description"] =
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500126 "File size is less than minimum allowed size[100B]";
127 return;
128 }
129 if (data.length() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500130 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600131 asyncResp->res.result(boost::beast::http::status::bad_request);
132 asyncResp->res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530133 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500134 return;
135 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500136
137 // Form the file path
138 loc /= fileID;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500139 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc;
140
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500141 // Check if the same file exists in the directory
142 bool fileExists = std::filesystem::exists(loc, ec);
143 if (ec)
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500144 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600145 asyncResp->res.result(
146 boost::beast::http::status::internal_server_error);
147 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500148 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
149 << ec;
150 return;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500151 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500152
153 std::uintmax_t newSizeToWrite = 0;
154 if (fileExists)
155 {
156 // File exists. Get the current file size
157 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec);
158 if (ec)
159 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600160 asyncResp->res.result(
161 boost::beast::http::status::internal_server_error);
162 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500163 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
164 << ec;
165 return;
166 }
167 // Calculate the difference in the file size.
168 // If the data.length is greater than the existing file size, then
169 // calculate the difference. Else consider the delta size as zero -
170 // because there is no increase in the total directory size.
171 // We need to add the diff only if the incoming data is larger than the
172 // existing filesize
173 if (data.length() > currentFileSize)
174 {
175 newSizeToWrite = data.length() - currentFileSize;
176 }
177 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
178 }
179 else
180 {
181 // This is a new file upload
182 newSizeToWrite = data.length();
183 }
184
185 // Calculate the total dir size before writing the new file
186 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
187
188 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
189 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600190 asyncResp->res.result(boost::beast::http::status::bad_request);
191 asyncResp->res.jsonValue["Description"] =
192 "File size does not fit in the savearea "
193 "directory maximum allowed size[10MB]";
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500194 return;
195 }
196
asmithakarun1c7b07c2019-09-09 03:42:59 -0500197 file.open(loc, std::ofstream::out);
Sunitha Harish3e919b52020-10-13 01:21:48 -0500198
199 // set the permission of the file to 600
200 std::filesystem::perms permission = std::filesystem::perms::owner_write |
201 std::filesystem::perms::owner_read;
202 std::filesystem::permissions(loc, permission);
203
asmithakarun1c7b07c2019-09-09 03:42:59 -0500204 if (file.fail())
205 {
206 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600207 asyncResp->res.result(
208 boost::beast::http::status::internal_server_error);
209 asyncResp->res.jsonValue["Description"] =
210 "Error while creating the file";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500211 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600212 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700213 file << data;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500214
Ed Tanous3174e4d2020-10-07 11:41:22 -0700215 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
216 // Push an event
217 if (fileExists)
218 {
219 BMCWEB_LOG_DEBUG << "config file is updated";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600220 asyncResp->res.jsonValue["Description"] = "File Updated";
Ed Tanous3174e4d2020-10-07 11:41:22 -0700221
222 redfish::EventServiceManager::getInstance().sendEvent(
223 redfish::messages::resourceChanged(), origin, "IBMConfigFile");
224 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600225 else
226 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700227 BMCWEB_LOG_DEBUG << "config file is created";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600228 asyncResp->res.jsonValue["Description"] = "File Created";
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500229
Ed Tanous3174e4d2020-10-07 11:41:22 -0700230 redfish::EventServiceManager::getInstance().sendEvent(
231 redfish::messages::resourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500232 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530233}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500234
zhanghch058d1b46d2021-04-01 11:18:24 +0800235inline void
236 handleConfigFileList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530237{
238 std::vector<std::string> pathObjList;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500239 std::filesystem::path loc(
240 "/var/lib/bmcweb/ibm-management-console/configfiles");
Ratan Guptad3630cb2019-12-14 11:21:35 +0530241 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
242 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500243 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530244 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700245 const std::filesystem::path& pathObj = file.path();
Ratan Guptad3630cb2019-12-14 11:21:35 +0530246 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
247 pathObj.filename().string());
248 }
249 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600250 asyncResp->res.jsonValue["@odata.type"] =
251 "#IBMConfigFile.v1_0_0.IBMConfigFile";
252 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
253 asyncResp->res.jsonValue["Id"] = "ConfigFiles";
254 asyncResp->res.jsonValue["Name"] = "ConfigFiles";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530255
Sunitha Harishdb81c072021-01-21 23:33:21 -0600256 asyncResp->res.jsonValue["Members"] = std::move(pathObjList);
257 asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530258 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500259 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600260 return;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530261}
262
zhanghch058d1b46d2021-04-01 11:18:24 +0800263inline void
264 deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530265{
266 std::vector<std::string> pathObjList;
267 std::error_code ec;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500268 std::filesystem::path loc(
269 "/var/lib/bmcweb/ibm-management-console/configfiles");
Ratan Guptad3630cb2019-12-14 11:21:35 +0530270 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
271 {
272 std::filesystem::remove_all(loc, ec);
273 if (ec)
274 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600275 asyncResp->res.result(
276 boost::beast::http::status::internal_server_error);
277 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530278 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
279 "config files directory. ec : "
280 << ec;
281 }
282 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600283 return;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500284}
285
zhanghch058d1b46d2021-04-01 11:18:24 +0800286inline void
287 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530288{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600289 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
290 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
291 asyncResp->res.jsonValue["Id"] = "LockService";
292 asyncResp->res.jsonValue["Name"] = "LockService";
Ratan Gupta734a1c32019-12-14 11:53:48 +0530293
Sunitha Harishdb81c072021-01-21 23:33:21 -0600294 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530295 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600296 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530297 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600298 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530299 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600300 return;
Ratan Gupta734a1c32019-12-14 11:53:48 +0530301}
302
Sunitha Harishdb81c072021-01-21 23:33:21 -0600303inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500305{
306 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500307 std::filesystem::path loc(
308 "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500309 if (!std::filesystem::exists(loc))
310 {
311 BMCWEB_LOG_ERROR << loc << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600312 asyncResp->res.result(boost::beast::http::status::not_found);
313 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500314 return;
315 }
316
317 std::ifstream readfile(loc.string());
318 if (!readfile)
319 {
320 BMCWEB_LOG_ERROR << loc.string() << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600321 asyncResp->res.result(boost::beast::http::status::not_found);
322 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500323 return;
324 }
325
326 std::string contentDispositionParam =
327 "attachment; filename=\"" + fileID + "\"";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600328 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500329 std::string fileData;
330 fileData = {std::istreambuf_iterator<char>(readfile),
331 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600332 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500333 return;
334}
335
Sunitha Harishdb81c072021-01-21 23:33:21 -0600336inline void
337 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
338 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500339{
Sunitha Harish3e919b52020-10-13 01:21:48 -0500340 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" +
341 fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500342 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700343 std::ifstream fileOpen(filePath.c_str());
344 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700345 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500346 if (remove(filePath.c_str()) == 0)
347 {
348 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600349 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500350 }
351 else
352 {
353 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600354 asyncResp->res.result(
355 boost::beast::http::status::internal_server_error);
356 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500357 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700358 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500359 else
360 {
361 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600362 asyncResp->res.result(boost::beast::http::status::not_found);
363 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600364 }
365 return;
366}
367
zhanghch058d1b46d2021-04-01 11:18:24 +0800368inline void
369 handleBroadcastService(const crow::Request& req,
370 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500371{
372 std::string broadcastMsg;
373
zhanghch058d1b46d2021-04-01 11:18:24 +0800374 if (!redfish::json_util::readJson(req, asyncResp->res, "Message",
375 broadcastMsg))
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500376 {
377 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600378 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500379 return;
380 }
381 if (broadcastMsg.size() > maxBroadcastMsgSize)
382 {
383 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600384 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500385 return;
386 }
387 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500388 return;
389}
390
zhanghch058d1b46d2021-04-01 11:18:24 +0800391inline void handleFileUrl(const crow::Request& req,
392 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500393 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600394{
Ed Tanousb41187f2019-10-24 16:30:02 -0700395 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600396 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600397 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600398 return;
399 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700400 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500401 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600402 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500403 return;
404 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700405 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500406 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600407 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500408 return;
409 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600410}
Ratan Gupta453fed02019-12-14 09:39:47 +0530411
Sunitha Harishdb81c072021-01-21 23:33:21 -0600412inline void
413 handleAcquireLockAPI(const crow::Request& req,
414 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
415 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530416{
417 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500418 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530419 {
420 std::string lockType;
421 uint64_t resourceId;
422
423 SegmentFlags segInfo;
424 std::vector<nlohmann::json> segmentFlags;
425
Sunitha Harishdb81c072021-01-21 23:33:21 -0600426 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
427 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530428 "SegmentFlags", segmentFlags))
429 {
430 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600431 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530432 return;
433 }
434 BMCWEB_LOG_DEBUG << lockType;
435 BMCWEB_LOG_DEBUG << resourceId;
436
437 BMCWEB_LOG_DEBUG << "Segment Flags are present";
438
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500439 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530440 {
441 std::string lockFlags;
442 uint32_t segmentLength;
443
Sunitha Harishdb81c072021-01-21 23:33:21 -0600444 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
445 lockFlags, "SegmentLength",
446 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530447 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600448 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530449 return;
450 }
451
452 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
453 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
454
455 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
456 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530457 lockRequestStructure.push_back(
458 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
459 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530460 }
461
462 // print lock request into journal
463
Ed Tanous4e087512020-09-28 18:41:25 -0700464 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530465 {
Ed Tanous4e087512020-09-28 18:41:25 -0700466 BMCWEB_LOG_DEBUG << std::get<0>(i);
467 BMCWEB_LOG_DEBUG << std::get<1>(i);
468 BMCWEB_LOG_DEBUG << std::get<2>(i);
469 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530470
Ed Tanous4e087512020-09-28 18:41:25 -0700471 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530472 {
473 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
474 }
475 }
476
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500477 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530478
Ratan Gupta07386c62019-12-14 14:06:09 +0530479 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530480
481 if (varAcquireLock.first)
482 {
483 // Either validity failure of there is a conflict with itself
484
485 auto validityStatus =
486 std::get<std::pair<bool, int>>(varAcquireLock.second);
487
488 if ((!validityStatus.first) && (validityStatus.second == 0))
489 {
490 BMCWEB_LOG_DEBUG << "Not a Valid record";
491 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600492 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530493 return;
494 }
495 if (validityStatus.first && (validityStatus.second == 1))
496 {
497 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600498 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530499 return;
500 }
501 }
502 else
503 {
504 auto conflictStatus =
505 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
506 if (!conflictStatus.first)
507 {
508 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600509 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530510
511 auto var = std::get<uint32_t>(conflictStatus.second);
512 nlohmann::json returnJson;
513 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600514 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530515 return;
516 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700517 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600518 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700519 auto var =
520 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
521 nlohmann::json returnJson, segments;
522 nlohmann::json myarray = nlohmann::json::array();
523 returnJson["TransactionID"] = var.first;
524 returnJson["SessionID"] = std::get<0>(var.second);
525 returnJson["HMCID"] = std::get<1>(var.second);
526 returnJson["LockType"] = std::get<2>(var.second);
527 returnJson["ResourceID"] = std::get<3>(var.second);
528
529 for (auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530530 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700531 segments["LockFlag"] = i.first;
532 segments["SegmentLength"] = i.second;
533 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530534 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700535
536 returnJson["SegmentFlags"] = myarray;
537
Sunitha Harishdb81c072021-01-21 23:33:21 -0600538 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700539 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530540 }
541}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600542inline void
543 handleRelaseAllAPI(const crow::Request& req,
544 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530545{
546 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600547 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530548 return;
549}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530550
Ed Tanous02379d32020-09-15 21:15:44 -0700551inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600552 handleReleaseLockAPI(const crow::Request& req,
553 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700554 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530555{
556 BMCWEB_LOG_DEBUG << listTransactionIds.size();
557 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700558 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530559 {
Ed Tanous4e087512020-09-28 18:41:25 -0700560 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530561 }
562
manojkiraneda3b6dea62019-12-13 17:05:36 +0530563 // validate the request ids
564
Ratan Gupta07386c62019-12-14 14:06:09 +0530565 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530566 listTransactionIds,
567 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530568
569 if (!varReleaselock.first)
570 {
571 // validation Failed
Sunitha Harishdb81c072021-01-21 23:33:21 -0600572 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530573 return;
574 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700575 auto statusRelease =
576 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
577 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530578 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700579 // The current hmc owns all the locks, so we already released
580 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700581 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530582 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700583
584 // valid rid, but the current hmc does not own all the locks
585 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600586 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700587
588 auto var = statusRelease.second;
589 nlohmann::json returnJson, segments;
590 nlohmann::json myArray = nlohmann::json::array();
591 returnJson["TransactionID"] = var.first;
592 returnJson["SessionID"] = std::get<0>(var.second);
593 returnJson["HMCID"] = std::get<1>(var.second);
594 returnJson["LockType"] = std::get<2>(var.second);
595 returnJson["ResourceID"] = std::get<3>(var.second);
596
597 for (auto& i : std::get<4>(var.second))
598 {
599 segments["LockFlag"] = i.first;
600 segments["SegmentLength"] = i.second;
601 myArray.push_back(segments);
602 }
603
604 returnJson["SegmentFlags"] = myArray;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600605 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700606 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530607}
608
Sunitha Harishdb81c072021-01-21 23:33:21 -0600609inline void
610 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
611 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530612{
613 BMCWEB_LOG_DEBUG << listSessionIds.size();
614
Ratan Gupta07386c62019-12-14 14:06:09 +0530615 auto status =
616 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530617 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
618
619 nlohmann::json lockRecords = nlohmann::json::array();
620
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500621 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530622 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500623 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530624 {
625 nlohmann::json returnJson;
626
627 returnJson["TransactionID"] = transactionId.first;
628 returnJson["SessionID"] = std::get<0>(lockRecord);
629 returnJson["HMCID"] = std::get<1>(lockRecord);
630 returnJson["LockType"] = std::get<2>(lockRecord);
631 returnJson["ResourceID"] = std::get<3>(lockRecord);
632
633 nlohmann::json segments;
634 nlohmann::json segmentInfoArray = nlohmann::json::array();
635
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500636 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530637 {
638 segments["LockFlag"] = segment.first;
639 segments["SegmentLength"] = segment.second;
640 segmentInfoArray.push_back(segments);
641 }
642
643 returnJson["SegmentFlags"] = segmentInfoArray;
644 lockRecords.push_back(returnJson);
645 }
646 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600647 asyncResp->res.result(boost::beast::http::status::ok);
648 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530649}
650
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500651inline bool isValidConfigFileName(const std::string& fileName,
652 crow::Response& res)
653{
654 if (fileName.empty())
655 {
656 BMCWEB_LOG_ERROR << "Empty filename";
657 res.jsonValue["Description"] = "Empty file path in the url";
658 return false;
659 }
660
661 // ConfigFile name is allowed to take upper and lowercase letters,
662 // numbers and hyphen
663 std::size_t found = fileName.find_first_not_of(
664 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
665 if (found != std::string::npos)
666 {
667 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
668 res.jsonValue["Description"] = "Unsupported character in filename";
669 return false;
670 }
671
672 // Check the filename length
673 if (fileName.length() > 20)
674 {
675 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
676 "Input filename length is: "
677 << fileName.length();
678 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
679 return false;
680 }
681
682 return true;
683}
684
Ed Tanous02379d32020-09-15 21:15:44 -0700685inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530686{
687
688 // allowed only for admin
689 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous432a8902021-06-14 15:28:56 -0700690 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700691 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800692 [](const crow::Request&,
693 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600694 asyncResp->res.jsonValue["@odata.type"] =
Ratan Gupta453fed02019-12-14 09:39:47 +0530695 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600696 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
697 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
698 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
699 asyncResp->res.jsonValue["ConfigFiles"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530700 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600701 asyncResp->res.jsonValue["LockService"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530702 {"@odata.id", "/ibm/v1/HMC/LockService"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600703 asyncResp->res.jsonValue["BroadcastService"] = {
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500704 {"@odata.id", "/ibm/v1/HMC/BroadcastService"}};
Ratan Gupta453fed02019-12-14 09:39:47 +0530705 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600706
Ratan Guptad3630cb2019-12-14 11:21:35 +0530707 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous432a8902021-06-14 15:28:56 -0700708 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700709 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800710 [](const crow::Request&,
711 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
712 handleConfigFileList(asyncResp);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530713 });
714
715 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500716 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous432a8902021-06-14 15:28:56 -0700717 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700718 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800719 [](const crow::Request&,
720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
721 deleteConfigFiles(asyncResp);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530722 });
723
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500724 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700725 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800726 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
727 boost::beast::http::verb::delete_)(
728 [](const crow::Request& req,
729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
730 const std::string& fileName) {
731 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
732 // Validate the incoming fileName
733 if (!isValidConfigFileName(fileName, asyncResp->res))
734 {
735 asyncResp->res.result(
736 boost::beast::http::status::bad_request);
737 return;
738 }
739 handleFileUrl(req, asyncResp, fileName);
740 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530741
742 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous432a8902021-06-14 15:28:56 -0700743 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700744 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800745 [](const crow::Request&,
746 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
747 getLockServiceData(asyncResp);
Ratan Gupta734a1c32019-12-14 11:53:48 +0530748 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530749
750 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700751 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800752 .methods(boost::beast::http::verb::post)(
753 [](const crow::Request& req,
754 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
755 std::vector<nlohmann::json> body;
756 if (!redfish::json_util::readJson(req, asyncResp->res,
757 "Request", body))
758 {
759 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
760 asyncResp->res.result(
761 boost::beast::http::status::bad_request);
762 return;
763 }
764 handleAcquireLockAPI(req, asyncResp, body);
765 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530766 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700767 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800768 .methods(boost::beast::http::verb::post)(
769 [](const crow::Request& req,
770 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
771 std::string type;
772 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530773
zhanghch058d1b46d2021-04-01 11:18:24 +0800774 if (!redfish::json_util::readJson(req, asyncResp->res, "Type",
775 type, "TransactionIDs",
776 listTransactionIds))
777 {
778 asyncResp->res.result(
779 boost::beast::http::status::bad_request);
780 return;
781 }
782 if (type == "Transaction")
783 {
784 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
785 }
786 else if (type == "Session")
787 {
788 handleRelaseAllAPI(req, asyncResp);
789 }
790 else
791 {
792 BMCWEB_LOG_DEBUG << " Value of Type : " << type
793 << "is Not a Valid key";
794 redfish::messages::propertyValueNotInList(asyncResp->res,
795 type, "Type");
796 }
797 });
manojkiraneda402b5712019-12-13 17:07:09 +0530798 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous432a8902021-06-14 15:28:56 -0700799 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800800 .methods(boost::beast::http::verb::post)(
801 [](const crow::Request& req,
802 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
803 ListOfSessionIds listSessionIds;
manojkiraneda402b5712019-12-13 17:07:09 +0530804
zhanghch058d1b46d2021-04-01 11:18:24 +0800805 if (!redfish::json_util::readJson(req, asyncResp->res,
806 "SessionIDs", listSessionIds))
807 {
808 asyncResp->res.result(
809 boost::beast::http::status::bad_request);
810 return;
811 }
812 handleGetLockListAPI(asyncResp, listSessionIds);
813 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500814
815 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous432a8902021-06-14 15:28:56 -0700816 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500817 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800818 [](const crow::Request& req,
819 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
820 handleBroadcastService(req, asyncResp);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500821 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530822}
823
824} // namespace ibm_mc
825} // namespace crow