network_manager: Allow direct access to interfaces

Change-Id: I3dc2bb944b8d3a7873e26ce2958a2cb1d9be95af
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/dhcp_configuration.cpp b/src/dhcp_configuration.cpp
index 6c96206..c14f547 100644
--- a/src/dhcp_configuration.cpp
+++ b/src/dhcp_configuration.cpp
@@ -26,7 +26,7 @@
 {
     config::Parser conf;
     {
-        auto interfaceStrList = getInterfaces();
+        auto interfaceStrList = getSystemInterfaces();
         if (!interfaceStrList.empty())
         {
             conf.setFile(config::pathForIntfConf(manager.getConfDir(),
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index bba2ca2..eecbe68 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -55,7 +55,7 @@
     auto isCreated = false;
     try
     {
-        auto interfaceStrList = getInterfaces();
+        auto interfaceStrList = getSystemInterfaces();
         for (const auto& interface : interfaceStrList)
         {
             // if the interface has '.' in the name, it means that this is a
@@ -106,7 +106,7 @@
     // clear all the interfaces first
     interfaces.clear();
 
-    auto interfaceStrList = getInterfaces();
+    auto interfaceStrList = getSystemInterfaces();
 
     for (auto& interface : interfaceStrList)
     {
@@ -181,13 +181,6 @@
 
 ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
 {
-    if (!hasInterface(interfaceName))
-    {
-        using ResourceErr =
-            phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
-        elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
-    }
-
     if (id == 0 || id >= 4095)
     {
         log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
@@ -196,7 +189,14 @@
             Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
     }
 
-    return interfaces[interfaceName]->createVLAN(id);
+    auto it = interfaces.find(interfaceName);
+    if (it == interfaces.end())
+    {
+        using ResourceErr =
+            phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
+        elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
+    }
+    return it->second->createVLAN(id);
 }
 
 void Manager::reset()
diff --git a/src/network_manager.hpp b/src/network_manager.hpp
index 89d909a..edd7125 100644
--- a/src/network_manager.hpp
+++ b/src/network_manager.hpp
@@ -129,23 +129,13 @@
      */
     void doReloadConfigs();
 
-    /** @brief Returns the number of interfaces under this manager.
+    /** @brief Get the interfaces owned by the manager
      *
-     * @return the number of interfaces managed by this manager.
+     * @return Interfaces reference.
      */
-    inline size_t getInterfaceCount()
+    inline const auto& getInterfaces() const
     {
-        return interfaces.size();
-    }
-
-    /** @brief Does the requested interface exist under this manager?
-     *
-     * @param[in] intf - the interface name to check.
-     * @return true if found, false otherwise.
-     */
-    inline bool hasInterface(std::string_view intf)
-    {
-        return interfaces.find(intf) != interfaces.end();
+        return interfaces;
     }
 
     /** @brief Get the routing table owned by the manager
diff --git a/src/types.hpp b/src/types.hpp
index 5465381..58ad04a 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -52,8 +52,6 @@
 // Byte representations for common address types in network byte order
 using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
 
-using InterfaceList = std::unordered_set<std::string>;
-
 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
 
 struct string_hash : public std::hash<std::string_view>
@@ -63,6 +61,8 @@
 template <typename V>
 using string_umap =
     std::unordered_map<std::string, V, string_hash, std::equal_to<>>;
+using string_uset =
+    std::unordered_set<std::string, string_hash, std::equal_to<>>;
 
 } // namespace network
 } // namespace phosphor
diff --git a/src/util.cpp b/src/util.cpp
index b1f4d39..9d58bcb 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -200,9 +200,9 @@
         [=]<int f>() noexcept { return isValidPrefix<f>(prefix); }, family);
 }
 
-InterfaceList getInterfaces()
+string_uset getSystemInterfaces()
 {
-    InterfaceList interfaces{};
+    string_uset interfaces;
     struct ifaddrs* ifaddr = nullptr;
 
     // attempt to fill struct with ifaddrs
diff --git a/src/util.hpp b/src/util.hpp
index 16dca64..ddc5454 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -148,7 +148,7 @@
 /** @brief Get all the interfaces from the system.
  *  @returns list of interface names.
  */
-InterfaceList getInterfaces();
+string_uset getSystemInterfaces();
 
 /** @brief Delete the given interface.
  *  @param[in] intf - interface name.
diff --git a/test/mock_network_manager.hpp b/test/mock_network_manager.hpp
index 559a667..af26771 100644
--- a/test/mock_network_manager.hpp
+++ b/test/mock_network_manager.hpp
@@ -26,7 +26,7 @@
     {
         // clear all the interfaces first
         interfaces.clear();
-        auto interfaceStrList = getInterfaces();
+        auto interfaceStrList = getSystemInterfaces();
         for (auto& interface : interfaceStrList)
         {
             fs::path objPath = objectPath;
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index 4e81d48..3d4cee8 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -56,8 +56,9 @@
     // Now create the interfaces which will call the mocked getifaddrs
     // which returns the above interface detail.
     createInterfaces();
-    EXPECT_EQ(1, manager.getInterfaceCount());
-    EXPECT_EQ(true, manager.hasInterface("igb1"));
+    EXPECT_EQ(1, manager.getInterfaces().size());
+    EXPECT_NE(manager.getInterfaces().end(),
+              manager.getInterfaces().find("igb1"));
 }
 
 // getifaddrs returns two interfaces.
@@ -72,9 +73,11 @@
     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
 
     createInterfaces();
-    EXPECT_EQ(2, manager.getInterfaceCount());
-    EXPECT_EQ(true, manager.hasInterface("igb0"));
-    EXPECT_EQ(true, manager.hasInterface("igb1"));
+    EXPECT_EQ(2, manager.getInterfaces().size());
+    EXPECT_NE(manager.getInterfaces().end(),
+              manager.getInterfaces().find("igb0"));
+    EXPECT_NE(manager.getInterfaces().end(),
+              manager.getInterfaces().find("igb1"));
 }
 } // namespace network
 } // namespace phosphor