JSON parser for VPD manager app
This commit implements a parser which parse and store vpd JSON at the
launch of the application.
An applicatin specific parser is required to support write keyword
functionality as reverse mapping of JSON items is required.
In this case the user passes an invetory path and EEPROM VPD for that
inventory path needs to be extracted.
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
Change-Id: I7f91b45982c59dfb0d36fe8e80ebd7373a93f147
diff --git a/vpd-manager/manager.cpp b/vpd-manager/manager.cpp
index f34ab68..bd3af72 100644
--- a/vpd-manager/manager.cpp
+++ b/vpd-manager/manager.cpp
@@ -5,7 +5,9 @@
#include "parser.hpp"
#include <exception>
+#include <fstream>
#include <iostream>
+#include <nlohmann/json.hpp>
#include <vector>
namespace openpower
@@ -24,17 +26,50 @@
void Manager::run()
{
+ try
+ {
+ processJSON();
+ }
+ catch (const std::exception& e)
+ {
+ std::cerr << e.what() << "\n";
+ }
+
while (true)
{
- try
+ _bus.process_discard();
+
+ // wait for event
+ _bus.wait();
+ }
+}
+
+void Manager::processJSON()
+{
+ std::ifstream json(INVENTORY_JSON, std::ios::binary);
+
+ if (!json)
+ {
+ throw std::runtime_error("json file not found");
+ }
+
+ jsonFile = nlohmann::json::parse(json);
+ if (jsonFile.find("frus") == jsonFile.end())
+ {
+ throw std::runtime_error("frus group not found in json");
+ }
+
+ const nlohmann::json& groupFRUS =
+ jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
+ for (const auto& itemFRUS : groupFRUS.items())
+ {
+ const std::vector<nlohmann::json>& groupEEPROM =
+ itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
+ for (const auto& itemEEPROM : groupEEPROM)
{
- _bus.process_discard();
- // wait for event
- _bus.wait();
- }
- catch (const std::exception& e)
- {
- std::cerr << e.what() << "\n";
+ frus.emplace(itemEEPROM["inventoryPath"]
+ .get_ref<const nlohmann::json::string_t&>(),
+ itemFRUS.key());
}
}
}
@@ -64,6 +99,7 @@
{
// implement the interface
}
+
} // namespace manager
} // namespace vpd
} // namespace openpower