entityName: move pieces into handler object
Move different items used by the handler into the handler and enable
unit-testing by parameterizing different aspects of the code.
Tested: Only ran unit-tests (added new ones).
Change-Id: Ia3b4b5792c0ac1ae5bc6513eadfc9ee35f7a369f
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/util.cpp b/util.cpp
new file mode 100644
index 0000000..02dab83
--- /dev/null
+++ b/util.cpp
@@ -0,0 +1,38 @@
+#include "util.hpp"
+
+#include <fstream>
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <string>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+namespace google
+{
+namespace ipmi
+{
+
+using namespace phosphor::logging;
+using InternalFailure =
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+
+nlohmann::json parseConfig(const std::string& file)
+{
+ std::ifstream jsonFile(file);
+ if (!jsonFile.is_open())
+ {
+ log<level::ERR>("Entity association JSON file not found");
+ elog<InternalFailure>();
+ }
+
+ auto data = nlohmann::json::parse(jsonFile, nullptr, false);
+ if (data.is_discarded())
+ {
+ log<level::ERR>("Entity association JSON parser failure");
+ elog<InternalFailure>();
+ }
+
+ return data;
+}
+
+} // namespace ipmi
+} // namespace google