Interface & Factory implementation for VPD Parsers
This commit abstracts the implementation logic of different parser.
A parser factory is implemented to provide instance of required
parser based on the type of vpd file needed to be parsed.
The interface should be derived to implement any further parser logic
related to vpd.
Status: This does not add any new function, so basic testing of
VPD parsing, VPD tool and writes was performed.
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
Change-Id: I3ce1a2d6b7e8d8984fd7800132e78ab8a9a21e56
diff --git a/ibm_vpd_app.cpp b/ibm_vpd_app.cpp
index b265919..25b77f1 100644
--- a/ibm_vpd_app.cpp
+++ b/ibm_vpd_app.cpp
@@ -1,9 +1,10 @@
#include "config.h"
#include "defines.hpp"
+#include "ipz_parser.hpp"
#include "keyword_vpd_parser.hpp"
#include "memory_vpd_parser.hpp"
-#include "parser.hpp"
+#include "parser_factory.hpp"
#include "utils.hpp"
#include <ctype.h>
@@ -24,8 +25,10 @@
using namespace openpower::vpd::constants;
namespace fs = filesystem;
using json = nlohmann::json;
+using namespace openpower::vpd::parser::factory;
using namespace openpower::vpd::inventory;
using namespace openpower::vpd::memory::parser;
+using namespace openpower::vpd::parser::interface;
/**
* @brief Expands location codes
@@ -309,13 +312,13 @@
*/
template <typename T>
static void populateDbus(const T& vpdMap, nlohmann::json& js,
- const string& filePath, const string& preIntrStr)
+ const string& filePath) //, const string &preIntrStr) {
{
inventory::InterfaceMap interfaces;
inventory::ObjectMap objects;
inventory::PropertyMap prop;
- bool isSystemVpd;
+ bool isSystemVpd = false;
for (const auto& item : js["frus"][filePath])
{
const auto& objectPath = item["inventoryPath"];
@@ -334,12 +337,12 @@
for (const auto& record : vpdMap)
{
populateFruSpecificInterfaces(
- record.second, preIntrStr + record.first, interfaces);
+ record.second, ipzVpdInf + record.first, interfaces);
}
}
else if constexpr (is_same<T, KeywordVpdMap>::value)
{
- populateFruSpecificInterfaces(vpdMap, preIntrStr, interfaces);
+ populateFruSpecificInterfaces(vpdMap, kwdVpdInf, interfaces);
}
if (js.find("commonInterfaces") != js.end())
{
@@ -360,7 +363,7 @@
if (vpdMap.find(recordName) != vpdMap.end())
{
populateFruSpecificInterfaces(
- vpdMap.at(recordName), preIntrStr + recordName,
+ vpdMap.at(recordName), ipzVpdInf + recordName,
interfaces);
}
}
@@ -464,48 +467,25 @@
return 0;
}
- Binary vpdVector(getVpdDataInVector(js, file));
- vpdType type = vpdTypeCheck(vpdVector);
+ Binary vpdVector = getVpdDataInVector(js, file);
- switch (type)
+ ParserInterface* parser =
+ ParserFactory::getParser(std::move(vpdVector));
+
+ variant<KeywordVpdMap, Store> parseResult;
+ parseResult = parser->parse();
+
+ if (auto pVal = get_if<Store>(&parseResult))
{
- case IPZ_VPD:
- {
- // Invoking IPZ Vpd Parser
- auto vpdStore = parse(move(vpdVector));
- const Parsed& vpdMap = vpdStore.getVpdMap();
- string preIntrStr = "com.ibm.ipzvpd.";
- // Write it to the inventory
- populateDbus(vpdMap, js, file, preIntrStr);
- }
- break;
-
- case KEYWORD_VPD:
- {
- // Creating Keyword Vpd Parser Object
- KeywordVpdParser parserObj(move(vpdVector));
- // Invoking KW Vpd Parser
- const auto& kwValMap = parserObj.parseKwVpd();
- string preIntrStr = "com.ibm.ipzvpd.VINI";
- populateDbus(kwValMap, js, file, preIntrStr);
- }
- break;
-
- case MEMORY_VPD:
- {
- // Get an object to call API & get the key-value map
- memoryVpdParser vpdParser(move(vpdVector));
- const auto& memKwValMap = vpdParser.parseMemVpd();
-
- string preIntrStr = "com.ibm.kwvpd.KWVPD";
- // js(define dimm sys path in js), ObjPath(define in JS)
- populateDbus(memKwValMap, js, file, preIntrStr);
- }
- break;
-
- default:
- throw runtime_error("Invalid VPD format");
+ populateDbus(pVal->getVpdMap(), js, file);
}
+ else if (auto pVal = get_if<KeywordVpdMap>(&parseResult))
+ {
+ populateDbus(*pVal, js, file);
+ }
+
+ // release the parser object
+ ParserFactory::freeParser(parser);
}
catch (exception& e)
{