Add ethernet objects under hypervisor network obj

In this commit, ethernet interface dbus objects are created
under the hypervisor network service.

busctl tree xyz.openbmc_project.Network.Hypervisor
└─/xyz
  └─/xyz/openbmc_project
    └─/xyz/openbmc_project/network
      └─/xyz/openbmc_project/network/hypervisor
        ├─/xyz/openbmc_project/network/hypervisor/config
        ├─/xyz/openbmc_project/network/hypervisor/eth0
        └─/xyz/openbmc_project/network/hypervisor/eth1

Signed-off-by: Asmitha Karunanithi <asmitk01@in.ibm.com>
Change-Id: I4222fa29a9df607c8291d7f81f7f3e837442bc26
diff --git a/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.cpp b/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.cpp
new file mode 100644
index 0000000..84c9598
--- /dev/null
+++ b/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.cpp
@@ -0,0 +1,18 @@
+#include "hyp_ethernet_interface.hpp"
+
+namespace phosphor
+{
+namespace network
+{
+
+using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+using Argument = xyz::openbmc_project::Common::InvalidArgument;
+
+biosTableType HypEthInterface::getBiosAttrsMap()
+{
+    return manager.getBIOSTableAttrs();
+}
+
+} // namespace network
+} // namespace phosphor
diff --git a/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.hpp b/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.hpp
new file mode 100644
index 0000000..d2e3b8b
--- /dev/null
+++ b/src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.hpp
@@ -0,0 +1,94 @@
+#pragma once
+
+#include "hyp_network_manager.hpp"
+#include "xyz/openbmc_project/Network/IP/Create/server.hpp"
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
+
+namespace phosphor
+{
+namespace network
+{
+
+class HypNetworkMgr; // forward declaration of hypervisor network manager.
+
+using namespace phosphor::logging;
+using HypIP = sdbusplus::xyz::openbmc_project::Network::server::IP;
+
+using CreateIface = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
+    sdbusplus::xyz::openbmc_project::Network::IP::server::Create>;
+
+using biosTableType = std::map<std::string, std::variant<int64_t, std::string>>;
+
+using HypEthernetIntf =
+    sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
+
+using ObjectPath = sdbusplus::message::object_path;
+
+static std::shared_ptr<sdbusplus::bus::match::match> matchBIOSAttrUpdate;
+
+/** @class HypEthernetInterface
+ *  @brief Hypervisor Ethernet Interface implementation.
+ */
+class HypEthInterface : public CreateIface
+{
+  public:
+    HypEthInterface() = delete;
+    HypEthInterface(const HypEthInterface&) = delete;
+    HypEthInterface& operator=(const HypEthInterface&) = delete;
+    HypEthInterface(HypEthInterface&&) = delete;
+    HypEthInterface& operator=(HypEthInterface&&) = delete;
+    virtual ~HypEthInterface() = default;
+
+    /** @brief Constructor to put object onto bus at a dbus path.
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] path - Path to attach at.
+     *  @param[in] parent - parent object.
+     */
+    HypEthInterface(sdbusplus::bus::bus& bus, const char* path,
+                    std::string_view intfName, HypNetworkMgr& parent) :
+        CreateIface(bus, path, CreateIface::action::defer_emit),
+        bus(bus), objectPath(path), manager(parent)
+    {
+        HypEthernetIntf::interfaceName(intfName.data(), true);
+        emit_object_added();
+    };
+
+    /** @brief Function to create ipAddress dbus object.
+     *  @param[in] addressType - Type of ip address.
+     *  @param[in] ipAddress- IP address.
+     *  @param[in] prefixLength - Length of prefix.
+     *  @param[in] gateway - Gateway ip address.
+     */
+
+    ObjectPath ip(HypIP::Protocol /*addressType*/, std::string /*ipAddress*/,
+                  uint8_t /*prefixLength*/, std::string /*gateway*/) override
+    {
+        return std::string();
+    };
+
+    /* @brief Function that returns parent's bios attrs map
+     */
+    biosTableType getBiosAttrsMap();
+
+    using HypEthernetIntf::interfaceName;
+
+  protected:
+    /** @brief sdbusplus DBus bus connection. */
+    sdbusplus::bus::bus& bus;
+
+    /** @brief object path */
+    std::string objectPath;
+
+    /** @brief Parent of this object */
+    HypNetworkMgr& manager;
+};
+
+} // namespace network
+} // namespace phosphor
diff --git a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.cpp b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.cpp
index ffacfe5..ef401a1 100644
--- a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.cpp
+++ b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.cpp
@@ -9,8 +9,6 @@
 
 using sdbusplus::exception::SdBusError;
 
-class HypNetworkMgr;
-
 namespace phosphor
 {
 namespace network
@@ -216,11 +214,6 @@
     }
 }
 
-biosTableType HypNetworkMgr::getBIOSTableAttrs()
-{
-    return biosTableAttrs;
-}
-
 void HypNetworkMgr::createIfObjects()
 {
     setBIOSTableAttrs();
@@ -235,14 +228,20 @@
     // created during init time to support the static
     // network configurations on the both.
     // create eth0 and eth1 objects
-    log<level::INFO>("Create eth0 and eth1 objects");
+    log<level::INFO>("Creating eth0 and eth1 objects");
+    interfaces.emplace("eth0",
+                       std::make_unique<HypEthInterface>(
+                           bus, (objectPath + "/eth0").c_str(), "eth0", *this));
+    interfaces.emplace("eth1",
+                       std::make_unique<HypEthInterface>(
+                           bus, (objectPath + "/eth1").c_str(), "eth1", *this));
 }
 
 void HypNetworkMgr::createSysConfObj()
 {
     systemConf.reset(nullptr);
-    this->systemConf = std::make_unique<phosphor::network::HypSysConfig>(
-        bus, objectPath + "/config", *this);
+    this->systemConf =
+        std::make_unique<HypSysConfig>(bus, objectPath + "/config", *this);
 }
 
 } // namespace network
diff --git a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.hpp b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.hpp
index 5eac26f..910fecd 100644
--- a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.hpp
+++ b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager.hpp
@@ -1,5 +1,8 @@
 #pragma once
+
+#include "hyp_ethernet_interface.hpp"
 #include "hyp_sys_config.hpp"
+#include "types.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
@@ -44,6 +47,7 @@
 };
 
 using SystemConfPtr = std::unique_ptr<HypSysConfig>;
+using ethIntfMapType = string_umap<std::unique_ptr<HypEthInterface>>;
 
 /** @class Manager
  *  @brief Implementation for the
@@ -70,7 +74,10 @@
      *
      * @return attributes list
      */
-    biosTableType getBIOSTableAttrs();
+    inline biosTableType getBIOSTableAttrs()
+    {
+        return biosTableAttrs;
+    }
 
     /** @brief Set specific attribute and its value to
      *         the biosTableAttrs data member
@@ -84,6 +91,15 @@
                           std::variant<std::string, int64_t> attrValue,
                           std::string attrType);
 
+    /** @brief Get the ethernet interfaces list data member
+     *
+     * @return ethernet interfaces list
+     */
+    inline const auto& getEthIntfList()
+    {
+        return interfaces;
+    }
+
     /** @brief Method to set all the interface 0 attributes
      *         to its default value in biosTableAttrs data member
      */
@@ -145,7 +161,7 @@
     /** @brief Persistent map of EthernetInterface dbus
      *         objects and their names
      */
-    std::map<std::string, std::shared_ptr<HypEthInterface>> interfaces;
+    ethIntfMapType interfaces;
 
     /** @brief map of bios table attrs and values */
     std::map<biosAttrName, biosAttrCurrValue> biosTableAttrs;
diff --git a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager_main.cpp b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager_main.cpp
index 55d80c2..ade3827 100644
--- a/src/ibm/hypervisor-network-mgr-src/hyp_network_manager_main.cpp
+++ b/src/ibm/hypervisor-network-mgr-src/hyp_network_manager_main.cpp
@@ -1,13 +1,25 @@
 #include "hyp_network_manager.hpp"
 
+#include <fmt/format.h>
+
+#include <phosphor-logging/log.hpp>
 #include <sdeventplus/event.hpp>
 
+using phosphor::logging::entry;
+using phosphor::logging::level;
+using phosphor::logging::log;
+
 constexpr char DEFAULT_HYP_NW_OBJPATH[] =
     "/xyz/openbmc_project/network/hypervisor";
 constexpr char HYP_DEFAULT_NETWORK_BUSNAME[] =
     "xyz.openbmc_project.Network.Hypervisor";
 
-int main(int /*argc*/, char** /*argv*/)
+namespace phosphor
+{
+namespace network
+{
+
+int main()
 {
     auto bus = sdbusplus::bus::new_default();
 
@@ -21,19 +33,34 @@
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
     // Create hypervisor network manager dbus object
-    phosphor::network::HypNetworkMgr manager(bus, DEFAULT_HYP_NW_OBJPATH);
+    auto manager = std::make_unique<HypNetworkMgr>(bus, DEFAULT_HYP_NW_OBJPATH);
 
     // Create the hypervisor eth interface objects
-    manager.createIfObjects();
+    manager->createIfObjects();
 
     // Create the hypervisor system config object
-    manager.createSysConfObj();
-    const phosphor::network::SystemConfPtr& systemConfigObj =
-        manager.getSystemConf();
+    manager->createSysConfObj();
+    const SystemConfPtr& systemConfigObj = manager->getSystemConf();
     systemConfigObj->setHostName();
 
     bus.request_name(HYP_DEFAULT_NETWORK_BUSNAME);
 
-    event.loop();
-    return 0;
+    return event.loop();
+}
+
+} // namespace network
+} // namespace phosphor
+
+int main(int /*argc*/, char** /*argv*/)
+{
+    try
+    {
+        return phosphor::network::main();
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "FAILED: {}", e.what());
+        fflush(stderr);
+        return 1;
+    }
 }
diff --git a/src/ibm/hypervisor-network-mgr-src/meson.build b/src/ibm/hypervisor-network-mgr-src/meson.build
index 08cb0ad..8f929ff 100644
--- a/src/ibm/hypervisor-network-mgr-src/meson.build
+++ b/src/ibm/hypervisor-network-mgr-src/meson.build
@@ -11,11 +11,21 @@
   install_dir: dependency('systemd').get_variable(
     pkgconfig: 'systemdsystemunitdir'))
 
+hyp_src_includes = include_directories('.')
+
+hyp_networkd_lib = static_library(
+  'hyp-networkd',
+  'hyp_network_manager.cpp',
+  'hyp_sys_config.cpp',
+  'hyp_ethernet_interface.cpp',
+  implicit_include_directories: false,
+  include_directories: [src_includes, hyp_src_includes],
+  dependencies: networkd_deps)
+
 executable(
   'hyp-network-manager',
   'hyp_network_manager_main.cpp',
-  'hyp_network_manager.cpp',
-  'hyp_sys_config.cpp',
+  link_with: hyp_networkd_lib,
   implicit_include_directories: false,
   dependencies: [
     networkd_dep,
@@ -23,4 +33,3 @@
   ],
   install: true,
   install_dir: get_option('bindir'))
-
diff --git a/test/ibm/hypervisor-network-mgr-test/meson.build b/test/ibm/hypervisor-network-mgr-test/meson.build
index d0ea5a1..65bd9f1 100644
--- a/test/ibm/hypervisor-network-mgr-test/meson.build
+++ b/test/ibm/hypervisor-network-mgr-test/meson.build
@@ -5,11 +5,6 @@
   'hyp_sys_config',
 ]
 
-hyp_test_src = declare_dependency(
-              sources: [
-                '../../../src/ibm/hypervisor-network-mgr-src/hyp_network_manager.cpp',
-                '../../../src/ibm/hypervisor-network-mgr-src/hyp_sys_config.cpp'])
-
 foreach t : hyp_tests
   test(
     t,
@@ -18,5 +13,6 @@
       'test_' + t + '.cpp',
       implicit_include_directories: false,
       include_directories: inc_dir,
-      dependencies: [test_dep, hyp_test_src]))
+      link_with: hyp_networkd_lib,
+      dependencies: test_dep))
 endforeach