ldap: Add persistence for LDAP mapper D-Bus objects

Change-Id: Ib8979a7c655f74c332d80e7fb221ef03e9a3f83c
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/phosphor-ldap-mapper/ldap_mapper_mgr.cpp b/phosphor-ldap-mapper/ldap_mapper_mgr.cpp
index 9fe40ad..a321331 100644
--- a/phosphor-ldap-mapper/ldap_mapper_mgr.cpp
+++ b/phosphor-ldap-mapper/ldap_mapper_mgr.cpp
@@ -5,6 +5,7 @@
 #include <phosphor-logging/elog-errors.hpp>
 #include "config.h"
 #include "ldap_mapper_mgr.hpp"
+#include "ldap_mapper_serialize.hpp"
 
 namespace phosphor
 {
@@ -38,6 +39,8 @@
     auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
         bus, mapperObject.c_str(), groupName, privilege, *this);
 
+    serialize(*entry, entryId);
+
     PrivilegeMapperList.emplace(entryId, std::move(entry));
 
     return mapperObject;
@@ -45,6 +48,11 @@
 
 void LDAPMapperMgr::deletePrivilegeMapper(Id id)
 {
+    // Delete the persistent representation of the privilege mapper.
+    fs::path mapperPath(LDAP_MAPPER_PERSIST_PATH);
+    mapperPath /= std::to_string(id);
+    fs::remove(mapperPath);
+
     PrivilegeMapperList.erase(id);
 }
 
@@ -84,5 +92,34 @@
     }
 }
 
+void LDAPMapperMgr::restore()
+{
+    namespace fs = std::experimental::filesystem;
+
+    fs::path dir(LDAP_MAPPER_PERSIST_PATH);
+    if (!fs::exists(dir) || fs::is_empty(dir))
+    {
+        return;
+    }
+
+    for (auto &file : fs::directory_iterator(dir))
+    {
+        std::string id = file.path().filename().c_str();
+        size_t idNum = std::stol(id);
+        auto entryPath = std::string(mapperMgrRoot) + '/' + id;
+        auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
+            bus, entryPath.c_str(), *this);
+        if (deserialize(file.path(), *entry))
+        {
+            entry->Ifaces::emit_object_added();
+            PrivilegeMapperList.emplace(idNum, std::move(entry));
+            if (idNum > entryId)
+            {
+                entryId = idNum;
+            }
+        }
+    }
+}
+
 } // namespace user
 } // namespace phosphor