blob: e6b5cb2ac426fe4d46b7e03d6402695113968485 [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
Ed Tanous02379d32020-09-15 21:15:44 -070045inline bool createSaveAreaPath(crow::Response& res)
Sunitha Harish97b0e432019-11-21 04:59:29 -060046{
47 // The path /var/lib/obmc will be created by initrdscripts
48 // Create the directories for the save-area files, when we get
49 // first file upload request
50 std::error_code ec;
51 if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec))
52 {
53 std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec);
54 }
55 if (ec)
56 {
57 res.result(boost::beast::http::status::internal_server_error);
58 res.jsonValue["Description"] = internalServerError;
59 BMCWEB_LOG_DEBUG
60 << "handleIbmPost: Failed to prepare save-area directory. ec : "
61 << ec;
62 return false;
63 }
64
65 if (!std::filesystem::is_directory(
66 "/var/lib/obmc/bmc-console-mgmt/save-area", ec))
67 {
68 std::filesystem::create_directory(
69 "/var/lib/obmc/bmc-console-mgmt/save-area", ec);
70 }
71 if (ec)
72 {
73 res.result(boost::beast::http::status::internal_server_error);
74 res.jsonValue["Description"] = internalServerError;
75 BMCWEB_LOG_DEBUG
76 << "handleIbmPost: Failed to prepare save-area directory. ec : "
77 << ec;
78 return false;
79 }
80 return true;
81}
Ed Tanous02379d32020-09-15 21:15:44 -070082
Sunitha Harishdb81c072021-01-21 23:33:21 -060083inline void handleFilePut(const crow::Request& req,
84 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -070085 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060086{
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050087 std::error_code ec;
Sunitha Harish97b0e432019-11-21 04:59:29 -060088 // Check the content-type of the request
Sunitha Harish086d32c2021-02-01 02:11:49 -060089 boost::beast::string_view contentType = req.getHeaderValue("content-type");
90 if (!boost::iequals(contentType, "application/octet-stream"))
Sunitha Harish97b0e432019-11-21 04:59:29 -060091 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060092 asyncResp->res.result(boost::beast::http::status::not_acceptable);
93 asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -060094 return;
95 }
Sunitha Harish086d32c2021-02-01 02:11:49 -060096 BMCWEB_LOG_DEBUG
97 << "File upload in application/octet-stream format. Continue..";
asmithakarun1c7b07c2019-09-09 03:42:59 -050098
99 BMCWEB_LOG_DEBUG
100 << "handleIbmPut: Request to create/update the save-area file";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600101 if (!createSaveAreaPath(asyncResp->res))
Sunitha Harish97b0e432019-11-21 04:59:29 -0600102 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600103 asyncResp->res.result(boost::beast::http::status::not_found);
104 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500105 return;
106 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500107
asmithakarun1c7b07c2019-09-09 03:42:59 -0500108 std::ofstream file;
109 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harish97b0e432019-11-21 04:59:29 -0600110
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500111 // Get the current size of the savearea directory
112 std::filesystem::recursive_directory_iterator iter(loc, ec);
113 if (ec)
114 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600115 asyncResp->res.result(
116 boost::beast::http::status::internal_server_error);
117 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500118 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
119 "directory iterator. ec : "
120 << ec;
121 return;
122 }
123 std::uintmax_t saveAreaDirSize = 0;
124 for (auto& it : iter)
125 {
126 if (!std::filesystem::is_directory(it, ec))
127 {
128 if (ec)
129 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600130 asyncResp->res.result(
131 boost::beast::http::status::internal_server_error);
132 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500133 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
134 "directory . ec : "
135 << ec;
136 return;
137 }
138 std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
139 if (ec)
140 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600141 asyncResp->res.result(
142 boost::beast::http::status::internal_server_error);
143 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500144 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
145 "file size inside the directory . ec : "
146 << ec;
147 return;
148 }
149 saveAreaDirSize += fileSize;
150 }
151 }
152 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
153
154 // Get the file size getting uploaded
Ed Tanous3174e4d2020-10-07 11:41:22 -0700155 const std::string& data = req.body;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500156 BMCWEB_LOG_DEBUG << "data length: " << data.length();
157
158 if (data.length() < minSaveareaFileSize)
159 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600160 asyncResp->res.result(boost::beast::http::status::bad_request);
161 asyncResp->res.jsonValue["Description"] =
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500162 "File size is less than minimum allowed size[100B]";
163 return;
164 }
165 if (data.length() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500166 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600167 asyncResp->res.result(boost::beast::http::status::bad_request);
168 asyncResp->res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530169 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500170 return;
171 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500172
173 // Form the file path
174 loc /= fileID;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500175 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc;
176
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500177 // Check if the same file exists in the directory
178 bool fileExists = std::filesystem::exists(loc, ec);
179 if (ec)
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500180 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600181 asyncResp->res.result(
182 boost::beast::http::status::internal_server_error);
183 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500184 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
185 << ec;
186 return;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500187 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500188
189 std::uintmax_t newSizeToWrite = 0;
190 if (fileExists)
191 {
192 // File exists. Get the current file size
193 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec);
194 if (ec)
195 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600196 asyncResp->res.result(
197 boost::beast::http::status::internal_server_error);
198 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500199 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
200 << ec;
201 return;
202 }
203 // Calculate the difference in the file size.
204 // If the data.length is greater than the existing file size, then
205 // calculate the difference. Else consider the delta size as zero -
206 // because there is no increase in the total directory size.
207 // We need to add the diff only if the incoming data is larger than the
208 // existing filesize
209 if (data.length() > currentFileSize)
210 {
211 newSizeToWrite = data.length() - currentFileSize;
212 }
213 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
214 }
215 else
216 {
217 // This is a new file upload
218 newSizeToWrite = data.length();
219 }
220
221 // Calculate the total dir size before writing the new file
222 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
223
224 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
225 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600226 asyncResp->res.result(boost::beast::http::status::bad_request);
227 asyncResp->res.jsonValue["Description"] =
228 "File size does not fit in the savearea "
229 "directory maximum allowed size[10MB]";
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500230 return;
231 }
232
asmithakarun1c7b07c2019-09-09 03:42:59 -0500233 file.open(loc, std::ofstream::out);
234 if (file.fail())
235 {
236 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600237 asyncResp->res.result(
238 boost::beast::http::status::internal_server_error);
239 asyncResp->res.jsonValue["Description"] =
240 "Error while creating the file";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500241 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600242 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700243 file << data;
244 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
245 // Push an event
246 if (fileExists)
247 {
248 BMCWEB_LOG_DEBUG << "config file is updated";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600249 asyncResp->res.jsonValue["Description"] = "File Updated";
Ed Tanous3174e4d2020-10-07 11:41:22 -0700250
251 redfish::EventServiceManager::getInstance().sendEvent(
252 redfish::messages::resourceChanged(), origin, "IBMConfigFile");
253 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600254 else
255 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700256 BMCWEB_LOG_DEBUG << "config file is created";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600257 asyncResp->res.jsonValue["Description"] = "File Created";
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500258
Ed Tanous3174e4d2020-10-07 11:41:22 -0700259 redfish::EventServiceManager::getInstance().sendEvent(
260 redfish::messages::resourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500261 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530262}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500263
Ed Tanous02379d32020-09-15 21:15:44 -0700264inline void handleConfigFileList(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530265{
266 std::vector<std::string> pathObjList;
267 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harishdb81c072021-01-21 23:33:21 -0600268 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
269 std::make_shared<bmcweb::AsyncResp>(res);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530270 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
271 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530273 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700274 const std::filesystem::path& pathObj = file.path();
Ratan Guptad3630cb2019-12-14 11:21:35 +0530275 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
276 pathObj.filename().string());
277 }
278 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600279 asyncResp->res.jsonValue["@odata.type"] =
280 "#IBMConfigFile.v1_0_0.IBMConfigFile";
281 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
282 asyncResp->res.jsonValue["Id"] = "ConfigFiles";
283 asyncResp->res.jsonValue["Name"] = "ConfigFiles";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530284
Sunitha Harishdb81c072021-01-21 23:33:21 -0600285 asyncResp->res.jsonValue["Members"] = std::move(pathObjList);
286 asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530287 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500288 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600289 return;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530290}
291
Ed Tanous02379d32020-09-15 21:15:44 -0700292inline void deleteConfigFiles(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530293{
294 std::vector<std::string> pathObjList;
295 std::error_code ec;
296 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harishdb81c072021-01-21 23:33:21 -0600297 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
298 std::make_shared<bmcweb::AsyncResp>(res);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530299 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
300 {
301 std::filesystem::remove_all(loc, ec);
302 if (ec)
303 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600304 asyncResp->res.result(
305 boost::beast::http::status::internal_server_error);
306 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530307 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
308 "config files directory. ec : "
309 << ec;
310 }
311 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600312 return;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500313}
314
Ed Tanous02379d32020-09-15 21:15:44 -0700315inline void getLockServiceData(crow::Response& res)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530316{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600317 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
318 std::make_shared<bmcweb::AsyncResp>(res);
319 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
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500397inline void handleBroadcastService(const crow::Request& req,
398 crow::Response& res)
399{
400 std::string broadcastMsg;
401
Sunitha Harishdb81c072021-01-21 23:33:21 -0600402 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
403 std::make_shared<bmcweb::AsyncResp>(res);
404
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500405 if (!redfish::json_util::readJson(req, res, "Message", broadcastMsg))
406 {
407 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600408 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500409 return;
410 }
411 if (broadcastMsg.size() > maxBroadcastMsgSize)
412 {
413 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600414 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500415 return;
416 }
417 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500418 return;
419}
420
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500421inline void handleFileUrl(const crow::Request& req, crow::Response& res,
422 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600423{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600424 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
425 std::make_shared<bmcweb::AsyncResp>(res);
426
Ed Tanousb41187f2019-10-24 16:30:02 -0700427 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600428 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600429 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600430 return;
431 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700432 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500433 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600434 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500435 return;
436 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700437 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500438 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600439 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500440 return;
441 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600442}
Ratan Gupta453fed02019-12-14 09:39:47 +0530443
Sunitha Harishdb81c072021-01-21 23:33:21 -0600444inline void
445 handleAcquireLockAPI(const crow::Request& req,
446 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
447 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530448{
449 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500450 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530451 {
452 std::string lockType;
453 uint64_t resourceId;
454
455 SegmentFlags segInfo;
456 std::vector<nlohmann::json> segmentFlags;
457
Sunitha Harishdb81c072021-01-21 23:33:21 -0600458 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
459 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530460 "SegmentFlags", segmentFlags))
461 {
462 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600463 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530464 return;
465 }
466 BMCWEB_LOG_DEBUG << lockType;
467 BMCWEB_LOG_DEBUG << resourceId;
468
469 BMCWEB_LOG_DEBUG << "Segment Flags are present";
470
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500471 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530472 {
473 std::string lockFlags;
474 uint32_t segmentLength;
475
Sunitha Harishdb81c072021-01-21 23:33:21 -0600476 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
477 lockFlags, "SegmentLength",
478 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530479 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600480 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530481 return;
482 }
483
484 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
485 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
486
487 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
488 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530489 lockRequestStructure.push_back(
490 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
491 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530492 }
493
494 // print lock request into journal
495
Ed Tanous4e087512020-09-28 18:41:25 -0700496 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530497 {
Ed Tanous4e087512020-09-28 18:41:25 -0700498 BMCWEB_LOG_DEBUG << std::get<0>(i);
499 BMCWEB_LOG_DEBUG << std::get<1>(i);
500 BMCWEB_LOG_DEBUG << std::get<2>(i);
501 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530502
Ed Tanous4e087512020-09-28 18:41:25 -0700503 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530504 {
505 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
506 }
507 }
508
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500509 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530510
Ratan Gupta07386c62019-12-14 14:06:09 +0530511 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530512
513 if (varAcquireLock.first)
514 {
515 // Either validity failure of there is a conflict with itself
516
517 auto validityStatus =
518 std::get<std::pair<bool, int>>(varAcquireLock.second);
519
520 if ((!validityStatus.first) && (validityStatus.second == 0))
521 {
522 BMCWEB_LOG_DEBUG << "Not a Valid record";
523 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600524 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530525 return;
526 }
527 if (validityStatus.first && (validityStatus.second == 1))
528 {
529 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600530 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530531 return;
532 }
533 }
534 else
535 {
536 auto conflictStatus =
537 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
538 if (!conflictStatus.first)
539 {
540 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600541 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530542
543 auto var = std::get<uint32_t>(conflictStatus.second);
544 nlohmann::json returnJson;
545 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600546 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530547 return;
548 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700549 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600550 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700551 auto var =
552 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
553 nlohmann::json returnJson, segments;
554 nlohmann::json myarray = nlohmann::json::array();
555 returnJson["TransactionID"] = var.first;
556 returnJson["SessionID"] = std::get<0>(var.second);
557 returnJson["HMCID"] = std::get<1>(var.second);
558 returnJson["LockType"] = std::get<2>(var.second);
559 returnJson["ResourceID"] = std::get<3>(var.second);
560
561 for (auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530562 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700563 segments["LockFlag"] = i.first;
564 segments["SegmentLength"] = i.second;
565 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530566 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700567
568 returnJson["SegmentFlags"] = myarray;
569
Sunitha Harishdb81c072021-01-21 23:33:21 -0600570 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700571 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530572 }
573}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600574inline void
575 handleRelaseAllAPI(const crow::Request& req,
576 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530577{
578 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600579 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530580 return;
581}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530582
Ed Tanous02379d32020-09-15 21:15:44 -0700583inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600584 handleReleaseLockAPI(const crow::Request& req,
585 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700586 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530587{
588 BMCWEB_LOG_DEBUG << listTransactionIds.size();
589 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700590 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530591 {
Ed Tanous4e087512020-09-28 18:41:25 -0700592 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530593 }
594
manojkiraneda3b6dea62019-12-13 17:05:36 +0530595 // validate the request ids
596
Ratan Gupta07386c62019-12-14 14:06:09 +0530597 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530598 listTransactionIds,
599 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530600
601 if (!varReleaselock.first)
602 {
603 // validation Failed
Sunitha Harishdb81c072021-01-21 23:33:21 -0600604 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530605 return;
606 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700607 auto statusRelease =
608 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
609 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530610 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700611 // The current hmc owns all the locks, so we already released
612 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700613 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530614 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700615
616 // valid rid, but the current hmc does not own all the locks
617 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600618 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700619
620 auto var = statusRelease.second;
621 nlohmann::json returnJson, segments;
622 nlohmann::json myArray = nlohmann::json::array();
623 returnJson["TransactionID"] = var.first;
624 returnJson["SessionID"] = std::get<0>(var.second);
625 returnJson["HMCID"] = std::get<1>(var.second);
626 returnJson["LockType"] = std::get<2>(var.second);
627 returnJson["ResourceID"] = std::get<3>(var.second);
628
629 for (auto& i : std::get<4>(var.second))
630 {
631 segments["LockFlag"] = i.first;
632 segments["SegmentLength"] = i.second;
633 myArray.push_back(segments);
634 }
635
636 returnJson["SegmentFlags"] = myArray;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600637 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700638 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530639}
640
Sunitha Harishdb81c072021-01-21 23:33:21 -0600641inline void
642 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
643 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530644{
645 BMCWEB_LOG_DEBUG << listSessionIds.size();
646
Ratan Gupta07386c62019-12-14 14:06:09 +0530647 auto status =
648 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530649 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
650
651 nlohmann::json lockRecords = nlohmann::json::array();
652
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500653 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530654 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500655 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530656 {
657 nlohmann::json returnJson;
658
659 returnJson["TransactionID"] = transactionId.first;
660 returnJson["SessionID"] = std::get<0>(lockRecord);
661 returnJson["HMCID"] = std::get<1>(lockRecord);
662 returnJson["LockType"] = std::get<2>(lockRecord);
663 returnJson["ResourceID"] = std::get<3>(lockRecord);
664
665 nlohmann::json segments;
666 nlohmann::json segmentInfoArray = nlohmann::json::array();
667
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500668 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530669 {
670 segments["LockFlag"] = segment.first;
671 segments["SegmentLength"] = segment.second;
672 segmentInfoArray.push_back(segments);
673 }
674
675 returnJson["SegmentFlags"] = segmentInfoArray;
676 lockRecords.push_back(returnJson);
677 }
678 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600679 asyncResp->res.result(boost::beast::http::status::ok);
680 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530681}
682
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500683inline bool isValidConfigFileName(const std::string& fileName,
684 crow::Response& res)
685{
686 if (fileName.empty())
687 {
688 BMCWEB_LOG_ERROR << "Empty filename";
689 res.jsonValue["Description"] = "Empty file path in the url";
690 return false;
691 }
692
693 // ConfigFile name is allowed to take upper and lowercase letters,
694 // numbers and hyphen
695 std::size_t found = fileName.find_first_not_of(
696 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
697 if (found != std::string::npos)
698 {
699 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
700 res.jsonValue["Description"] = "Unsupported character in filename";
701 return false;
702 }
703
704 // Check the filename length
705 if (fileName.length() > 20)
706 {
707 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
708 "Input filename length is: "
709 << fileName.length();
710 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
711 return false;
712 }
713
714 return true;
715}
716
Ed Tanous02379d32020-09-15 21:15:44 -0700717inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530718{
719
720 // allowed only for admin
721 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous23a21a12020-07-25 04:45:05 +0000722 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700723 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000724 [](const crow::Request&, crow::Response& res) {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600725 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
726 std::make_shared<bmcweb::AsyncResp>(res);
727
728 asyncResp->res.jsonValue["@odata.type"] =
Ratan Gupta453fed02019-12-14 09:39:47 +0530729 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600730 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
731 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
732 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
733 asyncResp->res.jsonValue["ConfigFiles"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530734 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600735 asyncResp->res.jsonValue["LockService"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530736 {"@odata.id", "/ibm/v1/HMC/LockService"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600737 asyncResp->res.jsonValue["BroadcastService"] = {
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500738 {"@odata.id", "/ibm/v1/HMC/BroadcastService"}};
Ratan Gupta453fed02019-12-14 09:39:47 +0530739 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600740
Ratan Guptad3630cb2019-12-14 11:21:35 +0530741 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous23a21a12020-07-25 04:45:05 +0000742 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700743 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000744 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530745 handleConfigFileList(res);
746 });
747
748 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500749 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous23a21a12020-07-25 04:45:05 +0000750 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700751 .methods(boost::beast::http::verb::post)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000752 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530753 deleteConfigFiles(res);
754 });
755
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500756 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000757 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500758 .methods(
759 boost::beast::http::verb::put, boost::beast::http::verb::get,
760 boost::beast::http::verb::delete_)([](const crow::Request& req,
761 crow::Response& res,
762 const std::string& fileName) {
763 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
764 std::make_shared<bmcweb::AsyncResp>(res);
765 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
766 // Validate the incoming fileName
767 if (!isValidConfigFileName(fileName, res))
768 {
769 asyncResp->res.result(boost::beast::http::status::bad_request);
770 return;
771 }
772 handleFileUrl(req, res, fileName);
773 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530774
775 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000776 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700777 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000778 [](const crow::Request&, crow::Response& res) {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530779 getLockServiceData(res);
780 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530781
782 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000783 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harishdb81c072021-01-21 23:33:21 -0600784 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
785 crow::Response& res) {
786 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
787 std::make_shared<bmcweb::AsyncResp>(res);
788
789 std::vector<nlohmann::json> body;
790 if (!redfish::json_util::readJson(req, res, "Request", body))
791 {
792 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
793 asyncResp->res.result(boost::beast::http::status::bad_request);
794 return;
795 }
796 handleAcquireLockAPI(req, asyncResp, body);
797 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530798 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000799 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700800 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
801 crow::Response& res) {
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530802 std::string type;
803 std::vector<uint32_t> listTransactionIds;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600804 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
805 std::make_shared<bmcweb::AsyncResp>(res);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530806
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530807 if (!redfish::json_util::readJson(req, res, "Type", type,
808 "TransactionIDs",
809 listTransactionIds))
810 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600811 asyncResp->res.result(boost::beast::http::status::bad_request);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530812 return;
813 }
814 if (type == "Transaction")
815 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600816 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530817 }
818 else if (type == "Session")
819 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600820 handleRelaseAllAPI(req, asyncResp);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530821 }
822 else
823 {
824 BMCWEB_LOG_DEBUG << " Value of Type : " << type
825 << "is Not a Valid key";
826 redfish::messages::propertyValueNotInList(res, type, "Type");
827 }
828 });
manojkiraneda402b5712019-12-13 17:07:09 +0530829 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous23a21a12020-07-25 04:45:05 +0000830 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harishdb81c072021-01-21 23:33:21 -0600831 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
832 crow::Response& res) {
833 ListOfSessionIds listSessionIds;
834 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
835 std::make_shared<bmcweb::AsyncResp>(res);
manojkiraneda402b5712019-12-13 17:07:09 +0530836
Sunitha Harishdb81c072021-01-21 23:33:21 -0600837 if (!redfish::json_util::readJson(req, res, "SessionIDs",
838 listSessionIds))
839 {
840 asyncResp->res.result(boost::beast::http::status::bad_request);
841 return;
842 }
843 handleGetLockListAPI(asyncResp, listSessionIds);
844 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500845
846 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000847 .privileges({"ConfigureComponents", "ConfigureManager"})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500848 .methods(boost::beast::http::verb::post)(
849 [](const crow::Request& req, crow::Response& res) {
850 handleBroadcastService(req, res);
851 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530852}
853
854} // namespace ibm_mc
855} // namespace crow