blob: 6e4e7bbbfc12d79dd56653e163462e840affdb05 [file] [log] [blame]
Ratan Gupta453fed02019-12-14 09:39:47 +05301#pragma once
Ratan Gupta453fed02019-12-14 09:39:47 +05302
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
4#include "async_resp.hpp"
5#include "error_messages.hpp"
6#include "event_service_manager.hpp"
7#include "ibm/locks.hpp"
8#include "resource_messages.hpp"
9#include "utils/json_utils.hpp"
10
Ed Tanous11ba3972022-07-11 09:50:41 -070011#include <boost/algorithm/string/predicate.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060012#include <boost/container/flat_set.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +053013#include <nlohmann/json.hpp>
Sunitha Harish97b0e432019-11-21 04:59:29 -060014#include <sdbusplus/message/types.hpp>
15
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 =
Sunitha Harishf8a43472022-08-08 02:07:12 -050037 25000000; // Allow save area dir size to be max 25MB
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050038constexpr 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
Sunitha Harishdb81c072021-01-21 23:33:21 -060045inline void handleFilePut(const crow::Request& req,
46 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -070047 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -060048{
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050049 std::error_code ec;
Sunitha Harish97b0e432019-11-21 04:59:29 -060050 // Check the content-type of the request
Sunitha Harish086d32c2021-02-01 02:11:49 -060051 boost::beast::string_view contentType = req.getHeaderValue("content-type");
52 if (!boost::iequals(contentType, "application/octet-stream"))
Sunitha Harish97b0e432019-11-21 04:59:29 -060053 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060054 asyncResp->res.result(boost::beast::http::status::not_acceptable);
55 asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -060056 return;
57 }
Sunitha Harish086d32c2021-02-01 02:11:49 -060058 BMCWEB_LOG_DEBUG
59 << "File upload in application/octet-stream format. Continue..";
asmithakarun1c7b07c2019-09-09 03:42:59 -050060
61 BMCWEB_LOG_DEBUG
62 << "handleIbmPut: Request to create/update the save-area file";
Sunitha Harish3e919b52020-10-13 01:21:48 -050063 std::string_view path =
64 "/var/lib/bmcweb/ibm-management-console/configfiles";
65 if (!crow::ibm_utils::createDirectory(path))
Sunitha Harish97b0e432019-11-21 04:59:29 -060066 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060067 asyncResp->res.result(boost::beast::http::status::not_found);
68 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -050069 return;
70 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050071
asmithakarun1c7b07c2019-09-09 03:42:59 -050072 std::ofstream file;
Sunitha Harish3e919b52020-10-13 01:21:48 -050073 std::filesystem::path loc(
74 "/var/lib/bmcweb/ibm-management-console/configfiles");
Sunitha Harish97b0e432019-11-21 04:59:29 -060075
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050076 // Get the current size of the savearea directory
77 std::filesystem::recursive_directory_iterator iter(loc, ec);
78 if (ec)
79 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060080 asyncResp->res.result(
81 boost::beast::http::status::internal_server_error);
82 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050083 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
84 "directory iterator. ec : "
85 << ec;
86 return;
87 }
88 std::uintmax_t saveAreaDirSize = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -080089 for (const auto& it : iter)
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050090 {
91 if (!std::filesystem::is_directory(it, ec))
92 {
93 if (ec)
94 {
Sunitha Harishdb81c072021-01-21 23:33:21 -060095 asyncResp->res.result(
96 boost::beast::http::status::internal_server_error);
97 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -050098 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
99 "directory . ec : "
100 << ec;
101 return;
102 }
103 std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
104 if (ec)
105 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600106 asyncResp->res.result(
107 boost::beast::http::status::internal_server_error);
108 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500109 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
110 "file size inside the directory . ec : "
111 << ec;
112 return;
113 }
114 saveAreaDirSize += fileSize;
115 }
116 }
117 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
118
119 // Get the file size getting uploaded
Ed Tanous3174e4d2020-10-07 11:41:22 -0700120 const std::string& data = req.body;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500121 BMCWEB_LOG_DEBUG << "data length: " << data.length();
122
123 if (data.length() < minSaveareaFileSize)
124 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600125 asyncResp->res.result(boost::beast::http::status::bad_request);
126 asyncResp->res.jsonValue["Description"] =
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500127 "File size is less than minimum allowed size[100B]";
128 return;
129 }
130 if (data.length() > maxSaveareaFileSize)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500131 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600132 asyncResp->res.result(boost::beast::http::status::bad_request);
133 asyncResp->res.jsonValue["Description"] =
Ratan Guptae46946a2020-05-11 13:22:59 +0530134 "File size exceeds maximum allowed size[500KB]";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500135 return;
136 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500137
138 // Form the file path
139 loc /= fileID;
Ed Tanous8cc8ede2022-02-28 10:20:59 -0800140 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc.string();
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500141
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500142 // Check if the same file exists in the directory
143 bool fileExists = std::filesystem::exists(loc, ec);
144 if (ec)
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500145 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600146 asyncResp->res.result(
147 boost::beast::http::status::internal_server_error);
148 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500149 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
150 << ec;
151 return;
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500152 }
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500153
154 std::uintmax_t newSizeToWrite = 0;
155 if (fileExists)
156 {
157 // File exists. Get the current file size
158 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec);
159 if (ec)
160 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600161 asyncResp->res.result(
162 boost::beast::http::status::internal_server_error);
163 asyncResp->res.jsonValue["Description"] = internalServerError;
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500164 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
165 << ec;
166 return;
167 }
168 // Calculate the difference in the file size.
169 // If the data.length is greater than the existing file size, then
170 // calculate the difference. Else consider the delta size as zero -
171 // because there is no increase in the total directory size.
172 // We need to add the diff only if the incoming data is larger than the
173 // existing filesize
174 if (data.length() > currentFileSize)
175 {
176 newSizeToWrite = data.length() - currentFileSize;
177 }
178 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
179 }
180 else
181 {
182 // This is a new file upload
183 newSizeToWrite = data.length();
184 }
185
186 // Calculate the total dir size before writing the new file
187 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
188
189 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
190 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600191 asyncResp->res.result(boost::beast::http::status::bad_request);
192 asyncResp->res.jsonValue["Description"] =
193 "File size does not fit in the savearea "
Sunitha Harishf8a43472022-08-08 02:07:12 -0500194 "directory maximum allowed size[25MB]";
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500195 return;
196 }
197
asmithakarun1c7b07c2019-09-09 03:42:59 -0500198 file.open(loc, std::ofstream::out);
Sunitha Harish3e919b52020-10-13 01:21:48 -0500199
200 // set the permission of the file to 600
201 std::filesystem::perms permission = std::filesystem::perms::owner_write |
202 std::filesystem::perms::owner_read;
203 std::filesystem::permissions(loc, permission);
204
asmithakarun1c7b07c2019-09-09 03:42:59 -0500205 if (file.fail())
206 {
207 BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600208 asyncResp->res.result(
209 boost::beast::http::status::internal_server_error);
210 asyncResp->res.jsonValue["Description"] =
211 "Error while creating the file";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500212 return;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600213 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700214 file << data;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500215
Ed Tanous3174e4d2020-10-07 11:41:22 -0700216 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID;
217 // Push an event
218 if (fileExists)
219 {
220 BMCWEB_LOG_DEBUG << "config file is updated";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600221 asyncResp->res.jsonValue["Description"] = "File Updated";
Ed Tanous3174e4d2020-10-07 11:41:22 -0700222
223 redfish::EventServiceManager::getInstance().sendEvent(
224 redfish::messages::resourceChanged(), origin, "IBMConfigFile");
225 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600226 else
227 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700228 BMCWEB_LOG_DEBUG << "config file is created";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600229 asyncResp->res.jsonValue["Description"] = "File Created";
Asmitha Karunanithi10693fa2020-07-27 02:27:49 -0500230
Ed Tanous3174e4d2020-10-07 11:41:22 -0700231 redfish::EventServiceManager::getInstance().sendEvent(
232 redfish::messages::resourceCreated(), origin, "IBMConfigFile");
asmithakarun1c7b07c2019-09-09 03:42:59 -0500233 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530234}
asmithakarun1c7b07c2019-09-09 03:42:59 -0500235
zhanghch058d1b46d2021-04-01 11:18:24 +0800236inline void
237 handleConfigFileList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530238{
239 std::vector<std::string> pathObjList;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500240 std::filesystem::path loc(
241 "/var/lib/bmcweb/ibm-management-console/configfiles");
Ratan Guptad3630cb2019-12-14 11:21:35 +0530242 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
243 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500244 for (const auto& file : std::filesystem::directory_iterator(loc))
Ratan Guptad3630cb2019-12-14 11:21:35 +0530245 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700246 const std::filesystem::path& pathObj = file.path();
cm-jishnu5a193962022-12-02 03:45:27 -0600247 if (std::filesystem::is_regular_file(pathObj))
248 {
249 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" +
250 pathObj.filename().string());
251 }
Ratan Guptad3630cb2019-12-14 11:21:35 +0530252 }
253 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600254 asyncResp->res.jsonValue["@odata.type"] =
255 "#IBMConfigFile.v1_0_0.IBMConfigFile";
256 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/";
257 asyncResp->res.jsonValue["Id"] = "ConfigFiles";
258 asyncResp->res.jsonValue["Name"] = "ConfigFiles";
Ratan Guptad3630cb2019-12-14 11:21:35 +0530259
Sunitha Harishdb81c072021-01-21 23:33:21 -0600260 asyncResp->res.jsonValue["Members"] = std::move(pathObjList);
261 asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = {
Ratan Guptad3630cb2019-12-14 11:21:35 +0530262 {"target",
Sunitha Harishe56f2542020-07-22 02:38:59 -0500263 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}};
Ratan Guptad3630cb2019-12-14 11:21:35 +0530264}
265
zhanghch058d1b46d2021-04-01 11:18:24 +0800266inline void
267 deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad3630cb2019-12-14 11:21:35 +0530268{
Ratan Guptad3630cb2019-12-14 11:21:35 +0530269 std::error_code ec;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500270 std::filesystem::path loc(
271 "/var/lib/bmcweb/ibm-management-console/configfiles");
Ratan Guptad3630cb2019-12-14 11:21:35 +0530272 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc))
273 {
274 std::filesystem::remove_all(loc, ec);
275 if (ec)
276 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600277 asyncResp->res.result(
278 boost::beast::http::status::internal_server_error);
279 asyncResp->res.jsonValue["Description"] = internalServerError;
Ratan Guptad3630cb2019-12-14 11:21:35 +0530280 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
281 "config files directory. ec : "
282 << ec;
283 }
284 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500285}
286
zhanghch058d1b46d2021-04-01 11:18:24 +0800287inline void
288 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Gupta734a1c32019-12-14 11:53:48 +0530289{
Sunitha Harishdb81c072021-01-21 23:33:21 -0600290 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService";
291 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/";
292 asyncResp->res.jsonValue["Id"] = "LockService";
293 asyncResp->res.jsonValue["Name"] = "LockService";
Ratan Gupta734a1c32019-12-14 11:53:48 +0530294
Sunitha Harishdb81c072021-01-21 23:33:21 -0600295 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530296 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600297 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530298 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600299 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = {
Ratan Gupta734a1c32019-12-14 11:53:48 +0530300 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}};
Ratan Gupta734a1c32019-12-14 11:53:48 +0530301}
302
Sunitha Harishdb81c072021-01-21 23:33:21 -0600303inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500305{
306 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
Sunitha Harish3e919b52020-10-13 01:21:48 -0500307 std::filesystem::path loc(
308 "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID);
cm-jishnu5a193962022-12-02 03:45:27 -0600309 if (!std::filesystem::exists(loc) || !std::filesystem::is_regular_file(loc))
asmithakarun1c7b07c2019-09-09 03:42:59 -0500310 {
Sunitha Harishf8a43472022-08-08 02:07:12 -0500311 BMCWEB_LOG_ERROR << loc.string() << " Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600312 asyncResp->res.result(boost::beast::http::status::not_found);
313 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500314 return;
315 }
316
317 std::ifstream readfile(loc.string());
318 if (!readfile)
319 {
Sunitha Harishf8a43472022-08-08 02:07:12 -0500320 BMCWEB_LOG_ERROR << loc.string() << " Not found";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600321 asyncResp->res.result(boost::beast::http::status::not_found);
322 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500323 return;
324 }
325
326 std::string contentDispositionParam =
327 "attachment; filename=\"" + fileID + "\"";
Ed Tanousd9f6c622022-03-17 09:12:17 -0700328 asyncResp->res.addHeader(boost::beast::http::field::content_disposition,
329 contentDispositionParam);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500330 std::string fileData;
331 fileData = {std::istreambuf_iterator<char>(readfile),
332 std::istreambuf_iterator<char>()};
Sunitha Harishdb81c072021-01-21 23:33:21 -0600333 asyncResp->res.jsonValue["Data"] = fileData;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500334}
335
Sunitha Harishdb81c072021-01-21 23:33:21 -0600336inline void
337 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
338 const std::string& fileID)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500339{
Sunitha Harish3e919b52020-10-13 01:21:48 -0500340 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" +
341 fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500342 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
Ed Tanous2c70f802020-09-28 14:29:23 -0700343 std::ifstream fileOpen(filePath.c_str());
344 if (static_cast<bool>(fileOpen))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700345 {
asmithakarun1c7b07c2019-09-09 03:42:59 -0500346 if (remove(filePath.c_str()) == 0)
347 {
348 BMCWEB_LOG_DEBUG << "File removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600349 asyncResp->res.jsonValue["Description"] = "File Deleted";
asmithakarun1c7b07c2019-09-09 03:42:59 -0500350 }
351 else
352 {
353 BMCWEB_LOG_ERROR << "File not removed!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600354 asyncResp->res.result(
355 boost::beast::http::status::internal_server_error);
356 asyncResp->res.jsonValue["Description"] = internalServerError;
asmithakarun1c7b07c2019-09-09 03:42:59 -0500357 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700358 }
asmithakarun1c7b07c2019-09-09 03:42:59 -0500359 else
360 {
361 BMCWEB_LOG_ERROR << "File not found!\n";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600362 asyncResp->res.result(boost::beast::http::status::not_found);
363 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
Sunitha Harish97b0e432019-11-21 04:59:29 -0600364 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600365}
366
zhanghch058d1b46d2021-04-01 11:18:24 +0800367inline void
368 handleBroadcastService(const crow::Request& req,
369 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500370{
371 std::string broadcastMsg;
372
Willy Tu15ed6782021-12-14 11:03:16 -0800373 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message",
374 broadcastMsg))
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500375 {
376 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600377 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500378 return;
379 }
380 if (broadcastMsg.size() > maxBroadcastMsgSize)
381 {
382 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600383 asyncResp->res.result(boost::beast::http::status::bad_request);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500384 return;
385 }
386 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg);
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500387}
388
zhanghch058d1b46d2021-04-01 11:18:24 +0800389inline void handleFileUrl(const crow::Request& req,
390 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500391 const std::string& fileID)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600392{
Ed Tanousb41187f2019-10-24 16:30:02 -0700393 if (req.method() == boost::beast::http::verb::put)
Sunitha Harish97b0e432019-11-21 04:59:29 -0600394 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600395 handleFilePut(req, asyncResp, fileID);
Sunitha Harish97b0e432019-11-21 04:59:29 -0600396 return;
397 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700398 if (req.method() == boost::beast::http::verb::get)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500399 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600400 handleFileGet(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500401 return;
402 }
Ed Tanousb41187f2019-10-24 16:30:02 -0700403 if (req.method() == boost::beast::http::verb::delete_)
asmithakarun1c7b07c2019-09-09 03:42:59 -0500404 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600405 handleFileDelete(asyncResp, fileID);
asmithakarun1c7b07c2019-09-09 03:42:59 -0500406 return;
407 }
Sunitha Harish97b0e432019-11-21 04:59:29 -0600408}
Ratan Gupta453fed02019-12-14 09:39:47 +0530409
Sunitha Harishdb81c072021-01-21 23:33:21 -0600410inline void
411 handleAcquireLockAPI(const crow::Request& req,
412 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
413 std::vector<nlohmann::json> body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530414{
415 LockRequests lockRequestStructure;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500416 for (auto& element : body)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530417 {
418 std::string lockType;
Ed Tanous543f4402022-01-06 13:12:53 -0800419 uint64_t resourceId = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530420
421 SegmentFlags segInfo;
422 std::vector<nlohmann::json> segmentFlags;
423
Sunitha Harishdb81c072021-01-21 23:33:21 -0600424 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType",
425 lockType, "ResourceID", resourceId,
manojkiraneda0b631ae2019-12-03 17:54:28 +0530426 "SegmentFlags", segmentFlags))
427 {
428 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600429 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530430 return;
431 }
432 BMCWEB_LOG_DEBUG << lockType;
433 BMCWEB_LOG_DEBUG << resourceId;
434
435 BMCWEB_LOG_DEBUG << "Segment Flags are present";
436
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500437 for (auto& e : segmentFlags)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530438 {
439 std::string lockFlags;
Ed Tanous543f4402022-01-06 13:12:53 -0800440 uint32_t segmentLength = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530441
Sunitha Harishdb81c072021-01-21 23:33:21 -0600442 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag",
443 lockFlags, "SegmentLength",
444 segmentLength))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530445 {
Sunitha Harishdb81c072021-01-21 23:33:21 -0600446 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530447 return;
448 }
449
450 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
451 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
452
453 segInfo.push_back(std::make_pair(lockFlags, segmentLength));
454 }
Ed Tanousbb759e32022-08-02 17:07:54 -0700455
456 lockRequestStructure.push_back(make_tuple(
457 req.session->uniqueId, req.session->clientId.value_or(""), lockType,
458 resourceId, segInfo));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530459 }
460
461 // print lock request into journal
462
Ed Tanous4e087512020-09-28 18:41:25 -0700463 for (auto& i : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530464 {
Ed Tanous4e087512020-09-28 18:41:25 -0700465 BMCWEB_LOG_DEBUG << std::get<0>(i);
466 BMCWEB_LOG_DEBUG << std::get<1>(i);
467 BMCWEB_LOG_DEBUG << std::get<2>(i);
468 BMCWEB_LOG_DEBUG << std::get<3>(i);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530469
Ed Tanous4e087512020-09-28 18:41:25 -0700470 for (const auto& p : std::get<4>(i))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530471 {
472 BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
473 }
474 }
475
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500476 const LockRequests& t = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530477
Ratan Gupta07386c62019-12-14 14:06:09 +0530478 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530479
480 if (varAcquireLock.first)
481 {
482 // Either validity failure of there is a conflict with itself
483
484 auto validityStatus =
485 std::get<std::pair<bool, int>>(varAcquireLock.second);
486
487 if ((!validityStatus.first) && (validityStatus.second == 0))
488 {
489 BMCWEB_LOG_DEBUG << "Not a Valid record";
490 BMCWEB_LOG_DEBUG << "Bad json in request";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600491 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530492 return;
493 }
494 if (validityStatus.first && (validityStatus.second == 1))
495 {
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500496 BMCWEB_LOG_ERROR << "There is a conflict within itself";
Sunitha Harishf8a43472022-08-08 02:07:12 -0500497 asyncResp->res.result(boost::beast::http::status::conflict);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530498 return;
499 }
500 }
501 else
502 {
503 auto conflictStatus =
504 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
505 if (!conflictStatus.first)
506 {
507 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600508 asyncResp->res.result(boost::beast::http::status::ok);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530509
510 auto var = std::get<uint32_t>(conflictStatus.second);
511 nlohmann::json returnJson;
512 returnJson["id"] = var;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600513 asyncResp->res.jsonValue["TransactionID"] = var;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530514 return;
515 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700516 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600517 asyncResp->res.result(boost::beast::http::status::conflict);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700518 auto var =
519 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
Ed Tanouse05aec52022-01-25 10:28:56 -0800520 nlohmann::json returnJson;
521 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700522 nlohmann::json myarray = nlohmann::json::array();
523 returnJson["TransactionID"] = var.first;
524 returnJson["SessionID"] = std::get<0>(var.second);
525 returnJson["HMCID"] = std::get<1>(var.second);
526 returnJson["LockType"] = std::get<2>(var.second);
527 returnJson["ResourceID"] = std::get<3>(var.second);
528
Ed Tanous02cad962022-06-30 16:50:15 -0700529 for (const auto& i : std::get<4>(var.second))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530530 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700531 segments["LockFlag"] = i.first;
532 segments["SegmentLength"] = i.second;
533 myarray.push_back(segments);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530534 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700535
536 returnJson["SegmentFlags"] = myarray;
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500537 BMCWEB_LOG_ERROR << "Conflicting lock record: " << returnJson;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600538 asyncResp->res.jsonValue["Record"] = returnJson;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700539 return;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530540 }
541}
Sunitha Harishdb81c072021-01-21 23:33:21 -0600542inline void
543 handleRelaseAllAPI(const crow::Request& req,
544 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530545{
546 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId);
Sunitha Harishdb81c072021-01-21 23:33:21 -0600547 asyncResp->res.result(boost::beast::http::status::ok);
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530548}
manojkiraneda0b631ae2019-12-03 17:54:28 +0530549
Ed Tanous02379d32020-09-15 21:15:44 -0700550inline void
Sunitha Harishdb81c072021-01-21 23:33:21 -0600551 handleReleaseLockAPI(const crow::Request& req,
552 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous02379d32020-09-15 21:15:44 -0700553 const std::vector<uint32_t>& listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530554{
555 BMCWEB_LOG_DEBUG << listTransactionIds.size();
556 BMCWEB_LOG_DEBUG << "Data is present";
Ed Tanous4e087512020-09-28 18:41:25 -0700557 for (unsigned int listTransactionId : listTransactionIds)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530558 {
Ed Tanous4e087512020-09-28 18:41:25 -0700559 BMCWEB_LOG_DEBUG << listTransactionId;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530560 }
561
manojkiraneda3b6dea62019-12-13 17:05:36 +0530562 // validate the request ids
563
Ratan Gupta07386c62019-12-14 14:06:09 +0530564 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock(
Ed Tanousbb759e32022-08-02 17:07:54 -0700565 listTransactionIds, std::make_pair(req.session->clientId.value_or(""),
566 req.session->uniqueId));
manojkiraneda3b6dea62019-12-13 17:05:36 +0530567
568 if (!varReleaselock.first)
569 {
570 // validation Failed
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500571 BMCWEB_LOG_ERROR << "handleReleaseLockAPI: validation failed";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600572 asyncResp->res.result(boost::beast::http::status::bad_request);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530573 return;
574 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700575 auto statusRelease =
576 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second);
577 if (statusRelease.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530578 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700579 // The current hmc owns all the locks, so we already released
580 // them
Ed Tanous3174e4d2020-10-07 11:41:22 -0700581 return;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530582 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700583
584 // valid rid, but the current hmc does not own all the locks
585 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
Sunitha Harishdb81c072021-01-21 23:33:21 -0600586 asyncResp->res.result(boost::beast::http::status::unauthorized);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700587
588 auto var = statusRelease.second;
Ed Tanouse05aec52022-01-25 10:28:56 -0800589 nlohmann::json returnJson;
590 nlohmann::json segments;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700591 nlohmann::json myArray = nlohmann::json::array();
592 returnJson["TransactionID"] = var.first;
593 returnJson["SessionID"] = std::get<0>(var.second);
594 returnJson["HMCID"] = std::get<1>(var.second);
595 returnJson["LockType"] = std::get<2>(var.second);
596 returnJson["ResourceID"] = std::get<3>(var.second);
597
Ed Tanous02cad962022-06-30 16:50:15 -0700598 for (const auto& i : std::get<4>(var.second))
Ed Tanous3174e4d2020-10-07 11:41:22 -0700599 {
600 segments["LockFlag"] = i.first;
601 segments["SegmentLength"] = i.second;
602 myArray.push_back(segments);
603 }
604
605 returnJson["SegmentFlags"] = myArray;
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500606 BMCWEB_LOG_DEBUG << "handleReleaseLockAPI: lockrecord: " << returnJson;
Sunitha Harishdb81c072021-01-21 23:33:21 -0600607 asyncResp->res.jsonValue["Record"] = returnJson;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530608}
609
Sunitha Harishdb81c072021-01-21 23:33:21 -0600610inline void
611 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
612 const ListOfSessionIds& listSessionIds)
manojkiraneda402b5712019-12-13 17:07:09 +0530613{
614 BMCWEB_LOG_DEBUG << listSessionIds.size();
615
Ratan Gupta07386c62019-12-14 14:06:09 +0530616 auto status =
617 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
manojkiraneda402b5712019-12-13 17:07:09 +0530618 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
619
620 nlohmann::json lockRecords = nlohmann::json::array();
621
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500622 for (const auto& transactionId : var)
manojkiraneda402b5712019-12-13 17:07:09 +0530623 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500624 for (const auto& lockRecord : transactionId.second)
manojkiraneda402b5712019-12-13 17:07:09 +0530625 {
626 nlohmann::json returnJson;
627
628 returnJson["TransactionID"] = transactionId.first;
629 returnJson["SessionID"] = std::get<0>(lockRecord);
630 returnJson["HMCID"] = std::get<1>(lockRecord);
631 returnJson["LockType"] = std::get<2>(lockRecord);
632 returnJson["ResourceID"] = std::get<3>(lockRecord);
633
634 nlohmann::json segments;
635 nlohmann::json segmentInfoArray = nlohmann::json::array();
636
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500637 for (const auto& segment : std::get<4>(lockRecord))
manojkiraneda402b5712019-12-13 17:07:09 +0530638 {
639 segments["LockFlag"] = segment.first;
640 segments["SegmentLength"] = segment.second;
641 segmentInfoArray.push_back(segments);
642 }
643
644 returnJson["SegmentFlags"] = segmentInfoArray;
645 lockRecords.push_back(returnJson);
646 }
647 }
Sunitha Harishdb81c072021-01-21 23:33:21 -0600648 asyncResp->res.result(boost::beast::http::status::ok);
649 asyncResp->res.jsonValue["Records"] = lockRecords;
manojkiraneda402b5712019-12-13 17:07:09 +0530650}
651
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500652inline bool isValidConfigFileName(const std::string& fileName,
653 crow::Response& res)
654{
655 if (fileName.empty())
656 {
657 BMCWEB_LOG_ERROR << "Empty filename";
658 res.jsonValue["Description"] = "Empty file path in the url";
659 return false;
660 }
661
662 // ConfigFile name is allowed to take upper and lowercase letters,
663 // numbers and hyphen
664 std::size_t found = fileName.find_first_not_of(
665 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
666 if (found != std::string::npos)
667 {
668 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
669 res.jsonValue["Description"] = "Unsupported character in filename";
670 return false;
671 }
672
673 // Check the filename length
674 if (fileName.length() > 20)
675 {
676 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
677 "Input filename length is: "
678 << fileName.length();
679 res.jsonValue["Description"] = "Filename must be maximum 20 characters";
680 return false;
681 }
682
683 return true;
684}
685
Ed Tanous02379d32020-09-15 21:15:44 -0700686inline void requestRoutes(App& app)
Ratan Gupta453fed02019-12-14 09:39:47 +0530687{
688
689 // allowed only for admin
690 BMCWEB_ROUTE(app, "/ibm/v1/")
Ed Tanous432a8902021-06-14 15:28:56 -0700691 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700692 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800693 [](const crow::Request&,
694 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700695 asyncResp->res.jsonValue["@odata.type"] =
696 "#ibmServiceRoot.v1_0_0.ibmServiceRoot";
697 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/";
698 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService";
699 asyncResp->res.jsonValue["Name"] = "IBM Service Root";
700 asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] =
701 "/ibm/v1/Host/ConfigFiles";
702 asyncResp->res.jsonValue["LockService"]["@odata.id"] =
703 "/ibm/v1/HMC/LockService";
704 asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] =
705 "/ibm/v1/HMC/BroadcastService";
706 });
Sunitha Harish97b0e432019-11-21 04:59:29 -0600707
Ratan Guptad3630cb2019-12-14 11:21:35 +0530708 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles")
Ed Tanous432a8902021-06-14 15:28:56 -0700709 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700710 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800711 [](const crow::Request&,
712 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700713 handleConfigFileList(asyncResp);
714 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530715
716 BMCWEB_ROUTE(app,
Sunitha Harishe56f2542020-07-22 02:38:59 -0500717 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll")
Ed Tanous432a8902021-06-14 15:28:56 -0700718 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700719 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800720 [](const crow::Request&,
721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700722 deleteConfigFiles(asyncResp);
723 });
Ratan Guptad3630cb2019-12-14 11:21:35 +0530724
Sunitha Harish7c0bbe72020-07-30 08:25:28 -0500725 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700726 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800727 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get,
728 boost::beast::http::verb::delete_)(
729 [](const crow::Request& req,
730 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
731 const std::string& fileName) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700732 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
733 // Validate the incoming fileName
734 if (!isValidConfigFileName(fileName, asyncResp->res))
735 {
736 asyncResp->res.result(boost::beast::http::status::bad_request);
737 return;
738 }
739 handleFileUrl(req, asyncResp, fileName);
740 });
Ratan Gupta734a1c32019-12-14 11:53:48 +0530741
742 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService")
Ed Tanous432a8902021-06-14 15:28:56 -0700743 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700744 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800745 [](const crow::Request&,
746 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700747 getLockServiceData(asyncResp);
748 });
manojkiraneda0b631ae2019-12-03 17:54:28 +0530749
750 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700751 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800752 .methods(boost::beast::http::verb::post)(
753 [](const crow::Request& req,
754 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700755 std::vector<nlohmann::json> body;
756 if (!redfish::json_util::readJsonAction(req, asyncResp->res, "Request",
757 body))
758 {
759 BMCWEB_LOG_DEBUG << "Not a Valid JSON";
760 asyncResp->res.result(boost::beast::http::status::bad_request);
761 return;
762 }
763 handleAcquireLockAPI(req, asyncResp, body);
764 });
manojkiraneda3b6dea62019-12-13 17:05:36 +0530765 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock")
Ed Tanous432a8902021-06-14 15:28:56 -0700766 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800767 .methods(boost::beast::http::verb::post)(
768 [](const crow::Request& req,
769 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700770 std::string type;
771 std::vector<uint32_t> listTransactionIds;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530772
Ed Tanous002d39b2022-05-31 08:59:27 -0700773 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Type",
774 type, "TransactionIDs",
775 listTransactionIds))
776 {
777 asyncResp->res.result(boost::beast::http::status::bad_request);
778 return;
779 }
780 if (type == "Transaction")
781 {
782 handleReleaseLockAPI(req, asyncResp, listTransactionIds);
783 }
784 else if (type == "Session")
785 {
786 handleRelaseAllAPI(req, asyncResp);
787 }
788 else
789 {
790 BMCWEB_LOG_DEBUG << " Value of Type : " << type
791 << "is Not a Valid key";
792 redfish::messages::propertyValueNotInList(asyncResp->res, type,
793 "Type");
794 }
795 });
manojkiraneda402b5712019-12-13 17:07:09 +0530796 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList")
Ed Tanous432a8902021-06-14 15:28:56 -0700797 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +0800798 .methods(boost::beast::http::verb::post)(
799 [](const crow::Request& req,
800 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700801 ListOfSessionIds listSessionIds;
manojkiraneda402b5712019-12-13 17:07:09 +0530802
Ed Tanous002d39b2022-05-31 08:59:27 -0700803 if (!redfish::json_util::readJsonPatch(req, asyncResp->res,
804 "SessionIDs", listSessionIds))
805 {
806 asyncResp->res.result(boost::beast::http::status::bad_request);
807 return;
808 }
809 handleGetLockListAPI(asyncResp, listSessionIds);
810 });
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500811
812 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService")
Ed Tanous432a8902021-06-14 15:28:56 -0700813 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Asmitha Karunanithi5738de52020-07-17 02:03:31 -0500814 .methods(boost::beast::http::verb::post)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800815 [](const crow::Request& req,
816 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700817 handleBroadcastService(req, asyncResp);
818 });
Ratan Gupta453fed02019-12-14 09:39:47 +0530819}
820
821} // namespace ibm_mc
822} // namespace crow