LDAP: start or stop nslcd on service starting

`nslcd` service should be started only if there is a valid endpoint.
This commit adds a call that starts or stops `nslcd.service` after
the Dbus objects has been deserialized, depending by the presence of
enabled endpoint.

Tested:
1. Made sure `nslcd` is stopped by default.
2. Added a configuration for AD/LDAP endpoint and made sure `nslcd`
   is started.
3. Rebooted BMC and made sure `nslcd` is started.

Change-Id: I06d91cb450e92bdfb12c4f65dce4e250113ab461
Signed-off-by: Alexander Filippov <a.filippov@yadro.com>
diff --git a/phosphor-ldap-config/ldap_config_mgr.cpp b/phosphor-ldap-config/ldap_config_mgr.cpp
index e8490e4..1218d18 100644
--- a/phosphor-ldap-config/ldap_config_mgr.cpp
+++ b/phosphor-ldap-config/ldap_config_mgr.cpp
@@ -12,6 +12,7 @@
 namespace ldap
 {
 
+constexpr auto nslcdService = "nslcd.service";
 constexpr auto nscdService = "nscd.service";
 constexpr auto LDAPscheme = "ldap";
 constexpr auto LDAPSscheme = "ldaps";
@@ -211,6 +212,9 @@
         openLDAPConfigPtr->restoreRoleMapping();
         openLDAPConfigPtr->emit_object_added();
     }
+
+    startOrStopService(phosphor::ldap::nslcdService,
+                       ADConfigPtr->enabled() || openLDAPConfigPtr->enabled());
 }
 
 } // namespace ldap
diff --git a/test/ldap_config_test.cpp b/test/ldap_config_test.cpp
index 3cba8cd..35fdaea 100644
--- a/test/ldap_config_test.cpp
+++ b/test/ldap_config_test.cpp
@@ -204,6 +204,39 @@
               ldap_base::Config::Type::OpenLdap);
 }
 
+TEST_F(TestLDAPConfig, testRestoresDefault)
+{
+    auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
+    auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
+    auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
+    auto dbusPersistentFilePath = std::string(dir.c_str());
+
+    if (fs::exists(configFilePath))
+    {
+        fs::remove(configFilePath);
+    }
+    EXPECT_FALSE(fs::exists(configFilePath));
+
+    MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
+                          dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
+                          tlsCertfile.c_str());
+
+    EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
+    EXPECT_CALL(manager, restartService("nslcd.service")).Times(0);
+    EXPECT_CALL(manager, restartService("nscd.service")).Times(0);
+
+    manager.restore();
+
+    EXPECT_NE(nullptr, manager.getADConfigPtr());
+    EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
+    EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
+              ldap_base::Config::Type::ActiveDirectory);
+    EXPECT_EQ(manager.getOpenLdapConfigPtr()->ldapType(),
+              ldap_base::Config::Type::OpenLdap);
+    EXPECT_FALSE(manager.getADConfigPtr()->enabled());
+    EXPECT_FALSE(manager.getOpenLdapConfigPtr()->enabled());
+}
+
 TEST_F(TestLDAPConfig, testRestores)
 {
     auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
@@ -221,7 +254,7 @@
                           dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
                           tlsCertfile.c_str());
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(1);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
     managerPtr->createConfig(
         "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
@@ -270,7 +303,7 @@
                           tlsCertfile.c_str());
 
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
 
     managerPtr->createConfig(
@@ -319,7 +352,7 @@
                           tlsCertfile.c_str());
 
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
 
     managerPtr->createConfig(
@@ -371,7 +404,7 @@
                           dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
                           tlsCertfile.c_str());
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
     managerPtr->createConfig(
         "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
@@ -421,7 +454,7 @@
                           dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
                           tlsCertfile.c_str());
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
     managerPtr->createConfig(
         "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
@@ -459,7 +492,7 @@
                           dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
                           tlsCertfile.c_str());
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(1);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
     managerPtr->createConfig(
         "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
@@ -499,7 +532,7 @@
                           dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
                           tlsCertfile.c_str());
     EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
-    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(1);
+    EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
     EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
     managerPtr->createConfig(
         "ldaps://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",