Implement configuration of NTP server support

Change-Id: I8d471dbc6ea7ec62cbcc157565a7b39cd5031caa
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/dhcp_configuration.hpp b/dhcp_configuration.hpp
index f486f22..e952073 100644
--- a/dhcp_configuration.hpp
+++ b/dhcp_configuration.hpp
@@ -11,9 +11,6 @@
 namespace network
 {
 
-constexpr auto networkdService = "systemd-networkd.service";
-constexpr auto timeSynchdService = "systemd-timesyncd.service";
-
 class Manager; // forward declaration of network manager.
 
 namespace dhcp
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index c3bac59..ffaac74 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "config_parser.hpp"
 #include "ethernet_interface.hpp"
 #include "ipaddress.hpp"
 #include "network_manager.hpp"
@@ -48,7 +49,7 @@
     interfaceName(intfName);
     EthernetInterfaceIntf::dHCPEnabled(dhcpEnabled);
     MacAddressIntf::mACAddress(getMACAddress(intfName));
-
+    EthernetInterfaceIntf::nTPServers(getNTPServersFromConf());
     // Emit deferred signal.
     if (emitSignal)
     {
@@ -381,6 +382,36 @@
     manager.writeToConfigurationFile();
 }
 
+ServerList EthernetInterface::getNTPServersFromConf()
+{
+    fs::path confPath = manager.getConfDir();
+
+    std::string fileName = systemd::config::networkFilePrefix + interfaceName() +
+                           systemd::config::networkFileSuffix;
+    confPath /= fileName;
+    ServerList servers;
+    try
+    {
+        config::Parser parser(confPath.string());
+        servers = parser.getValues("Network", "NTP");
+    }
+    catch (InternalFailure& e)
+    {
+        log<level::INFO>("Unable to find the NTP server configuration.");
+    }
+    return servers;
+}
+
+ServerList EthernetInterface::nTPServers(ServerList servers)
+{
+    auto ntpServers =  EthernetInterfaceIntf::nTPServers(servers);
+
+    writeConfigurationFile();
+    // timesynchd reads the NTP server configuration from the
+    // network file.
+    restartSystemdUnit(timeSynchdService);
+    return ntpServers;
+}
 // Need to merge the below function with the code which writes the
 // config file during factory reset.
 // TODO openbmc/openbmc#1751
@@ -443,6 +474,12 @@
         return;
     }
 
+    //Add the NTP server
+    for(const auto& ntp: EthernetInterfaceIntf::nTPServers())
+    {
+         stream << "NTP=" << ntp << "\n";
+    }
+
     // Static
     for (const auto& addr : addrs)
     {
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index 06a2e5f..8ef1795 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -32,6 +32,8 @@
 using MacAddressIntf =
     sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
 
+using ServerList = std::vector<std::string>;
+
 namespace fs = std::experimental::filesystem;
 
 class Manager; // forward declaration of network manager.
@@ -123,6 +125,11 @@
          */
         std::string mACAddress(std::string value) override;
 
+        /** @brief sets the NTP servers.
+         *  @param[in] value - vector of NTP servers.
+         */
+        ServerList nTPServers(ServerList value) override;
+
         /** @brief create Vlan interface.
          *  @param[in] id- VLAN identifier.
          */
@@ -193,6 +200,11 @@
         /** @brief write the dhcp section **/
         void writeDHCPSection(std::fstream& stream);;
 
+        /** @brief get the NTP server list from the network conf
+         *
+         */
+        ServerList getNTPServersFromConf();
+
         /** @brief Persistent sdbusplus DBus bus connection. */
         sdbusplus::bus::bus& bus;
 
diff --git a/util.hpp b/util.hpp
index 429b213..669c68f 100644
--- a/util.hpp
+++ b/util.hpp
@@ -60,6 +60,9 @@
 }//namespace internal
 }//namespace mac_address
 
+constexpr auto networkdService = "systemd-networkd.service";
+constexpr auto timeSynchdService = "systemd-timesyncd.service";
+
 /* @brief converts the given subnet into prefix notation.
  * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
  * @param[in] mask - Subnet Mask.