blob: 0d8e2878024e777467729f96c0b75bba685a271e [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>
Ed Tanous11ba3972022-07-11 09:50:41 -07005#include <boost/algorithm/string/predicate.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -06006#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;
Ed Tanous9eb808c2022-01-25 10:19:23 -080088 for (const auto& it : iter)
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050089 {
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;
Ed Tanous8cc8ede2022-02-28 10:20:59 -0800139 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc.string();
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500140
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"}};
Ratan Guptad3630cb2019-12-14 11:21:35 +0530260}
261
zhanghch058d1b46d2021-04-01 11:18:24 +0800262inline void
263 deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530264{
Ratan Guptad3630cb2019-12-14 11:21:35 +0530265 std::error_code ec;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500266 std::filesystem::path loc(
267 "/var/lib/bmcweb/ibm-management-console/configfiles");
Ratan Guptad3630cb2019-12-14 11:21:35 +0530268 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
269 {
270 std::filesystem::remove_all(loc, ec);
271 if (ec)
272 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600273 asyncResp->res.result(
274 boost::beast::http::status::internal_server_error);
275 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530276 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
277 "config files directory. ec : "
278 << ec;
279 }
280 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500281}
282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283inline void
284 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530285{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600286 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
287 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
288 asyncResp->res.jsonValue["Id"] = "LockService";
289 asyncResp->res.jsonValue["Name"] = "LockService";
Ratan Gupta734a1c32019-12-14 11:53:48 +0530290
Sunitha Harishdb81c072021-01-21 23:33:21 -0600291 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530292 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600293 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530294 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600295 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530296 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
Ratan Gupta734a1c32019-12-14 11:53:48 +0530297}
298
Sunitha Harishdb81c072021-01-21 23:33:21 -0600299inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
300 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500301{
302 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500303 std::filesystem::path loc(
304 "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500305 if (!std::filesystem::exists(loc))
306 {
Ed Tanous8cc8ede2022-02-28 10:20:59 -0800307 BMCWEB_LOG_ERROR << loc.string() << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600308 asyncResp->res.result(boost::beast::http::status::not_found);
309 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500310 return;
311 }
312
313 std::ifstream readfile(loc.string());
314 if (!readfile)
315 {
316 BMCWEB_LOG_ERROR << loc.string() << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600317 asyncResp->res.result(boost::beast::http::status::not_found);
318 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500319 return;
320 }
321
322 std::string contentDispositionParam =
323 "attachment; filename=\"" + fileID + "\"";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600324 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500325 std::string fileData;
326 fileData = {std::istreambuf_iterator<char>(readfile),
327 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600328 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500329}
330
Sunitha Harishdb81c072021-01-21 23:33:21 -0600331inline void
332 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
333 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500334{
Sunitha Harish3e919b52020-10-13 01:21:48 -0500335 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" +
336 fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500337 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700338 std::ifstream fileOpen(filePath.c_str());
339 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700340 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500341 if (remove(filePath.c_str()) == 0)
342 {
343 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600344 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500345 }
346 else
347 {
348 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600349 asyncResp->res.result(
350 boost::beast::http::status::internal_server_error);
351 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500352 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700353 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500354 else
355 {
356 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600357 asyncResp->res.result(boost::beast::http::status::not_found);
358 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600359 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600360}
361
zhanghch058d1b46d2021-04-01 11:18:24 +0800362inline void
363 handleBroadcastService(const crow::Request& req,
364 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500365{
366 std::string broadcastMsg;
367
Willy Tu15ed6782021-12-14 11:03:16 -0800368 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message",
369 broadcastMsg))
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500370 {
371 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600372 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500373 return;
374 }
375 if (broadcastMsg.size() > maxBroadcastMsgSize)
376 {
377 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
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 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500382}
383
zhanghch058d1b46d2021-04-01 11:18:24 +0800384inline void handleFileUrl(const crow::Request& req,
385 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500386 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600387{
Ed Tanousb41187f2019-10-24 16:30:02 -0700388 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600389 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600390 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600391 return;
392 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700393 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500394 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600395 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500396 return;
397 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700398 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500399 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600400 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500401 return;
402 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600403}
Ratan Gupta453fed02019-12-14 09:39:47 +0530404
Sunitha Harishdb81c072021-01-21 23:33:21 -0600405inline void
406 handleAcquireLockAPI(const crow::Request& req,
407 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
408 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530409{
410 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500411 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530412 {
413 std::string lockType;
Ed Tanous543f4402022-01-06 13:12:53 -0800414 uint64_t resourceId = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530415
416 SegmentFlags segInfo;
417 std::vector<nlohmann::json> segmentFlags;
418
Sunitha Harishdb81c072021-01-21 23:33:21 -0600419 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
420 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530421 "SegmentFlags", segmentFlags))
422 {
423 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600424 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530425 return;
426 }
427 BMCWEB_LOG_DEBUG << lockType;
428 BMCWEB_LOG_DEBUG << resourceId;
429
430 BMCWEB_LOG_DEBUG << "Segment Flags are present";
431
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500432 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530433 {
434 std::string lockFlags;
Ed Tanous543f4402022-01-06 13:12:53 -0800435 uint32_t segmentLength = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530436
Sunitha Harishdb81c072021-01-21 23:33:21 -0600437 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
438 lockFlags, "SegmentLength",
439 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530440 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600441 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530442 return;
443 }
444
445 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
446 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
447
448 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
449 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530450 lockRequestStructure.push_back(
451 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
452 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530453 }
454
455 // print lock request into journal
456
Ed Tanous4e087512020-09-28 18:41:25 -0700457 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530458 {
Ed Tanous4e087512020-09-28 18:41:25 -0700459 BMCWEB_LOG_DEBUG << std::get<0>(i);
460 BMCWEB_LOG_DEBUG << std::get<1>(i);
461 BMCWEB_LOG_DEBUG << std::get<2>(i);
462 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530463
Ed Tanous4e087512020-09-28 18:41:25 -0700464 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530465 {
466 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
467 }
468 }
469
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500470 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530471
Ratan Gupta07386c62019-12-14 14:06:09 +0530472 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530473
474 if (varAcquireLock.first)
475 {
476 // Either validity failure of there is a conflict with itself
477
478 auto validityStatus =
479 std::get<std::pair<bool, int>>(varAcquireLock.second);
480
481 if ((!validityStatus.first) && (validityStatus.second == 0))
482 {
483 BMCWEB_LOG_DEBUG << "Not a Valid record";
484 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600485 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530486 return;
487 }
488 if (validityStatus.first && (validityStatus.second == 1))
489 {
490 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600491 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530492 return;
493 }
494 }
495 else
496 {
497 auto conflictStatus =
498 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
499 if (!conflictStatus.first)
500 {
501 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600502 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530503
504 auto var = std::get<uint32_t>(conflictStatus.second);
505 nlohmann::json returnJson;
506 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600507 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530508 return;
509 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700510 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600511 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700512 auto var =
513 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
Ed Tanouse05aec52022-01-25 10:28:56 -0800514 nlohmann::json returnJson;
515 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700516 nlohmann::json myarray = nlohmann::json::array();
517 returnJson["TransactionID"] = var.first;
518 returnJson["SessionID"] = std::get<0>(var.second);
519 returnJson["HMCID"] = std::get<1>(var.second);
520 returnJson["LockType"] = std::get<2>(var.second);
521 returnJson["ResourceID"] = std::get<3>(var.second);
522
Ed Tanous02cad962022-06-30 16:50:15 -0700523 for (const auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530524 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700525 segments["LockFlag"] = i.first;
526 segments["SegmentLength"] = i.second;
527 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530528 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700529
530 returnJson["SegmentFlags"] = myarray;
531
Sunitha Harishdb81c072021-01-21 23:33:21 -0600532 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700533 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530534 }
535}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600536inline void
537 handleRelaseAllAPI(const crow::Request& req,
538 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530539{
540 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600541 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530542}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530543
Ed Tanous02379d32020-09-15 21:15:44 -0700544inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600545 handleReleaseLockAPI(const crow::Request& req,
546 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700547 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530548{
549 BMCWEB_LOG_DEBUG << listTransactionIds.size();
550 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700551 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530552 {
Ed Tanous4e087512020-09-28 18:41:25 -0700553 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530554 }
555
manojkiraneda3b6dea62019-12-13 17:05:36 +0530556 // validate the request ids
557
Ratan Gupta07386c62019-12-14 14:06:09 +0530558 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530559 listTransactionIds,
560 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530561
562 if (!varReleaselock.first)
563 {
564 // validation Failed
Sunitha Harishdb81c072021-01-21 23:33:21 -0600565 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530566 return;
567 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700568 auto statusRelease =
569 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
570 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530571 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700572 // The current hmc owns all the locks, so we already released
573 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700574 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530575 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700576
577 // valid rid, but the current hmc does not own all the locks
578 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600579 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700580
581 auto var = statusRelease.second;
Ed Tanouse05aec52022-01-25 10:28:56 -0800582 nlohmann::json returnJson;
583 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700584 nlohmann::json myArray = nlohmann::json::array();
585 returnJson["TransactionID"] = var.first;
586 returnJson["SessionID"] = std::get<0>(var.second);
587 returnJson["HMCID"] = std::get<1>(var.second);
588 returnJson["LockType"] = std::get<2>(var.second);
589 returnJson["ResourceID"] = std::get<3>(var.second);
590
Ed Tanous02cad962022-06-30 16:50:15 -0700591 for (const auto& i : std::get<4>(var.second))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700592 {
593 segments["LockFlag"] = i.first;
594 segments["SegmentLength"] = i.second;
595 myArray.push_back(segments);
596 }
597
598 returnJson["SegmentFlags"] = myArray;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600599 asyncResp->res.jsonValue["Record"] = returnJson;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530600}
601
Sunitha Harishdb81c072021-01-21 23:33:21 -0600602inline void
603 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
604 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530605{
606 BMCWEB_LOG_DEBUG << listSessionIds.size();
607
Ratan Gupta07386c62019-12-14 14:06:09 +0530608 auto status =
609 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530610 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
611
612 nlohmann::json lockRecords = nlohmann::json::array();
613
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500614 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530615 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500616 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530617 {
618 nlohmann::json returnJson;
619
620 returnJson["TransactionID"] = transactionId.first;
621 returnJson["SessionID"] = std::get<0>(lockRecord);
622 returnJson["HMCID"] = std::get<1>(lockRecord);
623 returnJson["LockType"] = std::get<2>(lockRecord);
624 returnJson["ResourceID"] = std::get<3>(lockRecord);
625
626 nlohmann::json segments;
627 nlohmann::json segmentInfoArray = nlohmann::json::array();
628
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500629 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530630 {
631 segments["LockFlag"] = segment.first;
632 segments["SegmentLength"] = segment.second;
633 segmentInfoArray.push_back(segments);
634 }
635
636 returnJson["SegmentFlags"] = segmentInfoArray;
637 lockRecords.push_back(returnJson);
638 }
639 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600640 asyncResp->res.result(boost::beast::http::status::ok);
641 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530642}
643
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500644inline bool isValidConfigFileName(const std::string& fileName,
645 crow::Response& res)
646{
647 if (fileName.empty())
648 {
649 BMCWEB_LOG_ERROR << "Empty filename";
650 res.jsonValue["Description"] = "Empty file path in the url";
651 return false;
652 }
653
654 // ConfigFile name is allowed to take upper and lowercase letters,
655 // numbers and hyphen
656 std::size_t found = fileName.find_first_not_of(
657 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
658 if (found != std::string::npos)
659 {
660 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
661 res.jsonValue["Description"] = "Unsupported character in filename";
662 return false;
663 }
664
665 // Check the filename length
666 if (fileName.length() > 20)
667 {
668 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
669 "Input filename length is: "
670 << fileName.length();
671 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
672 return false;
673 }
674
675 return true;
676}
677
Ed Tanous02379d32020-09-15 21:15:44 -0700678inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530679{
680
681 // allowed only for admin
682 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous432a8902021-06-14 15:28:56 -0700683 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700684 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800685 [](const crow::Request&,
686 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700687 asyncResp->res.jsonValue["@odata.type"] =
688 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
689 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
690 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
691 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
692 asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] =
693 "/ibm/v1/Host/ConfigFiles";
694 asyncResp->res.jsonValue["LockService"]["@odata.id"] =
695 "/ibm/v1/HMC/LockService";
696 asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] =
697 "/ibm/v1/HMC/BroadcastService";
698 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600699
Ratan Guptad3630cb2019-12-14 11:21:35 +0530700 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous432a8902021-06-14 15:28:56 -0700701 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700702 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800703 [](const crow::Request&,
704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700705 handleConfigFileList(asyncResp);
706 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530707
708 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500709 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous432a8902021-06-14 15:28:56 -0700710 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700711 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800712 [](const crow::Request&,
713 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700714 deleteConfigFiles(asyncResp);
715 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530716
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500717 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700718 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800719 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
720 boost::beast::http::verb::delete_)(
721 [](const crow::Request& req,
722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
723 const std::string& fileName) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700724 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
725 // Validate the incoming fileName
726 if (!isValidConfigFileName(fileName, asyncResp->res))
727 {
728 asyncResp->res.result(boost::beast::http::status::bad_request);
729 return;
730 }
731 handleFileUrl(req, asyncResp, fileName);
732 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530733
734 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous432a8902021-06-14 15:28:56 -0700735 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700736 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800737 [](const crow::Request&,
738 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700739 getLockServiceData(asyncResp);
740 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530741
742 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700743 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800744 .methods(boost::beast::http::verb::post)(
745 [](const crow::Request& req,
746 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700747 std::vector<nlohmann::json> body;
748 if (!redfish::json_util::readJsonAction(req, asyncResp->res, "Request",
749 body))
750 {
751 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
752 asyncResp->res.result(boost::beast::http::status::bad_request);
753 return;
754 }
755 handleAcquireLockAPI(req, asyncResp, body);
756 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530757 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700758 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800759 .methods(boost::beast::http::verb::post)(
760 [](const crow::Request& req,
761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700762 std::string type;
763 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530764
Ed Tanous002d39b2022-05-31 08:59:27 -0700765 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Type",
766 type, "TransactionIDs",
767 listTransactionIds))
768 {
769 asyncResp->res.result(boost::beast::http::status::bad_request);
770 return;
771 }
772 if (type == "Transaction")
773 {
774 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
775 }
776 else if (type == "Session")
777 {
778 handleRelaseAllAPI(req, asyncResp);
779 }
780 else
781 {
782 BMCWEB_LOG_DEBUG << " Value of Type : " << type
783 << "is Not a Valid key";
784 redfish::messages::propertyValueNotInList(asyncResp->res, type,
785 "Type");
786 }
787 });
manojkiraneda402b5712019-12-13 17:07:09 +0530788 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous432a8902021-06-14 15:28:56 -0700789 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800790 .methods(boost::beast::http::verb::post)(
791 [](const crow::Request& req,
792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700793 ListOfSessionIds listSessionIds;
manojkiraneda402b5712019-12-13 17:07:09 +0530794
Ed Tanous002d39b2022-05-31 08:59:27 -0700795 if (!redfish::json_util::readJsonPatch(req, asyncResp->res,
796 "SessionIDs", listSessionIds))
797 {
798 asyncResp->res.result(boost::beast::http::status::bad_request);
799 return;
800 }
801 handleGetLockListAPI(asyncResp, listSessionIds);
802 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500803
804 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous432a8902021-06-14 15:28:56 -0700805 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500806 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800807 [](const crow::Request& req,
808 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700809 handleBroadcastService(req, asyncResp);
810 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530811}
812
813} // namespace ibm_mc
814} // namespace crow