presence: Use shared JSON config file finding & loading header

Use the shared JSON config determination header for loading its
JSON configuration file.

Tested:
    run phosphor-fan-presence-tach can find and load config.json from
    /usr/share/phosphor-fan-presence/presence and
    /etc/phosphor-fan-presence/presence/ in witherspoon qemu

Change-Id: I5c4fdf7d96de7c956ca24b4a5751911cc1bf1f03
Signed-off-by: Jolie Ku <jolie_ku@wistron.com>
diff --git a/presence/Makefile.am b/presence/Makefile.am
index a9e62d3..d67f287 100644
--- a/presence/Makefile.am
+++ b/presence/Makefile.am
@@ -12,7 +12,7 @@
 	psensor.cpp \
 	tach.cpp \
 	tach_detect.cpp \
-	json_config.cpp
+	json_parser.cpp
 
 phosphor_fan_presence_tach_LDADD = \
 	$(SDBUSPLUS_LIBS) \
diff --git a/presence/json_config.cpp b/presence/json_parser.cpp
similarity index 77%
rename from presence/json_config.cpp
rename to presence/json_parser.cpp
index 859948d..83267a7 100644
--- a/presence/json_config.cpp
+++ b/presence/json_parser.cpp
@@ -13,11 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "json_config.hpp"
+#include "json_parser.hpp"
 
 #include "anyof.hpp"
 #include "fallback.hpp"
 #include "gpio.hpp"
+#include "json_config.hpp"
 #include "sdbusplus.hpp"
 #include "tach.hpp"
 
@@ -48,10 +49,10 @@
 
 JsonConfig::JsonConfig(sdbusplus::bus::bus& bus) : _bus(bus)
 {
-    // Determine the configuration file to use
-    _confFile = getConfFile();
+    using config = fan::JsonConfig;
+
     // Load and process the json configuration
-    load();
+    process(config::load(config::getConfFile(bus, confAppName, confFileName)));
 }
 
 const policies& JsonConfig::get()
@@ -64,12 +65,12 @@
 {
     try
     {
-        // Determine the configuration file to use
-        _confFile = getConfFile();
-        log<level::INFO>("Loading configuration",
-                         entry("JSON_FILE=%s", _confFile.c_str()));
+        using config = fan::JsonConfig;
+
         // Load and process the json configuration
-        load();
+        process(
+            config::load(config::getConfFile(_bus, confAppName, confFileName)));
+
         for (auto& p : _policies)
         {
             p->monitor();
@@ -83,81 +84,6 @@
     }
 }
 
-const fs::path JsonConfig::getConfFile()
-{
-    // Check override location
-    fs::path confFile = fs::path{confOverridePath} / confFileName;
-    if (fs::exists(confFile))
-    {
-        return confFile;
-    }
-
-    try
-    {
-        // Retrieve json config relative path location from dbus
-        auto confDbusValue =
-            util::SDBusPlus::getProperty<std::vector<std::string>>(
-                _bus, confDbusPath, confDbusIntf, confDbusProp);
-        // Look for a config file at each entry relative to the base
-        // path and use the first one found
-        auto it = std::find_if(confDbusValue.begin(), confDbusValue.end(),
-                               [&confFile](auto const& entry) {
-                                   confFile = fs::path{confBasePath} / entry /
-                                              confFileName;
-                                   return fs::exists(confFile);
-                               });
-        if (it == confDbusValue.end())
-        {
-            // Property exists, but no config file found. Use default base path
-            confFile = fs::path{confBasePath} / confFileName;
-        }
-    }
-    catch (const util::DBusError&)
-    {
-        // Property unavailable, attempt default base path
-        confFile = fs::path{confBasePath} / confFileName;
-    }
-
-    if (!fs::exists(confFile))
-    {
-        log<level::ERR>("No JSON config file found",
-                        entry("DEFAULT_FILE=%s", confFile.c_str()));
-        throw std::runtime_error("No JSON config file found");
-    }
-
-    return confFile;
-}
-
-void JsonConfig::load()
-{
-    std::ifstream file;
-    json jsonConf;
-
-    if (fs::exists(_confFile))
-    {
-        file.open(_confFile);
-        try
-        {
-            jsonConf = json::parse(file);
-        }
-        catch (std::exception& e)
-        {
-            log<level::ERR>("Failed to parse JSON config file",
-                            entry("JSON_FILE=%s", _confFile.c_str()),
-                            entry("JSON_ERROR=%s", e.what()));
-            throw std::runtime_error("Failed to parse JSON config file");
-        }
-    }
-    else
-    {
-        log<level::ERR>("Unable to open JSON config file",
-                        entry("JSON_FILE=%s", _confFile.c_str()));
-        throw std::runtime_error("Unable to open JSON config file");
-    }
-
-    process(jsonConf);
-}
-
 void JsonConfig::process(const json& jsonConf)
 {
     policies policies;
diff --git a/presence/json_config.hpp b/presence/json_parser.hpp
similarity index 81%
rename from presence/json_config.hpp
rename to presence/json_parser.hpp
index f9e1df6..9d99a40 100644
--- a/presence/json_config.hpp
+++ b/presence/json_parser.hpp
@@ -24,12 +24,7 @@
 using json = nlohmann::json;
 
 constexpr auto confFileName = "config.json";
-constexpr auto confOverridePath = "/etc/phosphor-fan-presence/presence";
-constexpr auto confBasePath = "/usr/share/phosphor-fan-presence/presence";
-constexpr auto confDbusPath = "/xyz/openbmc_project/inventory/system/chassis";
-constexpr auto confDbusIntf =
-    "xyz.openbmc_project.Inventory.Decorator.Compatible";
-constexpr auto confDbusProp = "Names";
+constexpr auto confAppName = "presence";
 
 using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
 
@@ -86,9 +81,6 @@
     /* The sdbusplus bus object */
     sdbusplus::bus::bus& _bus;
 
-    /* Config file to be used */
-    fs::path _confFile;
-
     /* List of Fan objects to have presence policies */
     std::vector<fanPolicy> _fans;
 
@@ -102,24 +94,6 @@
     static const std::map<std::string, rpolicyHandler> _rpolicies;
 
     /**
-     * Get the json configuration file. The first location found to contain
-     * the json config file is used from the following locations in order.
-     * 1.) From the confOverridePath location
-     * 2.) From config file found using property value(s) as a relative
-     * path extension on the base path from the dbus object where:
-     *     path = Path set in confDbusPath
-     *     interface = Interface set in confDbusIntf
-     *     property = Property set in confDbusProp
-     * 3.) *DEFAULT* - From the confBasePath location
-     */
-    const fs::path getConfFile();
-
-    /**
-     * @brief Load the json config file
-     */
-    void load();
-
-    /**
      * @brief Process the json config to extract the defined fan presence
      * policies.
      *
diff --git a/presence/tach_detect.cpp b/presence/tach_detect.cpp
index 1c1dcf9..41af199 100644
--- a/presence/tach_detect.cpp
+++ b/presence/tach_detect.cpp
@@ -15,7 +15,7 @@
  */
 #include "config.h"
 #ifdef PRESENCE_USE_JSON
-#include "json_config.hpp"
+#include "json_parser.hpp"
 #else
 #include "generated.hpp"
 #endif