Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #include "backup_restore.hpp" |
| 2 | |
| 3 | #include "constants.hpp" |
| 4 | #include "event_logger.hpp" |
| 5 | #include "exceptions.hpp" |
| 6 | #include "logger.hpp" |
| 7 | #include "parser.hpp" |
| 8 | #include "types.hpp" |
| 9 | |
| 10 | #include <utility/json_utility.hpp> |
| 11 | #include <utility/vpd_specific_utility.hpp> |
| 12 | |
| 13 | namespace vpd |
| 14 | { |
| 15 | BackupAndRestoreStatus BackupAndRestore::m_backupAndRestoreStatus = |
| 16 | BackupAndRestoreStatus::NotStarted; |
| 17 | |
| 18 | BackupAndRestore::BackupAndRestore(const nlohmann::json& i_sysCfgJsonObj) : |
| 19 | m_sysCfgJsonObj(i_sysCfgJsonObj) |
| 20 | { |
| 21 | std::string l_backupAndRestoreCfgFilePath = |
| 22 | i_sysCfgJsonObj.value("backupRestoreConfigPath", ""); |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 23 | |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 24 | uint16_t l_errCode = 0; |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 25 | m_backupAndRestoreCfgJsonObj = |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 26 | jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath, l_errCode); |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 27 | |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 28 | if (l_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 29 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 30 | throw JsonException( |
| 31 | "JSON parsing failed for file [" + l_backupAndRestoreCfgFilePath + |
| 32 | "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode), |
| 33 | l_backupAndRestoreCfgFilePath); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | std::tuple<types::VPDMapVariant, types::VPDMapVariant> |
| 38 | BackupAndRestore::backupAndRestore() |
| 39 | { |
| 40 | auto l_emptyVariantPair = |
| 41 | std::make_tuple(std::monostate{}, std::monostate{}); |
| 42 | |
| 43 | if (m_backupAndRestoreStatus >= BackupAndRestoreStatus::Invoked) |
| 44 | { |
| 45 | logging::logMessage("Backup and restore invoked already."); |
| 46 | return l_emptyVariantPair; |
| 47 | } |
| 48 | |
| 49 | m_backupAndRestoreStatus = BackupAndRestoreStatus::Invoked; |
| 50 | try |
| 51 | { |
| 52 | if (m_backupAndRestoreCfgJsonObj.empty() || |
| 53 | !m_backupAndRestoreCfgJsonObj.contains("source") || |
| 54 | !m_backupAndRestoreCfgJsonObj.contains("destination") || |
| 55 | !m_backupAndRestoreCfgJsonObj.contains("type") || |
| 56 | !m_backupAndRestoreCfgJsonObj.contains("backupMap")) |
| 57 | { |
| 58 | logging::logMessage( |
| 59 | "Backup restore config JSON is missing necessary tag(s), can't initiate backup and restore."); |
| 60 | return l_emptyVariantPair; |
| 61 | } |
| 62 | |
| 63 | std::string l_srcVpdPath; |
| 64 | types::VPDMapVariant l_srcVpdVariant; |
| 65 | if (l_srcVpdPath = m_backupAndRestoreCfgJsonObj["source"].value( |
| 66 | "hardwarePath", ""); |
| 67 | !l_srcVpdPath.empty() && std::filesystem::exists(l_srcVpdPath)) |
| 68 | { |
| 69 | std::shared_ptr<Parser> l_vpdParser = |
| 70 | std::make_shared<Parser>(l_srcVpdPath, m_sysCfgJsonObj); |
| 71 | l_srcVpdVariant = l_vpdParser->parse(); |
| 72 | } |
| 73 | else if (l_srcVpdPath = m_backupAndRestoreCfgJsonObj["source"].value( |
| 74 | "inventoryPath", ""); |
| 75 | l_srcVpdPath.empty()) |
| 76 | { |
| 77 | logging::logMessage( |
| 78 | "Couldn't extract source path, can't initiate backup and restore."); |
| 79 | return l_emptyVariantPair; |
| 80 | } |
| 81 | |
| 82 | std::string l_dstVpdPath; |
| 83 | types::VPDMapVariant l_dstVpdVariant; |
| 84 | if (l_dstVpdPath = m_backupAndRestoreCfgJsonObj["destination"].value( |
| 85 | "hardwarePath", ""); |
| 86 | !l_dstVpdPath.empty() && std::filesystem::exists(l_dstVpdPath)) |
| 87 | { |
| 88 | std::shared_ptr<Parser> l_vpdParser = |
| 89 | std::make_shared<Parser>(l_dstVpdPath, m_sysCfgJsonObj); |
| 90 | l_dstVpdVariant = l_vpdParser->parse(); |
| 91 | } |
| 92 | else if (l_dstVpdPath = m_backupAndRestoreCfgJsonObj["destination"] |
| 93 | .value("inventoryPath", ""); |
| 94 | l_dstVpdPath.empty()) |
| 95 | { |
| 96 | logging::logMessage( |
| 97 | "Couldn't extract destination path, can't initiate backup and restore."); |
| 98 | return l_emptyVariantPair; |
| 99 | } |
| 100 | |
| 101 | // Implement backup and restore for IPZ type VPD |
| 102 | auto l_backupAndRestoreType = |
| 103 | m_backupAndRestoreCfgJsonObj.value("type", ""); |
| 104 | if (l_backupAndRestoreType.compare("IPZ") == constants::STR_CMP_SUCCESS) |
| 105 | { |
| 106 | types::IPZVpdMap l_srcVpdMap; |
| 107 | if (auto l_srcVpdPtr = |
| 108 | std::get_if<types::IPZVpdMap>(&l_srcVpdVariant)) |
| 109 | { |
| 110 | l_srcVpdMap = *l_srcVpdPtr; |
| 111 | } |
| 112 | else if (!std::holds_alternative<std::monostate>(l_srcVpdVariant)) |
| 113 | { |
| 114 | logging::logMessage("Source VPD is not of IPZ type."); |
| 115 | return l_emptyVariantPair; |
| 116 | } |
| 117 | |
| 118 | types::IPZVpdMap l_dstVpdMap; |
| 119 | if (auto l_dstVpdPtr = |
| 120 | std::get_if<types::IPZVpdMap>(&l_dstVpdVariant)) |
| 121 | { |
| 122 | l_dstVpdMap = *l_dstVpdPtr; |
| 123 | } |
| 124 | else if (!std::holds_alternative<std::monostate>(l_dstVpdVariant)) |
| 125 | { |
| 126 | logging::logMessage("Destination VPD is not of IPZ type."); |
| 127 | return l_emptyVariantPair; |
| 128 | } |
| 129 | |
| 130 | backupAndRestoreIpzVpd(l_srcVpdMap, l_dstVpdMap, l_srcVpdPath, |
| 131 | l_dstVpdPath); |
| 132 | m_backupAndRestoreStatus = BackupAndRestoreStatus::Completed; |
| 133 | |
| 134 | return std::make_tuple(l_srcVpdMap, l_dstVpdMap); |
| 135 | } |
| 136 | // Note: add implementation here to support any other VPD type. |
| 137 | } |
| 138 | catch (const std::exception& ex) |
| 139 | { |
| 140 | logging::logMessage("Back up and restore failed with exception: " + |
| 141 | std::string(ex.what())); |
| 142 | } |
| 143 | return l_emptyVariantPair; |
| 144 | } |
| 145 | |
| 146 | void BackupAndRestore::backupAndRestoreIpzVpd( |
| 147 | types::IPZVpdMap& io_srcVpdMap, types::IPZVpdMap& io_dstVpdMap, |
| 148 | const std::string& i_srcPath, const std::string& i_dstPath) |
| 149 | { |
| 150 | if (!m_backupAndRestoreCfgJsonObj["backupMap"].is_array()) |
| 151 | { |
| 152 | logging::logMessage( |
| 153 | "Invalid value found for tag backupMap, in backup and restore config JSON."); |
| 154 | return; |
| 155 | } |
| 156 | |
Rekha Aparna | 0578dd2 | 2025-09-02 08:20:21 -0500 | [diff] [blame^] | 157 | uint16_t l_errCode = 0; |
| 158 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 159 | const std::string l_srcFruPath = |
Rekha Aparna | 0578dd2 | 2025-09-02 08:20:21 -0500 | [diff] [blame^] | 160 | jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_srcPath, l_errCode); |
| 161 | |
| 162 | if (l_errCode) |
| 163 | { |
| 164 | logging::logMessage( |
| 165 | "Failed to get source FRU path for [" + i_srcPath + |
| 166 | "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 167 | return; |
| 168 | } |
| 169 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 170 | const std::string l_dstFruPath = |
Rekha Aparna | 0578dd2 | 2025-09-02 08:20:21 -0500 | [diff] [blame^] | 171 | jsonUtility::getFruPathFromJson(m_sysCfgJsonObj, i_dstPath, l_errCode); |
| 172 | |
| 173 | if (l_errCode) |
| 174 | { |
| 175 | logging::logMessage( |
| 176 | "Failed to get destination FRU path for [" + i_dstPath + |
| 177 | "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 178 | return; |
| 179 | } |
| 180 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 181 | if (l_srcFruPath.empty() || l_dstFruPath.empty()) |
| 182 | { |
| 183 | logging::logMessage( |
| 184 | "Couldn't find either source or destination FRU path."); |
| 185 | return; |
| 186 | } |
| 187 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 188 | const std::string l_srcInvPath = jsonUtility::getInventoryObjPathFromJson( |
| 189 | m_sysCfgJsonObj, i_srcPath, l_errCode); |
| 190 | |
| 191 | if (l_srcInvPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 192 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 193 | if (l_errCode) |
| 194 | { |
| 195 | logging::logMessage( |
| 196 | "Couldn't find source inventory path. Error : " + |
| 197 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | logging::logMessage("Couldn't find source inventory path."); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | const std::string l_dstInvPath = jsonUtility::getInventoryObjPathFromJson( |
| 206 | m_sysCfgJsonObj, i_dstPath, l_errCode); |
| 207 | |
| 208 | if (l_dstInvPath.empty()) |
| 209 | { |
| 210 | if (l_errCode) |
| 211 | { |
| 212 | logging::logMessage( |
| 213 | "Couldn't find destination inventory path. Error : " + |
| 214 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | logging::logMessage("Couldn't find destination inventory path."); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | const std::string l_srcServiceName = |
| 223 | jsonUtility::getServiceName(m_sysCfgJsonObj, l_srcInvPath); |
| 224 | const std::string l_dstServiceName = |
| 225 | jsonUtility::getServiceName(m_sysCfgJsonObj, l_dstInvPath); |
| 226 | if (l_srcServiceName.empty() || l_dstServiceName.empty()) |
| 227 | { |
| 228 | logging::logMessage( |
| 229 | "Couldn't find either source or destination DBus service name."); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | for (const auto& l_aRecordKwInfo : |
| 234 | m_backupAndRestoreCfgJsonObj["backupMap"]) |
| 235 | { |
| 236 | const std::string& l_srcRecordName = |
| 237 | l_aRecordKwInfo.value("sourceRecord", ""); |
| 238 | const std::string& l_srcKeywordName = |
| 239 | l_aRecordKwInfo.value("sourceKeyword", ""); |
| 240 | const std::string& l_dstRecordName = |
| 241 | l_aRecordKwInfo.value("destinationRecord", ""); |
| 242 | const std::string& l_dstKeywordName = |
| 243 | l_aRecordKwInfo.value("destinationKeyword", ""); |
| 244 | |
| 245 | if (l_srcRecordName.empty() || l_dstRecordName.empty() || |
| 246 | l_srcKeywordName.empty() || l_dstKeywordName.empty()) |
| 247 | { |
| 248 | logging::logMessage( |
| 249 | "Record or keyword not found in the backup and restore config JSON."); |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if (!io_srcVpdMap.empty() && |
| 254 | io_srcVpdMap.find(l_srcRecordName) == io_srcVpdMap.end()) |
| 255 | { |
| 256 | logging::logMessage( |
| 257 | "Record: " + l_srcRecordName + |
| 258 | ", is not found in the source path: " + i_srcPath); |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (!io_dstVpdMap.empty() && |
| 263 | io_dstVpdMap.find(l_dstRecordName) == io_dstVpdMap.end()) |
| 264 | { |
| 265 | logging::logMessage( |
| 266 | "Record: " + l_dstRecordName + |
| 267 | ", is not found in the destination path: " + i_dstPath); |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | types::BinaryVector l_defaultBinaryValue; |
| 272 | if (l_aRecordKwInfo.contains("defaultValue") && |
| 273 | l_aRecordKwInfo["defaultValue"].is_array()) |
| 274 | { |
| 275 | l_defaultBinaryValue = |
| 276 | l_aRecordKwInfo["defaultValue"].get<types::BinaryVector>(); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | logging::logMessage( |
| 281 | "Couldn't read default value for record name: " + |
| 282 | l_srcRecordName + ", keyword name: " + l_srcKeywordName + |
| 283 | " from backup and restore config JSON file."); |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | bool l_isPelRequired = l_aRecordKwInfo.value("isPelRequired", false); |
| 288 | |
| 289 | types::BinaryVector l_srcBinaryValue; |
| 290 | std::string l_srcStrValue; |
| 291 | if (!io_srcVpdMap.empty()) |
| 292 | { |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 293 | l_srcStrValue = vpdSpecificUtility::getKwVal( |
| 294 | io_srcVpdMap.at(l_srcRecordName), l_srcKeywordName); |
| 295 | |
| 296 | if (l_srcStrValue.empty()) |
| 297 | { |
| 298 | std::runtime_error( |
| 299 | std::string("Failed to get value for keyword [") + |
| 300 | l_srcKeywordName + std::string("]")); |
| 301 | } |
| 302 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 303 | l_srcBinaryValue = |
| 304 | types::BinaryVector(l_srcStrValue.begin(), l_srcStrValue.end()); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | // Read keyword value from DBus |
| 309 | const auto l_value = dbusUtility::readDbusProperty( |
| 310 | l_srcServiceName, l_srcInvPath, |
| 311 | constants::ipzVpdInf + l_srcRecordName, l_srcKeywordName); |
| 312 | if (const auto l_binaryValue = |
| 313 | std::get_if<types::BinaryVector>(&l_value)) |
| 314 | { |
| 315 | l_srcBinaryValue = *l_binaryValue; |
| 316 | l_srcStrValue = std::string(l_srcBinaryValue.begin(), |
| 317 | l_srcBinaryValue.end()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | types::BinaryVector l_dstBinaryValue; |
| 322 | std::string l_dstStrValue; |
| 323 | if (!io_dstVpdMap.empty()) |
| 324 | { |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 325 | l_dstStrValue = vpdSpecificUtility::getKwVal( |
| 326 | io_dstVpdMap.at(l_dstRecordName), l_dstKeywordName); |
| 327 | |
| 328 | if (l_dstStrValue.empty()) |
| 329 | { |
| 330 | std::runtime_error( |
| 331 | std::string("Failed to get value for keyword [") + |
| 332 | l_dstKeywordName + std::string("]")); |
| 333 | } |
| 334 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 335 | l_dstBinaryValue = |
| 336 | types::BinaryVector(l_dstStrValue.begin(), l_dstStrValue.end()); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | // Read keyword value from DBus |
| 341 | const auto l_value = dbusUtility::readDbusProperty( |
| 342 | l_dstServiceName, l_dstInvPath, |
| 343 | constants::ipzVpdInf + l_dstRecordName, l_dstKeywordName); |
| 344 | if (const auto l_binaryValue = |
| 345 | std::get_if<types::BinaryVector>(&l_value)) |
| 346 | { |
| 347 | l_dstBinaryValue = *l_binaryValue; |
| 348 | l_dstStrValue = std::string(l_dstBinaryValue.begin(), |
| 349 | l_dstBinaryValue.end()); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if (l_srcBinaryValue != l_dstBinaryValue) |
| 354 | { |
| 355 | // ToDo: Handle if there is no valid default value in the backup and |
| 356 | // restore config JSON. |
| 357 | if (l_dstBinaryValue == l_defaultBinaryValue) |
| 358 | { |
| 359 | // Update keyword's value on hardware |
| 360 | auto l_vpdParser = |
| 361 | std::make_shared<Parser>(l_dstFruPath, m_sysCfgJsonObj); |
| 362 | |
| 363 | auto l_bytesUpdatedOnHardware = l_vpdParser->updateVpdKeyword( |
| 364 | types::IpzData(l_dstRecordName, l_dstKeywordName, |
| 365 | l_srcBinaryValue)); |
| 366 | |
| 367 | /* To keep the data in sync between hardware and parsed map |
| 368 | updating the io_dstVpdMap. This should only be done if write |
| 369 | on hardware returns success.*/ |
| 370 | if (!io_dstVpdMap.empty() && l_bytesUpdatedOnHardware > 0) |
| 371 | { |
| 372 | io_dstVpdMap[l_dstRecordName][l_dstKeywordName] = |
| 373 | l_srcStrValue; |
| 374 | } |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | if (l_srcBinaryValue == l_defaultBinaryValue) |
| 379 | { |
| 380 | // Update keyword's value on hardware |
| 381 | auto l_vpdParser = |
| 382 | std::make_shared<Parser>(l_srcFruPath, m_sysCfgJsonObj); |
| 383 | |
| 384 | auto l_bytesUpdatedOnHardware = l_vpdParser->updateVpdKeyword( |
| 385 | types::IpzData(l_srcRecordName, l_srcKeywordName, |
| 386 | l_dstBinaryValue)); |
| 387 | |
| 388 | /* To keep the data in sync between hardware and parsed map |
| 389 | updating the io_srcVpdMap. This should only be done if write |
| 390 | on hardware returns success.*/ |
| 391 | if (!io_srcVpdMap.empty() && l_bytesUpdatedOnHardware > 0) |
| 392 | { |
| 393 | io_srcVpdMap[l_srcRecordName][l_srcKeywordName] = |
| 394 | l_dstStrValue; |
| 395 | } |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | /** |
| 400 | * Update io_srcVpdMap to publish the same data on DBus, which |
| 401 | * is already present on the DBus. Because after calling |
| 402 | * backupAndRestore API the map value will get published to DBus |
| 403 | * in the worker flow. |
| 404 | */ |
| 405 | if (!io_srcVpdMap.empty() && io_dstVpdMap.empty()) |
| 406 | { |
| 407 | io_srcVpdMap[l_srcRecordName][l_srcKeywordName] = |
| 408 | l_dstStrValue; |
| 409 | } |
| 410 | |
| 411 | std::string l_errorMsg( |
| 412 | "Mismatch found between source and destination VPD for record : " + |
| 413 | l_srcRecordName + " and keyword : " + l_srcKeywordName + |
Anupama B R | 6f14283 | 2025-04-11 04:17:49 -0500 | [diff] [blame] | 414 | " . Value read from source : " + |
| 415 | commonUtility::convertByteVectorToHex(l_srcBinaryValue) + |
| 416 | " . Value read from destination : " + |
| 417 | commonUtility::convertByteVectorToHex(l_dstBinaryValue)); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 418 | |
| 419 | EventLogger::createSyncPel( |
| 420 | types::ErrorType::VpdMismatch, types::SeverityType::Warning, |
| 421 | __FILE__, __FUNCTION__, 0, l_errorMsg, std::nullopt, |
| 422 | std::nullopt, std::nullopt, std::nullopt); |
| 423 | } |
| 424 | } |
| 425 | else if (l_srcBinaryValue == l_defaultBinaryValue && |
| 426 | l_dstBinaryValue == l_defaultBinaryValue && l_isPelRequired) |
| 427 | { |
| 428 | std::string l_errorMsg( |
| 429 | "Default value found on both source and destination VPD, for record: " + |
| 430 | l_srcRecordName + " and keyword: " + l_srcKeywordName); |
| 431 | |
| 432 | EventLogger::createSyncPel( |
| 433 | types::ErrorType::DefaultValue, types::SeverityType::Error, |
| 434 | __FILE__, __FUNCTION__, 0, l_errorMsg, std::nullopt, |
| 435 | std::nullopt, std::nullopt, std::nullopt); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void BackupAndRestore::setBackupAndRestoreStatus( |
| 441 | const BackupAndRestoreStatus& i_status) |
| 442 | { |
| 443 | m_backupAndRestoreStatus = i_status; |
| 444 | } |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 445 | |
| 446 | int BackupAndRestore::updateKeywordOnPrimaryOrBackupPath( |
| 447 | const std::string& i_fruPath, |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 448 | const types::WriteVpdParams& i_paramsToWriteData) const noexcept |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 449 | { |
| 450 | if (i_fruPath.empty()) |
| 451 | { |
| 452 | logging::logMessage("Given FRU path is empty."); |
| 453 | return constants::FAILURE; |
| 454 | } |
| 455 | |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 456 | bool l_inputPathIsSourcePath = false; |
| 457 | bool l_inputPathIsDestinationPath = false; |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 458 | |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 459 | if (m_backupAndRestoreCfgJsonObj.contains("source") && |
| 460 | m_backupAndRestoreCfgJsonObj["source"].value("hardwarePath", "") == |
| 461 | i_fruPath && |
| 462 | m_backupAndRestoreCfgJsonObj.contains("destination") && |
| 463 | !m_backupAndRestoreCfgJsonObj["destination"] |
| 464 | .value("hardwarePath", "") |
| 465 | .empty()) |
| 466 | { |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 467 | l_inputPathIsSourcePath = true; |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 468 | } |
| 469 | else if (m_backupAndRestoreCfgJsonObj.contains("destination") && |
| 470 | m_backupAndRestoreCfgJsonObj["destination"].value( |
| 471 | "hardwarePath", "") == i_fruPath && |
| 472 | m_backupAndRestoreCfgJsonObj.contains("source") && |
| 473 | !m_backupAndRestoreCfgJsonObj["source"] |
| 474 | .value("hardwarePath", "") |
| 475 | .empty()) |
| 476 | { |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 477 | l_inputPathIsDestinationPath = true; |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 478 | } |
| 479 | else |
| 480 | { |
| 481 | // Input path is neither source or destination path of the |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 482 | // backup&restore JSON or source and destination paths are not hardware |
| 483 | // paths in the config JSON. |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 484 | return constants::SUCCESS; |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 487 | if (m_backupAndRestoreCfgJsonObj["backupMap"].is_array()) |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 488 | { |
| 489 | std::string l_inpRecordName; |
| 490 | std::string l_inpKeywordName; |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 491 | types::BinaryVector l_inpKeywordValue; |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 492 | |
| 493 | if (const types::IpzData* l_ipzData = |
| 494 | std::get_if<types::IpzData>(&i_paramsToWriteData)) |
| 495 | { |
| 496 | l_inpRecordName = std::get<0>(*l_ipzData); |
| 497 | l_inpKeywordName = std::get<1>(*l_ipzData); |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 498 | l_inpKeywordValue = std::get<2>(*l_ipzData); |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 499 | |
| 500 | if (l_inpRecordName.empty() || l_inpKeywordName.empty() || |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 501 | l_inpKeywordValue.empty()) |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 502 | { |
| 503 | logging::logMessage("Invalid input received"); |
| 504 | return constants::FAILURE; |
| 505 | } |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | // only IPZ type VPD is supported now. |
| 510 | return constants::SUCCESS; |
| 511 | } |
| 512 | |
| 513 | for (const auto& l_aRecordKwInfo : |
| 514 | m_backupAndRestoreCfgJsonObj["backupMap"]) |
| 515 | { |
| 516 | if (l_aRecordKwInfo.value("sourceRecord", "").empty() || |
| 517 | l_aRecordKwInfo.value("sourceKeyword", "").empty() || |
| 518 | l_aRecordKwInfo.value("destinationRecord", "").empty() || |
| 519 | l_aRecordKwInfo.value("destinationKeyword", "").empty()) |
| 520 | { |
| 521 | // invalid backup map found |
| 522 | logging::logMessage( |
| 523 | "Invalid backup map found, one or more field(s) found empty or not present in the config JSON: sourceRecord: " + |
| 524 | l_aRecordKwInfo.value("sourceRecord", "") + |
| 525 | ", sourceKeyword: " + |
| 526 | l_aRecordKwInfo.value("sourceKeyword", "") + |
| 527 | ", destinationRecord: " + |
| 528 | l_aRecordKwInfo.value("destinationRecord", "") + |
| 529 | ", destinationKeyword: " + |
| 530 | l_aRecordKwInfo.value("destinationKeyword", "")); |
| 531 | continue; |
| 532 | } |
| 533 | |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 534 | if (l_inputPathIsSourcePath && |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 535 | (l_aRecordKwInfo["sourceRecord"] == l_inpRecordName) && |
| 536 | (l_aRecordKwInfo["sourceKeyword"] == l_inpKeywordName)) |
| 537 | { |
| 538 | std::string l_fruPath( |
| 539 | m_backupAndRestoreCfgJsonObj["destination"] |
| 540 | ["hardwarePath"]); |
| 541 | Parser l_parserObj(l_fruPath, m_sysCfgJsonObj); |
| 542 | |
| 543 | return l_parserObj.updateVpdKeyword(std::make_tuple( |
| 544 | l_aRecordKwInfo["destinationRecord"], |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 545 | l_aRecordKwInfo["destinationKeyword"], l_inpKeywordValue)); |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 546 | } |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 547 | else if (l_inputPathIsDestinationPath && |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 548 | (l_aRecordKwInfo["destinationRecord"] == |
| 549 | l_inpRecordName) && |
| 550 | (l_aRecordKwInfo["destinationKeyword"] == |
| 551 | l_inpKeywordName)) |
| 552 | { |
| 553 | std::string l_fruPath( |
| 554 | m_backupAndRestoreCfgJsonObj["source"]["hardwarePath"]); |
| 555 | Parser l_parserObj(l_fruPath, m_sysCfgJsonObj); |
| 556 | |
| 557 | return l_parserObj.updateVpdKeyword(std::make_tuple( |
| 558 | l_aRecordKwInfo["sourceRecord"], |
Anupama B R | d966f27 | 2025-07-21 02:40:16 -0500 | [diff] [blame] | 559 | l_aRecordKwInfo["sourceKeyword"], l_inpKeywordValue)); |
Anupama B R | 7b293cd | 2025-03-28 05:35:01 -0500 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Received property is not part of backup & restore JSON. |
Anupama B R | 8dedd1e | 2025-03-24 07:43:47 -0500 | [diff] [blame] | 565 | return constants::SUCCESS; |
| 566 | } |
| 567 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 568 | } // namespace vpd |