Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <app.h> |
| 3 | #include <tinyxml2.h> |
| 4 | |
| 5 | #include <async_resp.hpp> |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 6 | #include <boost/algorithm/string.hpp> |
| 7 | #include <boost/container/flat_set.hpp> |
| 8 | #include <filesystem> |
| 9 | #include <fstream> |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 10 | #include <ibm/locks.hpp> |
| 11 | #include <nlohmann/json.hpp> |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 12 | #include <regex> |
| 13 | #include <sdbusplus/message/types.hpp> |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 14 | #include <utils/json_utils.hpp> |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 15 | |
Ratan Gupta | e46946a | 2020-05-11 13:22:59 +0530 | [diff] [blame] | 16 | // Allow save area file size to 500KB |
| 17 | #define MAX_SAVE_AREA_FILESIZE 500000 |
| 18 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 19 | using SType = std::string; |
| 20 | using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>; |
| 21 | using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>; |
| 22 | using LockRequests = std::vector<LockRequest>; |
| 23 | using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 24 | using RcGetLockList = |
| 25 | std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>; |
| 26 | using ListOfSessionIds = std::vector<std::string>; |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 27 | namespace crow |
| 28 | { |
| 29 | namespace ibm_mc |
| 30 | { |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 31 | constexpr const char *methodNotAllowedMsg = "Method Not Allowed"; |
| 32 | constexpr const char *resourceNotFoundMsg = "Resource Not Found"; |
| 33 | constexpr const char *contentNotAcceptableMsg = "Content Not Acceptable"; |
| 34 | constexpr const char *internalServerError = "Internal Server Error"; |
| 35 | |
| 36 | bool createSaveAreaPath(crow::Response &res) |
| 37 | { |
| 38 | // The path /var/lib/obmc will be created by initrdscripts |
| 39 | // Create the directories for the save-area files, when we get |
| 40 | // first file upload request |
| 41 | std::error_code ec; |
| 42 | if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec)) |
| 43 | { |
| 44 | std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec); |
| 45 | } |
| 46 | if (ec) |
| 47 | { |
| 48 | res.result(boost::beast::http::status::internal_server_error); |
| 49 | res.jsonValue["Description"] = internalServerError; |
| 50 | BMCWEB_LOG_DEBUG |
| 51 | << "handleIbmPost: Failed to prepare save-area directory. ec : " |
| 52 | << ec; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (!std::filesystem::is_directory( |
| 57 | "/var/lib/obmc/bmc-console-mgmt/save-area", ec)) |
| 58 | { |
| 59 | std::filesystem::create_directory( |
| 60 | "/var/lib/obmc/bmc-console-mgmt/save-area", ec); |
| 61 | } |
| 62 | if (ec) |
| 63 | { |
| 64 | res.result(boost::beast::http::status::internal_server_error); |
| 65 | res.jsonValue["Description"] = internalServerError; |
| 66 | BMCWEB_LOG_DEBUG |
| 67 | << "handleIbmPost: Failed to prepare save-area directory. ec : " |
| 68 | << ec; |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | void handleFilePut(const crow::Request &req, crow::Response &res, |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 74 | const std::string &fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 75 | { |
| 76 | // Check the content-type of the request |
| 77 | std::string_view contentType = req.getHeaderValue("content-type"); |
| 78 | if (boost::starts_with(contentType, "multipart/form-data")) |
| 79 | { |
| 80 | BMCWEB_LOG_DEBUG |
| 81 | << "This is multipart/form-data. Invalid content for PUT"; |
| 82 | |
| 83 | res.result(boost::beast::http::status::not_acceptable); |
| 84 | res.jsonValue["Description"] = contentNotAcceptableMsg; |
| 85 | return; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | BMCWEB_LOG_DEBUG << "Not a multipart/form-data. Continue.."; |
| 90 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 91 | |
| 92 | BMCWEB_LOG_DEBUG |
| 93 | << "handleIbmPut: Request to create/update the save-area file"; |
| 94 | if (!createSaveAreaPath(res)) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 95 | { |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 96 | res.result(boost::beast::http::status::not_found); |
| 97 | res.jsonValue["Description"] = resourceNotFoundMsg; |
| 98 | return; |
| 99 | } |
| 100 | // Create the file |
| 101 | std::ofstream file; |
| 102 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
| 103 | loc /= fileID; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 104 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 105 | std::string data = std::move(req.body); |
| 106 | BMCWEB_LOG_DEBUG << "data capaticty : " << data.capacity(); |
| 107 | if (data.capacity() > MAX_SAVE_AREA_FILESIZE) |
| 108 | { |
| 109 | res.result(boost::beast::http::status::bad_request); |
| 110 | res.jsonValue["Description"] = |
Ratan Gupta | e46946a | 2020-05-11 13:22:59 +0530 | [diff] [blame] | 111 | "File size exceeds maximum allowed size[500KB]"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 112 | return; |
| 113 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 114 | BMCWEB_LOG_DEBUG << "Creating file " << loc; |
| 115 | file.open(loc, std::ofstream::out); |
| 116 | if (file.fail()) |
| 117 | { |
| 118 | BMCWEB_LOG_DEBUG << "Error while opening the file for writing"; |
| 119 | res.result(boost::beast::http::status::internal_server_error); |
| 120 | res.jsonValue["Description"] = "Error while creating the file"; |
| 121 | return; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 125 | file << data; |
| 126 | BMCWEB_LOG_DEBUG << "save-area file is created"; |
| 127 | res.jsonValue["Description"] = "File Created"; |
| 128 | } |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 129 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 130 | |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 131 | void handleConfigFileList(crow::Response &res) |
| 132 | { |
| 133 | std::vector<std::string> pathObjList; |
| 134 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
| 135 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 136 | { |
| 137 | for (const auto &file : std::filesystem::directory_iterator(loc)) |
| 138 | { |
| 139 | std::filesystem::path pathObj(file.path()); |
| 140 | pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" + |
| 141 | pathObj.filename().string()); |
| 142 | } |
| 143 | } |
| 144 | res.jsonValue["@odata.type"] = "#FileCollection.v1_0_0.FileCollection"; |
| 145 | res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/"; |
| 146 | res.jsonValue["Id"] = "ConfigFiles"; |
| 147 | res.jsonValue["Name"] = "ConfigFiles"; |
| 148 | |
| 149 | res.jsonValue["Members"] = std::move(pathObjList); |
| 150 | res.jsonValue["Actions"]["#FileCollection.DeleteAll"] = { |
| 151 | {"target", |
| 152 | "/ibm/v1/Host/ConfigFiles/Actions/FileCollection.DeleteAll"}}; |
| 153 | res.end(); |
| 154 | } |
| 155 | |
| 156 | void deleteConfigFiles(crow::Response &res) |
| 157 | { |
| 158 | std::vector<std::string> pathObjList; |
| 159 | std::error_code ec; |
| 160 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
| 161 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 162 | { |
| 163 | std::filesystem::remove_all(loc, ec); |
| 164 | if (ec) |
| 165 | { |
| 166 | res.result(boost::beast::http::status::internal_server_error); |
| 167 | res.jsonValue["Description"] = internalServerError; |
| 168 | BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the " |
| 169 | "config files directory. ec : " |
| 170 | << ec; |
| 171 | } |
| 172 | } |
| 173 | res.end(); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 176 | void getLockServiceData(crow::Response &res) |
| 177 | { |
| 178 | res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService"; |
| 179 | res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/"; |
| 180 | res.jsonValue["Id"] = "LockService"; |
| 181 | res.jsonValue["Name"] = "LockService"; |
| 182 | |
| 183 | res.jsonValue["Actions"]["#LockService.AcquireLock"] = { |
| 184 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}}; |
| 185 | res.jsonValue["Actions"]["#LockService.ReleaseLock"] = { |
| 186 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}}; |
| 187 | res.jsonValue["Actions"]["#LockService.GetLockList"] = { |
| 188 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}}; |
| 189 | res.end(); |
| 190 | } |
| 191 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 192 | void handleFileGet(crow::Response &res, const std::string &fileID) |
| 193 | { |
| 194 | BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID; |
| 195 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" + |
| 196 | fileID); |
| 197 | if (!std::filesystem::exists(loc)) |
| 198 | { |
| 199 | BMCWEB_LOG_ERROR << loc << "Not found"; |
| 200 | res.result(boost::beast::http::status::not_found); |
| 201 | res.jsonValue["Description"] = resourceNotFoundMsg; |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | std::ifstream readfile(loc.string()); |
| 206 | if (!readfile) |
| 207 | { |
| 208 | BMCWEB_LOG_ERROR << loc.string() << "Not found"; |
| 209 | res.result(boost::beast::http::status::not_found); |
| 210 | res.jsonValue["Description"] = resourceNotFoundMsg; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | std::string contentDispositionParam = |
| 215 | "attachment; filename=\"" + fileID + "\""; |
| 216 | res.addHeader("Content-Disposition", contentDispositionParam); |
| 217 | std::string fileData; |
| 218 | fileData = {std::istreambuf_iterator<char>(readfile), |
| 219 | std::istreambuf_iterator<char>()}; |
| 220 | res.jsonValue["Data"] = fileData; |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | void handleFileDelete(crow::Response &res, const std::string &fileID) |
| 225 | { |
| 226 | std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID); |
| 227 | BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n"; |
| 228 | |
| 229 | std::ifstream file_open(filePath.c_str()); |
| 230 | if (static_cast<bool>(file_open)) |
| 231 | if (remove(filePath.c_str()) == 0) |
| 232 | { |
| 233 | BMCWEB_LOG_DEBUG << "File removed!\n"; |
| 234 | res.jsonValue["Description"] = "File Deleted"; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | BMCWEB_LOG_ERROR << "File not removed!\n"; |
| 239 | res.result(boost::beast::http::status::internal_server_error); |
| 240 | res.jsonValue["Description"] = internalServerError; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | BMCWEB_LOG_ERROR << "File not found!\n"; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 245 | res.result(boost::beast::http::status::not_found); |
| 246 | res.jsonValue["Description"] = resourceNotFoundMsg; |
| 247 | } |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | inline void handleFileUrl(const crow::Request &req, crow::Response &res, |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 252 | const std::string &fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 253 | { |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 254 | if (req.method() == "PUT"_method) |
| 255 | { |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 256 | handleFilePut(req, res, fileID); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 257 | res.end(); |
| 258 | return; |
| 259 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 260 | if (req.method() == "GET"_method) |
| 261 | { |
| 262 | handleFileGet(res, fileID); |
| 263 | res.end(); |
| 264 | return; |
| 265 | } |
| 266 | if (req.method() == "DELETE"_method) |
| 267 | { |
| 268 | handleFileDelete(res, fileID); |
| 269 | res.end(); |
| 270 | return; |
| 271 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 272 | } |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 273 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 274 | void handleAcquireLockAPI(const crow::Request &req, crow::Response &res, |
| 275 | std::vector<nlohmann::json> body) |
| 276 | { |
| 277 | LockRequests lockRequestStructure; |
| 278 | for (auto &element : body) |
| 279 | { |
| 280 | std::string lockType; |
| 281 | uint64_t resourceId; |
| 282 | |
| 283 | SegmentFlags segInfo; |
| 284 | std::vector<nlohmann::json> segmentFlags; |
| 285 | |
| 286 | if (!redfish::json_util::readJson(element, res, "LockType", lockType, |
| 287 | "ResourceID", resourceId, |
| 288 | "SegmentFlags", segmentFlags)) |
| 289 | { |
| 290 | BMCWEB_LOG_DEBUG << "Not a Valid JSON"; |
| 291 | res.result(boost::beast::http::status::bad_request); |
| 292 | res.end(); |
| 293 | return; |
| 294 | } |
| 295 | BMCWEB_LOG_DEBUG << lockType; |
| 296 | BMCWEB_LOG_DEBUG << resourceId; |
| 297 | |
| 298 | BMCWEB_LOG_DEBUG << "Segment Flags are present"; |
| 299 | |
| 300 | for (auto &e : segmentFlags) |
| 301 | { |
| 302 | std::string lockFlags; |
| 303 | uint32_t segmentLength; |
| 304 | |
| 305 | if (!redfish::json_util::readJson(e, res, "LockFlag", lockFlags, |
| 306 | "SegmentLength", segmentLength)) |
| 307 | { |
| 308 | res.result(boost::beast::http::status::bad_request); |
| 309 | res.end(); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags; |
| 314 | BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength; |
| 315 | |
| 316 | segInfo.push_back(std::make_pair(lockFlags, segmentLength)); |
| 317 | } |
| 318 | lockRequestStructure.push_back(make_tuple( |
| 319 | req.session->uniqueId, "hmc-id", lockType, resourceId, segInfo)); |
| 320 | } |
| 321 | |
| 322 | // print lock request into journal |
| 323 | |
| 324 | for (uint32_t i = 0; i < lockRequestStructure.size(); i++) |
| 325 | { |
| 326 | BMCWEB_LOG_DEBUG << std::get<0>(lockRequestStructure[i]); |
| 327 | BMCWEB_LOG_DEBUG << std::get<1>(lockRequestStructure[i]); |
| 328 | BMCWEB_LOG_DEBUG << std::get<2>(lockRequestStructure[i]); |
| 329 | BMCWEB_LOG_DEBUG << std::get<3>(lockRequestStructure[i]); |
| 330 | |
| 331 | for (const auto &p : std::get<4>(lockRequestStructure[i])) |
| 332 | { |
| 333 | BMCWEB_LOG_DEBUG << p.first << ", " << p.second; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | const LockRequests &t = lockRequestStructure; |
| 338 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame^] | 339 | auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 340 | |
| 341 | if (varAcquireLock.first) |
| 342 | { |
| 343 | // Either validity failure of there is a conflict with itself |
| 344 | |
| 345 | auto validityStatus = |
| 346 | std::get<std::pair<bool, int>>(varAcquireLock.second); |
| 347 | |
| 348 | if ((!validityStatus.first) && (validityStatus.second == 0)) |
| 349 | { |
| 350 | BMCWEB_LOG_DEBUG << "Not a Valid record"; |
| 351 | BMCWEB_LOG_DEBUG << "Bad json in request"; |
| 352 | res.result(boost::beast::http::status::bad_request); |
| 353 | res.end(); |
| 354 | return; |
| 355 | } |
| 356 | if (validityStatus.first && (validityStatus.second == 1)) |
| 357 | { |
| 358 | BMCWEB_LOG_DEBUG << "There is a conflict within itself"; |
| 359 | res.result(boost::beast::http::status::bad_request); |
| 360 | res.end(); |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | auto conflictStatus = |
| 367 | std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second); |
| 368 | if (!conflictStatus.first) |
| 369 | { |
| 370 | BMCWEB_LOG_DEBUG << "There is no conflict with the locktable"; |
| 371 | res.result(boost::beast::http::status::ok); |
| 372 | |
| 373 | auto var = std::get<uint32_t>(conflictStatus.second); |
| 374 | nlohmann::json returnJson; |
| 375 | returnJson["id"] = var; |
| 376 | res.jsonValue["TransactionID"] = var; |
| 377 | res.end(); |
| 378 | return; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | BMCWEB_LOG_DEBUG << "There is a conflict with the lock table"; |
| 383 | res.result(boost::beast::http::status::conflict); |
| 384 | auto var = std::get<std::pair<uint32_t, LockRequest>>( |
| 385 | conflictStatus.second); |
| 386 | nlohmann::json returnJson, segments; |
| 387 | nlohmann::json myarray = nlohmann::json::array(); |
| 388 | returnJson["TransactionID"] = var.first; |
| 389 | returnJson["SessionID"] = std::get<0>(var.second); |
| 390 | returnJson["HMCID"] = std::get<1>(var.second); |
| 391 | returnJson["LockType"] = std::get<2>(var.second); |
| 392 | returnJson["ResourceID"] = std::get<3>(var.second); |
| 393 | |
| 394 | for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++) |
| 395 | { |
| 396 | segments["LockFlag"] = std::get<4>(var.second)[i].first; |
| 397 | segments["SegmentLength"] = std::get<4>(var.second)[i].second; |
| 398 | myarray.push_back(segments); |
| 399 | } |
| 400 | |
| 401 | returnJson["SegmentFlags"] = myarray; |
| 402 | |
| 403 | res.jsonValue["Record"] = returnJson; |
| 404 | res.end(); |
| 405 | return; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 410 | void handleReleaseLockAPI(const crow::Request &req, crow::Response &res, |
| 411 | const std::vector<uint32_t> &listTransactionIds) |
| 412 | { |
| 413 | BMCWEB_LOG_DEBUG << listTransactionIds.size(); |
| 414 | BMCWEB_LOG_DEBUG << "Data is present"; |
| 415 | for (uint32_t i = 0; i < listTransactionIds.size(); i++) |
| 416 | { |
| 417 | BMCWEB_LOG_DEBUG << listTransactionIds[i]; |
| 418 | } |
| 419 | |
| 420 | std::string clientId = "hmc-id"; |
| 421 | std::string sessionId = req.session->uniqueId; |
| 422 | |
| 423 | // validate the request ids |
| 424 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame^] | 425 | auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock( |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 426 | listTransactionIds, std::make_pair(clientId, sessionId)); |
| 427 | |
| 428 | if (!varReleaselock.first) |
| 429 | { |
| 430 | // validation Failed |
| 431 | res.result(boost::beast::http::status::bad_request); |
| 432 | res.end(); |
| 433 | return; |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | auto statusRelease = |
| 438 | std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second); |
| 439 | if (statusRelease.first) |
| 440 | { |
| 441 | // The current hmc owns all the locks, so we already released |
| 442 | // them |
| 443 | res.result(boost::beast::http::status::ok); |
| 444 | res.end(); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | else |
| 449 | { |
| 450 | // valid rid, but the current hmc does not own all the locks |
| 451 | BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks"; |
| 452 | res.result(boost::beast::http::status::unauthorized); |
| 453 | |
| 454 | auto var = statusRelease.second; |
| 455 | nlohmann::json returnJson, segments; |
| 456 | nlohmann::json myArray = nlohmann::json::array(); |
| 457 | returnJson["TransactionID"] = var.first; |
| 458 | returnJson["SessionID"] = std::get<0>(var.second); |
| 459 | returnJson["HMCID"] = std::get<1>(var.second); |
| 460 | returnJson["LockType"] = std::get<2>(var.second); |
| 461 | returnJson["ResourceID"] = std::get<3>(var.second); |
| 462 | |
| 463 | for (uint32_t i = 0; i < std::get<4>(var.second).size(); i++) |
| 464 | { |
| 465 | segments["LockFlag"] = std::get<4>(var.second)[i].first; |
| 466 | segments["SegmentLength"] = std::get<4>(var.second)[i].second; |
| 467 | myArray.push_back(segments); |
| 468 | } |
| 469 | |
| 470 | returnJson["SegmentFlags"] = myArray; |
| 471 | res.jsonValue["Record"] = returnJson; |
| 472 | res.end(); |
| 473 | return; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 478 | void handleGetLockListAPI(const crow::Request &req, crow::Response &res, |
| 479 | const ListOfSessionIds &listSessionIds) |
| 480 | { |
| 481 | BMCWEB_LOG_DEBUG << listSessionIds.size(); |
| 482 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame^] | 483 | auto status = |
| 484 | crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 485 | auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); |
| 486 | |
| 487 | nlohmann::json lockRecords = nlohmann::json::array(); |
| 488 | |
| 489 | for (const auto &transactionId : var) |
| 490 | { |
| 491 | for (const auto &lockRecord : transactionId.second) |
| 492 | { |
| 493 | nlohmann::json returnJson; |
| 494 | |
| 495 | returnJson["TransactionID"] = transactionId.first; |
| 496 | returnJson["SessionID"] = std::get<0>(lockRecord); |
| 497 | returnJson["HMCID"] = std::get<1>(lockRecord); |
| 498 | returnJson["LockType"] = std::get<2>(lockRecord); |
| 499 | returnJson["ResourceID"] = std::get<3>(lockRecord); |
| 500 | |
| 501 | nlohmann::json segments; |
| 502 | nlohmann::json segmentInfoArray = nlohmann::json::array(); |
| 503 | |
| 504 | for (const auto &segment : std::get<4>(lockRecord)) |
| 505 | { |
| 506 | segments["LockFlag"] = segment.first; |
| 507 | segments["SegmentLength"] = segment.second; |
| 508 | segmentInfoArray.push_back(segments); |
| 509 | } |
| 510 | |
| 511 | returnJson["SegmentFlags"] = segmentInfoArray; |
| 512 | lockRecords.push_back(returnJson); |
| 513 | } |
| 514 | } |
| 515 | res.result(boost::beast::http::status::ok); |
| 516 | res.jsonValue["Records"] = lockRecords; |
| 517 | res.end(); |
| 518 | } |
| 519 | |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 520 | template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app) |
| 521 | { |
| 522 | |
| 523 | // allowed only for admin |
| 524 | BMCWEB_ROUTE(app, "/ibm/v1/") |
| 525 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 526 | .methods("GET"_method)( |
| 527 | [](const crow::Request &req, crow::Response &res) { |
| 528 | res.jsonValue["@odata.type"] = |
| 529 | "#ibmServiceRoot.v1_0_0.ibmServiceRoot"; |
| 530 | res.jsonValue["@odata.id"] = "/ibm/v1/"; |
| 531 | res.jsonValue["Id"] = "IBM Rest RootService"; |
| 532 | res.jsonValue["Name"] = "IBM Service Root"; |
| 533 | res.jsonValue["ConfigFiles"] = { |
| 534 | {"@odata.id", "/ibm/v1/Host/ConfigFiles"}}; |
| 535 | res.jsonValue["LockService"] = { |
| 536 | {"@odata.id", "/ibm/v1/HMC/LockService"}}; |
| 537 | res.end(); |
| 538 | }); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 539 | |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 540 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") |
| 541 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 542 | .methods("GET"_method)( |
| 543 | [](const crow::Request &req, crow::Response &res) { |
| 544 | handleConfigFileList(res); |
| 545 | }); |
| 546 | |
| 547 | BMCWEB_ROUTE(app, |
| 548 | "/ibm/v1/Host/ConfigFiles/Actions/FileCollection.DeleteAll") |
| 549 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 550 | .methods("POST"_method)( |
| 551 | [](const crow::Request &req, crow::Response &res) { |
| 552 | deleteConfigFiles(res); |
| 553 | }); |
| 554 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 555 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<path>") |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 556 | .requires({"ConfigureComponents", "ConfigureManager"}) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 557 | .methods("PUT"_method, "GET"_method, "DELETE"_method)( |
| 558 | [](const crow::Request &req, crow::Response &res, |
| 559 | const std::string &path) { handleFileUrl(req, res, path); }); |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 560 | |
| 561 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService") |
| 562 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 563 | .methods("GET"_method)( |
| 564 | [](const crow::Request &req, crow::Response &res) { |
| 565 | getLockServiceData(res); |
| 566 | }); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 567 | |
| 568 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock") |
| 569 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 570 | .methods("POST"_method)( |
| 571 | [](const crow::Request &req, crow::Response &res) { |
| 572 | std::vector<nlohmann::json> body; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 573 | if (!redfish::json_util::readJson(req, res, "Request", body)) |
| 574 | { |
| 575 | BMCWEB_LOG_DEBUG << "Not a Valid JSON"; |
| 576 | res.result(boost::beast::http::status::bad_request); |
| 577 | res.end(); |
| 578 | return; |
| 579 | } |
| 580 | handleAcquireLockAPI(req, res, body); |
| 581 | }); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 582 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock") |
| 583 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 584 | .methods("POST"_method)( |
| 585 | [](const crow::Request &req, crow::Response &res) { |
| 586 | std::vector<uint32_t> listTransactionIds; |
| 587 | |
| 588 | if (!redfish::json_util::readJson(req, res, "TransactionIDs", |
| 589 | listTransactionIds)) |
| 590 | { |
| 591 | res.result(boost::beast::http::status::bad_request); |
| 592 | res.end(); |
| 593 | return; |
| 594 | } |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 595 | handleReleaseLockAPI(req, res, listTransactionIds); |
| 596 | }); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 597 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList") |
| 598 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 599 | .methods("POST"_method)( |
| 600 | [](const crow::Request &req, crow::Response &res) { |
| 601 | ListOfSessionIds listSessionIds; |
| 602 | |
| 603 | if (!redfish::json_util::readJson(req, res, "SessionIDs", |
| 604 | listSessionIds)) |
| 605 | { |
| 606 | res.result(boost::beast::http::status::bad_request); |
| 607 | res.end(); |
| 608 | return; |
| 609 | } |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 610 | handleGetLockListAPI(req, res, listSessionIds); |
| 611 | }); |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | } // namespace ibm_mc |
| 615 | } // namespace crow |