Restructure the code so that it is unit testable

Change-Id: I2af7a26d054522beae82f58769f8c2420a22eca2
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 06dd416..fc21424 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -30,18 +30,25 @@
 
 EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
                                      const std::string& objPath,
-                                     bool dhcpEnabled,
-                                     const AddrList& addrs) :
+                                     bool dhcpEnabled) :
                                      Ifaces(bus, objPath.c_str(), true),
                                      bus(bus)
+
 {
     auto intfName = objPath.substr(objPath.rfind("/") + 1);
     interfaceName(intfName);
     dHCPEnabled(dhcpEnabled);
     mACAddress(getMACAddress());
+    // Emit deferred signal.
+    this->emit_object_added();
+}
+
+void EthernetInterface::setAddressList(const AddrList& addrs)
+{
     std::string gateway;
 
     IP::Protocol addressType = IP::Protocol::IPv4;
+    IP::AddressOrigin origin = IP::AddressOrigin::Static;
 
     for (auto addr : addrs)
     {
@@ -63,11 +70,10 @@
                         *this,
                         addressType,
                         addr.ipaddress,
+                        origin,
                         addr.prefix,
                         gateway)));
     }
-    // Emit deferred signal.
-    this->emit_object_added();
 }
 
 void EthernetInterface::iP(IP::Protocol protType,
@@ -75,6 +81,8 @@
                            uint8_t prefixLength,
                            std::string gateway)
 {
+    IP::AddressOrigin origin = IP::AddressOrigin::Static;
+
     std::string objectPath = generateObjectPath(protType,
                                                 ipaddress,
                                                 prefixLength,
@@ -88,6 +96,7 @@
                                 *this,
                                 protType,
                                 ipaddress,
+                                origin,
                                 prefixLength,
                                 gateway)));
 }
@@ -216,7 +225,13 @@
 
 void EthernetInterface::deleteObject(const std::string& ipaddress)
 {
-    this->addrs.erase(addrs.find(ipaddress));
+    auto it = addrs.find(ipaddress);
+    if( it == addrs.end())
+    {
+         log<level::ERR>("DeleteObject:Unable to find the object.");
+         return;
+    }
+    this->addrs.erase(it);
 }
 
 std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
@@ -225,14 +240,14 @@
                                                   const std::string& gateway) const
 {
     std::string type = convertForMessage(addressType);
-    type = type.substr(type.rfind('.')+1);
+    type = type.substr(type.rfind('.') + 1);
     std::transform(type.begin(), type.end(), type.begin(), ::tolower);
 
     std::experimental::filesystem::path objectPath;
     objectPath /= std::string(OBJ_NETWORK);
     objectPath /= interfaceName();
     objectPath /= type;
-    objectPath /= generateId(ipaddress,prefixLength,gateway);
+    objectPath /= generateId(ipaddress, prefixLength, gateway);
     return objectPath.string();
 
 }
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index 6ef1ec9..c755f06 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -24,12 +24,11 @@
 
 using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
 
-
 using LinkSpeed = uint16_t;
 using DuplexMode = uint8_t;
 using Autoneg = uint8_t;
 using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
-
+using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
 
 /** @class EthernetInterface
  *  @brief OpenBMC Ethernet Interface implementation.
@@ -49,17 +48,15 @@
         /** @brief Constructor to put object onto bus at a dbus path.
          *  @param[in] bus - Bus to attach to.
          *  @param[in] objPath - Path to attach at.
-         *  @param[in] intfName - name of the ethernet interface.
          *  @param[in] dhcpEnabled - is dhcp enabled(true/false).
          */
         EthernetInterface(sdbusplus::bus::bus& bus,
                           const std::string& objPath,
-                          bool dhcpEnabled,
-                          const AddrList& addrs);
+                          bool dhcpEnabled);
 
         /** @brief Function to create ipaddress dbus object.
          *  @param[in] addressType - Type of ip address.
-         *  @param[in] ipaddress- IP adress.
+         *  @param[in] ipaddress- IP address.
          *  @param[in] prefixLength - Length of prefix.
          *  @param[in] gateway - Gateway ip address.
          */
@@ -69,11 +66,21 @@
                 uint8_t prefixLength,
                 std::string gateway) override;
 
-        /** @brief delete the dbus object of the given ipaddress.
+        /* @brief delete the dbus object of the given ipaddress.
+         * @param[in] ipaddress - IP address.
          */
-
         void deleteObject(const std::string& ipaddress);
 
+        /* @brief creates the dbus object given in the address list.
+         * @param[in] addrs - address list for which dbus objects needs
+         *                    to create.
+         */
+        void setAddressList(const AddrList& addrs);
+
+        /* @brief Gets all the ip addresses.
+         * @returns the list of ipaddress.
+         */
+        const AddressMap& getAddresses() const { return addrs; }
 
     private:
 
@@ -119,8 +126,7 @@
         sdbusplus::bus::bus& bus;
 
         /** @brief Persistent map of IPAddress dbus objects and their names */
-        std::map<std::string, std::unique_ptr<IPAddress>> addrs;
-
+        AddressMap addrs;
 
 };
 
diff --git a/ipaddress.cpp b/ipaddress.cpp
index 3241102..b2407ef 100644
--- a/ipaddress.cpp
+++ b/ipaddress.cpp
@@ -15,6 +15,7 @@
           EthernetInterface& parent,
           IP::Protocol type,
           const std::string& ipaddress,
+          IP::AddressOrigin origin,
           uint8_t prefixLength,
           const std::string& gateway):
           IPIfaces(bus, objPath, true),
@@ -24,6 +25,8 @@
    this->prefixLength(prefixLength);
    this->gateway(gateway);
    this->type(type);
+   this->origin(origin);
+
    // Emit deferred signal.
    emit_object_added();
 }
diff --git a/ipaddress.hpp b/ipaddress.hpp
index 628f9cf..51c17ce 100644
--- a/ipaddress.hpp
+++ b/ipaddress.hpp
@@ -44,6 +44,7 @@
          *  @param[in] parent - Parent object.
          *  @param[in] type - ipaddress type(v4/v6).
          *  @param[in] ipAddress - ipadress.
+         *  @param[in] origin - origin of ipaddress(dhcp/static/SLAAC/LinkLocal).
          *  @param[in] prefixLength - Length of prefix.
          *  @param[in] gateway - gateway address.
          */
@@ -52,6 +53,7 @@
                   EthernetInterface& parent,
                   IP::Protocol type,
                   const std::string& ipAddress,
+                  IP::AddressOrigin origin,
                   uint8_t prefixLength,
                   const std::string& gateway);
 
diff --git a/network_manager.cpp b/network_manager.cpp
index a4a9b54..40fce1d 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -26,25 +26,31 @@
 namespace fs = std::experimental::filesystem;
 
 Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath):
-    details::VLANCreateIface(bus, objPath, true)
+    details::VLANCreateIface(bus, objPath, true),
+    bus(bus),
+    objectPath(objPath)
 {
+}
+
+void Manager::createInterfaces()
+{
+
     auto interfaceInfoList = getInterfaceAddrs();
 
     for (const auto& intfInfo : interfaceInfoList)
-
     {
-
-        fs::path objectPath = std::string(OBJ_NETWORK);
-        objectPath /= intfInfo.first;
+        fs::path objPath = objectPath;
+        objPath /= intfInfo.first;
 
         this->interfaces.emplace(std::make_pair(
                                      intfInfo.first,
                                      std::make_unique<
                                      phosphor::network::EthernetInterface>
                                      (bus,
-                                      objectPath.string(),
-                                      false,
-                                      intfInfo.second)));
+                                      objPath.string(),
+                                      false)));
+
+        interfaces[intfInfo.first]->setAddressList(intfInfo.second);
     }
 }
 
diff --git a/network_manager.hpp b/network_manager.hpp
index 553951c..e8dfa8e 100644
--- a/network_manager.hpp
+++ b/network_manager.hpp
@@ -74,18 +74,31 @@
 
         void vLAN(IntfName interfaceName, uint16_t id) override;
 
+        /** @brief Fetch the interface and the ipaddress details
+         *         from the system and create the ethernet interraces
+         *         dbus object.
+         */
+        void createInterfaces();
+
+
     private:
         /** @brief Get all the interfaces from the system.
          *  @returns list of interface names.
          */
         IntfAddrMap getInterfaceAddrs() const;
 
+        /** @brief Persistent sdbusplus DBus bus connection. */
+        sdbusplus::bus::bus& bus;
+
         /** @brief Persistent map of EthernetInterface dbus objects and their names */
         std::map<IntfName, std::unique_ptr<EthernetInterface>> interfaces;
 
         /** @brief BMC network reset - resets network configuration for BMC. */
         void reset() override;
 
+        /** @brief Path of Object. */
+        std::string objectPath;
+
 };
 
 } // namespace network
diff --git a/network_manager_main.cpp b/network_manager_main.cpp
index 5e79566..a53d32c 100644
--- a/network_manager_main.cpp
+++ b/network_manager_main.cpp
@@ -12,6 +12,8 @@
 
     phosphor::network::Manager manager(bus, OBJ_NETWORK);
 
+    manager.createInterfaces();
+
     bus.request_name(BUSNAME_NETWORK);
 
     while(true)