Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 1 | #pragma once |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 2 | #include <tinyxml2.h> |
| 3 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 4 | #include <app.hpp> |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 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> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 19 | using SType = std::string; |
| 20 | using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>; |
| 21 | using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>; |
| 22 | using LockRequests = std::vector<LockRequest>; |
| 23 | using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 24 | using RcGetLockList = |
| 25 | std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>; |
| 26 | using ListOfSessionIds = std::vector<std::string>; |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 27 | namespace crow |
| 28 | { |
| 29 | namespace ibm_mc |
| 30 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 31 | constexpr const char* methodNotAllowedMsg = "Method Not Allowed"; |
| 32 | constexpr const char* resourceNotFoundMsg = "Resource Not Found"; |
| 33 | constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable"; |
| 34 | constexpr const char* internalServerError = "Internal Server Error"; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 35 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 36 | constexpr size_t maxSaveareaDirSize = |
| 37 | 10000000; // Allow save area dir size to be max 10MB |
| 38 | constexpr size_t minSaveareaFileSize = |
| 39 | 100; // Allow save area file size of minimum 100B |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 40 | constexpr size_t maxSaveareaFileSize = |
| 41 | 500000; // Allow save area file size upto 500KB |
| 42 | constexpr size_t maxBroadcastMsgSize = |
| 43 | 1000; // Allow Broadcast message size upto 1KB |
| 44 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 45 | inline bool |
| 46 | createSaveAreaPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 47 | { |
| 48 | // The path /var/lib/obmc will be created by initrdscripts |
| 49 | // Create the directories for the save-area files, when we get |
| 50 | // first file upload request |
| 51 | std::error_code ec; |
| 52 | if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec)) |
| 53 | { |
| 54 | std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec); |
| 55 | } |
| 56 | if (ec) |
| 57 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 58 | asyncResp->res.result( |
| 59 | boost::beast::http::status::internal_server_error); |
| 60 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 61 | BMCWEB_LOG_DEBUG |
| 62 | << "handleIbmPost: Failed to prepare save-area directory. ec : " |
| 63 | << ec; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (!std::filesystem::is_directory( |
| 68 | "/var/lib/obmc/bmc-console-mgmt/save-area", ec)) |
| 69 | { |
| 70 | std::filesystem::create_directory( |
| 71 | "/var/lib/obmc/bmc-console-mgmt/save-area", ec); |
| 72 | } |
| 73 | if (ec) |
| 74 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 75 | asyncResp->res.result( |
| 76 | boost::beast::http::status::internal_server_error); |
| 77 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 78 | BMCWEB_LOG_DEBUG |
| 79 | << "handleIbmPost: Failed to prepare save-area directory. ec : " |
| 80 | << ec; |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 85 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 86 | inline void handleFilePut(const crow::Request& req, |
| 87 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 88 | const std::string& fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 89 | { |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 90 | std::error_code ec; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 91 | // Check the content-type of the request |
Sunitha Harish | 086d32c | 2021-02-01 02:11:49 -0600 | [diff] [blame] | 92 | boost::beast::string_view contentType = req.getHeaderValue("content-type"); |
| 93 | if (!boost::iequals(contentType, "application/octet-stream")) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 94 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 95 | asyncResp->res.result(boost::beast::http::status::not_acceptable); |
| 96 | asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 97 | return; |
| 98 | } |
Sunitha Harish | 086d32c | 2021-02-01 02:11:49 -0600 | [diff] [blame] | 99 | BMCWEB_LOG_DEBUG |
| 100 | << "File upload in application/octet-stream format. Continue.."; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 101 | |
| 102 | BMCWEB_LOG_DEBUG |
| 103 | << "handleIbmPut: Request to create/update the save-area file"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 104 | if (!createSaveAreaPath(asyncResp)) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 105 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 106 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 107 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 108 | return; |
| 109 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 110 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 111 | std::ofstream file; |
| 112 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 113 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 114 | // Get the current size of the savearea directory |
| 115 | std::filesystem::recursive_directory_iterator iter(loc, ec); |
| 116 | if (ec) |
| 117 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 118 | asyncResp->res.result( |
| 119 | boost::beast::http::status::internal_server_error); |
| 120 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 121 | BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area " |
| 122 | "directory iterator. ec : " |
| 123 | << ec; |
| 124 | return; |
| 125 | } |
| 126 | std::uintmax_t saveAreaDirSize = 0; |
| 127 | for (auto& it : iter) |
| 128 | { |
| 129 | if (!std::filesystem::is_directory(it, ec)) |
| 130 | { |
| 131 | if (ec) |
| 132 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 133 | asyncResp->res.result( |
| 134 | boost::beast::http::status::internal_server_error); |
| 135 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 136 | BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area " |
| 137 | "directory . ec : " |
| 138 | << ec; |
| 139 | return; |
| 140 | } |
| 141 | std::uintmax_t fileSize = std::filesystem::file_size(it, ec); |
| 142 | if (ec) |
| 143 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 144 | asyncResp->res.result( |
| 145 | boost::beast::http::status::internal_server_error); |
| 146 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 147 | BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area " |
| 148 | "file size inside the directory . ec : " |
| 149 | << ec; |
| 150 | return; |
| 151 | } |
| 152 | saveAreaDirSize += fileSize; |
| 153 | } |
| 154 | } |
| 155 | BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize; |
| 156 | |
| 157 | // Get the file size getting uploaded |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 158 | const std::string& data = req.body; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 159 | BMCWEB_LOG_DEBUG << "data length: " << data.length(); |
| 160 | |
| 161 | if (data.length() < minSaveareaFileSize) |
| 162 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 163 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 164 | asyncResp->res.jsonValue["Description"] = |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 165 | "File size is less than minimum allowed size[100B]"; |
| 166 | return; |
| 167 | } |
| 168 | if (data.length() > maxSaveareaFileSize) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 169 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 170 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 171 | asyncResp->res.jsonValue["Description"] = |
Ratan Gupta | e46946a | 2020-05-11 13:22:59 +0530 | [diff] [blame] | 172 | "File size exceeds maximum allowed size[500KB]"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 173 | return; |
| 174 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 175 | |
| 176 | // Form the file path |
| 177 | loc /= fileID; |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 178 | BMCWEB_LOG_DEBUG << "Writing to the file: " << loc; |
| 179 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 180 | // Check if the same file exists in the directory |
| 181 | bool fileExists = std::filesystem::exists(loc, ec); |
| 182 | if (ec) |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 183 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 184 | asyncResp->res.result( |
| 185 | boost::beast::http::status::internal_server_error); |
| 186 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 187 | BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : " |
| 188 | << ec; |
| 189 | return; |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 190 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 191 | |
| 192 | std::uintmax_t newSizeToWrite = 0; |
| 193 | if (fileExists) |
| 194 | { |
| 195 | // File exists. Get the current file size |
| 196 | std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec); |
| 197 | if (ec) |
| 198 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 199 | asyncResp->res.result( |
| 200 | boost::beast::http::status::internal_server_error); |
| 201 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 202 | BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : " |
| 203 | << ec; |
| 204 | return; |
| 205 | } |
| 206 | // Calculate the difference in the file size. |
| 207 | // If the data.length is greater than the existing file size, then |
| 208 | // calculate the difference. Else consider the delta size as zero - |
| 209 | // because there is no increase in the total directory size. |
| 210 | // We need to add the diff only if the incoming data is larger than the |
| 211 | // existing filesize |
| 212 | if (data.length() > currentFileSize) |
| 213 | { |
| 214 | newSizeToWrite = data.length() - currentFileSize; |
| 215 | } |
| 216 | BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | // This is a new file upload |
| 221 | newSizeToWrite = data.length(); |
| 222 | } |
| 223 | |
| 224 | // Calculate the total dir size before writing the new file |
| 225 | BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite; |
| 226 | |
| 227 | if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize) |
| 228 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 229 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 230 | asyncResp->res.jsonValue["Description"] = |
| 231 | "File size does not fit in the savearea " |
| 232 | "directory maximum allowed size[10MB]"; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 236 | file.open(loc, std::ofstream::out); |
| 237 | if (file.fail()) |
| 238 | { |
| 239 | BMCWEB_LOG_DEBUG << "Error while opening the file for writing"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 240 | asyncResp->res.result( |
| 241 | boost::beast::http::status::internal_server_error); |
| 242 | asyncResp->res.jsonValue["Description"] = |
| 243 | "Error while creating the file"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 244 | return; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 245 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 246 | file << data; |
| 247 | std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID; |
| 248 | // Push an event |
| 249 | if (fileExists) |
| 250 | { |
| 251 | BMCWEB_LOG_DEBUG << "config file is updated"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 252 | asyncResp->res.jsonValue["Description"] = "File Updated"; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 253 | |
| 254 | redfish::EventServiceManager::getInstance().sendEvent( |
| 255 | redfish::messages::resourceChanged(), origin, "IBMConfigFile"); |
| 256 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 257 | else |
| 258 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 259 | BMCWEB_LOG_DEBUG << "config file is created"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 260 | asyncResp->res.jsonValue["Description"] = "File Created"; |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 261 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 262 | redfish::EventServiceManager::getInstance().sendEvent( |
| 263 | redfish::messages::resourceCreated(), origin, "IBMConfigFile"); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 264 | } |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 265 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 266 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 267 | inline void |
| 268 | handleConfigFileList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 269 | { |
| 270 | std::vector<std::string> pathObjList; |
| 271 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
| 272 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 273 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 274 | for (const auto& file : std::filesystem::directory_iterator(loc)) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 275 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 276 | const std::filesystem::path& pathObj = file.path(); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 277 | pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" + |
| 278 | pathObj.filename().string()); |
| 279 | } |
| 280 | } |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 281 | asyncResp->res.jsonValue["@odata.type"] = |
| 282 | "#IBMConfigFile.v1_0_0.IBMConfigFile"; |
| 283 | asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/"; |
| 284 | asyncResp->res.jsonValue["Id"] = "ConfigFiles"; |
| 285 | asyncResp->res.jsonValue["Name"] = "ConfigFiles"; |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 286 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 287 | asyncResp->res.jsonValue["Members"] = std::move(pathObjList); |
| 288 | asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = { |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 289 | {"target", |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 290 | "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 291 | return; |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 292 | } |
| 293 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 294 | inline void |
| 295 | deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 296 | { |
| 297 | std::vector<std::string> pathObjList; |
| 298 | std::error_code ec; |
| 299 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area"); |
| 300 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 301 | { |
| 302 | std::filesystem::remove_all(loc, ec); |
| 303 | if (ec) |
| 304 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 305 | asyncResp->res.result( |
| 306 | boost::beast::http::status::internal_server_error); |
| 307 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 308 | BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the " |
| 309 | "config files directory. ec : " |
| 310 | << ec; |
| 311 | } |
| 312 | } |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 313 | return; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 314 | } |
| 315 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 316 | inline void |
| 317 | getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 318 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 319 | asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService"; |
| 320 | asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/"; |
| 321 | asyncResp->res.jsonValue["Id"] = "LockService"; |
| 322 | asyncResp->res.jsonValue["Name"] = "LockService"; |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 323 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 324 | asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = { |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 325 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 326 | asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = { |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 327 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 328 | asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = { |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 329 | {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 330 | return; |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 331 | } |
| 332 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 333 | inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 334 | const std::string& fileID) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 335 | { |
| 336 | BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID; |
| 337 | std::filesystem::path loc("/var/lib/obmc/bmc-console-mgmt/save-area/" + |
| 338 | fileID); |
| 339 | if (!std::filesystem::exists(loc)) |
| 340 | { |
| 341 | BMCWEB_LOG_ERROR << loc << "Not found"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 342 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 343 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
| 347 | std::ifstream readfile(loc.string()); |
| 348 | if (!readfile) |
| 349 | { |
| 350 | BMCWEB_LOG_ERROR << loc.string() << "Not found"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 351 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 352 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | std::string contentDispositionParam = |
| 357 | "attachment; filename=\"" + fileID + "\""; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 358 | asyncResp->res.addHeader("Content-Disposition", contentDispositionParam); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 359 | std::string fileData; |
| 360 | fileData = {std::istreambuf_iterator<char>(readfile), |
| 361 | std::istreambuf_iterator<char>()}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 362 | asyncResp->res.jsonValue["Data"] = fileData; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 366 | inline void |
| 367 | handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 368 | const std::string& fileID) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 369 | { |
| 370 | std::string filePath("/var/lib/obmc/bmc-console-mgmt/save-area/" + fileID); |
| 371 | BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 372 | std::ifstream fileOpen(filePath.c_str()); |
| 373 | if (static_cast<bool>(fileOpen)) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 374 | { |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 375 | if (remove(filePath.c_str()) == 0) |
| 376 | { |
| 377 | BMCWEB_LOG_DEBUG << "File removed!\n"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 378 | asyncResp->res.jsonValue["Description"] = "File Deleted"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
| 382 | BMCWEB_LOG_ERROR << "File not removed!\n"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 383 | asyncResp->res.result( |
| 384 | boost::beast::http::status::internal_server_error); |
| 385 | asyncResp->res.jsonValue["Description"] = internalServerError; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 386 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 387 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 388 | else |
| 389 | { |
| 390 | BMCWEB_LOG_ERROR << "File not found!\n"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 391 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 392 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 393 | } |
| 394 | return; |
| 395 | } |
| 396 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 397 | inline void |
| 398 | handleBroadcastService(const crow::Request& req, |
| 399 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 400 | { |
| 401 | std::string broadcastMsg; |
| 402 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 403 | if (!redfish::json_util::readJson(req, asyncResp->res, "Message", |
| 404 | broadcastMsg)) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 405 | { |
| 406 | BMCWEB_LOG_DEBUG << "Not a Valid JSON"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 407 | asyncResp->res.result(boost::beast::http::status::bad_request); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | if (broadcastMsg.size() > maxBroadcastMsgSize) |
| 411 | { |
| 412 | BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 413 | asyncResp->res.result(boost::beast::http::status::bad_request); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 420 | inline void handleFileUrl(const crow::Request& req, |
| 421 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 422 | const std::string& fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 423 | { |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 424 | if (req.method() == boost::beast::http::verb::put) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 425 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 426 | handleFilePut(req, asyncResp, fileID); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 427 | return; |
| 428 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 429 | if (req.method() == boost::beast::http::verb::get) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 430 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 431 | handleFileGet(asyncResp, fileID); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 432 | return; |
| 433 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 434 | if (req.method() == boost::beast::http::verb::delete_) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 435 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 436 | handleFileDelete(asyncResp, fileID); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 437 | return; |
| 438 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 439 | } |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 440 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 441 | inline void |
| 442 | handleAcquireLockAPI(const crow::Request& req, |
| 443 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 444 | std::vector<nlohmann::json> body) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 445 | { |
| 446 | LockRequests lockRequestStructure; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 447 | for (auto& element : body) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 448 | { |
| 449 | std::string lockType; |
| 450 | uint64_t resourceId; |
| 451 | |
| 452 | SegmentFlags segInfo; |
| 453 | std::vector<nlohmann::json> segmentFlags; |
| 454 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 455 | if (!redfish::json_util::readJson(element, asyncResp->res, "LockType", |
| 456 | lockType, "ResourceID", resourceId, |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 457 | "SegmentFlags", segmentFlags)) |
| 458 | { |
| 459 | BMCWEB_LOG_DEBUG << "Not a Valid JSON"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 460 | asyncResp->res.result(boost::beast::http::status::bad_request); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 461 | return; |
| 462 | } |
| 463 | BMCWEB_LOG_DEBUG << lockType; |
| 464 | BMCWEB_LOG_DEBUG << resourceId; |
| 465 | |
| 466 | BMCWEB_LOG_DEBUG << "Segment Flags are present"; |
| 467 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 468 | for (auto& e : segmentFlags) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 469 | { |
| 470 | std::string lockFlags; |
| 471 | uint32_t segmentLength; |
| 472 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 473 | if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag", |
| 474 | lockFlags, "SegmentLength", |
| 475 | segmentLength)) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 476 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 477 | asyncResp->res.result(boost::beast::http::status::bad_request); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
| 481 | BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags; |
| 482 | BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength; |
| 483 | |
| 484 | segInfo.push_back(std::make_pair(lockFlags, segmentLength)); |
| 485 | } |
Manojkiran Eda | 566329e | 2020-05-22 12:36:17 +0530 | [diff] [blame] | 486 | lockRequestStructure.push_back( |
| 487 | make_tuple(req.session->uniqueId, req.session->clientId, lockType, |
| 488 | resourceId, segInfo)); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // print lock request into journal |
| 492 | |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 493 | for (auto& i : lockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 494 | { |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 495 | BMCWEB_LOG_DEBUG << std::get<0>(i); |
| 496 | BMCWEB_LOG_DEBUG << std::get<1>(i); |
| 497 | BMCWEB_LOG_DEBUG << std::get<2>(i); |
| 498 | BMCWEB_LOG_DEBUG << std::get<3>(i); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 499 | |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 500 | for (const auto& p : std::get<4>(i)) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 501 | { |
| 502 | BMCWEB_LOG_DEBUG << p.first << ", " << p.second; |
| 503 | } |
| 504 | } |
| 505 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 506 | const LockRequests& t = lockRequestStructure; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 507 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 508 | auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 509 | |
| 510 | if (varAcquireLock.first) |
| 511 | { |
| 512 | // Either validity failure of there is a conflict with itself |
| 513 | |
| 514 | auto validityStatus = |
| 515 | std::get<std::pair<bool, int>>(varAcquireLock.second); |
| 516 | |
| 517 | if ((!validityStatus.first) && (validityStatus.second == 0)) |
| 518 | { |
| 519 | BMCWEB_LOG_DEBUG << "Not a Valid record"; |
| 520 | BMCWEB_LOG_DEBUG << "Bad json in request"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 521 | asyncResp->res.result(boost::beast::http::status::bad_request); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | if (validityStatus.first && (validityStatus.second == 1)) |
| 525 | { |
| 526 | BMCWEB_LOG_DEBUG << "There is a conflict within itself"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 527 | asyncResp->res.result(boost::beast::http::status::bad_request); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | auto conflictStatus = |
| 534 | std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second); |
| 535 | if (!conflictStatus.first) |
| 536 | { |
| 537 | BMCWEB_LOG_DEBUG << "There is no conflict with the locktable"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 538 | asyncResp->res.result(boost::beast::http::status::ok); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 539 | |
| 540 | auto var = std::get<uint32_t>(conflictStatus.second); |
| 541 | nlohmann::json returnJson; |
| 542 | returnJson["id"] = var; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 543 | asyncResp->res.jsonValue["TransactionID"] = var; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 544 | return; |
| 545 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 546 | BMCWEB_LOG_DEBUG << "There is a conflict with the lock table"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 547 | asyncResp->res.result(boost::beast::http::status::conflict); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 548 | auto var = |
| 549 | std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second); |
| 550 | nlohmann::json returnJson, segments; |
| 551 | nlohmann::json myarray = nlohmann::json::array(); |
| 552 | returnJson["TransactionID"] = var.first; |
| 553 | returnJson["SessionID"] = std::get<0>(var.second); |
| 554 | returnJson["HMCID"] = std::get<1>(var.second); |
| 555 | returnJson["LockType"] = std::get<2>(var.second); |
| 556 | returnJson["ResourceID"] = std::get<3>(var.second); |
| 557 | |
| 558 | for (auto& i : std::get<4>(var.second)) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 559 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 560 | segments["LockFlag"] = i.first; |
| 561 | segments["SegmentLength"] = i.second; |
| 562 | myarray.push_back(segments); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 563 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 564 | |
| 565 | returnJson["SegmentFlags"] = myarray; |
| 566 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 567 | asyncResp->res.jsonValue["Record"] = returnJson; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 568 | return; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 569 | } |
| 570 | } |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 571 | inline void |
| 572 | handleRelaseAllAPI(const crow::Request& req, |
| 573 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Manojkiran Eda | 5bb0ece | 2020-01-20 20:22:36 +0530 | [diff] [blame] | 574 | { |
| 575 | crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 576 | asyncResp->res.result(boost::beast::http::status::ok); |
Manojkiran Eda | 5bb0ece | 2020-01-20 20:22:36 +0530 | [diff] [blame] | 577 | return; |
| 578 | } |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 579 | |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 580 | inline void |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 581 | handleReleaseLockAPI(const crow::Request& req, |
| 582 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 583 | const std::vector<uint32_t>& listTransactionIds) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 584 | { |
| 585 | BMCWEB_LOG_DEBUG << listTransactionIds.size(); |
| 586 | BMCWEB_LOG_DEBUG << "Data is present"; |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 587 | for (unsigned int listTransactionId : listTransactionIds) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 588 | { |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 589 | BMCWEB_LOG_DEBUG << listTransactionId; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 590 | } |
| 591 | |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 592 | // validate the request ids |
| 593 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 594 | auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock( |
Manojkiran Eda | 566329e | 2020-05-22 12:36:17 +0530 | [diff] [blame] | 595 | listTransactionIds, |
| 596 | std::make_pair(req.session->clientId, req.session->uniqueId)); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 597 | |
| 598 | if (!varReleaselock.first) |
| 599 | { |
| 600 | // validation Failed |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 601 | asyncResp->res.result(boost::beast::http::status::bad_request); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 602 | return; |
| 603 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 604 | auto statusRelease = |
| 605 | std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second); |
| 606 | if (statusRelease.first) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 607 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 608 | // The current hmc owns all the locks, so we already released |
| 609 | // them |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 610 | return; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 611 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 612 | |
| 613 | // valid rid, but the current hmc does not own all the locks |
| 614 | BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 615 | asyncResp->res.result(boost::beast::http::status::unauthorized); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 616 | |
| 617 | auto var = statusRelease.second; |
| 618 | nlohmann::json returnJson, segments; |
| 619 | nlohmann::json myArray = nlohmann::json::array(); |
| 620 | returnJson["TransactionID"] = var.first; |
| 621 | returnJson["SessionID"] = std::get<0>(var.second); |
| 622 | returnJson["HMCID"] = std::get<1>(var.second); |
| 623 | returnJson["LockType"] = std::get<2>(var.second); |
| 624 | returnJson["ResourceID"] = std::get<3>(var.second); |
| 625 | |
| 626 | for (auto& i : std::get<4>(var.second)) |
| 627 | { |
| 628 | segments["LockFlag"] = i.first; |
| 629 | segments["SegmentLength"] = i.second; |
| 630 | myArray.push_back(segments); |
| 631 | } |
| 632 | |
| 633 | returnJson["SegmentFlags"] = myArray; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 634 | asyncResp->res.jsonValue["Record"] = returnJson; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 635 | return; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 636 | } |
| 637 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 638 | inline void |
| 639 | handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 640 | const ListOfSessionIds& listSessionIds) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 641 | { |
| 642 | BMCWEB_LOG_DEBUG << listSessionIds.size(); |
| 643 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 644 | auto status = |
| 645 | crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 646 | auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); |
| 647 | |
| 648 | nlohmann::json lockRecords = nlohmann::json::array(); |
| 649 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 650 | for (const auto& transactionId : var) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 651 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 652 | for (const auto& lockRecord : transactionId.second) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 653 | { |
| 654 | nlohmann::json returnJson; |
| 655 | |
| 656 | returnJson["TransactionID"] = transactionId.first; |
| 657 | returnJson["SessionID"] = std::get<0>(lockRecord); |
| 658 | returnJson["HMCID"] = std::get<1>(lockRecord); |
| 659 | returnJson["LockType"] = std::get<2>(lockRecord); |
| 660 | returnJson["ResourceID"] = std::get<3>(lockRecord); |
| 661 | |
| 662 | nlohmann::json segments; |
| 663 | nlohmann::json segmentInfoArray = nlohmann::json::array(); |
| 664 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 665 | for (const auto& segment : std::get<4>(lockRecord)) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 666 | { |
| 667 | segments["LockFlag"] = segment.first; |
| 668 | segments["SegmentLength"] = segment.second; |
| 669 | segmentInfoArray.push_back(segments); |
| 670 | } |
| 671 | |
| 672 | returnJson["SegmentFlags"] = segmentInfoArray; |
| 673 | lockRecords.push_back(returnJson); |
| 674 | } |
| 675 | } |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 676 | asyncResp->res.result(boost::beast::http::status::ok); |
| 677 | asyncResp->res.jsonValue["Records"] = lockRecords; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 678 | } |
| 679 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 680 | inline bool isValidConfigFileName(const std::string& fileName, |
| 681 | crow::Response& res) |
| 682 | { |
| 683 | if (fileName.empty()) |
| 684 | { |
| 685 | BMCWEB_LOG_ERROR << "Empty filename"; |
| 686 | res.jsonValue["Description"] = "Empty file path in the url"; |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | // ConfigFile name is allowed to take upper and lowercase letters, |
| 691 | // numbers and hyphen |
| 692 | std::size_t found = fileName.find_first_not_of( |
| 693 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-"); |
| 694 | if (found != std::string::npos) |
| 695 | { |
| 696 | BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName; |
| 697 | res.jsonValue["Description"] = "Unsupported character in filename"; |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | // Check the filename length |
| 702 | if (fileName.length() > 20) |
| 703 | { |
| 704 | BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. " |
| 705 | "Input filename length is: " |
| 706 | << fileName.length(); |
| 707 | res.jsonValue["Description"] = "Filename must be maximum 20 characters"; |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | return true; |
| 712 | } |
| 713 | |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 714 | inline void requestRoutes(App& app) |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 715 | { |
| 716 | |
| 717 | // allowed only for admin |
| 718 | BMCWEB_ROUTE(app, "/ibm/v1/") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 719 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 720 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 721 | [](const crow::Request&, |
| 722 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 723 | asyncResp->res.jsonValue["@odata.type"] = |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 724 | "#ibmServiceRoot.v1_0_0.ibmServiceRoot"; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 725 | asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/"; |
| 726 | asyncResp->res.jsonValue["Id"] = "IBM Rest RootService"; |
| 727 | asyncResp->res.jsonValue["Name"] = "IBM Service Root"; |
| 728 | asyncResp->res.jsonValue["ConfigFiles"] = { |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 729 | {"@odata.id", "/ibm/v1/Host/ConfigFiles"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 730 | asyncResp->res.jsonValue["LockService"] = { |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 731 | {"@odata.id", "/ibm/v1/HMC/LockService"}}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 732 | asyncResp->res.jsonValue["BroadcastService"] = { |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 733 | {"@odata.id", "/ibm/v1/HMC/BroadcastService"}}; |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 734 | }); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 735 | |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 736 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 737 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 738 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 739 | [](const crow::Request&, |
| 740 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 741 | handleConfigFileList(asyncResp); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 742 | }); |
| 743 | |
| 744 | BMCWEB_ROUTE(app, |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 745 | "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 746 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 747 | .methods(boost::beast::http::verb::post)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 748 | [](const crow::Request&, |
| 749 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 750 | deleteConfigFiles(asyncResp); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 751 | }); |
| 752 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 753 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 754 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 755 | .methods(boost::beast::http::verb::put, boost::beast::http::verb::get, |
| 756 | boost::beast::http::verb::delete_)( |
| 757 | [](const crow::Request& req, |
| 758 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 759 | const std::string& fileName) { |
| 760 | BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName; |
| 761 | // Validate the incoming fileName |
| 762 | if (!isValidConfigFileName(fileName, asyncResp->res)) |
| 763 | { |
| 764 | asyncResp->res.result( |
| 765 | boost::beast::http::status::bad_request); |
| 766 | return; |
| 767 | } |
| 768 | handleFileUrl(req, asyncResp, fileName); |
| 769 | }); |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 770 | |
| 771 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 772 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 773 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 774 | [](const crow::Request&, |
| 775 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 776 | getLockServiceData(asyncResp); |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 777 | }); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 778 | |
| 779 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 780 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 781 | .methods(boost::beast::http::verb::post)( |
| 782 | [](const crow::Request& req, |
| 783 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 784 | std::vector<nlohmann::json> body; |
| 785 | if (!redfish::json_util::readJson(req, asyncResp->res, |
| 786 | "Request", body)) |
| 787 | { |
| 788 | BMCWEB_LOG_DEBUG << "Not a Valid JSON"; |
| 789 | asyncResp->res.result( |
| 790 | boost::beast::http::status::bad_request); |
| 791 | return; |
| 792 | } |
| 793 | handleAcquireLockAPI(req, asyncResp, body); |
| 794 | }); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 795 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 796 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 797 | .methods(boost::beast::http::verb::post)( |
| 798 | [](const crow::Request& req, |
| 799 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 800 | std::string type; |
| 801 | std::vector<uint32_t> listTransactionIds; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 802 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 803 | if (!redfish::json_util::readJson(req, asyncResp->res, "Type", |
| 804 | type, "TransactionIDs", |
| 805 | listTransactionIds)) |
| 806 | { |
| 807 | asyncResp->res.result( |
| 808 | boost::beast::http::status::bad_request); |
| 809 | return; |
| 810 | } |
| 811 | if (type == "Transaction") |
| 812 | { |
| 813 | handleReleaseLockAPI(req, asyncResp, listTransactionIds); |
| 814 | } |
| 815 | else if (type == "Session") |
| 816 | { |
| 817 | handleRelaseAllAPI(req, asyncResp); |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | BMCWEB_LOG_DEBUG << " Value of Type : " << type |
| 822 | << "is Not a Valid key"; |
| 823 | redfish::messages::propertyValueNotInList(asyncResp->res, |
| 824 | type, "Type"); |
| 825 | } |
| 826 | }); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 827 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 828 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 829 | .methods(boost::beast::http::verb::post)( |
| 830 | [](const crow::Request& req, |
| 831 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 832 | ListOfSessionIds listSessionIds; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 833 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 834 | if (!redfish::json_util::readJson(req, asyncResp->res, |
| 835 | "SessionIDs", listSessionIds)) |
| 836 | { |
| 837 | asyncResp->res.result( |
| 838 | boost::beast::http::status::bad_request); |
| 839 | return; |
| 840 | } |
| 841 | handleGetLockListAPI(asyncResp, listSessionIds); |
| 842 | }); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 843 | |
| 844 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService") |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 845 | .privileges({"ConfigureComponents", "ConfigureManager"}) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 846 | .methods(boost::beast::http::verb::post)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 847 | [](const crow::Request& req, |
| 848 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 849 | handleBroadcastService(req, asyncResp); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 850 | }); |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | } // namespace ibm_mc |
| 854 | } // namespace crow |