network_config: Write configs from parser

Tested: Booted up a clean RWFS and saw that the configs were written
correctly from networkd.

Change-Id: I0915571f87ec803b127cd8c51af37832a2720fdd
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/network_config.cpp b/src/network_config.cpp
index 4f59643..249be58 100644
--- a/src/network_config.cpp
+++ b/src/network_config.cpp
@@ -2,8 +2,7 @@
 
 #include "network_config.hpp"
 
-#include <fstream>
-#include <string>
+#include "config_parser.hpp"
 
 namespace phosphor
 {
@@ -12,38 +11,39 @@
 
 namespace bmc
 {
-void writeDHCPDefault(const std::string& filename, const std::string& interface)
+void writeDHCPDefault(const std::filesystem::path& filename,
+                      std::string_view interface)
 {
-    std::ofstream filestream;
-
-    filestream.open(filename);
-    // Add the following line to your phosphor-network bbappend file
-    // to control IPV6_ACCEPT_RA
-    //   EXTRA_OECONF_append = " --enable-ipv6-accept-ra=yes"
-    // If this switch is not present or set to 'no'
-    // ENABLE_IPV6_ACCEPT_RA will be undefined.
-    // The new value is only assigned on first boot, when the default
-    // file is not present, or after the default file has been
-    // manually removed.
-    filestream << "[Match]\nName=" << interface <<
-                "\n[Network]\nDHCP=true\n"
+    config::Parser config;
+    config.map["Match"].emplace_back()["Name"].emplace_back(interface);
+    {
+        auto& network = config.map["Network"].emplace_back();
+        network["DHCP"].emplace_back("true");
+        auto& lla = network["LinkLocalAddressing"];
 #ifdef LINK_LOCAL_AUTOCONFIGURATION
-                "LinkLocalAddressing=yes\n"
+        lla.emplace_back("true");
 #else
-                "LinkLocalAddressing=no\n"
+        lla.emplace_back("false");
 #endif
+        auto& ra = network["IPv6AcceptRA"];
 #ifdef ENABLE_IPV6_ACCEPT_RA
-                "IPv6AcceptRA=true\n"
+        ra.emplace_back("true");
 #else
-                "IPv6AcceptRA=false\n"
+        ra.emplace_back("false");
 #endif
-                "[DHCP]\nClientIdentifier=mac\n"
-                "UseDNS=true\nUseDomains=true\n"
-                "UseNTP=true\nUseHostname=true\n"
-                "SendHostname=true\n"
-                "[IPv6AcceptRA]\nDHCPv6Client=true\n";
-
-    filestream.close();
+    }
+    {
+        auto& dhcp = config.map["DHCP"].emplace_back();
+        dhcp["ClientIdentifier"].emplace_back("mac");
+        dhcp["UseDNS"].emplace_back("true");
+        dhcp["UseDomains"].emplace_back("true");
+        dhcp["UseNTP"].emplace_back("true");
+        dhcp["UseHostname"].emplace_back("true");
+        dhcp["SendHostname"].emplace_back("true");
+    }
+    config.map["IPv6AcceptRA"].emplace_back()["DHCPv6Client"].emplace_back(
+        "true");
+    config.writeFile(filename);
 }
 } // namespace bmc
 
diff --git a/src/network_config.hpp b/src/network_config.hpp
index 7260ca2..a5c7300 100644
--- a/src/network_config.hpp
+++ b/src/network_config.hpp
@@ -1,4 +1,5 @@
-#include <string>
+#include <filesystem>
+#include <string_view>
 
 namespace phosphor
 {
@@ -7,8 +8,8 @@
 
 namespace bmc
 {
-void writeDHCPDefault(const std::string& filename,
-                      const std::string& interface);
+void writeDHCPDefault(const std::filesystem::path& filename,
+                      std::string_view interface);
 }
 
 } // namespace network
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 86b8550..7d288be 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -70,9 +70,9 @@
 
             // create the interface specific network file
             // if not existing.
-            if (!fs::is_regular_file(filePath.string()))
+            if (!fs::is_regular_file(filePath))
             {
-                bmc::writeDHCPDefault(filePath.string(), interface);
+                bmc::writeDHCPDefault(filePath, interface);
                 log<level::INFO>("Created the default network file.",
                                  entry("INTERFACE=%s", interface.c_str()));
                 isCreated = true;