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