Refactor get keyword value API exception handling
This commit refactors vpd specific utility API used to read value of a
keyword,in order to handle any exceptions thrown by it locally. All
utility methods should handle exceptions locally and log a journal log
in case of failure. The caller of the utility APIs should check the
return value to detect success/failure.
This commit also changes the caller of this API throughout the repo, in
order to check the return value.
Test:
```
- Install bitbaked image on Everest system
- After BMC boots, BMC should reach Ready state
- Check vpd-manager service status, should be active(running)
- Check no restarts in vpd-manager service
- Check vpd-manager "CollectionStatus" = "Completed"
- Check extra interfaces are processed properly
- Check backup restore working properly
```
Change-Id: I965313f512553ed5d39373dd871754e1a8fed5f3
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 09c4719..fbf713b 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -135,29 +135,40 @@
/**
* @brief An API to read value of a keyword.
*
- * Note: Throws exception. Caller needs to handle.
*
- * @param[in] kwdValueMap - A map having Kwd value pair.
- * @param[in] kwd - keyword name.
- * @param[out] kwdValue - Value of the keyword read from map.
+ * @param[in] i_kwdValueMap - A map having Kwd value pair.
+ * @param[in] i_kwd - keyword name.
+ *
+ * @return On success returns value of the keyword read from map, otherwise
+ * returns empty string.
*/
-inline void getKwVal(const types::IPZKwdValueMap& kwdValueMap,
- const std::string& kwd, std::string& kwdValue)
+inline std::string getKwVal(const types::IPZKwdValueMap& i_kwdValueMap,
+ const std::string& i_kwd) noexcept
{
- if (kwd.empty())
+ std::string l_kwdValue;
+ try
{
- logging::logMessage("Invalid parameters");
- throw std::runtime_error("Invalid parameters");
- }
+ if (i_kwd.empty())
+ {
+ throw std::runtime_error("Invalid parameters");
+ }
- auto itrToKwd = kwdValueMap.find(kwd);
- if (itrToKwd != kwdValueMap.end())
+ auto l_itrToKwd = i_kwdValueMap.find(i_kwd);
+ if (l_itrToKwd != i_kwdValueMap.end())
+ {
+ l_kwdValue = l_itrToKwd->second;
+ }
+ else
+ {
+ throw std::runtime_error("Keyword not found");
+ }
+ }
+ catch (const std::exception& l_ex)
{
- kwdValue = itrToKwd->second;
- return;
+ logging::logMessage("Failed to get value for keyword [" + i_kwd +
+ "]. Error : " + l_ex.what());
}
-
- throw std::runtime_error("Keyword not found");
+ return l_kwdValue;
}
/**
@@ -321,9 +332,19 @@
ipzVpdMap && (*ipzVpdMap).find(recordName) != (*ipzVpdMap).end())
{
auto itrToVCEN = (*ipzVpdMap).find(recordName);
- // The exceptions will be cautght at end.
- getKwVal(itrToVCEN->second, kwd1, firstKwdValue);
- getKwVal(itrToVCEN->second, kwd2, secondKwdValue);
+ firstKwdValue = getKwVal(itrToVCEN->second, kwd1);
+ if (firstKwdValue.empty())
+ {
+ throw std::runtime_error(
+ "Failed to get value for keyword [" + kwd1 + "]");
+ }
+
+ secondKwdValue = getKwVal(itrToVCEN->second, kwd2);
+ if (secondKwdValue.empty())
+ {
+ throw std::runtime_error(
+ "Failed to get value for keyword [" + kwd2 + "]");
+ }
}
else
{
@@ -488,9 +509,8 @@
"VINI record not found in parsed VPD. Can't find CCIN");
}
- std::string l_ccinFromVpd;
- vpdSpecificUtility::getKwVal(l_itrToRec->second, "CC",
- l_ccinFromVpd);
+ std::string l_ccinFromVpd{
+ vpdSpecificUtility::getKwVal(l_itrToRec->second, "CC")};
if (l_ccinFromVpd.empty())
{
throw DataException(
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 9de43b9..e275ea2 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -244,8 +244,16 @@
std::string l_srcStrValue;
if (!io_srcVpdMap.empty())
{
- vpdSpecificUtility::getKwVal(io_srcVpdMap.at(l_srcRecordName),
- l_srcKeywordName, l_srcStrValue);
+ l_srcStrValue = vpdSpecificUtility::getKwVal(
+ io_srcVpdMap.at(l_srcRecordName), l_srcKeywordName);
+
+ if (l_srcStrValue.empty())
+ {
+ std::runtime_error(
+ std::string("Failed to get value for keyword [") +
+ l_srcKeywordName + std::string("]"));
+ }
+
l_srcBinaryValue =
types::BinaryVector(l_srcStrValue.begin(), l_srcStrValue.end());
}
@@ -268,8 +276,16 @@
std::string l_dstStrValue;
if (!io_dstVpdMap.empty())
{
- vpdSpecificUtility::getKwVal(io_dstVpdMap.at(l_dstRecordName),
- l_dstKeywordName, l_dstStrValue);
+ l_dstStrValue = vpdSpecificUtility::getKwVal(
+ io_dstVpdMap.at(l_dstRecordName), l_dstKeywordName);
+
+ if (l_dstStrValue.empty())
+ {
+ std::runtime_error(
+ std::string("Failed to get value for keyword [") +
+ l_dstKeywordName + std::string("]"));
+ }
+
l_dstBinaryValue =
types::BinaryVector(l_dstStrValue.begin(), l_dstStrValue.end());
}
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 1529924..4bd61bf 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -931,9 +931,9 @@
return;
}
- std::string pgKeywordValue;
- vpdSpecificUtility::getKwVal(itrToRec->second, "PG",
- pgKeywordValue);
+ const std::string pgKeywordValue{
+ vpdSpecificUtility::getKwVal(itrToRec->second, "PG")};
+
if (!pgKeywordValue.empty())
{
if (isCPUIOGoodOnly(pgKeywordValue))
@@ -942,6 +942,10 @@
["PrettyName"] = "IO Module";
}
}
+ else
+ {
+ throw std::runtime_error("Failed to get value for keyword PG");
+ }
}
}
}
@@ -999,8 +1003,9 @@
return false;
}
- std::string ccinFromVpd;
- vpdSpecificUtility::getKwVal(itrToRec->second, "CC", ccinFromVpd);
+ std::string ccinFromVpd{
+ vpdSpecificUtility::getKwVal(itrToRec->second, "CC")};
+
if (ccinFromVpd.empty())
{
return false;
@@ -1187,13 +1192,27 @@
auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS);
if (l_itrToVsys != (*l_parsedVpdMap).end())
{
- std::string l_tmKwdValue;
- vpdSpecificUtility::getKwVal(l_itrToVsys->second, constants::kwdTM,
- l_tmKwdValue);
+ const std::string l_tmKwdValue{vpdSpecificUtility::getKwVal(
+ l_itrToVsys->second, constants::kwdTM)};
- std::string l_seKwdValue;
- vpdSpecificUtility::getKwVal(l_itrToVsys->second, constants::kwdSE,
- l_seKwdValue);
+ if (l_tmKwdValue.empty())
+ {
+ throw std::runtime_error(
+ std::string("Failed to get value for keyword [") +
+ constants::kwdTM +
+ std::string("] while creating Asset tag."));
+ }
+
+ const std::string l_seKwdValue{vpdSpecificUtility::getKwVal(
+ l_itrToVsys->second, constants::kwdSE)};
+
+ if (l_seKwdValue.empty())
+ {
+ throw std::runtime_error(
+ std::string("Failed to get value for keyword [") +
+ constants::kwdSE +
+ std::string("] while creating Asset tag."));
+ }
l_assetTag = std::string{"Server-"} + l_tmKwdValue +
std::string{"-"} + l_seKwdValue;