Implement network factory reset in network_manager

This commit implements a factory reset interface for the BMC
network. This factory reset is accomplished by removing any
/etc/systemd/network/*.network files, then writing new ones
configured for DHCP for each known interface.

Resolves openbmc/openbmc#1575

Change-Id: Ic006cd43fb336029479cffa783b56ab91e0339bd
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/network_manager.cpp b/network_manager.cpp
index e0ebdfc..95e1476 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -1,5 +1,6 @@
 #include "config.h"
 #include "network_manager.hpp"
+#include "network_config.hpp"
 
 #include <phosphor-logging/log.hpp>
 
@@ -12,6 +13,7 @@
 #include <dirent.h>
 #include <net/if.h>
 
+#include <string>
 
 namespace phosphor
 {
@@ -48,6 +50,57 @@
 {
 }
 
+void Manager::reset()
+{
+    const std::string networkConfig = "/etc/systemd/network/";
+    bool filesExist, interfacesMapped = false;
+
+    if(fs::is_directory(networkConfig))
+    {
+        for(auto& file : fs::directory_iterator(networkConfig))
+        {
+            std::string filename = file.path().filename().c_str();
+
+            if(filename.substr(filename.find_last_of(".") + 1) == "network")
+            {
+                fs::remove(file.path());
+                filesExist = true;
+            }
+        }
+
+        if(!filesExist)
+        {
+            log<level::INFO>("No existing network configuration was found.");
+        }
+
+        for (auto& intf : interfaces)
+        {
+            std::string filename = networkConfig + "00-bmc-" + intf.first +
+                    ".network";
+
+            bmc::writeDHCPDefault(filename, intf.first);
+            interfacesMapped = true;
+        }
+
+        if(interfacesMapped)
+        {
+            log<level::INFO>("Network configuration reset to DHCP.");
+        }
+        else
+        {
+            log<level::ERR>("No network interfaces are mapped.");
+            // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
+        }
+    }
+    else
+    {
+        log<level::ERR>("Network configuration directory not found!");
+        // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
+    }
+
+    return;
+}
+
 IntfAddrMap Manager::getInterfaceAddrs() const
 {
     IntfAddrMap intfMap;