David K. Bainbridge
Committed by Gerrit Code Review

ONOS-3326 allow more control over heuristics to select an IP address used for clustering

Change-Id: Ie313efe9249df7581234e7e2e5952cdf3ac43dbb
...@@ -7,6 +7,7 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -7,6 +7,7 @@ import static org.slf4j.LoggerFactory.getLogger;
7 import java.io.File; 7 import java.io.File;
8 import java.io.IOException; 8 import java.io.IOException;
9 import java.net.InetAddress; 9 import java.net.InetAddress;
10 +import java.net.Inet4Address;
10 import java.net.NetworkInterface; 11 import java.net.NetworkInterface;
11 import java.util.Arrays; 12 import java.util.Arrays;
12 import java.util.Collection; 13 import java.util.Collection;
...@@ -58,6 +59,7 @@ public class StaticClusterMetadataStore ...@@ -58,6 +59,7 @@ public class StaticClusterMetadataStore
58 59
59 private static final String ONOS_IP = "ONOS_IP"; 60 private static final String ONOS_IP = "ONOS_IP";
60 private static final String ONOS_INTERFACE = "ONOS_INTERFACE"; 61 private static final String ONOS_INTERFACE = "ONOS_INTERFACE";
62 + private static final String ONOS_ALLOW_IPV6 = "ONOS_ALLOW_IPV6";
61 private static final String DEFAULT_ONOS_INTERFACE = "eth0"; 63 private static final String DEFAULT_ONOS_INTERFACE = "eth0";
62 private static final String CLUSTER_METADATA_FILE = "../config/cluster.json"; 64 private static final String CLUSTER_METADATA_FILE = "../config/cluster.json";
63 private static final int DEFAULT_ONOS_PORT = 9876; 65 private static final int DEFAULT_ONOS_PORT = 9876;
...@@ -214,13 +216,25 @@ public class StaticClusterMetadataStore ...@@ -214,13 +216,25 @@ public class StaticClusterMetadataStore
214 useOnosInterface = DEFAULT_ONOS_INTERFACE; 216 useOnosInterface = DEFAULT_ONOS_INTERFACE;
215 } 217 }
216 218
219 + // Capture if they want to limit IP address selection to only IPv4 (default).
220 + boolean allowIPv6 = (System.getenv(ONOS_ALLOW_IPV6) != null);
221 +
217 Function<NetworkInterface, IpAddress> ipLookup = nif -> { 222 Function<NetworkInterface, IpAddress> ipLookup = nif -> {
218 - for (InetAddress address : Collections.list(nif.getInetAddresses())) { 223 + IpAddress fallback = null;
219 - if (address.isSiteLocalAddress()) { 224 +
220 - return IpAddress.valueOf(address); 225 + // nif can be null if the interface name specified doesn't exist on the node's host
226 + if (nif != null) {
227 + for (InetAddress address : Collections.list(nif.getInetAddresses())) {
228 + if (address.isSiteLocalAddress() && (allowIPv6 || address instanceof Inet4Address)) {
229 + return IpAddress.valueOf(address);
230 + }
231 + if (fallback == null && !address.isLoopbackAddress() && !address.isMulticastAddress()
232 + && (allowIPv6 || address instanceof Inet4Address)) {
233 + fallback = IpAddress.valueOf(address);
234 + }
221 } 235 }
222 } 236 }
223 - return null; 237 + return fallback;
224 }; 238 };
225 try { 239 try {
226 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface)); 240 IpAddress ip = ipLookup.apply(NetworkInterface.getByName(useOnosInterface));
...@@ -228,14 +242,17 @@ public class StaticClusterMetadataStore ...@@ -228,14 +242,17 @@ public class StaticClusterMetadataStore
228 return ip.toString(); 242 return ip.toString();
229 } 243 }
230 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) { 244 for (NetworkInterface nif : Collections.list(getNetworkInterfaces())) {
231 - ip = ipLookup.apply(nif); 245 + if (!nif.getName().equals(useOnosInterface)) {
232 - if (ip != null) { 246 + ip = ipLookup.apply(nif);
233 - return ip.toString(); 247 + if (ip != null) {
248 + return ip.toString();
249 + }
234 } 250 }
235 } 251 }
236 } catch (Exception e) { 252 } catch (Exception e) {
237 throw new IllegalStateException("Unable to get network interfaces", e); 253 throw new IllegalStateException("Unable to get network interfaces", e);
238 } 254 }
255 +
239 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString(); 256 return IpAddress.valueOf(InetAddress.getLoopbackAddress()).toString();
240 } 257 }
241 } 258 }
......