Update the voltha code to make use of the interface option and add
a new voltha swarm file.

Change-Id: Icf2bd5a35aea3a8aa55de58acb56bfa6f4b34e0d
diff --git a/common/utils/nethelpers.py b/common/utils/nethelpers.py
index b79d2b0..a082eeb 100644
--- a/common/utils/nethelpers.py
+++ b/common/utils/nethelpers.py
@@ -22,7 +22,6 @@
 
 import netifaces as ni
 
-
 def get_my_primary_interface():
     gateways = ni.gateways()
     assert 'default' in gateways, \
@@ -36,10 +35,13 @@
 
 
 def get_my_primary_local_ipv4(ifname=None):
-    ifname = get_my_primary_interface() if ifname is None else ifname
-    addresses = ni.ifaddresses(ifname)
-    ipv4 = addresses[AF_INET][0]['addr']
-    return ipv4
+    try:
+        ifname = get_my_primary_interface() if ifname is None else ifname
+        addresses = ni.ifaddresses(ifname)
+        ipv4 = addresses[AF_INET][0]['addr']
+        return ipv4
+    except Exception as e:
+        return None
 
 
 if __name__ == '__main__':
diff --git a/compose/docker-compose-vcli.yml b/compose/docker-compose-vcli.yml
index f3aafa3..3e543a5 100644
--- a/compose/docker-compose-vcli.yml
+++ b/compose/docker-compose-vcli.yml
@@ -20,6 +20,7 @@
       - -C consul:8500
       - -g voltha:50555
       - -s voltha:18880
+      - -G
     networks:
       - voltha-net
     ports:
diff --git a/compose/docker-compose-voltha-swarm.yml b/compose/docker-compose-voltha-swarm.yml
new file mode 100644
index 0000000..39532f7
--- /dev/null
+++ b/compose/docker-compose-voltha-swarm.yml
@@ -0,0 +1,30 @@
+version: "3"
+services:
+  voltha:
+    image: cord/voltha:latest
+    deploy:
+      replicas: 3
+    entrypoint:
+      - voltha/voltha/main.py
+      - -v
+      - --consul=consul:8500
+      - --fluentd=fluentd:24224
+      - --kafka=kafka
+      - --rest-port=8880
+      - --grpc-port=50555
+      - --instance-id-is-container-name
+      - --interface=eth2
+      - --backend=consul
+    networks:
+      - net
+    ports:
+      - "8880:8880"
+      - "18880:18880"
+      - "50555:50555"
+    volumes:
+      - /var/run/docker.sock:/tmp/docker.sock
+
+networks:
+  net:
+    external:
+      name: voltha_net
diff --git a/voltha/main.py b/voltha/main.py
index 787eb36..9eb12d4 100755
--- a/voltha/main.py
+++ b/voltha/main.py
@@ -52,13 +52,11 @@
 defs = dict(
     config=os.environ.get('CONFIG', './voltha.yml'),
     consul=os.environ.get('CONSUL', 'localhost:8500'),
-    external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
-                                         get_my_primary_local_ipv4()),
+    external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', None),
     fluentd=os.environ.get('FLUENTD', None),
     grpc_port=os.environ.get('GRPC_PORT', 50055),
     instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
-    internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
-                                         get_my_primary_local_ipv4()),
+    internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', None),
     interface=os.environ.get('INTERFACE', get_my_primary_interface()),
     rest_port=os.environ.get('REST_PORT', 8880),
     kafka=os.environ.get('KAFKA', 'localhost:9092'),
@@ -126,8 +124,7 @@
                         default=defs['instance_id'],
                         help=_help)
 
-    # TODO placeholder, not used yet
-    _help = 'ETH interface to send (default: %s)' % defs['interface']
+    _help = 'ETH interface to recieve (default: %s)' % defs['interface']
     parser.add_argument('-I', '--interface',
                         dest='interface',
                         action='store',
@@ -207,6 +204,14 @@
     if args.instance_id_is_container_name:
         args.instance_id = get_my_containers_name()
 
+    m_ip = get_my_primary_local_ipv4(args.interface)
+    if not m_ip:
+        m_ip = get_my_primary_local_ipv4()
+    if not args.external_host_address:
+        args.external_host_address = m_ip
+    if not args.internal_host_address:
+        args.internal_host_address = m_ip
+
     return args
 
 
@@ -284,7 +289,9 @@
     @inlineCallbacks
     def startup_components(self):
         try:
-            self.log.info('starting-internal-components')
+            self.log.info('starting-internal-components',
+                          internal_host=self.args.internal_host_address,
+                          external_host=self.args.external_host_address)
 
             registry.register('main', self)