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