2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.hosttracker.internal;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Dictionary;
16 import java.util.EnumSet;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.Timer;
23 import java.util.TimerTask;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.Future;
31 import org.apache.felix.dm.Component;
32 import org.eclipse.osgi.framework.console.CommandInterpreter;
33 import org.eclipse.osgi.framework.console.CommandProvider;
34 import org.opendaylight.controller.clustering.services.CacheConfigException;
35 import org.opendaylight.controller.clustering.services.CacheExistException;
36 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
37 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
38 import org.opendaylight.controller.clustering.services.IClusterServices;
39 import org.opendaylight.controller.hosttracker.IfHostListener;
40 import org.opendaylight.controller.hosttracker.IfIptoHost;
41 import org.opendaylight.controller.hosttracker.IfNewHostNotify;
42 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
43 import org.opendaylight.controller.hosttracker.hostAware.IHostFinder;
44 import org.opendaylight.controller.sal.core.ConstructionException;
45 import org.opendaylight.controller.sal.core.Edge;
46 import org.opendaylight.controller.sal.core.Host;
47 import org.opendaylight.controller.sal.core.Node;
48 import org.opendaylight.controller.sal.core.NodeConnector;
49 import org.opendaylight.controller.sal.core.Property;
50 import org.opendaylight.controller.sal.core.State;
51 import org.opendaylight.controller.sal.core.Tier;
52 import org.opendaylight.controller.sal.core.UpdateType;
53 import org.opendaylight.controller.sal.packet.address.DataLinkAddress;
54 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
55 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
56 import org.opendaylight.controller.sal.utils.GlobalConstants;
57 import org.opendaylight.controller.sal.utils.HexEncode;
58 import org.opendaylight.controller.sal.utils.NetUtils;
59 import org.opendaylight.controller.sal.utils.NodeCreator;
60 import org.opendaylight.controller.sal.utils.Status;
61 import org.opendaylight.controller.sal.utils.StatusCode;
62 import org.opendaylight.controller.switchmanager.IInventoryListener;
63 import org.opendaylight.controller.switchmanager.ISwitchManager;
64 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;
65 import org.opendaylight.controller.switchmanager.Subnet;
66 import org.opendaylight.controller.topologymanager.ITopologyManager;
67 import org.opendaylight.controller.topologymanager.ITopologyManagerAware;
68 import org.osgi.framework.BundleContext;
69 import org.osgi.framework.FrameworkUtil;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * @file HostTracker.java This class tracks the location of IP Hosts as to which
75 * Switch, Port, VLAN, they are connected to, as well as their MAC
76 * address. This is done dynamically as well as statically. The dynamic
77 * mechanism consists of listening to ARP messages as well sending ARP
78 * requests. Static mechanism consists of Northbound APIs to add or remove
79 * the hosts from the local database. ARP aging is also implemented to age
80 * out dynamically learned hosts. Interface methods are provided for other
81 * applications to 1. Query the local database for a single host 2. Get a
82 * list of all hosts 3. Get notification if a host is learned/added or
83 * removed the database
86 public class HostTracker implements IfIptoHost, IfHostListener, ISwitchManagerAware, IInventoryListener,
87 ITopologyManagerAware, ICacheUpdateAware<InetAddress, HostNodeConnector>, CommandProvider {
88 static final String ACTIVE_HOST_CACHE = "hosttracker.ActiveHosts";
89 static final String INACTIVE_HOST_CACHE = "hosttracker.InactiveHosts";
90 private static final Logger logger = LoggerFactory.getLogger(HostTracker.class);
91 protected IHostFinder hostFinder;
92 protected ConcurrentMap<InetAddress, HostNodeConnector> hostsDB;
94 * Following is a list of hosts which have been requested by NB APIs to be
95 * added, but either the switch or the port is not sup, so they will be
96 * added here until both come up
98 private ConcurrentMap<NodeConnector, HostNodeConnector> inactiveStaticHosts;
99 private final Set<IfNewHostNotify> newHostNotify = Collections.synchronizedSet(new HashSet<IfNewHostNotify>());
101 private ITopologyManager topologyManager;
102 protected IClusterContainerServices clusterContainerService = null;
103 protected ISwitchManager switchManager = null;
105 private Timer arpRefreshTimer;
106 private String containerName = null;
107 private ExecutorService executor;
108 protected boolean stopping;
109 private static class ARPPending {
110 protected InetAddress hostIP;
111 protected short sent_count;
112 protected HostTrackerCallable hostTrackerCallable;
114 public InetAddress getHostIP() {
118 public short getSent_count() {
122 public HostTrackerCallable getHostTrackerCallable() {
123 return hostTrackerCallable;
126 public void setHostIP(InetAddress networkAddr) {
127 this.hostIP = networkAddr;
130 public void setSent_count(short count) {
131 this.sent_count = count;
134 public void setHostTrackerCallable(HostTrackerCallable callable) {
135 hostTrackerCallable = callable;
139 // This list contains the hosts for which ARP requests are being sent
141 ConcurrentMap<InetAddress, ARPPending> ARPPendingList;
143 * This list below contains the hosts which were initially in ARPPendingList
144 * above, but ARP response didn't come from there hosts after multiple
145 * attempts over 8 seconds. The assumption is that the response didn't come
146 * back due to one of the following possibilities: 1. The L3 interface
147 * wasn't created for this host in the controller. This would cause
148 * arphandler not to know where to send the ARP 2. The host facing port is
149 * down 3. The IP host doesn't exist or is not responding to ARP requests
151 * Conditions 1 and 2 above can be recovered if ARP is sent when the
152 * relevant L3 interface is added or the port facing host comes up. Whenever
153 * L3 interface is added or host facing port comes up, ARP will be sent to
154 * hosts in this list.
156 * We can't recover from condition 3 above
158 ConcurrentMap<InetAddress, ARPPending> failedARPReqList;
160 public HostTracker() {
163 private void startUp() {
164 nonClusterObjectCreate();
170 timer.schedule(new OutStandingARPHandler(), 4000, 4000);
171 executor = Executors.newFixedThreadPool(2);
172 /* ARP Refresh Timer to go off every 5 seconds to implement ARP aging */
173 arpRefreshTimer = new Timer();
174 arpRefreshTimer.schedule(new ARPRefreshHandler(), 5000, 5000);
175 logger.debug("startUp: Caches created, timers started");
178 private void allocateCache() {
179 if (this.clusterContainerService == null) {
180 logger.error("un-initialized clusterContainerService, can't create cache");
183 logger.debug("Creating Cache for HostTracker");
185 this.clusterContainerService.createCache(ACTIVE_HOST_CACHE,
186 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
187 this.clusterContainerService.createCache(INACTIVE_HOST_CACHE,
188 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
189 } catch (CacheConfigException cce) {
190 logger.error("Cache couldn't be created for HostTracker - check cache mode");
191 } catch (CacheExistException cce) {
192 logger.error("Cache for HostTracker already exists, destroy and recreate");
194 logger.debug("Cache successfully created for HostTracker");
197 @SuppressWarnings({ "unchecked" })
198 private void retrieveCache() {
199 if (this.clusterContainerService == null) {
200 logger.error("un-initialized clusterContainerService, can't retrieve cache");
203 logger.debug("Retrieving cache for HostTrackerAH");
204 hostsDB = (ConcurrentMap<InetAddress, HostNodeConnector>) this.clusterContainerService
205 .getCache(ACTIVE_HOST_CACHE);
206 if (hostsDB == null) {
207 logger.error("Cache couldn't be retrieved for HostTracker");
209 logger.debug("Cache was successfully retrieved for HostTracker");
210 logger.debug("Retrieving cache for HostTrackerIH");
211 inactiveStaticHosts = (ConcurrentMap<NodeConnector, HostNodeConnector>) this.clusterContainerService
212 .getCache(INACTIVE_HOST_CACHE);
213 if (inactiveStaticHosts == null) {
214 logger.error("Cache couldn't be retrieved for HostTrackerIH");
216 logger.debug("Cache was successfully retrieved for HostTrackerIH");
219 public void nonClusterObjectCreate() {
220 hostsDB = new ConcurrentHashMap<InetAddress, HostNodeConnector>();
221 inactiveStaticHosts = new ConcurrentHashMap<NodeConnector, HostNodeConnector>();
222 ARPPendingList = new ConcurrentHashMap<InetAddress, ARPPending>();
223 failedARPReqList = new ConcurrentHashMap<InetAddress, ARPPending>();
226 public void shutDown() {
229 public void setnewHostNotify(IfNewHostNotify obj) {
230 this.newHostNotify.add(obj);
233 public void unsetnewHostNotify(IfNewHostNotify obj) {
234 this.newHostNotify.remove(obj);
237 public void setArpHandler(IHostFinder hostFinder) {
238 this.hostFinder = hostFinder;
241 public void unsetArpHandler(IHostFinder hostFinder) {
242 if (this.hostFinder == hostFinder) {
243 logger.debug("Arp Handler Service removed!");
244 this.hostFinder = null;
248 public void setTopologyManager(ITopologyManager s) {
249 this.topologyManager = s;
252 public void unsetTopologyManager(ITopologyManager s) {
253 if (this.topologyManager == s) {
254 logger.debug("Topology Manager Service removed!");
255 this.topologyManager = null;
259 private boolean hostExists(HostNodeConnector host) {
260 HostNodeConnector lhost = hostsDB.get(host.getNetworkAddress());
261 return host.equals(lhost);
264 private HostNodeConnector getHostFromOnActiveDB(InetAddress networkAddress) {
265 return hostsDB.get(networkAddress);
268 private Entry<NodeConnector, HostNodeConnector> getHostFromInactiveDB(InetAddress networkAddress) {
269 for (Entry<NodeConnector, HostNodeConnector> entry : inactiveStaticHosts.entrySet()) {
270 if (entry.getValue().equalsByIP(networkAddress)) {
271 logger.debug("getHostFromInactiveDB(): Inactive Host found for IP:{} ", networkAddress.getHostAddress());
275 logger.debug("getHostFromInactiveDB() Inactive Host Not found for IP: {}", networkAddress.getHostAddress());
279 private void removeHostFromInactiveDB(InetAddress networkAddress) {
280 NodeConnector nodeConnector = null;
281 for (Entry<NodeConnector, HostNodeConnector> entry : inactiveStaticHosts.entrySet()) {
282 if (entry.getValue().equalsByIP(networkAddress)) {
283 nodeConnector = entry.getKey();
287 if (nodeConnector != null) {
288 inactiveStaticHosts.remove(nodeConnector);
289 logger.debug("removeHostFromInactiveDB(): Host Removed for IP: {}", networkAddress.getHostAddress());
292 logger.debug("removeHostFromInactiveDB(): Host Not found for IP: {}", networkAddress.getHostAddress());
295 protected boolean hostMoved(HostNodeConnector host) {
296 if (hostQuery(host.getNetworkAddress()) != null) {
303 public HostNodeConnector hostQuery(InetAddress networkAddress) {
304 return hostsDB.get(networkAddress);
308 public Future<HostNodeConnector> discoverHost(InetAddress networkAddress) {
309 if (executor == null) {
310 logger.error("discoverHost: Null executor");
313 Callable<HostNodeConnector> worker = new HostTrackerCallable(this, networkAddress);
314 Future<HostNodeConnector> submit = executor.submit(worker);
319 public HostNodeConnector hostFind(InetAddress networkAddress) {
321 * Sometimes at boot with containers configured in the startup we hit
322 * this path (from TIF) when hostFinder has not been set yet Caller
323 * already handles the null return
326 if (hostFinder == null) {
327 logger.debug("Exiting hostFind, null hostFinder");
331 HostNodeConnector host = hostQuery(networkAddress);
333 logger.debug("hostFind(): Host found for IP: {}", networkAddress.getHostAddress());
337 /* Add this host to ARPPending List for any potential retries */
339 addToARPPendingList(networkAddress);
340 logger.debug("hostFind(): Host Not Found for IP: {}, Inititated Host Discovery ...",
341 networkAddress.getHostAddress());
343 /* host is not found, initiate a discovery */
345 hostFinder.find(networkAddress);
350 public Set<HostNodeConnector> getAllHosts() {
351 Set<HostNodeConnector> allHosts = new HashSet<HostNodeConnector>();
352 for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
353 HostNodeConnector host = entry.getValue();
356 logger.debug("Exiting getAllHosts, Found {} Hosts", allHosts.size());
361 public Set<HostNodeConnector> getActiveStaticHosts() {
362 Set<HostNodeConnector> list = new HashSet<HostNodeConnector>();
363 for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
364 HostNodeConnector host = entry.getValue();
365 if (host.isStaticHost()) {
369 logger.debug("getActiveStaticHosts(): Found {} Hosts", list.size());
374 public Set<HostNodeConnector> getInactiveStaticHosts() {
375 Set<HostNodeConnector> list = new HashSet<HostNodeConnector>();
376 for (Entry<NodeConnector, HostNodeConnector> entry : inactiveStaticHosts.entrySet()) {
377 list.add(entry.getValue());
379 logger.debug("getInactiveStaticHosts(): Found {} Hosts", list.size());
383 private void addToARPPendingList(InetAddress networkAddr) {
384 ARPPending arphost = new ARPPending();
386 arphost.setHostIP(networkAddr);
387 arphost.setSent_count((short) 1);
388 ARPPendingList.put(networkAddr, arphost);
389 logger.debug("Host Added to ARPPending List, IP: {}", networkAddr);
392 public void setCallableOnPendingARP(InetAddress networkAddr, HostTrackerCallable callable) {
394 for (Entry<InetAddress, ARPPending> entry : ARPPendingList.entrySet()) {
395 arphost = entry.getValue();
396 if (arphost.getHostIP().equals(networkAddr)) {
397 arphost.setHostTrackerCallable(callable);
402 private void processPendingARPReqs(InetAddress networkAddr) {
405 if ((arphost = ARPPendingList.remove(networkAddr)) != null) {
406 // Remove the arphost from ARPPendingList as it has been learned now
407 logger.debug("Host Removed from ARPPending List, IP: {}", networkAddr);
408 HostTrackerCallable htCallable = arphost.getHostTrackerCallable();
409 if (htCallable != null) {
416 * It could have been a host from the FailedARPReqList
419 if (failedARPReqList.containsKey(networkAddr)) {
420 failedARPReqList.remove(networkAddr);
421 logger.debug("Host Removed from FailedARPReqList List, IP: {}", networkAddr);
426 private void learnNewHost(HostNodeConnector host) {
427 host.initArpSendCountDown();
428 HostNodeConnector rHost = hostsDB.putIfAbsent(host.getNetworkAddress(), host);
430 // Another host is already learned for this IP address, replace it
431 replaceHost(host.getNetworkAddress(), rHost, host);
433 logger.debug("New Host Learned: MAC: {} IP: {}", HexEncode.bytesToHexString(host
434 .getDataLayerAddressBytes()), host.getNetworkAddress().getHostAddress());
438 private void replaceHost(InetAddress networkAddr, HostNodeConnector removedHost, HostNodeConnector newHost) {
439 // Ignore ARP messages from internal nodes
440 NodeConnector newHostNc = newHost.getnodeConnector();
441 boolean newHostIsInternal = topologyManager.isInternal(newHostNc);
442 if (newHostIsInternal) {
446 newHost.initArpSendCountDown();
448 if (hostsDB.replace(networkAddr, removedHost, newHost)) {
449 logger.debug("Host move occurred: Old Host IP:{}, New Host IP: {}", removedHost.getNetworkAddress()
450 .getHostAddress(), newHost.getNetworkAddress().getHostAddress());
451 logger.debug("Old Host MAC: {}, New Host MAC: {}",
452 HexEncode.bytesToHexString(removedHost.getDataLayerAddressBytes()),
453 HexEncode.bytesToHexString(newHost.getDataLayerAddressBytes()));
454 // Display the Old and New HostNodeConnectors also
455 logger.debug("Old {}, New {}", removedHost, newHost);
458 * Host replacement has failed, do the recovery
460 hostsDB.put(networkAddr, newHost);
461 logger.error("Host replacement failed. Overwrite the host. Repalced Host: {}, New Host: {}", removedHost,
464 notifyHostLearnedOrRemoved(removedHost, false);
465 notifyHostLearnedOrRemoved(newHost, true);
466 if (!newHost.isStaticHost()) {
467 processPendingARPReqs(networkAddr);
472 private void removeKnownHost(InetAddress key) {
473 HostNodeConnector host = hostsDB.get(key);
475 logger.debug("Removing Host: IP:{}", host.getNetworkAddress().getHostAddress());
478 logger.error("removeKnownHost(): Host for IP address {} not found in hostsDB", key.getHostAddress());
482 private class NotifyHostThread extends Thread {
484 private final HostNodeConnector host;
486 public NotifyHostThread(HostNodeConnector h) {
492 HostNodeConnector removedHost = null;
493 InetAddress networkAddr = host.getNetworkAddress();
495 /* Check for Host Move case */
496 if (hostMoved(host)) {
498 * Host has been moved from one location (switch,port, MAC, or
499 * VLAN) to another. Replace the existing host and its previous
500 * location parameters with new information, and notify the
501 * applications listening to host move.
503 removedHost = hostsDB.get(networkAddr);
504 if (removedHost != null) {
505 replaceHost(networkAddr, removedHost, host);
508 logger.error("Host to be removed not found in hostsDB");
515 /* check if there is an outstanding request for this host */
516 processPendingARPReqs(networkAddr);
517 notifyHostLearnedOrRemoved(host, true);
522 public void hostListener(HostNodeConnector host) {
524 logger.debug("ARP received for Host: IP {}, MAC {}, {}", host.getNetworkAddress().getHostAddress(),
525 HexEncode.bytesToHexString(host.getDataLayerAddressBytes()), host);
526 if (hostExists(host)) {
527 HostNodeConnector existinghost = hostsDB.get(host.getNetworkAddress());
528 existinghost.initArpSendCountDown();
531 new NotifyHostThread(host).start();
534 // Notify whoever is interested that a new host was learned (dynamically or
536 private void notifyHostLearnedOrRemoved(HostNodeConnector host, boolean add) {
537 // Update listeners if any
538 if (newHostNotify != null) {
539 logger.debug("Notifying Applications for Host {} Being {}", host.getNetworkAddress().getHostAddress(),
540 add ? "Added" : "Deleted");
541 synchronized (this.newHostNotify) {
542 for (IfNewHostNotify ta : newHostNotify) {
545 ta.notifyHTClient(host);
547 ta.notifyHTClientHostRemoved(host);
549 } catch (Exception e) {
550 logger.error("Exception on callback", e);
555 logger.error("notifyHostLearnedOrRemoved(): New host notify is null");
558 // Topology update is for some reason outside of listeners registry
560 Node node = host.getnodeconnectorNode();
562 NodeConnector p = host.getnodeConnector();
564 DataLinkAddress dla = new EthernetAddress(host.getDataLayerAddressBytes());
565 h = new Host(dla, host.getNetworkAddress());
566 } catch (ConstructionException ce) {
571 if (topologyManager != null && p != null && h != null) {
572 logger.debug("Notifying Topology Manager for Host {} Being {}", h.getNetworkAddress().getHostAddress(),
573 add ? "Added" : "Deleted");
575 Tier tier = new Tier(1);
576 switchManager.setNodeProp(node, tier);
577 topologyManager.updateHostLink(p, h, UpdateType.ADDED, null);
579 // No need to reset the tiering if no other hosts are currently
581 // If this switch was discovered to be an access switch, it
582 // still is even if the host is down
583 Tier tier = new Tier(0);
584 switchManager.setNodeProp(node, tier);
585 topologyManager.updateHostLink(p, h, UpdateType.REMOVED, null);
591 * When a new Host is learnt by the hosttracker module, it places the
592 * directly connected Node in Tier-1 & using this function, updates the Tier
593 * value for all other Nodes in the network hierarchy.
595 * This is a recursive function and it takes care of updating the Tier value
596 * for all the connected and eligible Nodes.
599 * Node that represents one of the Vertex in the Topology Graph.
601 * The Tier on which n belongs
603 @SuppressWarnings("unused")
604 private void updateSwitchTiers(Node n, int currentTier) {
605 Map<Node, Set<Edge>> ndlinks = topologyManager.getNodeEdges();
606 if (ndlinks == null) {
607 logger.debug("updateSwitchTiers(): ndlinks null for Node: {}, Tier:{}", n, currentTier);
610 Set<Edge> links = ndlinks.get(n);
612 logger.debug("updateSwitchTiers(): links null for ndlinks:{}", ndlinks);
615 ArrayList<Node> needsVisiting = new ArrayList<Node>();
616 for (Edge lt : links) {
617 if (!lt.getHeadNodeConnector().getType().equals(NodeConnector.NodeConnectorIDType.OPENFLOW)) {
618 // We don't want to work on Node that are not openflow
622 Node dstNode = lt.getHeadNodeConnector().getNode();
623 if (switchNeedsTieringUpdate(dstNode, currentTier + 1)) {
624 Tier t = new Tier(currentTier + 1);
625 switchManager.setNodeProp(dstNode, t);
626 needsVisiting.add(dstNode);
631 * Due to the nature of the problem, having a separate loop for nodes
632 * that needs visiting provides a decent walk optimization.
634 for (Node node : needsVisiting) {
635 updateSwitchTiers(node, currentTier + 1);
640 * Internal convenience routine to check the eligibility of a Switch for a
641 * Tier update. Any Node with Tier=0 or a Tier value that is greater than
642 * the new Tier Value is eligible for the update.
645 * Node for which the Tier update eligibility is checked
648 * @return <code>true</code> if the Node is eligible for Tier Update
649 * <code>false</code> otherwise
652 private boolean switchNeedsTieringUpdate(Node n, int tier) {
654 logger.error("switchNeedsTieringUpdate(): Null node for tier: {}", tier);
658 * Node could have gone down
660 if (!switchManager.getNodes().contains(n)) {
663 // This is the case where Tier was never set for this node
664 Tier t = (Tier) switchManager.getNodeProp(n, Tier.TierPropName);
668 if (t.getValue() == 0) {
670 } else if (t.getValue() > tier) {
677 * Internal convenience routine to clear all the Tier values to 0. This
678 * cleanup is performed during cases such as Topology Change where the
679 * existing Tier values might become incorrect
681 @SuppressWarnings("unused")
682 private void clearTiers() {
683 Set<Node> nodes = null;
684 if (switchManager == null) {
685 logger.error("clearTiers(): Null switchManager");
688 nodes = switchManager.getNodes();
690 for (Node n : nodes) {
691 Tier t = new Tier(0);
692 switchManager.setNodeProp(n, t);
697 * Internal convenience routine to print the hierarchies of switches.
699 @SuppressWarnings("unused")
700 private void logHierarchies(ArrayList<ArrayList<String>> hierarchies) {
701 String hierarchyString = null;
703 for (ArrayList<String> hierarchy : hierarchies) {
704 StringBuffer buf = new StringBuffer();
705 buf.append("Hierarchy#" + num + " : ");
706 for (String switchName : hierarchy) {
707 buf.append(switchName + "/");
709 logger.debug("{} -> {}", getContainerName(), buf);
715 * getHostNetworkHierarchy is the Back-end routine for the North-Bound API
716 * that returns the Network Hierarchy for a given Host. This API is
717 * typically used by applications like Hadoop for Rack Awareness
721 * IP-Address of the host/node.
722 * @return Network Hierarchies represented by an Array of Array (of
723 * Switch-Ids as String).
726 public List<List<String>> getHostNetworkHierarchy(InetAddress hostAddress) {
727 HostNodeConnector host = hostQuery(hostAddress);
732 List<List<String>> hierarchies = new ArrayList<List<String>>();
733 ArrayList<String> currHierarchy = new ArrayList<String>();
734 hierarchies.add(currHierarchy);
736 Node node = host.getnodeconnectorNode();
737 updateCurrentHierarchy(node, currHierarchy, hierarchies);
742 * dpidToHostNameHack is a hack function for Cisco Live Hadoop Demo. Mininet
743 * is used as the network for Hadoop Demos & in order to give a meaningful
744 * rack-awareness switch names, the DPID is organized in ASCII Characters
745 * and retrieved as string.
749 * @return Ascii String represented by the DPID.
751 private String dpidToHostNameHack(long dpid) {
752 String hex = Long.toHexString(dpid);
754 StringBuffer sb = new StringBuffer();
756 for (int i = 0; i < hex.length(); i++) {
757 result = (int) ((dpid >> (i * 8)) & 0xff);
764 sb.append(String.format("%c", result));
766 return sb.reverse().toString();
770 * A convenient recursive routine to obtain the Hierarchy of Switches.
773 * Current Node in the Recursive routine.
774 * @param currHierarchy
775 * Array of Nodes that make this hierarchy on which the Current
777 * @param fullHierarchy
778 * Array of multiple Hierarchies that represent a given host.
780 @SuppressWarnings("unchecked")
781 private void updateCurrentHierarchy(Node node, ArrayList<String> currHierarchy, List<List<String>> fullHierarchy) {
782 // currHierarchy.add(String.format("%x", currSw.getId()));
783 currHierarchy.add(dpidToHostNameHack((Long) node.getID()));
784 // Shallow copy as required
785 ArrayList<String> currHierarchyClone = (ArrayList<String>) currHierarchy.clone();
787 Map<Node, Set<Edge>> ndlinks = topologyManager.getNodeEdges();
788 if (ndlinks == null) {
789 logger.debug("updateCurrentHierarchy(): topologyManager returned null ndlinks for node: {}", node);
792 Node n = NodeCreator.createOFNode((Long) node.getID());
793 Set<Edge> links = ndlinks.get(n);
795 logger.debug("updateCurrentHierarchy(): Null links for ndlinks");
798 for (Edge lt : links) {
799 if (!lt.getHeadNodeConnector().getType().equals(NodeConnector.NodeConnectorIDType.OPENFLOW)) {
800 // We don't want to work on Node that are not openflow
804 Node dstNode = lt.getHeadNodeConnector().getNode();
806 Tier nodeTier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
808 * If the host is directly attached to the src node, then the node
809 * should have been assigned the "Access" tier in
810 * notifyHostLearnedOrRemoved. If not, it would be assigned
811 * "Unknown" tier. Thus the tier of host attached node cannot be
812 * null. If the src node here, is the next node in the hierarchy of
813 * the nodes, then its tier cannot be null
816 Tier dstNodeTier = (Tier) switchManager.getNodeProp(dstNode, Tier.TierPropName);
818 * Skip if the tier of the destination node is null
820 if (dstNodeTier == null) {
823 if (dstNodeTier.getValue() > nodeTier.getValue()) {
824 ArrayList<String> buildHierarchy = currHierarchy;
825 if (currHierarchy.size() > currHierarchyClone.size()) {
826 // Shallow copy as required
827 buildHierarchy = (ArrayList<String>) currHierarchyClone.clone();
828 fullHierarchy.add(buildHierarchy);
830 updateCurrentHierarchy(dstNode, buildHierarchy, fullHierarchy);
835 private void debugEdgeUpdate(Edge e, UpdateType type, Set<Property> props) {
837 Short srcPort = null;
839 Short dstPort = null;
840 boolean added = false;
841 String srcType = null;
842 String dstType = null;
844 if (e == null || type == null) {
845 logger.error("Edge or Update type are null!");
848 srcType = e.getTailNodeConnector().getType();
849 dstType = e.getHeadNodeConnector().getType();
851 if (srcType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
852 logger.debug("Skip updates for {}", e);
856 if (!srcType.equals(NodeConnector.NodeConnectorIDType.OPENFLOW)) {
857 logger.debug("For now we cannot handle updates for non-openflow nodes");
861 if (dstType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
862 logger.debug("Skip updates for {}", e);
866 if (!dstType.equals(NodeConnector.NodeConnectorIDType.OPENFLOW)) {
867 logger.debug("For now we cannot handle updates for non-openflow nodes");
871 // At this point we know we got an openflow update, so
872 // lets fill everything accordingly.
873 srcNid = (Long) e.getTailNodeConnector().getNode().getID();
874 srcPort = (Short) e.getTailNodeConnector().getID();
875 dstNid = (Long) e.getHeadNodeConnector().getNode().getID();
876 dstPort = (Short) e.getHeadNodeConnector().getID();
878 // Now lets update the added flag
889 logger.debug("HostTracker Topology linkUpdate handling src:{}[port {}] dst:{}[port {}] added: {}",
890 new Object[] { srcNid, srcPort, dstNid, dstPort, added });
894 public void edgeUpdate(List<TopoEdgeUpdate> topoedgeupdateList) {
895 if (logger.isDebugEnabled()) {
896 for (TopoEdgeUpdate topoEdgeUpdate : topoedgeupdateList) {
897 Edge e = topoEdgeUpdate.getEdge();
898 Set<Property> p = topoEdgeUpdate.getProperty();
899 UpdateType type = topoEdgeUpdate.getUpdateType();
901 debugEdgeUpdate(e, type, p);
907 public void subnetNotify(Subnet sub, boolean add) {
908 logger.debug("Received subnet notification: {} add={}", sub, add);
910 for (Entry<InetAddress, ARPPending> entry : failedARPReqList.entrySet()) {
912 arphost = entry.getValue();
913 if (hostFinder == null) {
914 logger.warn("ARPHandler Services are not available on subnet addition");
917 logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress());
918 hostFinder.find(arphost.getHostIP());
923 class OutStandingARPHandler extends TimerTask {
930 /* This routine runs every 4 seconds */
931 logger.trace("Number of Entries in ARP Pending/Failed Lists: ARPPendingList = {}, failedARPReqList = {}",
932 ARPPendingList.size(), failedARPReqList.size());
933 for (Entry<InetAddress, ARPPending> entry : ARPPendingList.entrySet()) {
934 arphost = entry.getValue();
936 if (hostsDB.containsKey(arphost.getHostIP())) {
937 // this host is already learned, shouldn't be in
939 // Remove it and continue
940 logger.warn("Learned Host {} found in ARPPendingList", arphost.getHostIP());
941 ARPPendingList.remove(entry.getKey());
944 if (arphost.getSent_count() < switchManager.getHostRetryCount()) {
946 * No reply has been received of first ARP Req, send the
947 * next one. Before sending the ARP, check if ARPHandler is
950 if (hostFinder == null) {
951 logger.warn("ARPHandler Services are not available for Outstanding ARPs");
954 hostFinder.find(arphost.getHostIP());
955 arphost.sent_count++;
956 logger.debug("ARP Sent from ARPPending List, IP: {}", arphost.getHostIP().getHostAddress());
957 } else if (arphost.getSent_count() >= switchManager.getHostRetryCount()) {
959 * ARP requests have been sent without receiving a reply,
960 * remove this from the pending list
962 ARPPendingList.remove(entry.getKey());
963 logger.debug("ARP reply not received after multiple attempts, removing from Pending List IP: {}",
964 arphost.getHostIP().getHostAddress());
966 * Add this host to a different list which will be processed
969 logger.debug("Adding the host to FailedARPReqList IP: {}", arphost.getHostIP().getHostAddress());
970 failedARPReqList.put(entry.getKey(), arphost);
973 logger.error("Inavlid arp_sent count for entry: {}", entry);
979 private class ARPRefreshHandler extends TimerTask {
985 if ((clusterContainerService != null) && !clusterContainerService.amICoordinator()) {
988 if ((switchManager != null) && !switchManager.isHostRefreshEnabled()) {
990 * The host probe procedure was disabled by CLI
994 if (hostsDB == null) {
995 /* hostsDB is not allocated yet */
996 logger.error("ARPRefreshHandler(): hostsDB is not allocated yet:");
999 for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
1000 HostNodeConnector host = entry.getValue();
1001 if (host.isStaticHost()) {
1002 /* this host was learned via API3, don't age it out */
1006 short arp_cntdown = host.getArpSendCountDown();
1008 if (arp_cntdown > switchManager.getHostRetryCount()) {
1009 host.setArpSendCountDown(arp_cntdown);
1010 } else if (arp_cntdown <= 0) {
1012 * No ARP Reply received in last 2 minutes, remove this host
1013 * and inform applications
1015 removeKnownHost(entry.getKey());
1016 notifyHostLearnedOrRemoved(host, false);
1017 } else if (arp_cntdown <= switchManager.getHostRetryCount()) {
1019 * Use the services of arphandler to check if host is still
1022 if (logger.isTraceEnabled()) {
1024 "ARP Probing ({}) for {}({})",
1025 new Object[] { arp_cntdown, host.getNetworkAddress().getHostAddress(),
1026 HexEncode.bytesToHexString(host.getDataLayerAddressBytes()) });
1028 host.setArpSendCountDown(arp_cntdown);
1029 if (hostFinder == null) {
1031 * If hostfinder is not available, then can't send the
1032 * probe. However, continue the age out the hosts since
1033 * we don't know if the host is indeed out there or not.
1035 logger.warn("ARPHandler is not avaialable, can't send the probe");
1038 hostFinder.probe(host);
1045 * Inform the controller IP to MAC binding of a host and its connectivity to
1046 * an openflow switch in terms of Node, port, and VLAN.
1048 * @param networkAddr
1049 * IP address of the host
1051 * Address MAC address of the host
1053 * NodeConnector to which host is connected
1055 * Port of the switch to which host is connected
1057 * Vlan of which this host is member of
1059 * @return Status The status object as described in {@code Status}
1060 * indicating the result of this action.
1063 public Status addStaticHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) {
1064 if (dataLayerAddress.length != NetUtils.MACAddrLengthInBytes) {
1065 return new Status(StatusCode.BADREQUEST, "Invalid MAC address");
1069 return new Status(StatusCode.BADREQUEST, "Invalid NodeConnector");
1071 HostNodeConnector host = null;
1073 host = new HostNodeConnector(dataLayerAddress, networkAddr, nc, vlan);
1074 if (hostExists(host)) {
1075 // This host is already learned either via ARP or through a
1076 // northbound request
1077 HostNodeConnector transHost = hostsDB.get(networkAddr);
1078 transHost.setStaticHost(true);
1079 return new Status(StatusCode.SUCCESS, null);
1082 if (hostsDB.get(networkAddr) != null) {
1083 // There is already a host with this IP address (but behind
1084 // a different (switch, port, vlan) tuple. Return an error
1085 return new Status(StatusCode.CONFLICT, "Existing IP, Use PUT to update");
1087 host.setStaticHost(true);
1089 * Check if the nc is an ISL port
1091 if (topologyManager != null) {
1092 if (topologyManager.isInternal(nc)) {
1093 return new Status(StatusCode.BADREQUEST, "Cannot add host on ISL port");
1097 * Before adding host, Check if the switch and the port have already
1100 if (switchManager.isNodeConnectorEnabled(nc)) {
1102 processPendingARPReqs(networkAddr);
1103 notifyHostLearnedOrRemoved(host, true);
1105 inactiveStaticHosts.put(nc, host);
1106 logger.debug("Switch or switchport is not up, adding host {} to inactive list",
1107 networkAddr.getHostName());
1109 return new Status(StatusCode.SUCCESS, null);
1110 } catch (ConstructionException e) {
1111 logger.error("", e);
1112 return new Status(StatusCode.INTERNALERROR, "Host could not be created");
1118 * Update the controller IP to MAC binding of a host and its connectivity to
1119 * an openflow switch in terms of switch id, switch port, and VLAN.
1121 * @param networkAddr
1122 * IP address of the host
1124 * Address MAC address of the host
1126 * NodeConnector to which host is connected
1128 * Port of the switch to which host is connected
1130 * Vlan of which this host is member of
1132 * @return Status The status object as described in {@code Status}
1133 * indicating the result of this action.
1135 public Status updateHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) {
1136 HostNodeConnector tobeUpdatedHost;
1137 HostNodeConnector host = null;
1139 if (dataLayerAddress.length != NetUtils.MACAddrLengthInBytes) {
1140 return new Status(StatusCode.BADREQUEST, "Invalid MAC address");
1144 return new Status(StatusCode.BADREQUEST, "Invalid NodeConnector");
1148 host = new HostNodeConnector(dataLayerAddress, networkAddr, nc, vlan);
1149 if (hostExists(host)) {
1150 return new Status(StatusCode.BADREQUEST, "Host already exists");
1153 if ((tobeUpdatedHost = hostsDB.get(networkAddr)) != null) {
1154 if (hostsDB.replace(networkAddr, tobeUpdatedHost, host)) {
1155 logger.debug("Host replaced from hostsDB. Old host: {} New Host: {}", tobeUpdatedHost, host);
1156 notifyHostLearnedOrRemoved(tobeUpdatedHost, false);
1157 notifyHostLearnedOrRemoved(host, true);
1158 return new Status(StatusCode.SUCCESS);
1160 logger.error("Static host replacement failed from hostsDB, Replaced Host: {}, New Host: {}",
1161 tobeUpdatedHost, host);
1162 return new Status(StatusCode.INTERNALERROR,
1163 "Host Replacement Failed due to presence of another host with same IP");
1167 // Check if the host exists in inactive hosts database
1168 if ((tobeUpdatedHost = inactiveStaticHosts.get(nc)) != null) {
1169 if (inactiveStaticHosts.replace(nc, tobeUpdatedHost, host)) {
1170 logger.debug("Host replaced from inactive hostsDB. Old host: {} New Host: {}", tobeUpdatedHost,
1172 return new Status(StatusCode.SUCCESS);
1174 logger.error("Static host replacement failed, Replaced Host: {}, New Host: {}", tobeUpdatedHost,
1176 return new Status(StatusCode.INTERNALERROR,
1177 "Host Replacement Failed due to presence of another host with same IP");
1181 // Host doesn't exist
1182 return new Status(StatusCode.BADREQUEST, "Host doesn't exists, can't update");
1183 } catch (ConstructionException e) {
1184 logger.error("", e);
1185 return new Status(StatusCode.INTERNALERROR, "host object creation failure");
1190 * Remove from the controller IP to MAC binding of a host and its
1191 * connectivity to an openflow switch
1193 * @param networkAddr
1194 * IP address of the host
1196 * @return boolean true if the host was removed successfully, false
1200 public Status removeStaticHostReq(InetAddress networkAddress) {
1201 // Check if host is in active hosts database
1202 HostNodeConnector host = getHostFromOnActiveDB(networkAddress);
1205 if (!host.isStaticHost()) {
1206 return new Status(StatusCode.FORBIDDEN, "Host " + networkAddress.getHostName() + " is not static");
1208 // Remove and notify
1209 notifyHostLearnedOrRemoved(host, false);
1210 removeKnownHost(networkAddress);
1211 return new Status(StatusCode.SUCCESS, null);
1214 // Check if host is in inactive hosts database
1215 Entry<NodeConnector, HostNodeConnector> entry = getHostFromInactiveDB(networkAddress);
1216 if (entry != null) {
1217 host = entry.getValue();
1219 if (!host.isStaticHost()) {
1220 return new Status(StatusCode.FORBIDDEN, "Host " + networkAddress.getHostName() + " is not static");
1222 this.removeHostFromInactiveDB(networkAddress);
1223 return new Status(StatusCode.SUCCESS, null);
1226 // Host is neither in active nor inactive hosts database
1227 return new Status(StatusCode.NOTFOUND, "Host does not exist");
1231 public void modeChangeNotify(Node node, boolean proactive) {
1232 logger.debug("Set Switch {} Mode to {}", node.getID(), proactive);
1236 public void notifyNode(Node node, UpdateType type, Map<String, Property> propMap) {
1243 logger.debug("Received removed node {}", node);
1244 for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
1245 HostNodeConnector host = entry.getValue();
1246 if (host.getnodeconnectorNode().equals(node)) {
1247 logger.debug("Node: {} is down, remove from Hosts_DB", node);
1248 removeKnownHost(entry.getKey());
1249 notifyHostLearnedOrRemoved(host, false);
1259 public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
1260 if (nodeConnector == null) {
1272 State state = (State) propMap.get(State.StatePropName);
1273 if ((state != null) && (state.getValue() == State.EDGE_UP)) {
1282 handleNodeConnectorStatusUp(nodeConnector);
1284 handleNodeConnectorStatusDown(nodeConnector);
1289 public Status addStaticHost(String networkAddress, String dataLayerAddress, NodeConnector nc, String vlan) {
1291 InetAddress ip = InetAddress.getByName(networkAddress);
1293 return new Status(StatusCode.BADREQUEST, "Invalid NodeId");
1295 return addStaticHostReq(ip, HexEncode.bytesFromHexString(dataLayerAddress), nc, Short.valueOf(vlan));
1296 } catch (UnknownHostException e) {
1297 logger.error("", e);
1298 return new Status(StatusCode.BADREQUEST, "Invalid Address");
1303 public Status removeStaticHost(String networkAddress) {
1304 InetAddress address;
1306 address = InetAddress.getByName(networkAddress);
1307 return removeStaticHostReq(address);
1308 } catch (UnknownHostException e) {
1309 logger.error("", e);
1310 return new Status(StatusCode.BADREQUEST, "Invalid Address");
1314 private void handleNodeConnectorStatusUp(NodeConnector nodeConnector) {
1316 HostNodeConnector host = null;
1318 logger.debug("handleNodeConnectorStatusUp {}", nodeConnector);
1320 for (Entry<InetAddress, ARPPending> entry : failedARPReqList.entrySet()) {
1321 arphost = entry.getValue();
1322 logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress());
1323 if (hostFinder == null) {
1324 logger.warn("ARPHandler is not available at interface up");
1325 logger.warn("Since this event is missed, host(s) connected to interface {} may not be discovered",
1330 // Send a broadcast ARP only on the interface which just came up.
1331 // Use hostFinder's "probe" method
1333 byte[] dataLayerAddress = NetUtils.getBroadcastMACAddr();
1334 host = new HostNodeConnector(dataLayerAddress, arphost.getHostIP(), nodeConnector, (short) 0);
1335 hostFinder.probe(host);
1336 } catch (ConstructionException e) {
1337 logger.debug("HostNodeConnector couldn't be created for Host: {}, NodeConnector: {}",
1338 arphost.getHostIP(), nodeConnector);
1339 logger.error("", e);
1341 logger.debug("Done. handleNodeConnectorStatusUp {}", nodeConnector);
1344 host = inactiveStaticHosts.get(nodeConnector);
1346 inactiveStaticHosts.remove(nodeConnector);
1348 processPendingARPReqs(host.getNetworkAddress());
1349 notifyHostLearnedOrRemoved(host, true);
1353 private void handleNodeConnectorStatusDown(NodeConnector nodeConnector) {
1354 logger.debug("handleNodeConnectorStatusDown {}", nodeConnector);
1356 for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
1357 HostNodeConnector host = entry.getValue();
1358 if (host.getnodeConnector().equals(nodeConnector)) {
1359 logger.debug(" NodeConnector: {} is down, remove from Hosts_DB", nodeConnector);
1360 removeKnownHost(entry.getKey());
1361 notifyHostLearnedOrRemoved(host, false);
1366 void setClusterContainerService(IClusterContainerServices s) {
1367 logger.debug("Cluster Service set");
1368 this.clusterContainerService = s;
1371 void unsetClusterContainerService(IClusterContainerServices s) {
1372 if (this.clusterContainerService == s) {
1373 logger.debug("Cluster Service removed!");
1374 this.clusterContainerService = null;
1378 void setSwitchManager(ISwitchManager s) {
1379 logger.debug("SwitchManager set");
1380 this.switchManager = s;
1383 void unsetSwitchManager(ISwitchManager s) {
1384 if (this.switchManager == s) {
1385 logger.debug("SwitchManager removed!");
1386 this.switchManager = null;
1390 public String getContainerName() {
1391 if (containerName == null) {
1392 return GlobalConstants.DEFAULT.toString();
1394 return containerName;
1398 * Function called by the dependency manager when all the required
1399 * dependencies are satisfied
1402 void init(Component c) {
1403 Dictionary<?, ?> props = c.getServiceProperties();
1404 if (props != null) {
1405 this.containerName = (String) props.get("containerName");
1406 logger.debug("Running containerName: {}", this.containerName);
1408 // In the Global instance case the containerName is empty
1409 this.containerName = "";
1415 * Function called by the dependency manager when at least one dependency
1416 * become unsatisfied or when the component is shutting down because for
1417 * example bundle is being stopped.
1424 * Function called by dependency manager after "init ()" is called and after
1425 * the services provided by the class are registered in the service registry
1429 registerWithOSGIConsole();
1433 * Function called by the dependency manager before the services exported by
1434 * the component are unregistered, this will be followed by a "destroy ()"
1443 arpRefreshTimer.cancel();
1445 executor.shutdownNow();
1449 public void edgeOverUtilized(Edge edge) {
1454 public void edgeUtilBackToNormal(Edge edge) {
1459 public void entryCreated(InetAddress key, String cacheName, boolean originLocal) {
1463 processPendingARPReqs(key);
1467 public void entryUpdated(InetAddress key, HostNodeConnector new_value, String cacheName, boolean originLocal) {
1471 public void entryDeleted(InetAddress key, String cacheName, boolean originLocal) {
1474 private void registerWithOSGIConsole() {
1475 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
1476 bundleContext.registerService(CommandProvider.class.getName(), this, null);
1480 public String getHelp() {
1484 public void _dumpPendingARPReqList(CommandInterpreter ci) {
1486 for (Entry<InetAddress, ARPPending> entry : ARPPendingList.entrySet()) {
1487 arphost = entry.getValue();
1488 ci.println(arphost.getHostIP().toString());
1492 public void _dumpFailedARPReqList(CommandInterpreter ci) {
1494 for (Entry<InetAddress, ARPPending> entry : failedARPReqList.entrySet()) {
1495 arphost = entry.getValue();
1496 ci.println(arphost.getHostIP().toString());