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