1*fa5e4d32SSunny Srivastava #include "logger.hpp"
2*fa5e4d32SSunny Srivastava #include "parser.hpp"
3*fa5e4d32SSunny Srivastava #include "parser_interface.hpp"
4*fa5e4d32SSunny Srivastava #include "types.hpp"
5*fa5e4d32SSunny Srivastava #include "worker.hpp"
6*fa5e4d32SSunny Srivastava
7*fa5e4d32SSunny Srivastava #include <CLI/CLI.hpp>
8*fa5e4d32SSunny Srivastava #include <nlohmann/json.hpp>
9*fa5e4d32SSunny Srivastava #include <parser_factory.hpp>
10*fa5e4d32SSunny Srivastava
11*fa5e4d32SSunny Srivastava #include <filesystem>
12*fa5e4d32SSunny Srivastava #include <iostream>
13*fa5e4d32SSunny Srivastava
14*fa5e4d32SSunny Srivastava /**
15*fa5e4d32SSunny Srivastava * @brief This file implements a generic parser APP.
16*fa5e4d32SSunny Srivastava *
17*fa5e4d32SSunny Srivastava * It recieves path of the VPD file(mandatory) and path to a config
18*fa5e4d32SSunny Srivastava * file(optional) as arguments. It will parse the data and return parsed data in
19*fa5e4d32SSunny Srivastava * a required format.
20*fa5e4d32SSunny Srivastava *
21*fa5e4d32SSunny Srivastava * Steps to get parsed VPD.
22*fa5e4d32SSunny Srivastava * - Pass VPD file path and config file (if applicable).
23*fa5e4d32SSunny Srivastava * - Read VPD file to vector.
24*fa5e4d32SSunny Srivastava * - Pass that to parser_factory to get the parser and call parse API on that
25*fa5e4d32SSunny Srivastava * parser object to get the Parsed VPD map.
26*fa5e4d32SSunny Srivastava * - If VPD format is other than the existing formats. Follow the steps
27*fa5e4d32SSunny Srivastava * - a) Add logic in parser_factory.cpp, vpdTypeCheck API to detect the format.
28*fa5e4d32SSunny Srivastava * - b) Implement a custom parser class.
29*fa5e4d32SSunny Srivastava * - c) Override parse API in the newly added parser class.
30*fa5e4d32SSunny Srivastava * - d) Add type of parsed data returned by parse API into types.hpp,
31*fa5e4d32SSunny Srivastava * "VPDMapVariant".
32*fa5e4d32SSunny Srivastava *
33*fa5e4d32SSunny Srivastava */
34*fa5e4d32SSunny Srivastava
main(int argc,char ** argv)35*fa5e4d32SSunny Srivastava int main(int argc, char** argv)
36*fa5e4d32SSunny Srivastava {
37*fa5e4d32SSunny Srivastava try
38*fa5e4d32SSunny Srivastava {
39*fa5e4d32SSunny Srivastava std::string vpdFilePath{};
40*fa5e4d32SSunny Srivastava CLI::App app{"VPD-parser-app - APP to parse VPD. "};
41*fa5e4d32SSunny Srivastava
42*fa5e4d32SSunny Srivastava app.add_option("-f, --file", vpdFilePath, "VPD file path")->required();
43*fa5e4d32SSunny Srivastava
44*fa5e4d32SSunny Srivastava std::string configFilePath{};
45*fa5e4d32SSunny Srivastava
46*fa5e4d32SSunny Srivastava app.add_option("-c,--config", configFilePath, "Path to JSON config");
47*fa5e4d32SSunny Srivastava
48*fa5e4d32SSunny Srivastava CLI11_PARSE(app, argc, argv);
49*fa5e4d32SSunny Srivastava
50*fa5e4d32SSunny Srivastava vpd::logging::logMessage("VPD file path recieved" + vpdFilePath);
51*fa5e4d32SSunny Srivastava
52*fa5e4d32SSunny Srivastava // VPD file path is a mandatory parameter to execute any parser.
53*fa5e4d32SSunny Srivastava if (vpdFilePath.empty())
54*fa5e4d32SSunny Srivastava {
55*fa5e4d32SSunny Srivastava throw std::runtime_error("Empty VPD file path");
56*fa5e4d32SSunny Srivastava }
57*fa5e4d32SSunny Srivastava
58*fa5e4d32SSunny Srivastava nlohmann::json json;
59*fa5e4d32SSunny Srivastava vpd::types::VPDMapVariant parsedVpdDataMap;
60*fa5e4d32SSunny Srivastava
61*fa5e4d32SSunny Srivastava // Below are two different ways of parsing the VPD.
62*fa5e4d32SSunny Srivastava if (!configFilePath.empty())
63*fa5e4d32SSunny Srivastava {
64*fa5e4d32SSunny Srivastava vpd::logging::logMessage(
65*fa5e4d32SSunny Srivastava "Processing with config file - " + configFilePath);
66*fa5e4d32SSunny Srivastava
67*fa5e4d32SSunny Srivastava std::shared_ptr<vpd::Worker> objWorker =
68*fa5e4d32SSunny Srivastava std::make_shared<vpd::Worker>(configFilePath);
69*fa5e4d32SSunny Srivastava parsedVpdDataMap = objWorker->parseVpdFile(vpdFilePath);
70*fa5e4d32SSunny Srivastava
71*fa5e4d32SSunny Srivastava // Based on requirement, call appropriate public API of worker class
72*fa5e4d32SSunny Srivastava /*If required to publish the FRU data on Dbus*/
73*fa5e4d32SSunny Srivastava // objWorker->publishFruDataOnDbus(parsedVpdDataMap);
74*fa5e4d32SSunny Srivastava }
75*fa5e4d32SSunny Srivastava else
76*fa5e4d32SSunny Srivastava {
77*fa5e4d32SSunny Srivastava // Will work with empty JSON
78*fa5e4d32SSunny Srivastava std::shared_ptr<vpd::Parser> vpdParser =
79*fa5e4d32SSunny Srivastava std::make_shared<vpd::Parser>(vpdFilePath, json);
80*fa5e4d32SSunny Srivastava parsedVpdDataMap = vpdParser->parse();
81*fa5e4d32SSunny Srivastava }
82*fa5e4d32SSunny Srivastava
83*fa5e4d32SSunny Srivastava // If custom handling is required then custom logic to be implemented
84*fa5e4d32SSunny Srivastava // based on the type of variant,
85*fa5e4d32SSunny Srivastava // eg: for IPZ VPD format
86*fa5e4d32SSunny Srivastava if (auto ipzVpdMap =
87*fa5e4d32SSunny Srivastava std::get_if<vpd::types::IPZVpdMap>(&parsedVpdDataMap))
88*fa5e4d32SSunny Srivastava {
89*fa5e4d32SSunny Srivastava // get rid of unused variable warning/error
90*fa5e4d32SSunny Srivastava (void)ipzVpdMap;
91*fa5e4d32SSunny Srivastava // implement code that needs to handle parsed IPZ VPD.
92*fa5e4d32SSunny Srivastava }
93*fa5e4d32SSunny Srivastava }
94*fa5e4d32SSunny Srivastava catch (const std::exception& ex)
95*fa5e4d32SSunny Srivastava {
96*fa5e4d32SSunny Srivastava vpd::logging::logMessage(ex.what());
97*fa5e4d32SSunny Srivastava return -1;
98*fa5e4d32SSunny Srivastava }
99*fa5e4d32SSunny Srivastava
100*fa5e4d32SSunny Srivastava return 0;
101*fa5e4d32SSunny Srivastava }
102