Generate the ip address object path with the use of hash

ipaddress object path have the id for unique identification
of ipaddres and the id would be hash of ipaddress,prefix
and gateway.

This was needed to make sure that there is no duplication
of address object path.

Change-Id: If6830d6e186e3271467ce0084c1dbf3c4995f1dd
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 20b3e31..75cb761 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -15,6 +15,7 @@
 
 #include <string>
 #include <algorithm>
+#include <sstream>
 #include <experimental/filesystem>
 
 namespace phosphor
@@ -49,7 +50,10 @@
             addressType = IP::Protocol::IPv6;
         }
 
-        std::string ipAddressObjectPath = getAddressObjectPath(addressType);
+        std::string ipAddressObjectPath = generateObjectPath(addressType,
+                                                             addr.ipaddress,
+                                                             addr.prefix,
+                                                             gateway);
         this->addrs.emplace(
                 std::make_pair(
                         addr.ipaddress,
@@ -71,9 +75,10 @@
                            uint8_t prefixLength,
                            std::string gateway)
 {
-
-    IP::Protocol protocolType  = protType;
-    std::string objectPath = getAddressObjectPath(protocolType);
+    std::string objectPath = generateObjectPath(protType,
+                                                ipaddress,
+                                                prefixLength,
+                                                gateway);
 
     this->addrs.emplace(
             std::make_pair(ipaddress,
@@ -81,7 +86,7 @@
                                 bus,
                                 objectPath.c_str(),
                                 *this,
-                                protocolType,
+                                protType,
                                 ipaddress,
                                 prefixLength,
                                 gateway)));
@@ -194,20 +199,19 @@
     return macAddress;
 }
 
-size_t EthernetInterface::getAddressCount(IP::Protocol addressType) const
+std::string EthernetInterface::generateId(const std::string& ipaddress,
+                                          uint8_t prefixLength,
+                                          const std::string& gateway)
 {
-    size_t count = 0;
+    std::stringstream hexId;
+    std::string hashString = ipaddress;
+    hashString += std::to_string(prefixLength);
+    hashString += gateway;
 
-    std::for_each(addrs.cbegin(), addrs.cend(),
-                  [&count,addressType](const auto & addr)
-    {
-        if (addr.second->type() == addressType)
-        {
-            count += 1;
-        }
-    });
-
-    return count;
+    // Only want 8 hex digits.
+    hexId << std::hex << ((std::hash<std::string> {}(
+                              hashString)) & 0xFFFFFFFF);
+    return hexId.str();
 }
 
 void EthernetInterface::deleteObject(const std::string& ipaddress)
@@ -215,10 +219,11 @@
     this->addrs.erase(addrs.find(ipaddress));
 }
 
-std::string EthernetInterface::getAddressObjectPath(IP::Protocol
-                                                    addressType) const
+std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
+                                                  const std::string& ipaddress,
+                                                  uint8_t prefixLength,
+                                                  const std::string& gateway) const
 {
-
     std::string type = convertForMessage(addressType);
     type = type.substr(type.rfind('.')+1);
     std::transform(type.begin(), type.end(), type.begin(), ::tolower);
@@ -227,7 +232,7 @@
     objectPath /= std::string(OBJ_NETWORK);
     objectPath /= interfaceName();
     objectPath /= type;
-    objectPath /= std::to_string(getAddressCount(addressType));
+    objectPath /= generateId(ipaddress,prefixLength,gateway);
     return objectPath.string();
 
 }