blob: 5e49a9a5f242a60d4bdc28e29e4bdae7455bf72c [file] [log] [blame]
Ratan Gupta453fed02019-12-14 09:39:47 +05301#pragma once
Ratan Gupta453fed02019-12-14 09:39:47 +05302#include <tinyxml2.h>
3
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include <app.hpp>
Ratan Gupta453fed02019-12-14 09:39:47 +05305#include <async_resp.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -06006#include <boost/algorithm/string.hpp>
7#include <boost/container/flat_set.hpp>
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +05308#include <error_messages.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -05009#include <event_service_manager.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053010#include <ibm/locks.hpp>
11#include <nlohmann/json.hpp>
Sunitha Harish96330b92020-06-26 05:42:14 -050012#include <resource_messages.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060013#include <sdbusplus/message/types.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053014#include <utils/json_utils.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060015
Gunnar Mills1214b7e2020-06-04 10:11:30 -050016#include <filesystem>
17#include <fstream>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050018
manojkiraneda0b631ae2019-12-03 17:54:28 +053019using SType = std::string;
20using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>;
21using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
22using LockRequests = std::vector<LockRequest>;
23using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053024using RcGetLockList =
25 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
26using ListOfSessionIds = std::vector<std::string>;
Ratan Gupta453fed02019-12-14 09:39:47 +053027namespace crow
28{
29namespace ibm_mc
30{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031constexpr const char* methodNotAllowedMsg = "Method Not Allowed";
32constexpr const char* resourceNotFoundMsg = "Resource Not Found";
33constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable";
34constexpr const char* internalServerError = "Internal Server Error";
Sunitha Harish97b0e432019-11-21 04:59:29 -060035
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050036constexpr size_t maxSaveareaDirSize =
37 10000000; // Allow save area dir size to be max 10MB
38constexpr size_t minSaveareaFileSize =
39 100; // Allow save area file size of minimum 100B
Asmitha Karunanithi5738de52020-07-17 02:03:31 -050040constexpr size_t maxSaveareaFileSize =
41 500000; // Allow save area file size upto 500KB
42constexpr size_t maxBroadcastMsgSize =
43 1000; // Allow Broadcast message size upto 1KB
44
zhanghch058d1b46d2021-04-01 11:18:24 +080045inline bool
46 createSaveAreaPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Sunitha Harish97b0e432019-11-21 04:59:29 -060047{
48 // The path /var/lib/obmc will be created by initrdscripts
49 // Create the directories for the save-area files, when we get
50 // first file upload request
51 std::error_code ec;
52 if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec))
53 {
54 std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec);
55 }
56 if (ec)
57 {
zhanghch058d1b46d2021-04-01 11:18:24 +080058 asyncResp->res.result(
59 boost::beast::http::status::internal_server_error);
60 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish97b0e432019-11-21 04:59:29 -060061 BMCWEB_LOG_DEBUG
62 << "handleIbmPost: Failed to prepare save-area directory. ec : "
63 << ec;
64 return false;
65 }
66
67 if (!std::filesystem::is_directory(
68 "/var/lib/obmc/bmc-console-mgmt/save-area", ec))
69 {
70 std::filesystem::create_directory(
71 "/var/lib/obmc/bmc-console-mgmt/save-area", ec);
72 }
73 if (ec)
74 {
zhanghch058d1b46d2021-04-01 11:18:24 +080075 asyncResp->res.result(
76 boost::beast::http::status::internal_server_error);
77 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish97b0e432019-11-21 04:59:29 -060078 BMCWEB_LOG_DEBUG
79 << "handleIbmPost: Failed to prepare save-area directory. ec : "
80 << ec;
81 return false;
82 }
83 return true;
84}
Ed Tanous02379d32020-09-15 21:15:44 -070085
Sunitha Harishdb81c072021-01-21 23:33:21 -060086inline void handleFilePut(const crow::Request& req,
87 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -070088 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060089{
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050090 std::error_code ec;
Sunitha Harish97b0e432019-11-21 04:59:29 -060091 // Check the content-type of the request
Sunitha Harish086d32c2021-02-01 02:11:49 -060092 boost::beast::string_view contentType = req.getHeaderValue("content-type");
93 if (!boost::iequals(contentType, "application/octet-stream"))
Sunitha Harish97b0e432019-11-21 04:59:29 -060094 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060095 asyncResp->res.result(boost::beast::http::status::not_acceptable);
96 asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -060097 return;
98 }
Sunitha Harish086d32c2021-02-01 02:11:49 -060099 BMCWEB_LOG_DEBUG
100 << "File upload in application/octet-stream format. Continue..";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500101
102 BMCWEB_LOG_DEBUG
103 << "handleIbmPut: Request to create/update the save-area file";
zhanghch058d1b46d2021-04-01 11:18:24 +0800104 if (!createSaveAreaPath(asyncResp))
Sunitha Harish97b0e432019-11-21 04:59:29 -0600105 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600106 asyncResp->res.result(boost::beast::http::status::not_found);
107 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500108 return;
109 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500110
asmithakarun1c7b07c2019-09-09 03:42:59 -0500111 std::ofstream file;
112 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harish97b0e432019-11-21 04:59:29 -0600113
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500114 // Get the current size of the savearea directory
115 std::filesystem::recursive_directory_iterator iter(loc, ec);
116 if (ec)
117 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600118 asyncResp->res.result(
119 boost::beast::http::status::internal_server_error);
120 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500121 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
122 "directory iterator. ec : "
123 << ec;
124 return;
125 }
126 std::uintmax_t saveAreaDirSize = 0;
127 for (auto& it : iter)
128 {
129 if (!std::filesystem::is_directory(it, ec))
130 {
131 if (ec)
132 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600133 asyncResp->res.result(
134 boost::beast::http::status::internal_server_error);
135 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500136 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
137 "directory . ec : "
138 << ec;
139 return;
140 }
141 std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
142 if (ec)
143 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600144 asyncResp->res.result(
145 boost::beast::http::status::internal_server_error);
146 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500147 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
148 "file size inside the directory . ec : "
149 << ec;
150 return;
151 }
152 saveAreaDirSize += fileSize;
153 }
154 }
155 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
156
157 // Get the file size getting uploaded
Ed Tanous3174e4d2020-10-07 11:41:22 -0700158 const std::string& data = req.body;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500159 BMCWEB_LOG_DEBUG << "data length: " << data.length();
160
161 if (data.length() < minSaveareaFileSize)
162 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600163 asyncResp->res.result(boost::beast::http::status::bad_request);
164 asyncResp->res.jsonValue["Description"] =
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500165 "File size is less than minimum allowed size[100B]";
166 return;
167 }
168 if (data.length() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500169 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600170 asyncResp->res.result(boost::beast::http::status::bad_request);
171 asyncResp->res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530172 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500173 return;
174 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500175
176 // Form the file path
177 loc /= fileID;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500178 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc;
179
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500180 // Check if the same file exists in the directory
181 bool fileExists = std::filesystem::exists(loc, ec);
182 if (ec)
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500183 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600184 asyncResp->res.result(
185 boost::beast::http::status::internal_server_error);
186 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500187 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
188 << ec;
189 return;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500190 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500191
192 std::uintmax_t newSizeToWrite = 0;
193 if (fileExists)
194 {
195 // File exists. Get the current file size
196 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec);
197 if (ec)
198 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600199 asyncResp->res.result(
200 boost::beast::http::status::internal_server_error);
201 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500202 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
203 << ec;
204 return;
205 }
206 // Calculate the difference in the file size.
207 // If the data.length is greater than the existing file size, then
208 // calculate the difference. Else consider the delta size as zero -
209 // because there is no increase in the total directory size.
210 // We need to add the diff only if the incoming data is larger than the
211 // existing filesize
212 if (data.length() > currentFileSize)
213 {
214 newSizeToWrite = data.length() - currentFileSize;
215 }
216 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
217 }
218 else
219 {
220 // This is a new file upload
221 newSizeToWrite = data.length();
222 }
223
224 // Calculate the total dir size before writing the new file
225 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
226
227 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
228 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600229 asyncResp->res.result(boost::beast::http::status::bad_request);
230 asyncResp->res.jsonValue["Description"] =
231 "File size does not fit in the savearea "
232 "directory maximum allowed size[10MB]";
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500233 return;
234 }
235
asmithakarun1c7b07c2019-09-09 03:42:59 -0500236 file.open(loc, std::ofstream::out);
237 if (file.fail())
238 {
239 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600240 asyncResp->res.result(
241 boost::beast::http::status::internal_server_error);
242 asyncResp->res.jsonValue["Description"] =
243 "Error while creating the file";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500244 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600245 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700246 file << data;
247 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
248 // Push an event
249 if (fileExists)
250 {
251 BMCWEB_LOG_DEBUG << "config file is updated";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600252 asyncResp->res.jsonValue["Description"] = "File Updated";
Ed Tanous3174e4d2020-10-07 11:41:22 -0700253
254 redfish::EventServiceManager::getInstance().sendEvent(
255 redfish::messages::resourceChanged(), origin, "IBMConfigFile");
256 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600257 else
258 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700259 BMCWEB_LOG_DEBUG << "config file is created";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600260 asyncResp->res.jsonValue["Description"] = "File Created";
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500261
Ed Tanous3174e4d2020-10-07 11:41:22 -0700262 redfish::EventServiceManager::getInstance().sendEvent(
263 redfish::messages::resourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500264 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530265}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500266
zhanghch058d1b46d2021-04-01 11:18:24 +0800267inline void
268 handleConfigFileList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530269{
270 std::vector<std::string> pathObjList;
271 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
272 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
273 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500274 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530275 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700276 const std::filesystem::path& pathObj = file.path();
Ratan Guptad3630cb2019-12-14 11:21:35 +0530277 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
278 pathObj.filename().string());
279 }
280 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600281 asyncResp->res.jsonValue["@odata.type"] =
282 "#IBMConfigFile.v1_0_0.IBMConfigFile";
283 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
284 asyncResp->res.jsonValue["Id"] = "ConfigFiles";
285 asyncResp->res.jsonValue["Name"] = "ConfigFiles";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530286
Sunitha Harishdb81c072021-01-21 23:33:21 -0600287 asyncResp->res.jsonValue["Members"] = std::move(pathObjList);
288 asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530289 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500290 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600291 return;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530292}
293
zhanghch058d1b46d2021-04-01 11:18:24 +0800294inline void
295 deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530296{
297 std::vector<std::string> pathObjList;
298 std::error_code ec;
299 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
300 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
301 {
302 std::filesystem::remove_all(loc, ec);
303 if (ec)
304 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600305 asyncResp->res.result(
306 boost::beast::http::status::internal_server_error);
307 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530308 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
309 "config files directory. ec : "
310 << ec;
311 }
312 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600313 return;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500314}
315
zhanghch058d1b46d2021-04-01 11:18:24 +0800316inline void
317 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530318{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600319 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
320 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
321 asyncResp->res.jsonValue["Id"] = "LockService";
322 asyncResp->res.jsonValue["Name"] = "LockService";
Ratan Gupta734a1c32019-12-14 11:53:48 +0530323
Sunitha Harishdb81c072021-01-21 23:33:21 -0600324 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530325 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600326 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530327 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600328 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530329 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600330 return;
Ratan Gupta734a1c32019-12-14 11:53:48 +0530331}
332
Sunitha Harishdb81c072021-01-21 23:33:21 -0600333inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500335{
336 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
337 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" +
338 fileID);
339 if (!std::filesystem::exists(loc))
340 {
341 BMCWEB_LOG_ERROR << loc << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600342 asyncResp->res.result(boost::beast::http::status::not_found);
343 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500344 return;
345 }
346
347 std::ifstream readfile(loc.string());
348 if (!readfile)
349 {
350 BMCWEB_LOG_ERROR << loc.string() << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600351 asyncResp->res.result(boost::beast::http::status::not_found);
352 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500353 return;
354 }
355
356 std::string contentDispositionParam =
357 "attachment; filename=\"" + fileID + "\"";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600358 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500359 std::string fileData;
360 fileData = {std::istreambuf_iterator<char>(readfile),
361 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600362 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500363 return;
364}
365
Sunitha Harishdb81c072021-01-21 23:33:21 -0600366inline void
367 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
368 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500369{
370 std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID);
371 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 std::ifstream fileOpen(filePath.c_str());
373 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700374 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500375 if (remove(filePath.c_str()) == 0)
376 {
377 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600378 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500379 }
380 else
381 {
382 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600383 asyncResp->res.result(
384 boost::beast::http::status::internal_server_error);
385 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500386 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700387 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500388 else
389 {
390 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600391 asyncResp->res.result(boost::beast::http::status::not_found);
392 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600393 }
394 return;
395}
396
zhanghch058d1b46d2021-04-01 11:18:24 +0800397inline void
398 handleBroadcastService(const crow::Request& req,
399 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500400{
401 std::string broadcastMsg;
402
zhanghch058d1b46d2021-04-01 11:18:24 +0800403 if (!redfish::json_util::readJson(req, asyncResp->res, "Message",
404 broadcastMsg))
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500405 {
406 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600407 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500408 return;
409 }
410 if (broadcastMsg.size() > maxBroadcastMsgSize)
411 {
412 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600413 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500414 return;
415 }
416 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500417 return;
418}
419
zhanghch058d1b46d2021-04-01 11:18:24 +0800420inline void handleFileUrl(const crow::Request& req,
421 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500422 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600423{
Ed Tanousb41187f2019-10-24 16:30:02 -0700424 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600425 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600426 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600427 return;
428 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700429 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500430 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600431 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500432 return;
433 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700434 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500435 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600436 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500437 return;
438 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600439}
Ratan Gupta453fed02019-12-14 09:39:47 +0530440
Sunitha Harishdb81c072021-01-21 23:33:21 -0600441inline void
442 handleAcquireLockAPI(const crow::Request& req,
443 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
444 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530445{
446 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500447 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530448 {
449 std::string lockType;
450 uint64_t resourceId;
451
452 SegmentFlags segInfo;
453 std::vector<nlohmann::json> segmentFlags;
454
Sunitha Harishdb81c072021-01-21 23:33:21 -0600455 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
456 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530457 "SegmentFlags", segmentFlags))
458 {
459 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600460 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530461 return;
462 }
463 BMCWEB_LOG_DEBUG << lockType;
464 BMCWEB_LOG_DEBUG << resourceId;
465
466 BMCWEB_LOG_DEBUG << "Segment Flags are present";
467
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500468 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530469 {
470 std::string lockFlags;
471 uint32_t segmentLength;
472
Sunitha Harishdb81c072021-01-21 23:33:21 -0600473 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
474 lockFlags, "SegmentLength",
475 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530476 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600477 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530478 return;
479 }
480
481 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
482 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
483
484 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
485 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530486 lockRequestStructure.push_back(
487 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
488 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530489 }
490
491 // print lock request into journal
492
Ed Tanous4e087512020-09-28 18:41:25 -0700493 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530494 {
Ed Tanous4e087512020-09-28 18:41:25 -0700495 BMCWEB_LOG_DEBUG << std::get<0>(i);
496 BMCWEB_LOG_DEBUG << std::get<1>(i);
497 BMCWEB_LOG_DEBUG << std::get<2>(i);
498 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530499
Ed Tanous4e087512020-09-28 18:41:25 -0700500 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530501 {
502 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
503 }
504 }
505
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500506 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530507
Ratan Gupta07386c62019-12-14 14:06:09 +0530508 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530509
510 if (varAcquireLock.first)
511 {
512 // Either validity failure of there is a conflict with itself
513
514 auto validityStatus =
515 std::get<std::pair<bool, int>>(varAcquireLock.second);
516
517 if ((!validityStatus.first) && (validityStatus.second == 0))
518 {
519 BMCWEB_LOG_DEBUG << "Not a Valid record";
520 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600521 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530522 return;
523 }
524 if (validityStatus.first && (validityStatus.second == 1))
525 {
526 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600527 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530528 return;
529 }
530 }
531 else
532 {
533 auto conflictStatus =
534 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
535 if (!conflictStatus.first)
536 {
537 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600538 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530539
540 auto var = std::get<uint32_t>(conflictStatus.second);
541 nlohmann::json returnJson;
542 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600543 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530544 return;
545 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700546 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600547 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700548 auto var =
549 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
550 nlohmann::json returnJson, segments;
551 nlohmann::json myarray = nlohmann::json::array();
552 returnJson["TransactionID"] = var.first;
553 returnJson["SessionID"] = std::get<0>(var.second);
554 returnJson["HMCID"] = std::get<1>(var.second);
555 returnJson["LockType"] = std::get<2>(var.second);
556 returnJson["ResourceID"] = std::get<3>(var.second);
557
558 for (auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530559 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700560 segments["LockFlag"] = i.first;
561 segments["SegmentLength"] = i.second;
562 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530563 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700564
565 returnJson["SegmentFlags"] = myarray;
566
Sunitha Harishdb81c072021-01-21 23:33:21 -0600567 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700568 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530569 }
570}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600571inline void
572 handleRelaseAllAPI(const crow::Request& req,
573 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530574{
575 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600576 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530577 return;
578}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530579
Ed Tanous02379d32020-09-15 21:15:44 -0700580inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600581 handleReleaseLockAPI(const crow::Request& req,
582 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700583 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530584{
585 BMCWEB_LOG_DEBUG << listTransactionIds.size();
586 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700587 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530588 {
Ed Tanous4e087512020-09-28 18:41:25 -0700589 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530590 }
591
manojkiraneda3b6dea62019-12-13 17:05:36 +0530592 // validate the request ids
593
Ratan Gupta07386c62019-12-14 14:06:09 +0530594 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530595 listTransactionIds,
596 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530597
598 if (!varReleaselock.first)
599 {
600 // validation Failed
Sunitha Harishdb81c072021-01-21 23:33:21 -0600601 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530602 return;
603 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700604 auto statusRelease =
605 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
606 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530607 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700608 // The current hmc owns all the locks, so we already released
609 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700610 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530611 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700612
613 // valid rid, but the current hmc does not own all the locks
614 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600615 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700616
617 auto var = statusRelease.second;
618 nlohmann::json returnJson, segments;
619 nlohmann::json myArray = nlohmann::json::array();
620 returnJson["TransactionID"] = var.first;
621 returnJson["SessionID"] = std::get<0>(var.second);
622 returnJson["HMCID"] = std::get<1>(var.second);
623 returnJson["LockType"] = std::get<2>(var.second);
624 returnJson["ResourceID"] = std::get<3>(var.second);
625
626 for (auto& i : std::get<4>(var.second))
627 {
628 segments["LockFlag"] = i.first;
629 segments["SegmentLength"] = i.second;
630 myArray.push_back(segments);
631 }
632
633 returnJson["SegmentFlags"] = myArray;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600634 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700635 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530636}
637
Sunitha Harishdb81c072021-01-21 23:33:21 -0600638inline void
639 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
640 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530641{
642 BMCWEB_LOG_DEBUG << listSessionIds.size();
643
Ratan Gupta07386c62019-12-14 14:06:09 +0530644 auto status =
645 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530646 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
647
648 nlohmann::json lockRecords = nlohmann::json::array();
649
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500650 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530651 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500652 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530653 {
654 nlohmann::json returnJson;
655
656 returnJson["TransactionID"] = transactionId.first;
657 returnJson["SessionID"] = std::get<0>(lockRecord);
658 returnJson["HMCID"] = std::get<1>(lockRecord);
659 returnJson["LockType"] = std::get<2>(lockRecord);
660 returnJson["ResourceID"] = std::get<3>(lockRecord);
661
662 nlohmann::json segments;
663 nlohmann::json segmentInfoArray = nlohmann::json::array();
664
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500665 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530666 {
667 segments["LockFlag"] = segment.first;
668 segments["SegmentLength"] = segment.second;
669 segmentInfoArray.push_back(segments);
670 }
671
672 returnJson["SegmentFlags"] = segmentInfoArray;
673 lockRecords.push_back(returnJson);
674 }
675 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600676 asyncResp->res.result(boost::beast::http::status::ok);
677 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530678}
679
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500680inline bool isValidConfigFileName(const std::string& fileName,
681 crow::Response& res)
682{
683 if (fileName.empty())
684 {
685 BMCWEB_LOG_ERROR << "Empty filename";
686 res.jsonValue["Description"] = "Empty file path in the url";
687 return false;
688 }
689
690 // ConfigFile name is allowed to take upper and lowercase letters,
691 // numbers and hyphen
692 std::size_t found = fileName.find_first_not_of(
693 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
694 if (found != std::string::npos)
695 {
696 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
697 res.jsonValue["Description"] = "Unsupported character in filename";
698 return false;
699 }
700
701 // Check the filename length
702 if (fileName.length() > 20)
703 {
704 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
705 "Input filename length is: "
706 << fileName.length();
707 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
708 return false;
709 }
710
711 return true;
712}
713
Ed Tanous02379d32020-09-15 21:15:44 -0700714inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530715{
716
717 // allowed only for admin
718 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous23a21a12020-07-25 04:45:05 +0000719 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700720 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800721 [](const crow::Request&,
722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600723 asyncResp->res.jsonValue["@odata.type"] =
Ratan Gupta453fed02019-12-14 09:39:47 +0530724 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600725 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
726 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
727 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
728 asyncResp->res.jsonValue["ConfigFiles"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530729 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600730 asyncResp->res.jsonValue["LockService"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530731 {"@odata.id", "/ibm/v1/HMC/LockService"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600732 asyncResp->res.jsonValue["BroadcastService"] = {
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500733 {"@odata.id", "/ibm/v1/HMC/BroadcastService"}};
Ratan Gupta453fed02019-12-14 09:39:47 +0530734 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600735
Ratan Guptad3630cb2019-12-14 11:21:35 +0530736 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous23a21a12020-07-25 04:45:05 +0000737 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700738 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800739 [](const crow::Request&,
740 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
741 handleConfigFileList(asyncResp);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530742 });
743
744 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500745 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous23a21a12020-07-25 04:45:05 +0000746 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700747 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800748 [](const crow::Request&,
749 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
750 deleteConfigFiles(asyncResp);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530751 });
752
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500753 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000754 .privileges({"ConfigureComponents", "ConfigureManager"})
zhanghch058d1b46d2021-04-01 11:18:24 +0800755 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
756 boost::beast::http::verb::delete_)(
757 [](const crow::Request& req,
758 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
759 const std::string& fileName) {
760 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
761 // Validate the incoming fileName
762 if (!isValidConfigFileName(fileName, asyncResp->res))
763 {
764 asyncResp->res.result(
765 boost::beast::http::status::bad_request);
766 return;
767 }
768 handleFileUrl(req, asyncResp, fileName);
769 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530770
771 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000772 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700773 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800774 [](const crow::Request&,
775 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
776 getLockServiceData(asyncResp);
Ratan Gupta734a1c32019-12-14 11:53:48 +0530777 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530778
779 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000780 .privileges({"ConfigureComponents", "ConfigureManager"})
zhanghch058d1b46d2021-04-01 11:18:24 +0800781 .methods(boost::beast::http::verb::post)(
782 [](const crow::Request& req,
783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
784 std::vector<nlohmann::json> body;
785 if (!redfish::json_util::readJson(req, asyncResp->res,
786 "Request", body))
787 {
788 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
789 asyncResp->res.result(
790 boost::beast::http::status::bad_request);
791 return;
792 }
793 handleAcquireLockAPI(req, asyncResp, body);
794 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530795 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000796 .privileges({"ConfigureComponents", "ConfigureManager"})
zhanghch058d1b46d2021-04-01 11:18:24 +0800797 .methods(boost::beast::http::verb::post)(
798 [](const crow::Request& req,
799 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
800 std::string type;
801 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530802
zhanghch058d1b46d2021-04-01 11:18:24 +0800803 if (!redfish::json_util::readJson(req, asyncResp->res, "Type",
804 type, "TransactionIDs",
805 listTransactionIds))
806 {
807 asyncResp->res.result(
808 boost::beast::http::status::bad_request);
809 return;
810 }
811 if (type == "Transaction")
812 {
813 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
814 }
815 else if (type == "Session")
816 {
817 handleRelaseAllAPI(req, asyncResp);
818 }
819 else
820 {
821 BMCWEB_LOG_DEBUG << " Value of Type : " << type
822 << "is Not a Valid key";
823 redfish::messages::propertyValueNotInList(asyncResp->res,
824 type, "Type");
825 }
826 });
manojkiraneda402b5712019-12-13 17:07:09 +0530827 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous23a21a12020-07-25 04:45:05 +0000828 .privileges({"ConfigureComponents", "ConfigureManager"})
zhanghch058d1b46d2021-04-01 11:18:24 +0800829 .methods(boost::beast::http::verb::post)(
830 [](const crow::Request& req,
831 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
832 ListOfSessionIds listSessionIds;
manojkiraneda402b5712019-12-13 17:07:09 +0530833
zhanghch058d1b46d2021-04-01 11:18:24 +0800834 if (!redfish::json_util::readJson(req, asyncResp->res,
835 "SessionIDs", listSessionIds))
836 {
837 asyncResp->res.result(
838 boost::beast::http::status::bad_request);
839 return;
840 }
841 handleGetLockListAPI(asyncResp, listSessionIds);
842 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500843
844 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000845 .privileges({"ConfigureComponents", "ConfigureManager"})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500846 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800847 [](const crow::Request& req,
848 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
849 handleBroadcastService(req, asyncResp);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500850 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530851}
852
853} // namespace ibm_mc
854} // namespace crow