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