routing_table: Deduplicate rebuilds

We don't need to rebuild the routing table for every single interface
and system configuration object. It now only rebuilds the table once
every refresh of the object tree.

Change-Id: I6cc1cfa1ee6ad17307e3e9c7a2eaa352675ba7e8
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index a98b176..a42488d 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -5,7 +5,6 @@
 #include "config_parser.hpp"
 #include "neighbor.hpp"
 #include "network_manager.hpp"
-#include "routing_table.hpp"
 #include "vlan_interface.hpp"
 
 #include <arpa/inet.h>
@@ -91,13 +90,12 @@
     EthernetInterfaceIntf::dhcpEnabled(dhcpEnabled);
     EthernetInterfaceIntf::ipv6AcceptRA(getIPv6AcceptRAFromConf());
     EthernetInterfaceIntf::nicEnabled(enabled ? *enabled : queryNicEnabled());
-    route::Table routingTable;
-    auto gatewayList = routingTable.getDefaultGateway();
-    auto gateway6List = routingTable.getDefaultGateway6();
+    const auto& gatewayList = manager.getRouteTable().getDefaultGateway();
+    const auto& gateway6List = manager.getRouteTable().getDefaultGateway6();
     std::string defaultGateway;
     std::string defaultGateway6;
 
-    for (auto& gateway : gatewayList)
+    for (const auto& gateway : gatewayList)
     {
         if (gateway.first == intfName)
         {
@@ -106,7 +104,7 @@
         }
     }
 
-    for (auto& gateway6 : gateway6List)
+    for (const auto& gateway6 : gateway6List)
     {
         if (gateway6.first == intfName)
         {
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index e2d48f1..48e3848 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -167,6 +167,8 @@
 
 void Manager::createChildObjects()
 {
+    routeTable.refresh();
+
     // creates the ethernet interface dbus object.
     createInterfaces();
 
diff --git a/src/network_manager.hpp b/src/network_manager.hpp
index ceb62c4..a93940f 100644
--- a/src/network_manager.hpp
+++ b/src/network_manager.hpp
@@ -2,6 +2,7 @@
 
 #include "dhcp_configuration.hpp"
 #include "ethernet_interface.hpp"
+#include "routing_table.hpp"
 #include "system_configuration.hpp"
 #include "vlan_interface.hpp"
 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
@@ -155,6 +156,15 @@
         return (interfaces.find(intf) != interfaces.end());
     }
 
+    /** @brief Get the routing table owned by the manager
+     *
+     * @return Routing table reference.
+     */
+    inline const auto& getRouteTable() const
+    {
+        return routeTable;
+    }
+
   protected:
     /** @brief Persistent sdbusplus DBus bus connection. */
     sdbusplus::bus::bus& bus;
@@ -177,6 +187,9 @@
 
     /** @brief Network Configuration directory. */
     fs::path confDir;
+
+    /** @brief The routing table */
+    route::Table routeTable;
 };
 
 } // namespace network
diff --git a/src/routing_table.cpp b/src/routing_table.cpp
index 356338e..70131e8 100644
--- a/src/routing_table.cpp
+++ b/src/routing_table.cpp
@@ -23,7 +23,7 @@
 using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 
-Table::Table()
+void Table::refresh()
 {
     try
     {
diff --git a/src/routing_table.hpp b/src/routing_table.hpp
index 9b3b77e..efe054d 100644
--- a/src/routing_table.hpp
+++ b/src/routing_table.hpp
@@ -41,29 +41,25 @@
 class Table
 {
   public:
-    Table();
-    ~Table() = default;
-    Table(const Table&) = default;
-    Table& operator=(const Table&) = default;
-    Table(Table&&) = default;
-    Table& operator=(Table&&) = default;
+    /** @brief Rebuilds the routing table from the kernel */
+    void refresh();
 
     /**
      * @brief gets the default v4 gateway.
      *
      * @returns the default v4 gateway list.
      */
-    std::map<std::string, std::string> getDefaultGateway() const
+    inline const auto& getDefaultGateway() const
     {
         return defaultGateway;
-    };
+    }
 
     /**
      * @brief gets the default v6 gateway.
      *
      * @returns the default v6 gateway list.
      */
-    std::map<std::string, std::string> getDefaultGateway6() const
+    inline const auto& getDefaultGateway6() const
     {
         return defaultGateway6;
     };
diff --git a/src/system_configuration.cpp b/src/system_configuration.cpp
index c37cfc6..d1c1ed0 100644
--- a/src/system_configuration.cpp
+++ b/src/system_configuration.cpp
@@ -3,7 +3,6 @@
 #include "system_configuration.hpp"
 
 #include "network_manager.hpp"
-#include "routing_table.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
@@ -36,11 +35,10 @@
     bus(bus), manager(parent)
 {
     auto name = getHostNameFromSystem();
-    route::Table routingTable;
 
     SystemConfigIntf::hostName(name);
-    auto gatewayList = routingTable.getDefaultGateway();
-    auto gateway6List = routingTable.getDefaultGateway6();
+    const auto& gatewayList = manager.getRouteTable().getDefaultGateway();
+    const auto& gateway6List = manager.getRouteTable().getDefaultGateway6();
     // Assign first entry of gateway list
     std::string gateway;
     std::string gateway6;