X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fhosttracker%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fhosttracker%2Finternal%2FHostTracker.java;h=9cae4348b12121d7495cb06bec851987bb426b74;hp=a6e1a32c54c257aecce985219f9f1aaa035a71d4;hb=0e53568409974e90df1272860706d2f47bec5aa6;hpb=6c927a9467f3b38ba6acd4e4d1631e902fbf9557 diff --git a/opendaylight/hosttracker/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/HostTracker.java b/opendaylight/hosttracker/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/HostTracker.java index a6e1a32c54..9cae4348b1 100644 --- a/opendaylight/hosttracker/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/HostTracker.java +++ b/opendaylight/hosttracker/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/HostTracker.java @@ -24,6 +24,7 @@ import java.util.TimerTask; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -36,6 +37,10 @@ import org.opendaylight.controller.clustering.services.CacheExistException; import org.opendaylight.controller.clustering.services.ICacheUpdateAware; import org.opendaylight.controller.clustering.services.IClusterContainerServices; import org.opendaylight.controller.clustering.services.IClusterServices; +import org.opendaylight.controller.hosttracker.HostIdFactory; +import org.opendaylight.controller.hosttracker.IHostId; +import org.opendaylight.controller.hosttracker.IPHostId; +import org.opendaylight.controller.hosttracker.IPMacHostId; import org.opendaylight.controller.hosttracker.IfHostListener; import org.opendaylight.controller.hosttracker.IfIptoHost; import org.opendaylight.controller.hosttracker.IfNewHostNotify; @@ -83,13 +88,25 @@ import org.slf4j.LoggerFactory; * removed the database */ +/*** + * + * HostTracker db key scheme implementation support. Support has been added for + * IP only or IP + MAC scheme as of now. User can use either of the schemes + * based on the configuration done in config.ini file. By default IP only key + * scheme is choosen. The attribute to be set in config.ini is + * hosttracker.keyscheme. It could have a value of 0 or 1 as of now. 0 is for IP + * only scheme. 1 is for IP + MAC scheme. + * + * + */ + public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAware, IInventoryListener, - ITopologyManagerAware, ICacheUpdateAware, CommandProvider { + ITopologyManagerAware, ICacheUpdateAware, CommandProvider { static final String ACTIVE_HOST_CACHE = "hosttracker.ActiveHosts"; static final String INACTIVE_HOST_CACHE = "hosttracker.InactiveHosts"; private static final Logger logger = LoggerFactory.getLogger(HostTracker.class); - protected IHostFinder hostFinder; - protected ConcurrentMap hostsDB; + protected final Set hostFinder = new CopyOnWriteArraySet();; + protected ConcurrentMap hostsDB; /* * Following is a list of hosts which have been requested by NB APIs to be * added, but either the switch or the port is not sup, so they will be @@ -106,13 +123,17 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw private String containerName = null; private ExecutorService executor; protected boolean stopping; + private static boolean hostRefresh = true; + private static int hostRetryCount = 5; + private String keyScheme = null; + private static class ARPPending { - protected InetAddress hostIP; + protected IHostId hostId; protected short sent_count; protected HostTrackerCallable hostTrackerCallable; - public InetAddress getHostIP() { - return hostIP; + public IHostId getHostId() { + return hostId; } public short getSent_count() { @@ -123,8 +144,8 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw return hostTrackerCallable; } - public void setHostIP(InetAddress networkAddr) { - this.hostIP = networkAddr; + public void setHostId(IHostId id) { + this.hostId = id; } public void setSent_count(short count) { @@ -138,7 +159,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw // This list contains the hosts for which ARP requests are being sent // periodically - ConcurrentMap ARPPendingList; + ConcurrentMap ARPPendingList; /* * This list below contains the hosts which were initially in ARPPendingList * above, but ARP response didn't come from there hosts after multiple @@ -155,7 +176,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * * We can't recover from condition 3 above */ - ConcurrentMap failedARPReqList; + ConcurrentMap failedARPReqList; public HostTracker() { } @@ -172,6 +193,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw /* ARP Refresh Timer to go off every 5 seconds to implement ARP aging */ arpRefreshTimer = new Timer(); arpRefreshTimer.schedule(new ARPRefreshHandler(), 5000, 5000); + keyScheme = HostIdFactory.getScheme(); logger.debug("startUp: Caches created, timers started"); } @@ -201,8 +223,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw return; } logger.debug("Retrieving cache for HostTrackerAH"); - hostsDB = (ConcurrentMap) this.clusterContainerService - .getCache(ACTIVE_HOST_CACHE); + hostsDB = (ConcurrentMap) this.clusterContainerService.getCache(ACTIVE_HOST_CACHE); if (hostsDB == null) { logger.error("Cache couldn't be retrieved for HostTracker"); } @@ -217,10 +238,10 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } public void nonClusterObjectCreate() { - hostsDB = new ConcurrentHashMap(); + hostsDB = new ConcurrentHashMap(); inactiveStaticHosts = new ConcurrentHashMap(); - ARPPendingList = new ConcurrentHashMap(); - failedARPReqList = new ConcurrentHashMap(); + ARPPendingList = new ConcurrentHashMap(); + failedARPReqList = new ConcurrentHashMap(); } public void shutDown() { @@ -235,13 +256,15 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } public void setArpHandler(IHostFinder hostFinder) { - this.hostFinder = hostFinder; + if (this.hostFinder != null) { + this.hostFinder.add(hostFinder); + } } public void unsetArpHandler(IHostFinder hostFinder) { - if (this.hostFinder == hostFinder) { + if (this.hostFinder != null) { logger.debug("Arp Handler Service removed!"); - this.hostFinder = null; + this.hostFinder.remove(hostFinder); } } @@ -257,66 +280,72 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } private boolean hostExists(HostNodeConnector host) { - HostNodeConnector lhost = hostsDB.get(host.getNetworkAddress()); + IHostId id = HostIdFactory.create(host.getNetworkAddress(), host.getDataLayerAddress()); + HostNodeConnector lhost = hostsDB.get(id); return host.equals(lhost); } - private HostNodeConnector getHostFromOnActiveDB(InetAddress networkAddress) { - return hostsDB.get(networkAddress); + private HostNodeConnector getHostFromOnActiveDB(IHostId id) { + return hostsDB.get(id); } - private Entry getHostFromInactiveDB(InetAddress networkAddress) { + private Entry getHostFromInactiveDB(IHostId id) { for (Entry entry : inactiveStaticHosts.entrySet()) { - if (entry.getValue().equalsByIP(networkAddress)) { - logger.debug("getHostFromInactiveDB(): Inactive Host found for IP:{} ", networkAddress.getHostAddress()); + HostNodeConnector hnc = entry.getValue(); + IHostId cmpId = HostIdFactory.create(hnc.getNetworkAddress(), hnc.getDataLayerAddress()); + if (cmpId.equals(id)) { + logger.debug("getHostFromInactiveDB(): Inactive Host found for ID:{} ", decodeIPFromId(id)); return entry; } } - logger.debug("getHostFromInactiveDB() Inactive Host Not found for IP: {}", networkAddress.getHostAddress()); + logger.debug("getHostFromInactiveDB() Inactive Host Not found for ID: {}", decodeIPFromId(id)); return null; } - private void removeHostFromInactiveDB(InetAddress networkAddress) { + private void removeHostFromInactiveDB(IHostId id) { NodeConnector nodeConnector = null; for (Entry entry : inactiveStaticHosts.entrySet()) { - if (entry.getValue().equalsByIP(networkAddress)) { + HostNodeConnector hnc = entry.getValue(); + IHostId cmpId = HostIdFactory.create(hnc.getNetworkAddress(), hnc.getDataLayerAddress()); + if (cmpId.equals(id)) { nodeConnector = entry.getKey(); break; } } if (nodeConnector != null) { inactiveStaticHosts.remove(nodeConnector); - logger.debug("removeHostFromInactiveDB(): Host Removed for IP: {}", networkAddress.getHostAddress()); + logger.debug("removeHostFromInactiveDB(): Host Removed for IP: {}", decodeIPFromId(id)); return; } - logger.debug("removeHostFromInactiveDB(): Host Not found for IP: {}", networkAddress.getHostAddress()); + logger.debug("removeHostFromInactiveDB(): Host Not found for IP: {}", decodeIPFromId(id)); } protected boolean hostMoved(HostNodeConnector host) { - if (hostQuery(host.getNetworkAddress()) != null) { + IHostId id = HostIdFactory.create(host.getNetworkAddress(), host.getDataLayerAddress()); + if (hostQuery(id) != null) { return true; } return false; } @Override - public HostNodeConnector hostQuery(InetAddress networkAddress) { - return hostsDB.get(networkAddress); + public HostNodeConnector hostQuery(IHostId id) { + return hostsDB.get(id); } @Override - public Future discoverHost(InetAddress networkAddress) { + public Future discoverHost(IHostId id) { if (executor == null) { - logger.error("discoverHost: Null executor"); + logger.debug("discoverHost: Null executor"); return null; } - Callable worker = new HostTrackerCallable(this, networkAddress); + Callable worker = new HostTrackerCallable(this, id); Future submit = executor.submit(worker); return submit; } @Override - public HostNodeConnector hostFind(InetAddress networkAddress) { + public HostNodeConnector hostFind(IHostId id) { /* * Sometimes at boot with containers configured in the startup we hit * this path (from TIF) when hostFinder has not been set yet Caller @@ -328,83 +357,75 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw return null; } - HostNodeConnector host = hostQuery(networkAddress); + HostNodeConnector host = hostQuery(id); if (host != null) { - logger.debug("hostFind(): Host found for IP: {}", networkAddress.getHostAddress()); + logger.debug("hostFind(): Host found for IP: {}", id); return host; } /* Add this host to ARPPending List for any potential retries */ - addToARPPendingList(networkAddress); - logger.debug("hostFind(): Host Not Found for IP: {}, Inititated Host Discovery ...", - networkAddress.getHostAddress()); + addToARPPendingList(id); + logger.debug("hostFind(): Host Not Found for IP: {}, Inititated Host Discovery ...", id); /* host is not found, initiate a discovery */ - - hostFinder.find(networkAddress); + for (IHostFinder hf : hostFinder) { + InetAddress addr = decodeIPFromId(id); + hf.find(addr); + } return null; } @Override public Set getAllHosts() { - Set allHosts = new HashSet(); - for (Entry entry : hostsDB.entrySet()) { - HostNodeConnector host = entry.getValue(); - allHosts.add(host); - } - logger.debug("Exiting getAllHosts, Found {} Hosts", allHosts.size()); + Set allHosts = new HashSet(hostsDB.values()); return allHosts; } @Override public Set getActiveStaticHosts() { Set list = new HashSet(); - for (Entry entry : hostsDB.entrySet()) { + for (Entry entry : hostsDB.entrySet()) { HostNodeConnector host = entry.getValue(); if (host.isStaticHost()) { list.add(host); } } - logger.debug("getActiveStaticHosts(): Found {} Hosts", list.size()); return list; } @Override public Set getInactiveStaticHosts() { - Set list = new HashSet(); - for (Entry entry : inactiveStaticHosts.entrySet()) { - list.add(entry.getValue()); - } - logger.debug("getInactiveStaticHosts(): Found {} Hosts", list.size()); + Set list = new HashSet(inactiveStaticHosts.values()); return list; } - private void addToARPPendingList(InetAddress networkAddr) { + private void addToARPPendingList(IHostId id) { ARPPending arphost = new ARPPending(); - arphost.setHostIP(networkAddr); + arphost.setHostId(id); arphost.setSent_count((short) 1); - ARPPendingList.put(networkAddr, arphost); - logger.debug("Host Added to ARPPending List, IP: {}", networkAddr); + ARPPendingList.put(id, arphost); + logger.debug("Host Added to ARPPending List, IP: {}", decodeIPFromId(id)); + } - public void setCallableOnPendingARP(InetAddress networkAddr, HostTrackerCallable callable) { + public void setCallableOnPendingARP(IHostId id, HostTrackerCallable callable) { ARPPending arphost; - for (Entry entry : ARPPendingList.entrySet()) { + for (Entry entry : ARPPendingList.entrySet()) { arphost = entry.getValue(); - if (arphost.getHostIP().equals(networkAddr)) { + if (arphost.getHostId().equals(id)) { arphost.setHostTrackerCallable(callable); } } } - private void processPendingARPReqs(InetAddress networkAddr) { + private void processPendingARPReqs(IHostId id) { ARPPending arphost; - if ((arphost = ARPPendingList.remove(networkAddr)) != null) { + if ((arphost = ARPPendingList.remove(id)) != null) { // Remove the arphost from ARPPendingList as it has been learned now - logger.debug("Host Removed from ARPPending List, IP: {}", networkAddr); + logger.debug("Host Removed from ARPPending List, IP: {}", id); HostTrackerCallable htCallable = arphost.getHostTrackerCallable(); if (htCallable != null) { htCallable.wakeup(); @@ -416,26 +437,27 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * It could have been a host from the FailedARPReqList */ - if (failedARPReqList.containsKey(networkAddr)) { - failedARPReqList.remove(networkAddr); - logger.debug("Host Removed from FailedARPReqList List, IP: {}", networkAddr); + if (failedARPReqList.containsKey(id)) { + failedARPReqList.remove(id); + logger.debug("Host Removed from FailedARPReqList List, IP: {}", decodeIPFromId(id)); } } // Learn a new Host private void learnNewHost(HostNodeConnector host) { + IHostId id = HostIdFactory.create(host.getNetworkAddress(), host.getDataLayerAddress()); host.initArpSendCountDown(); - HostNodeConnector rHost = hostsDB.putIfAbsent(host.getNetworkAddress(), host); + HostNodeConnector rHost = hostsDB.putIfAbsent(id, host); if (rHost != null) { // Another host is already learned for this IP address, replace it - replaceHost(host.getNetworkAddress(), rHost, host); + replaceHost(id, 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) { + private void replaceHost(IHostId id, HostNodeConnector removedHost, HostNodeConnector newHost) { // Ignore ARP messages from internal nodes NodeConnector newHostNc = newHost.getnodeConnector(); boolean newHostIsInternal = topologyManager.isInternal(newHostNc); @@ -445,7 +467,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw newHost.initArpSendCountDown(); - if (hostsDB.replace(networkAddr, removedHost, newHost)) { + if (hostsDB.replace(id, 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: {}", @@ -457,25 +479,25 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw /* * Host replacement has failed, do the recovery */ - hostsDB.put(networkAddr, newHost); - logger.error("Host replacement failed. Overwrite the host. Repalced Host: {}, New Host: {}", removedHost, + hostsDB.put(id, newHost); + logger.error("Host replacement failed. Overwrite the host. Replaced Host: {}, New Host: {}", removedHost, newHost); } notifyHostLearnedOrRemoved(removedHost, false); notifyHostLearnedOrRemoved(newHost, true); if (!newHost.isStaticHost()) { - processPendingARPReqs(networkAddr); + processPendingARPReqs(id); } } // Remove known Host - private void removeKnownHost(InetAddress key) { + private void removeKnownHost(IHostId key) { HostNodeConnector host = hostsDB.get(key); if (host != null) { logger.debug("Removing Host: IP:{}", host.getNetworkAddress().getHostAddress()); hostsDB.remove(key); } else { - logger.error("removeKnownHost(): Host for IP address {} not found in hostsDB", key.getHostAddress()); + logger.error("removeKnownHost(): Host for IP address {} not found in hostsDB", decodeIPFromId(key)); } } @@ -491,7 +513,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw public void run() { HostNodeConnector removedHost = null; InetAddress networkAddr = host.getNetworkAddress(); - + IHostId id = HostIdFactory.create(networkAddr, host.getDataLayerAddress()); /* Check for Host Move case */ if (hostMoved(host)) { /* @@ -500,9 +522,10 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * location parameters with new information, and notify the * applications listening to host move. */ - removedHost = hostsDB.get(networkAddr); + + removedHost = hostsDB.get(id); if (removedHost != null) { - replaceHost(networkAddr, removedHost, host); + replaceHost(id, removedHost, host); return; } else { logger.error("Host to be removed not found in hostsDB"); @@ -513,19 +536,23 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw learnNewHost(host); /* check if there is an outstanding request for this host */ - processPendingARPReqs(networkAddr); + processPendingARPReqs(id); notifyHostLearnedOrRemoved(host, true); } } @Override public void hostListener(HostNodeConnector host) { - - logger.debug("ARP received for Host: IP {}, MAC {}, {}", host.getNetworkAddress().getHostAddress(), + logger.debug("Received for Host: IP {}, MAC {}, {}", host.getNetworkAddress().getHostAddress(), HexEncode.bytesToHexString(host.getDataLayerAddressBytes()), host); if (hostExists(host)) { - HostNodeConnector existinghost = hostsDB.get(host.getNetworkAddress()); + IHostId id = HostIdFactory.create(host.getNetworkAddress(), host.getDataLayerAddress()); + HostNodeConnector existinghost = hostsDB.get(id); existinghost.initArpSendCountDown(); + // Update the host + + hostsDB.put(id, existinghost); + logger.debug("hostListener returned without adding the host"); return; } new NotifyHostThread(host).start(); @@ -547,7 +574,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw ta.notifyHTClientHostRemoved(host); } } catch (Exception e) { - logger.error("Exception on callback", e); + logger.error("Exception on new host notification", e); } } } @@ -702,9 +729,9 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw int num = 1; for (ArrayList hierarchy : hierarchies) { StringBuffer buf = new StringBuffer(); - buf.append("Hierarchy#" + num + " : "); + buf.append("Hierarchy#").append(num).append(" : "); for (String switchName : hierarchy) { - buf.append(switchName + "/"); + buf.append(switchName).append("/"); } logger.debug("{} -> {}", getContainerName(), buf); num++; @@ -723,8 +750,8 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * Switch-Ids as String). */ @Override - public List> getHostNetworkHierarchy(InetAddress hostAddress) { - HostNodeConnector host = hostQuery(hostAddress); + public List> getHostNetworkHierarchy(IHostId id) { + HostNodeConnector host = hostQuery(id); if (host == null) { return null; } @@ -779,7 +806,6 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw */ @SuppressWarnings("unchecked") private void updateCurrentHierarchy(Node node, ArrayList currHierarchy, List> fullHierarchy) { - // currHierarchy.add(String.format("%x", currSw.getId())); currHierarchy.add(dpidToHostNameHack((Long) node.getID())); // Shallow copy as required ArrayList currHierarchyClone = (ArrayList) currHierarchy.clone(); @@ -907,15 +933,17 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw public void subnetNotify(Subnet sub, boolean add) { logger.debug("Received subnet notification: {} add={}", sub, add); if (add) { - for (Entry entry : failedARPReqList.entrySet()) { + for (Entry entry : failedARPReqList.entrySet()) { ARPPending arphost; arphost = entry.getValue(); if (hostFinder == null) { logger.warn("ARPHandler Services are not available on subnet addition"); continue; } - logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress()); - hostFinder.find(arphost.getHostIP()); + logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", decodeIPFromId(arphost.getHostId())); + for (IHostFinder hf : hostFinder) { + hf.find(decodeIPFromId(arphost.getHostId())); + } } } } @@ -930,18 +958,18 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw /* This routine runs every 4 seconds */ logger.trace("Number of Entries in ARP Pending/Failed Lists: ARPPendingList = {}, failedARPReqList = {}", ARPPendingList.size(), failedARPReqList.size()); - for (Entry entry : ARPPendingList.entrySet()) { + for (Entry entry : ARPPendingList.entrySet()) { arphost = entry.getValue(); - if (hostsDB.containsKey(arphost.getHostIP())) { + if (hostsDB.containsKey(arphost.getHostId())) { // this host is already learned, shouldn't be in // ARPPendingList // Remove it and continue - logger.warn("Learned Host {} found in ARPPendingList", arphost.getHostIP()); + logger.warn("Learned Host {} found in ARPPendingList", decodeIPFromId(arphost.getHostId())); ARPPendingList.remove(entry.getKey()); continue; } - if (arphost.getSent_count() < switchManager.getHostRetryCount()) { + if (arphost.getSent_count() < hostRetryCount) { /* * No reply has been received of first ARP Req, send the * next one. Before sending the ARP, check if ARPHandler is @@ -951,22 +979,24 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw logger.warn("ARPHandler Services are not available for Outstanding ARPs"); continue; } - hostFinder.find(arphost.getHostIP()); + for (IHostFinder hf : hostFinder) { + hf.find(decodeIPFromId(arphost.getHostId())); + } arphost.sent_count++; - logger.debug("ARP Sent from ARPPending List, IP: {}", arphost.getHostIP().getHostAddress()); - } else if (arphost.getSent_count() >= switchManager.getHostRetryCount()) { + logger.debug("ARP Sent from ARPPending List, IP: {}", decodeIPFromId(arphost.getHostId())); + } else if (arphost.getSent_count() >= hostRetryCount) { /* * ARP requests have been sent without receiving a reply, * remove this from the pending list */ ARPPendingList.remove(entry.getKey()); logger.debug("ARP reply not received after multiple attempts, removing from Pending List IP: {}", - arphost.getHostIP().getHostAddress()); + decodeIPFromId(arphost.getHostId())); /* * Add this host to a different list which will be processed * on link up events */ - logger.debug("Adding the host to FailedARPReqList IP: {}", arphost.getHostIP().getHostAddress()); + logger.debug("Adding the host to FailedARPReqList IP: {}", decodeIPFromId(arphost.getHostId())); failedARPReqList.put(entry.getKey(), arphost); } else { @@ -985,9 +1015,9 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw if ((clusterContainerService != null) && !clusterContainerService.amICoordinator()) { return; } - if ((switchManager != null) && !switchManager.isHostRefreshEnabled()) { + if (!hostRefresh) { /* - * The host probe procedure was disabled by CLI + * The host probe procedure is turned off */ return; } @@ -996,7 +1026,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw logger.error("ARPRefreshHandler(): hostsDB is not allocated yet:"); return; } - for (Entry entry : hostsDB.entrySet()) { + for (Entry entry : hostsDB.entrySet()) { HostNodeConnector host = entry.getValue(); if (host.isStaticHost()) { /* this host was learned via API3, don't age it out */ @@ -1005,7 +1035,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw short arp_cntdown = host.getArpSendCountDown(); arp_cntdown--; - if (arp_cntdown > switchManager.getHostRetryCount()) { + if (arp_cntdown > hostRetryCount) { host.setArpSendCountDown(arp_cntdown); } else if (arp_cntdown <= 0) { /* @@ -1014,7 +1044,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw */ removeKnownHost(entry.getKey()); notifyHostLearnedOrRemoved(host, false); - } else if (arp_cntdown <= switchManager.getHostRetryCount()) { + } else if (arp_cntdown <= hostRetryCount) { /* * Use the services of arphandler to check if host is still * there @@ -1032,10 +1062,12 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * probe. However, continue the age out the hosts since * we don't know if the host is indeed out there or not. */ - logger.warn("ARPHandler is not avaialable, can't send the probe"); + logger.trace("ARPHandler is not avaialable, can't send the probe"); continue; } - hostFinder.probe(host); + for (IHostFinder hf : hostFinder) { + hf.probe(host); + } } } } @@ -1060,7 +1092,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * indicating the result of this action. */ - public Status addStaticHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) { + protected Status addStaticHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) { if (dataLayerAddress.length != NetUtils.MACAddrLengthInBytes) { return new Status(StatusCode.BADREQUEST, "Invalid MAC address"); } @@ -1071,18 +1103,19 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw HostNodeConnector host = null; try { host = new HostNodeConnector(dataLayerAddress, networkAddr, nc, vlan); + IHostId id = HostIdFactory.create(networkAddr, new EthernetAddress(dataLayerAddress)); if (hostExists(host)) { // This host is already learned either via ARP or through a // northbound request HostNodeConnector transHost = hostsDB.get(networkAddr); transHost.setStaticHost(true); - return new Status(StatusCode.SUCCESS, null); + return new Status(StatusCode.SUCCESS); } - if (hostsDB.get(networkAddr) != null) { + if (hostsDB.get(id) != null) { // There is already a host with this IP address (but behind // a different (switch, port, vlan) tuple. Return an error - return new Status(StatusCode.CONFLICT, "Existing IP, Use PUT to update"); + return new Status(StatusCode.CONFLICT, "Host with this IP already exists."); } host.setStaticHost(true); /* @@ -1099,14 +1132,14 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw */ if (switchManager.isNodeConnectorEnabled(nc)) { learnNewHost(host); - processPendingARPReqs(networkAddr); + processPendingARPReqs(id); notifyHostLearnedOrRemoved(host, true); } else { inactiveStaticHosts.put(nc, host); logger.debug("Switch or switchport is not up, adding host {} to inactive list", networkAddr.getHostName()); } - return new Status(StatusCode.SUCCESS, null); + return new Status(StatusCode.SUCCESS); } catch (ConstructionException e) { logger.error("", e); return new Status(StatusCode.INTERNALERROR, "Host could not be created"); @@ -1150,8 +1183,10 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw return new Status(StatusCode.BADREQUEST, "Host already exists"); } + IHostId id = HostIdFactory.create(networkAddr, new EthernetAddress(dataLayerAddress)); + if ((tobeUpdatedHost = hostsDB.get(networkAddr)) != null) { - if (hostsDB.replace(networkAddr, tobeUpdatedHost, host)) { + if (hostsDB.replace(id, tobeUpdatedHost, host)) { logger.debug("Host replaced from hostsDB. Old host: {} New Host: {}", tobeUpdatedHost, host); notifyHostLearnedOrRemoved(tobeUpdatedHost, false); notifyHostLearnedOrRemoved(host, true); @@ -1197,9 +1232,10 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw * otherwise */ - public Status removeStaticHostReq(InetAddress networkAddress) { + public Status removeStaticHostReq(InetAddress networkAddress, DataLinkAddress mac) { // Check if host is in active hosts database - HostNodeConnector host = getHostFromOnActiveDB(networkAddress); + IHostId id = HostIdFactory.create(networkAddress, mac); + HostNodeConnector host = getHostFromOnActiveDB(id); if (host != null) { // Validation check if (!host.isStaticHost()) { @@ -1207,19 +1243,19 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } // Remove and notify notifyHostLearnedOrRemoved(host, false); - removeKnownHost(networkAddress); + removeKnownHost(id); return new Status(StatusCode.SUCCESS, null); } // Check if host is in inactive hosts database - Entry entry = getHostFromInactiveDB(networkAddress); + Entry entry = getHostFromInactiveDB(id); if (entry != null) { host = entry.getValue(); // Validation check if (!host.isStaticHost()) { return new Status(StatusCode.FORBIDDEN, "Host " + networkAddress.getHostName() + " is not static"); } - this.removeHostFromInactiveDB(networkAddress); + this.removeHostFromInactiveDB(id); return new Status(StatusCode.SUCCESS, null); } @@ -1241,7 +1277,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw switch (type) { case REMOVED: logger.debug("Received removed node {}", node); - for (Entry entry : hostsDB.entrySet()) { + for (Entry entry : hostsDB.entrySet()) { HostNodeConnector host = entry.getValue(); if (host.getnodeconnectorNode().equals(node)) { logger.debug("Node: {} is down, remove from Hosts_DB", node); @@ -1289,37 +1325,87 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw public Status addStaticHost(String networkAddress, String dataLayerAddress, NodeConnector nc, String vlan) { try { InetAddress ip = InetAddress.getByName(networkAddress); - if (nc == null) { - return new Status(StatusCode.BADREQUEST, "Invalid NodeId"); + short vl = 0; + if (vlan != null && !vlan.isEmpty()) { + vl = Short.decode(vlan); + if (vl < 1 || vl > 4095) { + return new Status(StatusCode.BADREQUEST, "Host vlan out of range [1 - 4095]"); + } } - return addStaticHostReq(ip, HexEncode.bytesFromHexString(dataLayerAddress), nc, Short.valueOf(vlan)); + + return addStaticHostReq(ip, HexEncode.bytesFromHexString(dataLayerAddress), nc, vl); + } catch (UnknownHostException e) { - logger.error("", e); - return new Status(StatusCode.BADREQUEST, "Invalid Address"); + logger.debug("Invalid host IP specified when adding static host", e); + return new Status(StatusCode.BADREQUEST, "Invalid Host IP Address"); + } catch (NumberFormatException nfe) { + logger.debug("Invalid host vlan or MAC specified when adding static host", nfe); + return new Status(StatusCode.BADREQUEST, "Invalid Host vLan/MAC"); } } @Override public Status removeStaticHost(String networkAddress) { - InetAddress address; try { - address = InetAddress.getByName(networkAddress); - return removeStaticHostReq(address); + if ((keyScheme != null) && (!keyScheme.equals(HostIdFactory.DEFAULT_IP_KEY_SCHEME))) { + return new Status(StatusCode.NOTALLOWED, "Host DB Key scheme used is not IP only scheme."); + } + InetAddress address = InetAddress.getByName(networkAddress); + return removeStaticHostReq(address, null); } catch (UnknownHostException e) { - logger.error("", e); - return new Status(StatusCode.BADREQUEST, "Invalid Address"); + logger.debug("Invalid IP Address when trying to remove host", e); + return new Status(StatusCode.BADREQUEST, "Invalid IP Address when trying to remove host"); } } + @Override + public Status removeStaticHostUsingIPAndMac(String networkAddress, String macAddress) { + try { + if ((keyScheme != null) && (keyScheme.equals(HostIdFactory.DEFAULT_IP_KEY_SCHEME))) { + return new Status(StatusCode.NOTALLOWED, "Host DB Key scheme used is not IP only scheme."); + } + InetAddress address = InetAddress.getByName(networkAddress); + DataLinkAddress mac = new EthernetAddress(HexEncode.bytesFromHexString(macAddress)); + return removeStaticHostReq(address, mac); + } catch (UnknownHostException e) { + logger.debug("Invalid IP Address when trying to remove host", e); + return new Status(StatusCode.BADREQUEST, "Invalid IP Address when trying to remove host"); + } catch (ConstructionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return new Status(StatusCode.BADREQUEST, "Invalid Input parameters have been passed."); + } + } + + private InetAddress decodeIPFromId(IHostId id) { + if ((keyScheme != null) && (keyScheme.equals(HostIdFactory.DEFAULT_IP_KEY_SCHEME))) { + IPHostId ipId = (IPHostId) id; + return (ipId.getIpAddress()); + } else if ((keyScheme != null) && (keyScheme.equals(HostIdFactory.IP_MAC_KEY_SCHEME))) { + IPMacHostId ipMacId = (IPMacHostId) id; + return (ipMacId.getIpAddress()); + } + return null; + } + + private DataLinkAddress decodeMacFromId(IHostId id) { + if ((keyScheme != null) && (!keyScheme.equals(HostIdFactory.DEFAULT_IP_KEY_SCHEME))) { + IPMacHostId ipMacId = (IPMacHostId) id; + return (ipMacId.getMacAddr()); + } + + return null; + } + private void handleNodeConnectorStatusUp(NodeConnector nodeConnector) { ARPPending arphost; HostNodeConnector host = null; - logger.debug("handleNodeConnectorStatusUp {}", nodeConnector); + logger.trace("handleNodeConnectorStatusUp {}", nodeConnector); - for (Entry entry : failedARPReqList.entrySet()) { + for (Entry entry : failedARPReqList.entrySet()) { arphost = entry.getValue(); - logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress()); + logger.trace("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostId()); if (hostFinder == null) { logger.warn("ARPHandler is not available at interface up"); logger.warn("Since this event is missed, host(s) connected to interface {} may not be discovered", @@ -1331,29 +1417,32 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw // Use hostFinder's "probe" method try { byte[] dataLayerAddress = NetUtils.getBroadcastMACAddr(); - host = new HostNodeConnector(dataLayerAddress, arphost.getHostIP(), nodeConnector, (short) 0); - hostFinder.probe(host); + host = new HostNodeConnector(dataLayerAddress, decodeIPFromId(arphost.getHostId()), nodeConnector, + (short) 0); + for (IHostFinder hf : hostFinder) { + hf.probe(host); + } } catch (ConstructionException e) { logger.debug("HostNodeConnector couldn't be created for Host: {}, NodeConnector: {}", - arphost.getHostIP(), nodeConnector); + arphost.getHostId(), nodeConnector); logger.error("", e); } - logger.debug("Done. handleNodeConnectorStatusUp {}", nodeConnector); } host = inactiveStaticHosts.get(nodeConnector); if (host != null) { inactiveStaticHosts.remove(nodeConnector); learnNewHost(host); - processPendingARPReqs(host.getNetworkAddress()); + IHostId id = HostIdFactory.create(host.getNetworkAddress(), host.getDataLayerAddress()); + processPendingARPReqs(id); notifyHostLearnedOrRemoved(host, true); } } private void handleNodeConnectorStatusDown(NodeConnector nodeConnector) { - logger.debug("handleNodeConnectorStatusDown {}", nodeConnector); + logger.trace("handleNodeConnectorStatusDown {}", nodeConnector); - for (Entry entry : hostsDB.entrySet()) { + for (Entry entry : hostsDB.entrySet()) { HostNodeConnector host = entry.getValue(); if (host.getnodeConnector().equals(nodeConnector)) { logger.debug(" NodeConnector: {} is down, remove from Hosts_DB", nodeConnector); @@ -1409,6 +1498,8 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw this.containerName = ""; } startUp(); + + logger.debug("key Scheme in hosttracker is {}", keyScheme); } /** @@ -1456,7 +1547,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } @Override - public void entryCreated(InetAddress key, String cacheName, boolean originLocal) { + public void entryCreated(IHostId key, String cacheName, boolean originLocal) { if (originLocal) { return; } @@ -1464,11 +1555,11 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } @Override - public void entryUpdated(InetAddress key, HostNodeConnector new_value, String cacheName, boolean originLocal) { + public void entryUpdated(IHostId key, HostNodeConnector new_value, String cacheName, boolean originLocal) { } @Override - public void entryDeleted(InetAddress key, String cacheName, boolean originLocal) { + public void entryDeleted(IHostId key, String cacheName, boolean originLocal) { } private void registerWithOSGIConsole() { @@ -1483,17 +1574,41 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw public void _dumpPendingARPReqList(CommandInterpreter ci) { ARPPending arphost; - for (Entry entry : ARPPendingList.entrySet()) { + for (Entry entry : ARPPendingList.entrySet()) { arphost = entry.getValue(); - ci.println(arphost.getHostIP().toString()); + ci.println(arphost.getHostId().toString()); } } public void _dumpFailedARPReqList(CommandInterpreter ci) { ARPPending arphost; - for (Entry entry : failedARPReqList.entrySet()) { + for (Entry entry : failedARPReqList.entrySet()) { arphost = entry.getValue(); - ci.println(arphost.getHostIP().toString()); + ci.println(arphost.getHostId().toString()); } } + + @Override + public HostNodeConnector hostFind(InetAddress addr) { + IHostId id = HostIdFactory.create(addr, null); + return (hostFind(id)); + } + + @Override + public HostNodeConnector hostQuery(InetAddress addr) { + IHostId id = HostIdFactory.create(addr, null); + return (hostQuery(id)); + } + + @Override + public Future discoverHost(InetAddress addr) { + IHostId id = HostIdFactory.create(addr, null); + return discoverHost(id); + } + + @Override + public List> getHostNetworkHierarchy(InetAddress addr) { + IHostId id = HostIdFactory.create(addr, null); + return getHostNetworkHierarchy(id); + } }