Write the config data into the nslcd.conf file

In Config object we have the property enabled, when
it is true then write that config object into nslcd.conf

TestedBy: Unit tested

Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
Change-Id: I0c7bcf0f6557adb9314c94768b1adac39459fbe4
diff --git a/phosphor-ldap-config/ldap_config.cpp b/phosphor-ldap-config/ldap_config.cpp
index aa984c6..746f324 100644
--- a/phosphor-ldap-config/ldap_config.cpp
+++ b/phosphor-ldap-config/ldap_config.cpp
@@ -50,7 +50,10 @@
     ConfigIface::userNameAttribute(userNameAttr);
     ConfigIface::groupNameAttribute(groupNameAttr);
     // Don't update the bindDN password under ConfigIface::
-    writeConfig();
+    if (enabled())
+    {
+        writeConfig();
+    }
     // Emit deferred signal.
     this->emit_object_added();
     parent.startOrStopService(nslcdService, enabled());
@@ -187,8 +190,11 @@
     lDAPBindPassword = value;
     try
     {
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -234,8 +240,11 @@
             elog<NoCACertificate>();
         }
         val = ConfigIface::lDAPServerURI(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -276,8 +285,11 @@
         }
 
         val = ConfigIface::lDAPBindDN(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -314,8 +326,11 @@
         }
 
         val = ConfigIface::lDAPBaseDN(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -344,8 +359,12 @@
         }
 
         val = ConfigIface::lDAPSearchScope(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -375,9 +394,15 @@
             return value;
         }
         isEnable = EnableIface::enabled(value);
+        if (isEnable)
+        {
+            writeConfig();
+        }
+        // TODO in later commit, one of the config would be active
+        // at any moment of time.
+        parent.startOrStopService(nslcdService, value);
         // save the enabled property.
         serialize(*this, parent.dbusPersistentPath);
-        parent.startOrStopService(nslcdService, value);
     }
     catch (const InternalFailure& e)
     {
@@ -402,8 +427,12 @@
         }
 
         val = ConfigIface::userNameAttribute(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
@@ -428,8 +457,12 @@
         }
 
         val = ConfigIface::groupNameAttribute(value);
-        writeConfig();
-        parent.startOrStopService(nslcdService, enabled());
+        if (enabled())
+        {
+            writeConfig();
+
+            parent.startOrStopService(nslcdService, enabled());
+        }
     }
     catch (const InternalFailure& e)
     {
diff --git a/test/ldap_config_test.cpp b/test/ldap_config_test.cpp
index 74c8171..cd06bf8 100644
--- a/test/ldap_config_test.cpp
+++ b/test/ldap_config_test.cpp
@@ -116,15 +116,29 @@
                           dbusPersistentFilePath.c_str(),
                           tlsCacertfile.c_str());
 
-    EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
+    EXPECT_CALL(manager, stopService("nslcd.service")).Times(2);
     EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
-    EXPECT_CALL(manager, restartService("nscd.service")).Times(1);
+    EXPECT_CALL(manager, restartService("nscd.service")).Times(2);
     manager.createConfig(
         "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
         "MyLdap12", ldap_base::Create::SearchScope::sub,
         ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
     manager.getADConfigPtr()->enabled(true);
 
+    manager.createConfig("ldap://9.194.251.137/", "cn=Users",
+                         "cn=Users,dc=test", "MyLdap123",
+                         ldap_base::Create::SearchScope::sub,
+                         ldap_base::Create::Type::OpenLdap, "uid", "gid");
+    manager.getOpenLdapConfigPtr()->enabled(false);
+
+    // Below setting of username/groupname attr is to make sure
+    // that in-active config should not call the start/stop service.
+    manager.getOpenLdapConfigPtr()->userNameAttribute("abc");
+    EXPECT_EQ(manager.getOpenLdapConfigPtr()->userNameAttribute(), "abc");
+
+    manager.getOpenLdapConfigPtr()->groupNameAttribute("def");
+    EXPECT_EQ(manager.getOpenLdapConfigPtr()->groupNameAttribute(), "def");
+
     EXPECT_TRUE(fs::exists(configFilePath));
     EXPECT_EQ(manager.getADConfigPtr()->lDAPServerURI(),
               "ldap://9.194.251.136/");