This update add two arguments to voltha when it gets started:
   1) inter-core-subnet: the subnet to connect to for inter core communication
   2) pon-subnet: the subnet to connect to for PON communication

This requires that the voltha_net is created with a specified subnet and that
subnet is specified in the docker compose file, e.g.:

docker network create --driver overlay --subnet 10.0.1.0/24 voltha_net

And in the compose.yml file add the following options:

      - --inter-core-subnet=10.0.1.0/24
      - --pon-subnet=10.0.1.0/24

This update guarantees that the voltha instance is connected to the correct
network.

Change-Id: I5d29ab54282c4ba9aff5ba165fdb37352cfaa0fd
diff --git a/common/utils/nethelpers.py b/common/utils/nethelpers.py
index a082eeb..a4610a2 100644
--- a/common/utils/nethelpers.py
+++ b/common/utils/nethelpers.py
@@ -21,8 +21,17 @@
 from netifaces import AF_INET
 
 import netifaces as ni
+import netaddr
 
-def get_my_primary_interface():
+
+def _get_all_interfaces():
+    m_interfaces = []
+    for iface in ni.interfaces():
+        m_interfaces.append((iface, ni.ifaddresses(iface)))
+    return m_interfaces
+
+
+def _get_my_primary_interface():
     gateways = ni.gateways()
     assert 'default' in gateways, \
         ("No default gateway on host/container, "
@@ -34,7 +43,33 @@
     return interface_name
 
 
-def get_my_primary_local_ipv4(ifname=None):
+def get_my_primary_local_ipv4(inter_core_subnet=None, ifname=None):
+    if not inter_core_subnet:
+        return _get_my_primary_local_ipv4(ifname)
+    # My IP should belong to the specified subnet
+    for iface in ni.interfaces():
+        m_ip = ni.ifaddresses(iface)[AF_INET][0]['addr']
+        _ip = netaddr.IPAddress(m_ip).value
+        m_network = netaddr.IPNetwork(inter_core_subnet)
+        if _ip >= m_network.first and _ip <= m_network.last:
+            return m_ip
+    return None
+
+
+def get_my_primary_interface(pon_subnet=None):
+    if not pon_subnet:
+        return _get_my_primary_interface()
+    # My interface should have an IP that belongs to the specified subnet
+    for iface in ni.interfaces():
+        m_ip = ni.ifaddresses(iface)[AF_INET][0]['addr']
+        m_ip = netaddr.IPAddress(m_ip).value
+        m_network = netaddr.IPNetwork(pon_subnet)
+        if m_ip >= m_network.first and m_ip <= m_network.last:
+            return iface
+    return None
+
+
+def _get_my_primary_local_ipv4(ifname=None):
     try:
         ifname = get_my_primary_interface() if ifname is None else ifname
         addresses = ni.ifaddresses(ifname)
@@ -43,6 +78,5 @@
     except Exception as e:
         return None
 
-
 if __name__ == '__main__':
     print get_my_primary_local_ipv4()