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/types.hpp b/types.hpp
index cfe0530..09217af 100644
--- a/types.hpp
+++ b/types.hpp
@@ -4,6 +4,7 @@
#include <map>
#include <sdbusplus/server.hpp>
#include <string>
+#include <unordered_map>
#include <vector>
namespace openpower
@@ -31,6 +32,9 @@
using Object = sdbusplus::message::object_path;
using ObjectMap = std::map<Object, InterfaceMap>;
+using VPDfilepath = std::string;
+using FrusMap = std::unordered_map<Path, VPDfilepath>;
+
using namespace std::string_literals;
constexpr auto pimPath = "/xyz/openbmc_project/inventory";
constexpr auto pimIntf = "xyz.openbmc_project.Inventory.Manager";
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
diff --git a/vpd-manager/manager.hpp b/vpd-manager/manager.hpp
index e542103..e04250c 100644
--- a/vpd-manager/manager.hpp
+++ b/vpd-manager/manager.hpp
@@ -3,6 +3,8 @@
#include "types.hpp"
#include <com/ibm/VPD/Manager/server.hpp>
+#include <map>
+#include <nlohmann/json.hpp>
#include <sdbusplus/server.hpp>
namespace sdbusplus
@@ -117,11 +119,22 @@
void run();
private:
+ /** @brief process the given JSON file
+ */
+ void processJSON();
+
/** @brief Persistent sdbusplus DBus bus connection. */
sdbusplus::bus::bus _bus;
/** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
sdbusplus::server::manager::manager _manager;
+
+ // file to store parsed json
+ nlohmann::json jsonFile;
+
+ // map to hold mapping to inventory path to vpd file path
+ // we need as map here as it is in reverse order to that of json
+ inventory::FrusMap frus;
};
} // namespace manager
diff --git a/vpd-manager/meson.build b/vpd-manager/meson.build
index a879b2b..0b655fd 100644
--- a/vpd-manager/meson.build
+++ b/vpd-manager/meson.build
@@ -8,12 +8,14 @@
phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces')
compiler = meson.get_compiler('cpp')
+compiler.has_header('nlohmann/json.hpp')
configure_file( output: 'config.h',
configuration: {
'BUSNAME' : '"' + get_option('BUSNAME') + '"',
'OBJPATH' : '"' + get_option('OBJPATH') + '"',
'IFACE' : '"' + get_option('IFACE') + '"',
+ 'INVENTORY_JSON' : '"' + get_option('INVENTORY_JSON') + '"'
}
)