phosphor-ldap-conf: implement restore and add error handling

Upon startup, restore D-Bus properties from LDAP config file if
it exists.

Change-Id: I63b5a41eec8937ddbd5e8b4471936376602b6b0e
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/phosphor-ldap-config/ldap_configuration.hpp b/phosphor-ldap-config/ldap_configuration.hpp
index f2bf02a..a0324c1 100644
--- a/phosphor-ldap-config/ldap_configuration.hpp
+++ b/phosphor-ldap-config/ldap_configuration.hpp
@@ -1,9 +1,14 @@
 #pragma once
 
-#include <sdbusplus/bus.hpp>
-#include <sdbusplus/server/object.hpp>
+#include "config.h"
 #include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
 #include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
 #include <string>
 
 namespace phosphor
@@ -15,6 +20,8 @@
 static constexpr auto LDAPNsSwitchFile = "/etc/nsswitch_ldap.conf";
 static constexpr auto linuxNsSwitchFile = "/etc/nsswitch_linux.conf";
 
+using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server;
 using ConfigIface = sdbusplus::server::object::object<ldap_base::Config>;
 using CreateIface = sdbusplus::server::object::object<ldap_base::Create>;
@@ -144,9 +151,19 @@
      *  @param[in] filePath - LDAP configuration file.
      */
     ConfigMgr(sdbusplus::bus::bus& bus, const char* path) :
-        CreateIface(bus, path), bus(bus)
+        CreateIface(bus, path, true), bus(bus)
     {
-        // TODO  restore config object if config file exists.
+        try
+        {
+            restore(LDAP_CONFIG_FILE);
+            emit_object_added();
+        }
+        catch (const std::exception& e)
+        {
+            configPtr.reset(nullptr);
+            log<level::ERR>(e.what());
+            elog<InternalFailure>();
+        }
     }
 
     /** @brief concrete implementation of the pure virtual funtion
@@ -179,6 +196,11 @@
 
     /** @brief Pointer to a Config D-Bus object */
     std::unique_ptr<Config> configPtr = nullptr;
+
+    /** @brief Populate existing config into D-Bus properties
+     *  @param[in] filePath - LDAP config file path
+     */
+    virtual void restore(const char* filePath);
 };
 } // namespace ldap
 } // namespace phosphor