entitymap: add json file loading if present
Step 3 to handle the entity-map transition from YAML to JSON. This
patchset adds a method that will process the json file, if present to
build the map.
Tested: This patchset has not been tested.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I79297aef59844a21f20c0a77de7a21264e2ed96a
diff --git a/entity_map_json.cpp b/entity_map_json.cpp
index db45b05..b6fc6d6 100644
--- a/entity_map_json.cpp
+++ b/entity_map_json.cpp
@@ -1,6 +1,7 @@
#include "entity_map_json.hpp"
#include <exception>
+#include <fstream>
#include <ipmid/types.hpp>
#include <nlohmann/json.hpp>
#include <string>
@@ -11,6 +12,27 @@
namespace sensor
{
+EntityInfoMap buildEntityMapFromFile()
+{
+ const char* entityMapJsonFilename =
+ "/usr/share/ipmi-providers/entity-map.json";
+ EntityInfoMap builtMap;
+
+ std::ifstream mapFile(entityMapJsonFilename);
+ if (!mapFile.is_open())
+ {
+ return builtMap;
+ }
+
+ auto data = nlohmann::json::parse(mapFile, nullptr, false);
+ if (data.is_discarded())
+ {
+ return builtMap;
+ }
+
+ return buildJsonEntityMap(data);
+}
+
EntityInfoMap buildJsonEntityMap(const nlohmann::json& data)
{
EntityInfoMap builtMap;