ethernet_interface: Merge VLAN into EthernetInterface

This simplifies the ethernet interface as it codepended on the vlan
interface to make VLANs. It also removes the naming dependency and
tracks VLANs by adapter type and parent indexes.

Change-Id: I8db09cc2474472e6d4a06c6be5cf0440ee48d132
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/meson.build b/test/meson.build
index 651f542..3f84156 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -54,7 +54,6 @@
   'system_queries',
   'types',
   'util',
-  'vlan_interface',
 ]
 
 run_with_tmp = find_program('run_with_tmp', native: true)
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index 459bacf..7e3e211 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -1,3 +1,4 @@
+#include "config_parser.hpp"
 #include "mock_network_manager.hpp"
 #include "mock_syscall.hpp"
 
@@ -6,6 +7,7 @@
 #include <netinet/in.h>
 #include <stdlib.h>
 
+#include <filesystem>
 #include <sdbusplus/bus.hpp>
 #include <stdplus/gtest/tmp.hpp>
 
@@ -21,7 +23,7 @@
 
 class TestNetworkManager : public stdplus::gtest::TestWithTmp
 {
-  public:
+  protected:
     sdbusplus::bus_t bus;
     MockManager manager;
     TestNetworkManager() :
@@ -35,13 +37,18 @@
     {
         manager.createInterfaces();
     }
+
+    void deleteVLAN(std::string_view ifname)
+    {
+        manager.interfaces.find(ifname)->second->vlan->delete_();
+    }
 };
 
 // getifaddrs will not return any interface
 TEST_F(TestNetworkManager, NoInterface)
 {
     createInterfaces();
-    EXPECT_TRUE(manager.getInterfaces().empty());
+    EXPECT_TRUE(manager.interfaces.empty());
 }
 // getifaddrs returns single interface.
 TEST_F(TestNetworkManager, WithSingleInterface)
@@ -52,7 +59,7 @@
     // Now create the interfaces which will call the mocked getifaddrs
     // which returns the above interface detail.
     createInterfaces();
-    EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1")));
+    EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
 }
 
 // getifaddrs returns two interfaces.
@@ -62,8 +69,34 @@
     system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
 
     createInterfaces();
-    EXPECT_THAT(manager.getInterfaces(),
+    EXPECT_THAT(manager.interfaces,
                 UnorderedElementsAre(Key("igb0"), Key("igb1")));
 }
+
+TEST_F(TestNetworkManager, WithVLAN)
+{
+    EXPECT_THROW(manager.vlan("", 8000), std::exception);
+    EXPECT_THROW(manager.vlan("", 0), std::exception);
+    EXPECT_THROW(manager.vlan("eth0", 2), std::exception);
+
+    system::mock_addIF({.idx = 1, .flags = 0, .name = "eth0"});
+    manager.createInterfaces();
+    EXPECT_NO_THROW(manager.vlan("eth0", 2));
+    EXPECT_NO_THROW(manager.vlan("eth0", 4094));
+    EXPECT_THAT(
+        manager.interfaces,
+        UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094")));
+    auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2");
+    auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094");
+    EXPECT_TRUE(std::filesystem::is_regular_file(netdev1));
+    EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
+
+    deleteVLAN("eth0.2");
+    EXPECT_THAT(manager.interfaces,
+                UnorderedElementsAre(Key("eth0"), Key("eth0.4094")));
+    EXPECT_FALSE(std::filesystem::is_regular_file(netdev1));
+    EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
+}
+
 } // namespace network
 } // namespace phosphor
diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp
deleted file mode 100644
index d5b6989..0000000
--- a/test/test_vlan_interface.cpp
+++ /dev/null
@@ -1,159 +0,0 @@
-#include "config_parser.hpp"
-#include "ipaddress.hpp"
-#include "mock_network_manager.hpp"
-#include "mock_syscall.hpp"
-#include "system_queries.hpp"
-#include "vlan_interface.hpp"
-
-#include <arpa/inet.h>
-#include <net/if.h>
-#include <netinet/in.h>
-
-#include <filesystem>
-#include <sdbusplus/bus.hpp>
-#include <stdplus/gtest/tmp.hpp>
-
-#include <gtest/gtest.h>
-
-namespace phosphor
-{
-namespace network
-{
-
-namespace fs = std::filesystem;
-using std::literals::string_view_literals::operator""sv;
-
-class TestVlanInterface : public stdplus::gtest::TestWithTmp
-{
-  public:
-    sdbusplus::bus_t bus;
-    std::string confDir;
-    MockManager manager;
-    EthernetInterface interface;
-    TestVlanInterface() :
-        bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
-        manager(bus, "/xyz/openbmc_test/network", confDir),
-        interface(makeInterface(bus, manager))
-
-    {
-    }
-
-    static EthernetInterface makeInterface(sdbusplus::bus_t& bus,
-                                           MockManager& manager)
-    {
-        system::mock_clear();
-        system::InterfaceInfo info{.idx = 1, .flags = 0, .name = "test0"};
-        system::mock_addIF(info);
-        return {bus,
-                manager,
-                info,
-                "/xyz/openbmc_test/network"sv,
-                config::Parser(),
-                /*emitSignal=*/false,
-                /*nicEnabled=*/true};
-    }
-
-    void createVlan(uint16_t id)
-    {
-        system::mock_addIF(system::InterfaceInfo{
-            .idx = id + 10u, .flags = 0, .name = fmt::format("test0.{}", id)});
-        interface.createVLAN(id);
-    }
-
-    void deleteVlan(const std::string& interfaceName)
-    {
-        interface.deleteVLANObject(interfaceName);
-    }
-
-    int countIPObjects()
-    {
-        return interface.getAddresses().size();
-    }
-
-    bool isIPObjectExist(const std::string& ipaddress)
-    {
-        auto address = interface.getAddresses().find(ipaddress);
-        if (address == interface.getAddresses().end())
-        {
-            return false;
-        }
-        return true;
-    }
-
-    bool deleteIPObject(const std::string& ipaddress)
-    {
-        auto address = interface.getAddresses().find(ipaddress);
-        if (address == interface.getAddresses().end())
-        {
-            return false;
-        }
-        address->second->delete_();
-        return true;
-    }
-
-    void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
-                        uint8_t subnetMask, const std::string& gateway)
-    {
-        interface.ip(addressType, ipaddress, subnetMask, gateway);
-    }
-};
-
-TEST_F(TestVlanInterface, createVLAN)
-{
-    createVlan(50);
-    fs::path filePath = confDir;
-    filePath /= "test0.50.netdev";
-
-    config::Parser parser(filePath);
-    EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
-                              {"NetDev",
-                               {
-                                   {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
-                               }},
-                              {"VLAN", {{{"Id", {"50"}}}}},
-                          }));
-}
-
-TEST_F(TestVlanInterface, deleteVLAN)
-{
-    createVlan(50);
-    deleteVlan("test0.50");
-
-    fs::path filePath = confDir;
-    filePath /= "test0.50.netdev";
-    EXPECT_FALSE(fs::is_regular_file(filePath));
-}
-
-TEST_F(TestVlanInterface, createMultipleVLAN)
-{
-    createVlan(50);
-    createVlan(60);
-
-    fs::path filePath = confDir;
-    filePath /= "test0.50.netdev";
-    config::Parser parser(filePath);
-    EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
-                              {"NetDev",
-                               {
-                                   {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
-                               }},
-                              {"VLAN", {{{"Id", {"50"}}}}},
-                          }));
-
-    filePath = confDir;
-    filePath /= "test0.60.netdev";
-    parser.setFile(filePath);
-    EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
-                              {"NetDev",
-                               {
-                                   {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
-                               }},
-                              {"VLAN", {{{"Id", {"60"}}}}},
-                          }));
-
-    deleteVlan("test0.50");
-    deleteVlan("test0.60");
-}
-
-} // namespace network
-} // namespace phosphor