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