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=df62c1985a62b5e7b11db450a39586d3ce0a8d58;hp=a6e1a32c54c257aecce985219f9f1aaa035a71d4;hb=9139d6ad1d16ba323d5fef0f71fdfde83b679125;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..df62c1985a 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; @@ -88,7 +89,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw 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 final Set hostFinder = new CopyOnWriteArraySet();; protected ConcurrentMap hostsDB; /* * Following is a list of hosts which have been requested by NB APIs to be @@ -106,6 +107,8 @@ 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 static class ARPPending { protected InetAddress hostIP; protected short sent_count; @@ -235,13 +238,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); } } @@ -307,7 +312,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw @Override public Future discoverHost(InetAddress networkAddress) { if (executor == null) { - logger.error("discoverHost: Null executor"); + logger.debug("discoverHost: Null executor"); return null; } Callable worker = new HostTrackerCallable(this, networkAddress); @@ -341,19 +346,15 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw networkAddress.getHostAddress()); /* host is not found, initiate a discovery */ - - hostFinder.find(networkAddress); + for (IHostFinder hf : hostFinder) { + hf.find(networkAddress); + } 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; } @@ -366,17 +367,12 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw 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; } @@ -520,12 +516,13 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw @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()); existinghost.initArpSendCountDown(); + // Update the host + hostsDB.put(host.getNetworkAddress(), existinghost); return; } new NotifyHostThread(host).start(); @@ -547,7 +544,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 +699,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++; @@ -779,7 +776,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(); @@ -915,7 +911,9 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw continue; } logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress()); - hostFinder.find(arphost.getHostIP()); + for (IHostFinder hf : hostFinder) { + hf.find(arphost.getHostIP()); + } } } } @@ -941,7 +939,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw 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,10 +949,12 @@ 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(arphost.getHostIP()); + } arphost.sent_count++; logger.debug("ARP Sent from ARPPending List, IP: {}", arphost.getHostIP().getHostAddress()); - } else if (arphost.getSent_count() >= switchManager.getHostRetryCount()) { + } else if (arphost.getSent_count() >= hostRetryCount) { /* * ARP requests have been sent without receiving a reply, * remove this from the pending list @@ -985,9 +985,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; } @@ -1005,7 +1005,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 +1014,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 +1032,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 +1062,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"); } @@ -1076,13 +1078,13 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw // 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) { // 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); /* @@ -1106,7 +1108,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw 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"); @@ -1289,25 +1291,33 @@ 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); + InetAddress address = InetAddress.getByName(networkAddress); return removeStaticHostReq(address); } 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"); } } @@ -1315,11 +1325,11 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw ARPPending arphost; HostNodeConnector host = null; - logger.debug("handleNodeConnectorStatusUp {}", nodeConnector); + logger.trace("handleNodeConnectorStatusUp {}", nodeConnector); 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.getHostIP().getHostAddress()); 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", @@ -1332,13 +1342,14 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw try { byte[] dataLayerAddress = NetUtils.getBroadcastMACAddr(); host = new HostNodeConnector(dataLayerAddress, arphost.getHostIP(), nodeConnector, (short) 0); - hostFinder.probe(host); + for (IHostFinder hf : hostFinder) { + hf.probe(host); + } } catch (ConstructionException e) { logger.debug("HostNodeConnector couldn't be created for Host: {}, NodeConnector: {}", arphost.getHostIP(), nodeConnector); logger.error("", e); } - logger.debug("Done. handleNodeConnectorStatusUp {}", nodeConnector); } host = inactiveStaticHosts.get(nodeConnector); @@ -1351,7 +1362,7 @@ public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAw } private void handleNodeConnectorStatusDown(NodeConnector nodeConnector) { - logger.debug("handleNodeConnectorStatusDown {}", nodeConnector); + logger.trace("handleNodeConnectorStatusDown {}", nodeConnector); for (Entry entry : hostsDB.entrySet()) { HostNodeConnector host = entry.getValue();