Conditional enable the ldap configuration

If any of the existing ldap config(openldap/AD) is
already enabled,The other ldap configuration can't be
enabled.

TestedBy: Unit-Tested

          Tested the above behaviour.It throws the
          error back if try to enable the configuration
          when there is already active configuration.

          If there is no active configuration then it
          allows to enable the configuration.

Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
Change-Id: I5b6008036152cd36e5422bb372a05c8a3ec3d24b
diff --git a/phosphor-ldap-config/ldap_config.cpp b/phosphor-ldap-config/ldap_config.cpp
index 126cc46..03c6ffc 100644
--- a/phosphor-ldap-config/ldap_config.cpp
+++ b/phosphor-ldap-config/ldap_config.cpp
@@ -444,22 +444,28 @@
 
 bool Config::enabled(bool value)
 {
-    bool isEnable;
+    if (value == enabled())
+    {
+        return value;
+    }
+    // Let parent decide that can we enable this config.
+    // It may happen that other config is already enabled,
+    // Current implementation support only one config can
+    // be active at a time.
+    return parent.enableService(*this, value);
+}
+
+bool Config::enableService(bool value)
+{
+    bool isEnable = false;
     try
     {
-        if (value == enabled())
-        {
-            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 object.
         serialize();
     }
     catch (const InternalFailure& e)
diff --git a/phosphor-ldap-config/ldap_config.hpp b/phosphor-ldap-config/ldap_config.hpp
index 6eceb50..0d2adf1 100644
--- a/phosphor-ldap-config/ldap_config.hpp
+++ b/phosphor-ldap-config/ldap_config.hpp
@@ -183,6 +183,12 @@
      */
     bool deserialize();
 
+    /** @brief enable or disable the service with the given value
+     *  @param[in] value - enable/disble
+     *  @returns value of changed status
+     */
+    bool enableService(bool value);
+
   private:
     bool secureLDAP;
     std::string lDAPBindPassword{};
diff --git a/phosphor-ldap-config/ldap_config_mgr.cpp b/phosphor-ldap-config/ldap_config_mgr.cpp
index 65d4bda..9142538 100644
--- a/phosphor-ldap-config/ldap_config_mgr.cpp
+++ b/phosphor-ldap-config/ldap_config_mgr.cpp
@@ -19,6 +19,8 @@
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 namespace fs = std::filesystem;
 using Argument = xyz::openbmc_project::Common::InvalidArgument;
+using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
+using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed;
 
 using Line = std::string;
 using Key = std::string;
@@ -172,6 +174,24 @@
     }
 }
 
+bool ConfigMgr::enableService(Config& config, bool value)
+{
+    if (value)
+    {
+        if (openLDAPConfigPtr && openLDAPConfigPtr->enabled())
+        {
+            elog<NotAllowed>(NotAllowedArgument::REASON(
+                "OpenLDAP service is already active"));
+        }
+        if (ADConfigPtr && ADConfigPtr->enabled())
+        {
+            elog<NotAllowed>(NotAllowedArgument::REASON(
+                "ActiveDirectory service is already active"));
+        }
+    }
+    return config.enableService(value);
+}
+
 void ConfigMgr::restore()
 {
     createDefaultObjects();
diff --git a/phosphor-ldap-config/ldap_config_mgr.hpp b/phosphor-ldap-config/ldap_config_mgr.hpp
index 812528b..dc04546 100644
--- a/phosphor-ldap-config/ldap_config_mgr.hpp
+++ b/phosphor-ldap-config/ldap_config_mgr.hpp
@@ -102,6 +102,11 @@
     /** @brief Populate existing config into D-Bus properties
      */
     virtual void restore();
+    /** @brief enable/disable the ldap service
+     *  @param[in] config - config  which needs to be enabled/disabled
+     *  @param[in] value - boolean value to start/stop
+     */
+    bool enableService(Config& config, bool value);
 
     /* ldap service enabled property would be saved under
      * this path.