blob: cab6523133b45ce26ea19aa559df8de43bc3fc49 [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
89 std::string_view contentType = req.getHeaderValue("content-type");
90 if (boost::starts_with(contentType, "multipart/form-data"))
91 {
92 BMCWEB_LOG_DEBUG
93 << "This is multipart/form-data. Invalid content for PUT";
94
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 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070099 BMCWEB_LOG_DEBUG << "Not a multipart/form-data. Continue..";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500100
101 BMCWEB_LOG_DEBUG
102 << "handleIbmPut: Request to create/update the save-area file";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600103 if (!createSaveAreaPath(asyncResp->res))
Sunitha Harish97b0e432019-11-21 04:59:29 -0600104 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600105 asyncResp->res.result(boost::beast::http::status::not_found);
106 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500107 return;
108 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500109
asmithakarun1c7b07c2019-09-09 03:42:59 -0500110 std::ofstream file;
111 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harish97b0e432019-11-21 04:59:29 -0600112
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500113 // Get the current size of the savearea directory
114 std::filesystem::recursive_directory_iterator iter(loc, ec);
115 if (ec)
116 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600117 asyncResp->res.result(
118 boost::beast::http::status::internal_server_error);
119 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500120 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
121 "directory iterator. ec : "
122 << ec;
123 return;
124 }
125 std::uintmax_t saveAreaDirSize = 0;
126 for (auto& it : iter)
127 {
128 if (!std::filesystem::is_directory(it, ec))
129 {
130 if (ec)
131 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600132 asyncResp->res.result(
133 boost::beast::http::status::internal_server_error);
134 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500135 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
136 "directory . ec : "
137 << ec;
138 return;
139 }
140 std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
141 if (ec)
142 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600143 asyncResp->res.result(
144 boost::beast::http::status::internal_server_error);
145 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500146 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
147 "file size inside the directory . ec : "
148 << ec;
149 return;
150 }
151 saveAreaDirSize += fileSize;
152 }
153 }
154 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
155
156 // Get the file size getting uploaded
Ed Tanous3174e4d2020-10-07 11:41:22 -0700157 const std::string& data = req.body;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500158 BMCWEB_LOG_DEBUG << "data length: " << data.length();
159
160 if (data.length() < minSaveareaFileSize)
161 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600162 asyncResp->res.result(boost::beast::http::status::bad_request);
163 asyncResp->res.jsonValue["Description"] =
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500164 "File size is less than minimum allowed size[100B]";
165 return;
166 }
167 if (data.length() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500168 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600169 asyncResp->res.result(boost::beast::http::status::bad_request);
170 asyncResp->res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530171 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500172 return;
173 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500174
175 // Form the file path
176 loc /= fileID;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500177 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc;
178
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500179 // Check if the same file exists in the directory
180 bool fileExists = std::filesystem::exists(loc, ec);
181 if (ec)
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500182 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600183 asyncResp->res.result(
184 boost::beast::http::status::internal_server_error);
185 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500186 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
187 << ec;
188 return;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500189 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500190
191 std::uintmax_t newSizeToWrite = 0;
192 if (fileExists)
193 {
194 // File exists. Get the current file size
195 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec);
196 if (ec)
197 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600198 asyncResp->res.result(
199 boost::beast::http::status::internal_server_error);
200 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500201 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
202 << ec;
203 return;
204 }
205 // Calculate the difference in the file size.
206 // If the data.length is greater than the existing file size, then
207 // calculate the difference. Else consider the delta size as zero -
208 // because there is no increase in the total directory size.
209 // We need to add the diff only if the incoming data is larger than the
210 // existing filesize
211 if (data.length() > currentFileSize)
212 {
213 newSizeToWrite = data.length() - currentFileSize;
214 }
215 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
216 }
217 else
218 {
219 // This is a new file upload
220 newSizeToWrite = data.length();
221 }
222
223 // Calculate the total dir size before writing the new file
224 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
225
226 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
227 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600228 asyncResp->res.result(boost::beast::http::status::bad_request);
229 asyncResp->res.jsonValue["Description"] =
230 "File size does not fit in the savearea "
231 "directory maximum allowed size[10MB]";
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500232 return;
233 }
234
asmithakarun1c7b07c2019-09-09 03:42:59 -0500235 file.open(loc, std::ofstream::out);
236 if (file.fail())
237 {
238 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600239 asyncResp->res.result(
240 boost::beast::http::status::internal_server_error);
241 asyncResp->res.jsonValue["Description"] =
242 "Error while creating the file";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500243 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600244 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700245 file << data;
246 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
247 // Push an event
248 if (fileExists)
249 {
250 BMCWEB_LOG_DEBUG << "config file is updated";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600251 asyncResp->res.jsonValue["Description"] = "File Updated";
Ed Tanous3174e4d2020-10-07 11:41:22 -0700252
253 redfish::EventServiceManager::getInstance().sendEvent(
254 redfish::messages::resourceChanged(), origin, "IBMConfigFile");
255 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600256 else
257 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700258 BMCWEB_LOG_DEBUG << "config file is created";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600259 asyncResp->res.jsonValue["Description"] = "File Created";
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500260
Ed Tanous3174e4d2020-10-07 11:41:22 -0700261 redfish::EventServiceManager::getInstance().sendEvent(
262 redfish::messages::resourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500263 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530264}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500265
Ed Tanous02379d32020-09-15 21:15:44 -0700266inline void handleConfigFileList(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530267{
268 std::vector<std::string> pathObjList;
269 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harishdb81c072021-01-21 23:33:21 -0600270 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
271 std::make_shared<bmcweb::AsyncResp>(res);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530272 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
Ed Tanous02379d32020-09-15 21:15:44 -0700294inline void deleteConfigFiles(crow::Response& res)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530295{
296 std::vector<std::string> pathObjList;
297 std::error_code ec;
298 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area");
Sunitha Harishdb81c072021-01-21 23:33:21 -0600299 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
300 std::make_shared<bmcweb::AsyncResp>(res);
Ratan Guptad3630cb2019-12-14 11:21:35 +0530301 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
302 {
303 std::filesystem::remove_all(loc, ec);
304 if (ec)
305 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600306 asyncResp->res.result(
307 boost::beast::http::status::internal_server_error);
308 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530309 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
310 "config files directory. ec : "
311 << ec;
312 }
313 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600314 return;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500315}
316
Ed Tanous02379d32020-09-15 21:15:44 -0700317inline void getLockServiceData(crow::Response& res)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530318{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600319 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
320 std::make_shared<bmcweb::AsyncResp>(res);
321 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
322 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
323 asyncResp->res.jsonValue["Id"] = "LockService";
324 asyncResp->res.jsonValue["Name"] = "LockService";
Ratan Gupta734a1c32019-12-14 11:53:48 +0530325
Sunitha Harishdb81c072021-01-21 23:33:21 -0600326 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530327 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600328 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530329 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600330 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530331 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600332 return;
Ratan Gupta734a1c32019-12-14 11:53:48 +0530333}
334
Sunitha Harishdb81c072021-01-21 23:33:21 -0600335inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
336 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500337{
338 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
339 std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" +
340 fileID);
341 if (!std::filesystem::exists(loc))
342 {
343 BMCWEB_LOG_ERROR << loc << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600344 asyncResp->res.result(boost::beast::http::status::not_found);
345 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500346 return;
347 }
348
349 std::ifstream readfile(loc.string());
350 if (!readfile)
351 {
352 BMCWEB_LOG_ERROR << loc.string() << "Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600353 asyncResp->res.result(boost::beast::http::status::not_found);
354 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500355 return;
356 }
357
358 std::string contentDispositionParam =
359 "attachment; filename=\"" + fileID + "\"";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600360 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500361 std::string fileData;
362 fileData = {std::istreambuf_iterator<char>(readfile),
363 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600364 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500365 return;
366}
367
Sunitha Harishdb81c072021-01-21 23:33:21 -0600368inline void
369 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
370 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500371{
372 std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID);
373 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700374 std::ifstream fileOpen(filePath.c_str());
375 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700376 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500377 if (remove(filePath.c_str()) == 0)
378 {
379 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600380 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500381 }
382 else
383 {
384 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600385 asyncResp->res.result(
386 boost::beast::http::status::internal_server_error);
387 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500388 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700389 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500390 else
391 {
392 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600393 asyncResp->res.result(boost::beast::http::status::not_found);
394 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600395 }
396 return;
397}
398
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500399inline void handleBroadcastService(const crow::Request& req,
400 crow::Response& res)
401{
402 std::string broadcastMsg;
403
Sunitha Harishdb81c072021-01-21 23:33:21 -0600404 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
405 std::make_shared<bmcweb::AsyncResp>(res);
406
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500407 if (!redfish::json_util::readJson(req, res, "Message", broadcastMsg))
408 {
409 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600410 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500411 return;
412 }
413 if (broadcastMsg.size() > maxBroadcastMsgSize)
414 {
415 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600416 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500417 return;
418 }
419 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500420 return;
421}
422
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500423inline void handleFileUrl(const crow::Request& req, crow::Response& res,
424 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600425{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600426 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
427 std::make_shared<bmcweb::AsyncResp>(res);
428
Ed Tanousb41187f2019-10-24 16:30:02 -0700429 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600430 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600431 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600432 return;
433 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700434 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500435 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600436 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500437 return;
438 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700439 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500440 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600441 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500442 return;
443 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600444}
Ratan Gupta453fed02019-12-14 09:39:47 +0530445
Sunitha Harishdb81c072021-01-21 23:33:21 -0600446inline void
447 handleAcquireLockAPI(const crow::Request& req,
448 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
449 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530450{
451 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530453 {
454 std::string lockType;
455 uint64_t resourceId;
456
457 SegmentFlags segInfo;
458 std::vector<nlohmann::json> segmentFlags;
459
Sunitha Harishdb81c072021-01-21 23:33:21 -0600460 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
461 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530462 "SegmentFlags", segmentFlags))
463 {
464 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600465 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530466 return;
467 }
468 BMCWEB_LOG_DEBUG << lockType;
469 BMCWEB_LOG_DEBUG << resourceId;
470
471 BMCWEB_LOG_DEBUG << "Segment Flags are present";
472
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500473 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530474 {
475 std::string lockFlags;
476 uint32_t segmentLength;
477
Sunitha Harishdb81c072021-01-21 23:33:21 -0600478 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
479 lockFlags, "SegmentLength",
480 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530481 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600482 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530483 return;
484 }
485
486 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
487 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
488
489 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
490 }
Manojkiran Eda566329e2020-05-22 12:36:17 +0530491 lockRequestStructure.push_back(
492 make_tuple(req.session->uniqueId, req.session->clientId, lockType,
493 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530494 }
495
496 // print lock request into journal
497
Ed Tanous4e087512020-09-28 18:41:25 -0700498 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530499 {
Ed Tanous4e087512020-09-28 18:41:25 -0700500 BMCWEB_LOG_DEBUG << std::get<0>(i);
501 BMCWEB_LOG_DEBUG << std::get<1>(i);
502 BMCWEB_LOG_DEBUG << std::get<2>(i);
503 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530504
Ed Tanous4e087512020-09-28 18:41:25 -0700505 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530506 {
507 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
508 }
509 }
510
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500511 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530512
Ratan Gupta07386c62019-12-14 14:06:09 +0530513 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530514
515 if (varAcquireLock.first)
516 {
517 // Either validity failure of there is a conflict with itself
518
519 auto validityStatus =
520 std::get<std::pair<bool, int>>(varAcquireLock.second);
521
522 if ((!validityStatus.first) && (validityStatus.second == 0))
523 {
524 BMCWEB_LOG_DEBUG << "Not a Valid record";
525 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600526 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530527 return;
528 }
529 if (validityStatus.first && (validityStatus.second == 1))
530 {
531 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600532 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530533 return;
534 }
535 }
536 else
537 {
538 auto conflictStatus =
539 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
540 if (!conflictStatus.first)
541 {
542 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600543 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530544
545 auto var = std::get<uint32_t>(conflictStatus.second);
546 nlohmann::json returnJson;
547 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600548 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530549 return;
550 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700551 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600552 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700553 auto var =
554 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
555 nlohmann::json returnJson, segments;
556 nlohmann::json myarray = nlohmann::json::array();
557 returnJson["TransactionID"] = var.first;
558 returnJson["SessionID"] = std::get<0>(var.second);
559 returnJson["HMCID"] = std::get<1>(var.second);
560 returnJson["LockType"] = std::get<2>(var.second);
561 returnJson["ResourceID"] = std::get<3>(var.second);
562
563 for (auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530564 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700565 segments["LockFlag"] = i.first;
566 segments["SegmentLength"] = i.second;
567 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530568 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700569
570 returnJson["SegmentFlags"] = myarray;
571
Sunitha Harishdb81c072021-01-21 23:33:21 -0600572 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700573 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530574 }
575}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600576inline void
577 handleRelaseAllAPI(const crow::Request& req,
578 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530579{
580 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600581 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530582 return;
583}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530584
Ed Tanous02379d32020-09-15 21:15:44 -0700585inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600586 handleReleaseLockAPI(const crow::Request& req,
587 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700588 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530589{
590 BMCWEB_LOG_DEBUG << listTransactionIds.size();
591 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700592 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530593 {
Ed Tanous4e087512020-09-28 18:41:25 -0700594 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530595 }
596
manojkiraneda3b6dea62019-12-13 17:05:36 +0530597 // validate the request ids
598
Ratan Gupta07386c62019-12-14 14:06:09 +0530599 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Manojkiran Eda566329e2020-05-22 12:36:17 +0530600 listTransactionIds,
601 std::make_pair(req.session->clientId, req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530602
603 if (!varReleaselock.first)
604 {
605 // validation Failed
Sunitha Harishdb81c072021-01-21 23:33:21 -0600606 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530607 return;
608 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700609 auto statusRelease =
610 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
611 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530612 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700613 // The current hmc owns all the locks, so we already released
614 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700615 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530616 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700617
618 // valid rid, but the current hmc does not own all the locks
619 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600620 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700621
622 auto var = statusRelease.second;
623 nlohmann::json returnJson, segments;
624 nlohmann::json myArray = nlohmann::json::array();
625 returnJson["TransactionID"] = var.first;
626 returnJson["SessionID"] = std::get<0>(var.second);
627 returnJson["HMCID"] = std::get<1>(var.second);
628 returnJson["LockType"] = std::get<2>(var.second);
629 returnJson["ResourceID"] = std::get<3>(var.second);
630
631 for (auto& i : std::get<4>(var.second))
632 {
633 segments["LockFlag"] = i.first;
634 segments["SegmentLength"] = i.second;
635 myArray.push_back(segments);
636 }
637
638 returnJson["SegmentFlags"] = myArray;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600639 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700640 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530641}
642
Sunitha Harishdb81c072021-01-21 23:33:21 -0600643inline void
644 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
645 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530646{
647 BMCWEB_LOG_DEBUG << listSessionIds.size();
648
Ratan Gupta07386c62019-12-14 14:06:09 +0530649 auto status =
650 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530651 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
652
653 nlohmann::json lockRecords = nlohmann::json::array();
654
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500655 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530656 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500657 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530658 {
659 nlohmann::json returnJson;
660
661 returnJson["TransactionID"] = transactionId.first;
662 returnJson["SessionID"] = std::get<0>(lockRecord);
663 returnJson["HMCID"] = std::get<1>(lockRecord);
664 returnJson["LockType"] = std::get<2>(lockRecord);
665 returnJson["ResourceID"] = std::get<3>(lockRecord);
666
667 nlohmann::json segments;
668 nlohmann::json segmentInfoArray = nlohmann::json::array();
669
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500670 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530671 {
672 segments["LockFlag"] = segment.first;
673 segments["SegmentLength"] = segment.second;
674 segmentInfoArray.push_back(segments);
675 }
676
677 returnJson["SegmentFlags"] = segmentInfoArray;
678 lockRecords.push_back(returnJson);
679 }
680 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600681 asyncResp->res.result(boost::beast::http::status::ok);
682 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530683}
684
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500685inline bool isValidConfigFileName(const std::string& fileName,
686 crow::Response& res)
687{
688 if (fileName.empty())
689 {
690 BMCWEB_LOG_ERROR << "Empty filename";
691 res.jsonValue["Description"] = "Empty file path in the url";
692 return false;
693 }
694
695 // ConfigFile name is allowed to take upper and lowercase letters,
696 // numbers and hyphen
697 std::size_t found = fileName.find_first_not_of(
698 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
699 if (found != std::string::npos)
700 {
701 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
702 res.jsonValue["Description"] = "Unsupported character in filename";
703 return false;
704 }
705
706 // Check the filename length
707 if (fileName.length() > 20)
708 {
709 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
710 "Input filename length is: "
711 << fileName.length();
712 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
713 return false;
714 }
715
716 return true;
717}
718
Ed Tanous02379d32020-09-15 21:15:44 -0700719inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530720{
721
722 // allowed only for admin
723 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous23a21a12020-07-25 04:45:05 +0000724 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700725 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000726 [](const crow::Request&, crow::Response& res) {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600727 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
728 std::make_shared<bmcweb::AsyncResp>(res);
729
730 asyncResp->res.jsonValue["@odata.type"] =
Ratan Gupta453fed02019-12-14 09:39:47 +0530731 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600732 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
733 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
734 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
735 asyncResp->res.jsonValue["ConfigFiles"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530736 {"@odata.id", "/ibm/v1/Host/ConfigFiles"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600737 asyncResp->res.jsonValue["LockService"] = {
Ratan Gupta453fed02019-12-14 09:39:47 +0530738 {"@odata.id", "/ibm/v1/HMC/LockService"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600739 asyncResp->res.jsonValue["BroadcastService"] = {
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500740 {"@odata.id", "/ibm/v1/HMC/BroadcastService"}};
Ratan Gupta453fed02019-12-14 09:39:47 +0530741 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600742
Ratan Guptad3630cb2019-12-14 11:21:35 +0530743 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous23a21a12020-07-25 04:45:05 +0000744 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700745 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000746 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530747 handleConfigFileList(res);
748 });
749
750 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500751 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous23a21a12020-07-25 04:45:05 +0000752 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700753 .methods(boost::beast::http::verb::post)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000754 [](const crow::Request&, crow::Response& res) {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530755 deleteConfigFiles(res);
756 });
757
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500758 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous23a21a12020-07-25 04:45:05 +0000759 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500760 .methods(
761 boost::beast::http::verb::put, boost::beast::http::verb::get,
762 boost::beast::http::verb::delete_)([](const crow::Request& req,
763 crow::Response& res,
764 const std::string& fileName) {
765 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
766 std::make_shared<bmcweb::AsyncResp>(res);
767 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
768 // Validate the incoming fileName
769 if (!isValidConfigFileName(fileName, res))
770 {
771 asyncResp->res.result(boost::beast::http::status::bad_request);
772 return;
773 }
774 handleFileUrl(req, res, fileName);
775 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530776
777 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000778 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700779 .methods(boost::beast::http::verb::get)(
Ed Tanouscb13a392020-07-25 19:02:03 +0000780 [](const crow::Request&, crow::Response& res) {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530781 getLockServiceData(res);
782 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530783
784 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000785 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harishdb81c072021-01-21 23:33:21 -0600786 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
787 crow::Response& res) {
788 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
789 std::make_shared<bmcweb::AsyncResp>(res);
790
791 std::vector<nlohmann::json> body;
792 if (!redfish::json_util::readJson(req, res, "Request", body))
793 {
794 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
795 asyncResp->res.result(boost::beast::http::status::bad_request);
796 return;
797 }
798 handleAcquireLockAPI(req, asyncResp, body);
799 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530800 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous23a21a12020-07-25 04:45:05 +0000801 .privileges({"ConfigureComponents", "ConfigureManager"})
Ed Tanousb41187f2019-10-24 16:30:02 -0700802 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
803 crow::Response& res) {
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530804 std::string type;
805 std::vector<uint32_t> listTransactionIds;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600806 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
807 std::make_shared<bmcweb::AsyncResp>(res);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530808
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530809 if (!redfish::json_util::readJson(req, res, "Type", type,
810 "TransactionIDs",
811 listTransactionIds))
812 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600813 asyncResp->res.result(boost::beast::http::status::bad_request);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530814 return;
815 }
816 if (type == "Transaction")
817 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600818 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530819 }
820 else if (type == "Session")
821 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600822 handleRelaseAllAPI(req, asyncResp);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530823 }
824 else
825 {
826 BMCWEB_LOG_DEBUG << " Value of Type : " << type
827 << "is Not a Valid key";
828 redfish::messages::propertyValueNotInList(res, type, "Type");
829 }
830 });
manojkiraneda402b5712019-12-13 17:07:09 +0530831 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous23a21a12020-07-25 04:45:05 +0000832 .privileges({"ConfigureComponents", "ConfigureManager"})
Sunitha Harishdb81c072021-01-21 23:33:21 -0600833 .methods(boost::beast::http::verb::post)([](const crow::Request& req,
834 crow::Response& res) {
835 ListOfSessionIds listSessionIds;
836 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
837 std::make_shared<bmcweb::AsyncResp>(res);
manojkiraneda402b5712019-12-13 17:07:09 +0530838
Sunitha Harishdb81c072021-01-21 23:33:21 -0600839 if (!redfish::json_util::readJson(req, res, "SessionIDs",
840 listSessionIds))
841 {
842 asyncResp->res.result(boost::beast::http::status::bad_request);
843 return;
844 }
845 handleGetLockListAPI(asyncResp, listSessionIds);
846 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500847
848 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous23a21a12020-07-25 04:45:05 +0000849 .privileges({"ConfigureComponents", "ConfigureManager"})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500850 .methods(boost::beast::http::verb::post)(
851 [](const crow::Request& req, crow::Response& res) {
852 handleBroadcastService(req, res);
853 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530854}
855
856} // namespace ibm_mc
857} // namespace crow