Convert all systemd-networkd service restarts to reloads

This removes all of the current invocations of managed service
restarting, and instead opts for targeted reloading of
configuration files.

It also happens to fix some bugs around NIC enabled setting, as the
systemd-networkd service now has up to date runtime configurations at
the time that objects are refreshed.

Change-Id: I64cd4f3fcd66eb2ad23b973eb86a71606e528e37
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/dhcp_configuration.cpp b/src/dhcp_configuration.cpp
index 690ae47..6fb24d0 100644
--- a/src/dhcp_configuration.cpp
+++ b/src/dhcp_configuration.cpp
@@ -26,7 +26,9 @@
     }
 
     auto name = ConfigIntf::sendHostNameEnabled(value);
+
     manager.writeToConfigurationFile();
+    manager.reloadConfigs();
 
     return name;
 }
@@ -40,7 +42,7 @@
 
     auto name = ConfigIntf::hostNameEnabled(value);
     manager.writeToConfigurationFile();
-    manager.restartSystemdUnit(phosphor::network::networkdService);
+    manager.reloadConfigs();
 
     return name;
 }
@@ -54,8 +56,7 @@
 
     auto ntp = ConfigIntf::ntpEnabled(value);
     manager.writeToConfigurationFile();
-    manager.restartSystemdUnit(phosphor::network::networkdService);
-    manager.restartSystemdUnit(phosphor::network::timeSynchdService);
+    manager.reloadConfigs();
 
     return ntp;
 }
@@ -69,7 +70,7 @@
 
     auto dns = ConfigIntf::dnsEnabled(value);
     manager.writeToConfigurationFile();
-    manager.restartSystemdUnit(phosphor::network::networkdService);
+    manager.reloadConfigs();
 
     return dns;
 }
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index bdae08f..a98b176 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -322,7 +322,9 @@
                                      bus, objectPath.c_str(), *this, protType,
                                      ipaddress, origin, prefixLength, gateway));
 
-    manager.writeToConfigurationFile();
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return objectPath;
 }
 
@@ -350,7 +352,10 @@
                             std::make_shared<phosphor::network::Neighbor>(
                                 bus, objectPath.c_str(), *this, ipAddress,
                                 macAddress, Neighbor::State::Permanent));
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return objectPath;
 }
 
@@ -464,7 +469,9 @@
         return;
     }
     this->addrs.erase(it);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
 }
 
 void EthernetInterface::deleteStaticNeighborObject(const std::string& ipAddress)
@@ -477,7 +484,9 @@
         return;
     }
     staticNeighbors.erase(it);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
 }
 
 void EthernetInterface::deleteVLANFromSystem(const std::string& interface)
@@ -529,7 +538,8 @@
     // delete the interface
     vlanInterfaces.erase(it);
 
-    manager.writeToConfigurationFile();
+    writeConfigurationFile();
+    manager.reloadConfigs();
 }
 
 std::string EthernetInterface::generateObjectPath(
@@ -566,7 +576,10 @@
         return value;
     }
     EthernetInterfaceIntf::ipv6AcceptRA(value);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return value;
 }
 
@@ -576,9 +589,11 @@
     {
         return value;
     }
-
     EthernetInterfaceIntf::dhcpEnabled(value);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return value;
 }
 
@@ -779,7 +794,9 @@
         return EthernetInterfaceIntf::nicEnabled();
     }
     EthernetInterfaceIntf::nicEnabled(value);
+
     writeConfigurationFile();
+    manager.reloadConfigs();
 
     return value;
 }
@@ -807,10 +824,9 @@
     try
     {
         EthernetInterfaceIntf::staticNameServers(value);
+
         writeConfigurationFile();
-        // resolved reads the DNS server configuration from the
-        // network file.
-        manager.restartSystemdUnit(networkdService);
+        manager.reloadConfigs();
     }
     catch (const InternalFailure& e)
     {
@@ -963,8 +979,9 @@
     vlanIntf->writeDeviceFile();
 
     this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf));
-    // write the new vlan device entry to the configuration(network) file.
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
 
     return path;
 }
@@ -1016,9 +1033,8 @@
     auto ntpServers = EthernetInterfaceIntf::ntpServers(servers);
 
     writeConfigurationFile();
-    // timesynchd reads the NTP server configuration from the
-    // network file.
-    manager.restartSystemdUnit(networkdService);
+    manager.reloadConfigs();
+
     return ntpServers;
 }
 // Need to merge the below function with the code which writes the
@@ -1225,7 +1241,8 @@
         //      through https://github.com/systemd/systemd/issues/6696
         execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(),
                 "down");
-        manager.writeToConfigurationFile();
+        writeConfigurationFile();
+        manager.reloadConfigs();
     }
 
 #ifdef HAVE_UBOOT_ENV
@@ -1253,7 +1270,9 @@
 
     // clear all the ip on the interface
     addrs.clear();
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
 }
 
 std::string EthernetInterface::defaultGateway(std::string gateway)
@@ -1272,7 +1291,10 @@
                               Argument::ARGUMENT_VALUE(gateway.c_str()));
     }
     gw = EthernetInterfaceIntf::defaultGateway(gateway);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return gw;
 }
 
@@ -1292,7 +1314,10 @@
                               Argument::ARGUMENT_VALUE(gateway.c_str()));
     }
     gw = EthernetInterfaceIntf::defaultGateway6(gateway);
-    manager.writeToConfigurationFile();
+
+    writeConfigurationFile();
+    manager.reloadConfigs();
+
     return gw;
 }
 } // namespace network
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index ab8a636..e2d48f1 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -104,14 +104,6 @@
         log<level::ERR>("Unable to create the default network file");
     }
 
-    if (isCreated)
-    {
-        // if files created restart the network.
-        // don't need to call the create child objects as eventhandler
-        // will create it.
-        restartSystemdUnit(phosphor::network::networkdService);
-    }
-
     return isCreated;
 }
 
@@ -220,7 +212,6 @@
     {
         intf.second->writeConfigurationFile();
     }
-    restartTimers();
 }
 
 #ifdef SYNC_MAC_FROM_INVENTORY
diff --git a/src/network_manager_main.cpp b/src/network_manager_main.cpp
index 983616f..fb08fdf 100644
--- a/src/network_manager_main.cpp
+++ b/src/network_manager_main.cpp
@@ -309,13 +309,9 @@
     // is not there for any interface.
     // Parameter false means don't create the network
     // files forcefully.
-    if (!phosphor::network::manager->createDefaultNetworkFiles(false))
+    if (phosphor::network::manager->createDefaultNetworkFiles(false))
     {
-        // this will add the additional fixes which is needed
-        // in the existing network file.
-        phosphor::network::manager->writeToConfigurationFile();
-        // whenever the configuration file gets written it restart
-        // the network which creates the network objects
+        phosphor::network::manager->reloadConfigs();
     }
 
     // RtnetLink socket
@@ -331,5 +327,9 @@
     in >> configJson;
     phosphor::network::watchEthernetInterface(bus, configJson);
 #endif
+
+    // Trigger the initial object scan
+    phosphor::network::refreshObjects();
+
     sd_event_loop(eventPtr.get());
 }
diff --git a/src/system_configuration.cpp b/src/system_configuration.cpp
index 6c30da2..c37cfc6 100644
--- a/src/system_configuration.cpp
+++ b/src/system_configuration.cpp
@@ -122,7 +122,10 @@
             InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
     }
     gw = SystemConfigIntf::defaultGateway(gateway);
+
     manager.writeToConfigurationFile();
+    manager.reloadConfigs();
+
     return gw;
 }
 
@@ -143,7 +146,10 @@
             InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
     }
     gw = SystemConfigIntf::defaultGateway6(gateway);
+
     manager.writeToConfigurationFile();
+    manager.reloadConfigs();
+
     return gw;
 }
 
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index a84e0eb..a7e7302 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -172,7 +172,7 @@
 TEST_F(TestEthernetInterface, addStaticNameServers)
 {
     ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"};
-    EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
+    EXPECT_CALL(manager, reloadConfigs());
     interface.staticNameServers(servers);
     fs::path filePath = confDir;
     filePath /= "00-bmc-test0.network";
@@ -201,7 +201,7 @@
 TEST_F(TestEthernetInterface, addNTPServers)
 {
     ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
-    EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1);
+    EXPECT_CALL(manager, reloadConfigs());
     interface.ntpServers(servers);
     fs::path filePath = confDir;
     filePath /= "00-bmc-test0.network";