blob: 97ac497aec5b73ff637a0d19fa01256021dc9102 [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 =
Sunitha Harishf8a43472022-08-08 02:07:12 -050036 25000000; // Allow save area dir size to be max 25MB
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050037constexpr 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 "
Sunitha Harishf8a43472022-08-08 02:07:12 -0500193 "directory maximum allowed size[25MB]";
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 {
Sunitha Harishf8a43472022-08-08 02:07:12 -0500307 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 {
Sunitha Harishf8a43472022-08-08 02:07:12 -0500316 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 + "\"";
Ed Tanousd9f6c622022-03-17 09:12:17 -0700324 asyncResp->res.addHeader(boost::beast::http::field::content_disposition,
325 contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500326 std::string fileData;
327 fileData = {std::istreambuf_iterator<char>(readfile),
328 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600329 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500330}
331
Sunitha Harishdb81c072021-01-21 23:33:21 -0600332inline void
333 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500335{
Sunitha Harish3e919b52020-10-13 01:21:48 -0500336 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" +
337 fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500338 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700339 std::ifstream fileOpen(filePath.c_str());
340 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700341 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500342 if (remove(filePath.c_str()) == 0)
343 {
344 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600345 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500346 }
347 else
348 {
349 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600350 asyncResp->res.result(
351 boost::beast::http::status::internal_server_error);
352 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500353 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700354 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500355 else
356 {
357 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600358 asyncResp->res.result(boost::beast::http::status::not_found);
359 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600360 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600361}
362
zhanghch058d1b46d2021-04-01 11:18:24 +0800363inline void
364 handleBroadcastService(const crow::Request& req,
365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500366{
367 std::string broadcastMsg;
368
Willy Tu15ed6782021-12-14 11:03:16 -0800369 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message",
370 broadcastMsg))
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500371 {
372 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600373 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500374 return;
375 }
376 if (broadcastMsg.size() > maxBroadcastMsgSize)
377 {
378 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600379 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500380 return;
381 }
382 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500383}
384
zhanghch058d1b46d2021-04-01 11:18:24 +0800385inline void handleFileUrl(const crow::Request& req,
386 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500387 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600388{
Ed Tanousb41187f2019-10-24 16:30:02 -0700389 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600390 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600391 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600392 return;
393 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700394 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500395 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600396 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500397 return;
398 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700399 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500400 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600401 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500402 return;
403 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600404}
Ratan Gupta453fed02019-12-14 09:39:47 +0530405
Sunitha Harishdb81c072021-01-21 23:33:21 -0600406inline void
407 handleAcquireLockAPI(const crow::Request& req,
408 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
409 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530410{
411 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500412 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530413 {
414 std::string lockType;
Ed Tanous543f4402022-01-06 13:12:53 -0800415 uint64_t resourceId = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530416
417 SegmentFlags segInfo;
418 std::vector<nlohmann::json> segmentFlags;
419
Sunitha Harishdb81c072021-01-21 23:33:21 -0600420 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
421 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530422 "SegmentFlags", segmentFlags))
423 {
424 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600425 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530426 return;
427 }
428 BMCWEB_LOG_DEBUG << lockType;
429 BMCWEB_LOG_DEBUG << resourceId;
430
431 BMCWEB_LOG_DEBUG << "Segment Flags are present";
432
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500433 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530434 {
435 std::string lockFlags;
Ed Tanous543f4402022-01-06 13:12:53 -0800436 uint32_t segmentLength = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530437
Sunitha Harishdb81c072021-01-21 23:33:21 -0600438 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
439 lockFlags, "SegmentLength",
440 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530441 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600442 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530443 return;
444 }
445
446 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
447 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
448
449 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
450 }
Ed Tanousbb759e32022-08-02 17:07:54 -0700451
452 lockRequestStructure.push_back(make_tuple(
453 req.session->uniqueId, req.session->clientId.value_or(""), lockType,
454 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530455 }
456
457 // print lock request into journal
458
Ed Tanous4e087512020-09-28 18:41:25 -0700459 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530460 {
Ed Tanous4e087512020-09-28 18:41:25 -0700461 BMCWEB_LOG_DEBUG << std::get<0>(i);
462 BMCWEB_LOG_DEBUG << std::get<1>(i);
463 BMCWEB_LOG_DEBUG << std::get<2>(i);
464 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530465
Ed Tanous4e087512020-09-28 18:41:25 -0700466 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530467 {
468 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
469 }
470 }
471
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500472 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530473
Ratan Gupta07386c62019-12-14 14:06:09 +0530474 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530475
476 if (varAcquireLock.first)
477 {
478 // Either validity failure of there is a conflict with itself
479
480 auto validityStatus =
481 std::get<std::pair<bool, int>>(varAcquireLock.second);
482
483 if ((!validityStatus.first) && (validityStatus.second == 0))
484 {
485 BMCWEB_LOG_DEBUG << "Not a Valid record";
486 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600487 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530488 return;
489 }
490 if (validityStatus.first && (validityStatus.second == 1))
491 {
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500492 BMCWEB_LOG_ERROR << "There is a conflict within itself";
Sunitha Harishf8a43472022-08-08 02:07:12 -0500493 asyncResp->res.result(boost::beast::http::status::conflict);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530494 return;
495 }
496 }
497 else
498 {
499 auto conflictStatus =
500 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
501 if (!conflictStatus.first)
502 {
503 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600504 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530505
506 auto var = std::get<uint32_t>(conflictStatus.second);
507 nlohmann::json returnJson;
508 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600509 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530510 return;
511 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700512 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600513 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700514 auto var =
515 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
Ed Tanouse05aec52022-01-25 10:28:56 -0800516 nlohmann::json returnJson;
517 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700518 nlohmann::json myarray = nlohmann::json::array();
519 returnJson["TransactionID"] = var.first;
520 returnJson["SessionID"] = std::get<0>(var.second);
521 returnJson["HMCID"] = std::get<1>(var.second);
522 returnJson["LockType"] = std::get<2>(var.second);
523 returnJson["ResourceID"] = std::get<3>(var.second);
524
Ed Tanous02cad962022-06-30 16:50:15 -0700525 for (const auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530526 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700527 segments["LockFlag"] = i.first;
528 segments["SegmentLength"] = i.second;
529 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530530 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700531
532 returnJson["SegmentFlags"] = myarray;
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500533 BMCWEB_LOG_ERROR << "Conflicting lock record: " << returnJson;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600534 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700535 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530536 }
537}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600538inline void
539 handleRelaseAllAPI(const crow::Request& req,
540 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530541{
542 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600543 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530544}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530545
Ed Tanous02379d32020-09-15 21:15:44 -0700546inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600547 handleReleaseLockAPI(const crow::Request& req,
548 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700549 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530550{
551 BMCWEB_LOG_DEBUG << listTransactionIds.size();
552 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700553 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530554 {
Ed Tanous4e087512020-09-28 18:41:25 -0700555 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530556 }
557
manojkiraneda3b6dea62019-12-13 17:05:36 +0530558 // validate the request ids
559
Ratan Gupta07386c62019-12-14 14:06:09 +0530560 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Ed Tanousbb759e32022-08-02 17:07:54 -0700561 listTransactionIds, std::make_pair(req.session->clientId.value_or(""),
562 req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530563
564 if (!varReleaselock.first)
565 {
566 // validation Failed
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500567 BMCWEB_LOG_ERROR << "handleReleaseLockAPI: validation failed";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600568 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530569 return;
570 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700571 auto statusRelease =
572 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
573 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530574 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700575 // The current hmc owns all the locks, so we already released
576 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700577 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530578 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700579
580 // valid rid, but the current hmc does not own all the locks
581 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600582 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700583
584 auto var = statusRelease.second;
Ed Tanouse05aec52022-01-25 10:28:56 -0800585 nlohmann::json returnJson;
586 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700587 nlohmann::json myArray = nlohmann::json::array();
588 returnJson["TransactionID"] = var.first;
589 returnJson["SessionID"] = std::get<0>(var.second);
590 returnJson["HMCID"] = std::get<1>(var.second);
591 returnJson["LockType"] = std::get<2>(var.second);
592 returnJson["ResourceID"] = std::get<3>(var.second);
593
Ed Tanous02cad962022-06-30 16:50:15 -0700594 for (const auto& i : std::get<4>(var.second))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700595 {
596 segments["LockFlag"] = i.first;
597 segments["SegmentLength"] = i.second;
598 myArray.push_back(segments);
599 }
600
601 returnJson["SegmentFlags"] = myArray;
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500602 BMCWEB_LOG_DEBUG << "handleReleaseLockAPI: lockrecord: " << returnJson;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600603 asyncResp->res.jsonValue["Record"] = returnJson;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530604}
605
Sunitha Harishdb81c072021-01-21 23:33:21 -0600606inline void
607 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
608 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530609{
610 BMCWEB_LOG_DEBUG << listSessionIds.size();
611
Ratan Gupta07386c62019-12-14 14:06:09 +0530612 auto status =
613 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530614 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
615
616 nlohmann::json lockRecords = nlohmann::json::array();
617
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500618 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530619 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500620 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530621 {
622 nlohmann::json returnJson;
623
624 returnJson["TransactionID"] = transactionId.first;
625 returnJson["SessionID"] = std::get<0>(lockRecord);
626 returnJson["HMCID"] = std::get<1>(lockRecord);
627 returnJson["LockType"] = std::get<2>(lockRecord);
628 returnJson["ResourceID"] = std::get<3>(lockRecord);
629
630 nlohmann::json segments;
631 nlohmann::json segmentInfoArray = nlohmann::json::array();
632
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500633 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530634 {
635 segments["LockFlag"] = segment.first;
636 segments["SegmentLength"] = segment.second;
637 segmentInfoArray.push_back(segments);
638 }
639
640 returnJson["SegmentFlags"] = segmentInfoArray;
641 lockRecords.push_back(returnJson);
642 }
643 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600644 asyncResp->res.result(boost::beast::http::status::ok);
645 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530646}
647
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500648inline bool isValidConfigFileName(const std::string& fileName,
649 crow::Response& res)
650{
651 if (fileName.empty())
652 {
653 BMCWEB_LOG_ERROR << "Empty filename";
654 res.jsonValue["Description"] = "Empty file path in the url";
655 return false;
656 }
657
658 // ConfigFile name is allowed to take upper and lowercase letters,
659 // numbers and hyphen
660 std::size_t found = fileName.find_first_not_of(
661 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
662 if (found != std::string::npos)
663 {
664 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
665 res.jsonValue["Description"] = "Unsupported character in filename";
666 return false;
667 }
668
669 // Check the filename length
670 if (fileName.length() > 20)
671 {
672 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
673 "Input filename length is: "
674 << fileName.length();
675 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
676 return false;
677 }
678
679 return true;
680}
681
Ed Tanous02379d32020-09-15 21:15:44 -0700682inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530683{
684
685 // allowed only for admin
686 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous432a8902021-06-14 15:28:56 -0700687 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700688 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800689 [](const crow::Request&,
690 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700691 asyncResp->res.jsonValue["@odata.type"] =
692 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
693 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
694 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
695 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
696 asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] =
697 "/ibm/v1/Host/ConfigFiles";
698 asyncResp->res.jsonValue["LockService"]["@odata.id"] =
699 "/ibm/v1/HMC/LockService";
700 asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] =
701 "/ibm/v1/HMC/BroadcastService";
702 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600703
Ratan Guptad3630cb2019-12-14 11:21:35 +0530704 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous432a8902021-06-14 15:28:56 -0700705 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700706 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800707 [](const crow::Request&,
708 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700709 handleConfigFileList(asyncResp);
710 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530711
712 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500713 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous432a8902021-06-14 15:28:56 -0700714 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700715 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800716 [](const crow::Request&,
717 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700718 deleteConfigFiles(asyncResp);
719 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530720
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500721 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700722 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800723 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
724 boost::beast::http::verb::delete_)(
725 [](const crow::Request& req,
726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
727 const std::string& fileName) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700728 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
729 // Validate the incoming fileName
730 if (!isValidConfigFileName(fileName, asyncResp->res))
731 {
732 asyncResp->res.result(boost::beast::http::status::bad_request);
733 return;
734 }
735 handleFileUrl(req, asyncResp, fileName);
736 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530737
738 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous432a8902021-06-14 15:28:56 -0700739 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700740 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800741 [](const crow::Request&,
742 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700743 getLockServiceData(asyncResp);
744 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530745
746 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700747 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800748 .methods(boost::beast::http::verb::post)(
749 [](const crow::Request& req,
750 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700751 std::vector<nlohmann::json> body;
752 if (!redfish::json_util::readJsonAction(req, asyncResp->res, "Request",
753 body))
754 {
755 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
756 asyncResp->res.result(boost::beast::http::status::bad_request);
757 return;
758 }
759 handleAcquireLockAPI(req, asyncResp, body);
760 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530761 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700762 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800763 .methods(boost::beast::http::verb::post)(
764 [](const crow::Request& req,
765 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700766 std::string type;
767 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530768
Ed Tanous002d39b2022-05-31 08:59:27 -0700769 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Type",
770 type, "TransactionIDs",
771 listTransactionIds))
772 {
773 asyncResp->res.result(boost::beast::http::status::bad_request);
774 return;
775 }
776 if (type == "Transaction")
777 {
778 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
779 }
780 else if (type == "Session")
781 {
782 handleRelaseAllAPI(req, asyncResp);
783 }
784 else
785 {
786 BMCWEB_LOG_DEBUG << " Value of Type : " << type
787 << "is Not a Valid key";
788 redfish::messages::propertyValueNotInList(asyncResp->res, type,
789 "Type");
790 }
791 });
manojkiraneda402b5712019-12-13 17:07:09 +0530792 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous432a8902021-06-14 15:28:56 -0700793 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800794 .methods(boost::beast::http::verb::post)(
795 [](const crow::Request& req,
796 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700797 ListOfSessionIds listSessionIds;
manojkiraneda402b5712019-12-13 17:07:09 +0530798
Ed Tanous002d39b2022-05-31 08:59:27 -0700799 if (!redfish::json_util::readJsonPatch(req, asyncResp->res,
800 "SessionIDs", listSessionIds))
801 {
802 asyncResp->res.result(boost::beast::http::status::bad_request);
803 return;
804 }
805 handleGetLockListAPI(asyncResp, listSessionIds);
806 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500807
808 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous432a8902021-06-14 15:28:56 -0700809 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500810 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800811 [](const crow::Request& req,
812 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 handleBroadcastService(req, asyncResp);
814 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530815}
816
817} // namespace ibm_mc
818} // namespace crow