- hostsDB.put(host.getNetworkAddress(), host);
- logger.debug("New Host Learned: MAC: {} IP: {}",
- HexEncode.bytesToHexString(host.getDataLayerAddressBytes()),
- host.getNetworkAddress().getHostAddress());
+ HostNodeConnector rHost = hostsDB.putIfAbsent(host.getNetworkAddress(), host);
+ if (rHost != null) {
+ // Another host is already learned for this IP address, replace it
+ replaceHost(host.getNetworkAddress(), rHost, host);
+ } else {
+ logger.debug("New Host Learned: MAC: {} IP: {}", HexEncode.bytesToHexString(host
+ .getDataLayerAddressBytes()), host.getNetworkAddress().getHostAddress());
+ }
+ }
+
+ private void replaceHost(InetAddress networkAddr, HostNodeConnector removedHost, HostNodeConnector newHost) {
+ newHost.initArpSendCountDown();
+ if (hostsDB.replace(networkAddr, removedHost, newHost)) {
+ logger.debug("Host move occurred: Old Host IP:{}, New Host IP: {}", removedHost.getNetworkAddress()
+ .getHostAddress(), newHost.getNetworkAddress().getHostAddress());
+ logger.debug("Old Host MAC: {}, New Host MAC: {}",
+ HexEncode.bytesToHexString(removedHost.getDataLayerAddressBytes()),
+ HexEncode.bytesToHexString(newHost.getDataLayerAddressBytes()));
+ // Display the Old and New HostNodeConnectors also
+ logger.debug("Old {}, New {}", removedHost, newHost);
+ } else {
+ /*
+ * Host replacement has failed, do the recovery
+ */
+ hostsDB.put(networkAddr, newHost);
+ logger.error("Host replacement failed. Overwrite the host. Repalced Host: {}, New Host: {}", removedHost,
+ newHost);
+ }
+ notifyHostLearnedOrRemoved(removedHost, false);
+ notifyHostLearnedOrRemoved(newHost, true);
+ if (!newHost.isStaticHost()) {
+ ProcPendingARPReqs(networkAddr);
+ }