Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.hostmanager; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Date; |
| 6 | import java.util.HashMap; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 7 | import java.util.HashSet; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 8 | import java.util.List; |
| 9 | import java.util.Map; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 10 | import java.util.Set; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 11 | import java.util.concurrent.CopyOnWriteArrayList; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 12 | import java.util.concurrent.Executors; |
| 13 | import java.util.concurrent.ScheduledExecutorService; |
| 14 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.FloodlightContext; |
| 17 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 18 | import net.floodlightcontroller.core.IOFMessageListener; |
| 19 | import net.floodlightcontroller.core.IOFSwitch; |
| 20 | import net.floodlightcontroller.core.IUpdate; |
| 21 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 22 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.topology.ITopologyService; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.topology.Port; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.topology.Topology; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.Dpid; |
| 31 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 32 | |
| 33 | import org.openflow.protocol.OFMessage; |
| 34 | import org.openflow.protocol.OFPacketIn; |
| 35 | import org.openflow.protocol.OFType; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 38 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 39 | public class HostManager implements IFloodlightModule, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | IOFMessageListener, |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 41 | IHostService { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 42 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 43 | private static final Logger log = LoggerFactory.getLogger(HostManager.class); |
| 44 | private static final long HOST_CLEANING_INITIAL_DELAY = 30; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 45 | private int cleanupSecondConfig = 60 * 60; |
| 46 | private int agingMillisecConfig = 60 * 60 * 1000; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 47 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 48 | private CopyOnWriteArrayList<IHostListener> hostListeners; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | private IFloodlightProviderService floodlightProvider; |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 50 | private static final ScheduledExecutorService EXECUTOR_SERVICE = |
| 51 | Executors.newSingleThreadScheduledExecutor(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 52 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 53 | private ITopologyService topologyService; |
| 54 | private Topology topology; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 55 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 56 | public enum HostUpdateType { |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 57 | ADD, DELETE, UPDATE; |
| 58 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 60 | private class HostUpdate implements IUpdate { |
| 61 | private final Host host; |
| 62 | private final HostUpdateType type; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 63 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 64 | public HostUpdate(Host host, HostUpdateType type) { |
| 65 | this.host = host; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | this.type = type; |
| 67 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public void dispatch() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 71 | if (type == HostUpdateType.ADD) { |
| 72 | for (IHostListener listener : hostListeners) { |
| 73 | listener.hostAdded(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | } |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 75 | } else if (type == HostUpdateType.DELETE) { |
| 76 | for (IHostListener listener : hostListeners) { |
| 77 | listener.hostRemoved(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 82 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public String getName() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 85 | return "hostmanager"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 90 | // We want link discovery to consume LLDP first otherwise we'll |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 91 | // end up reading bad host info from LLDP packets |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | return type == OFType.PACKET_IN && "linkdiscovery".equals(name); |
| 93 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 94 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 97 | return type == OFType.PACKET_IN && |
| 98 | ("proxyarpmanager".equals(name) || "onosforwarding".equals(name)); |
| 99 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 100 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 103 | if (msg.getType().equals(OFType.PACKET_IN) && |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 104 | (msg instanceof OFPacketIn)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | OFPacketIn pi = (OFPacketIn) msg; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 106 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 108 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 109 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | return processPacketIn(sw, pi, eth); |
| 111 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 112 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | return Command.CONTINUE; |
| 114 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 115 | |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 116 | // This "protected" modifier is for unit test. |
| 117 | // The above "receive" method couldn't be tested |
| 118 | // because of IFloodlightProviderService static final field. |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 119 | protected Command processPacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 120 | if (log.isTraceEnabled()) { |
| 121 | log.trace("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort()); |
| 122 | } |
| 123 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 124 | final Dpid dpid = new Dpid(sw.getId()); |
| 125 | final PortNumber portNum = new PortNumber(pi.getInPort()); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 126 | Long mac = eth.getSourceMAC().toLong(); |
| 127 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 128 | Host srcHost = |
| 129 | getSourceHostFromPacket(eth, dpid.value(), portNum.value()); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 130 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 131 | if (srcHost == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | return Command.STOP; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 133 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 134 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 135 | // If the switch port we try to attach a new host already has a link, |
| 136 | // then don't add the host |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 137 | // TODO We probably don't need to check this here, it should be done in |
| 138 | // the Topology module. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 139 | topology.acquireReadLock(); |
| 140 | try { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 141 | if (topology.getOutgoingLink(dpid, portNum) != null || |
| 142 | topology.getIncomingLink(dpid, portNum) != null) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 143 | log.debug("Not adding host {} as " + |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 144 | "there is a link on the port: dpid {} port {}", |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 145 | srcHost.getMacAddress(), dpid, portNum); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 146 | return Command.CONTINUE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | } |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 148 | } finally { |
| 149 | topology.releaseReadLock(); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 150 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 151 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 152 | addHost(mac, srcHost); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 153 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | if (log.isTraceEnabled()) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 155 | log.trace("Add host info: {}", srcHost); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 156 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 157 | return Command.CONTINUE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 159 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 160 | // Thread to delete hosts periodically. |
| 161 | // Remove all hosts from the map first and then finally delete hosts |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 162 | // from the DB. |
| 163 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 164 | // TODO This should be sharded based on host 'owner' (i.e. the instance |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 165 | // that owns the switch it is attached to). Currently any instance can |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 166 | // issue deletes for any host, which permits race conditions and could |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 167 | // cause the Topology replicas to diverge. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 168 | private class HostCleaner implements Runnable { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public void run() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 171 | log.debug("called HostCleaner"); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 172 | topology.acquireReadLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 173 | try { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 174 | Set<net.onrc.onos.core.topology.Host> deleteSet = new HashSet<>(); |
| 175 | for (net.onrc.onos.core.topology.Host host : topology.getHosts()) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 176 | long now = System.currentTimeMillis(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 177 | if ((now - host.getLastSeenTime() > agingMillisecConfig)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | if (log.isTraceEnabled()) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 179 | log.trace("Removing host info: mac {}, now {}, lastSeenTime {}, diff {}", |
| 180 | host.getMacAddress(), now, host.getLastSeenTime(), now - host.getLastSeenTime()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | } |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 182 | deleteSet.add(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 185 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 186 | for (net.onrc.onos.core.topology.Host host : deleteSet) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 187 | deleteHostByMac(host.getMacAddress()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | } |
| 189 | } catch (Exception e) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 190 | // Any exception thrown by the task will prevent the Executor |
| 191 | // from running the next iteration, so we need to catch and log |
| 192 | // all exceptions here. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 193 | log.error("Exception in host cleanup thread:", e); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 194 | } finally { |
| 195 | topology.releaseReadLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 199 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 200 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 201 | * Parse a host from an {@link Ethernet} packet. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | * |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 203 | * @param eth the packet to parse |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 204 | * @param swdpid the switch on which the packet arrived |
| 205 | * @param port the port on which the packet arrived |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 206 | * @return the host from the packet |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 207 | */ |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 208 | protected Host getSourceHostFromPacket(Ethernet eth, |
| 209 | long swdpid, long port) { |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 210 | MACAddress sourceMac = eth.getSourceMAC(); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 211 | |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 212 | // Ignore broadcast/multicast source |
| 213 | if (sourceMac.isBroadcast() || sourceMac.isBroadcast()) { |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 214 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 215 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 216 | |
| 217 | short vlan = eth.getVlanID(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 218 | return new Host(sourceMac, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 219 | ((vlan >= 0) ? vlan : null), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 220 | swdpid, |
| 221 | port, |
| 222 | new Date()); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 227 | List<Class<? extends IFloodlightService>> services = |
| 228 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 229 | services.add(IHostService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | return services; |
| 231 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 232 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 233 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 234 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 235 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 236 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 237 | impls.put(IHostService.class, this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 238 | return impls; |
| 239 | } |
| 240 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 241 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 243 | List<Class<? extends IFloodlightService>> dependencies = |
| 244 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 245 | dependencies.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 246 | dependencies.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 247 | return dependencies; |
| 248 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 249 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void init(FloodlightModuleContext context) |
| 252 | throws FloodlightModuleException { |
| 253 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 254 | hostListeners = new CopyOnWriteArrayList<IHostListener>(); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 255 | topologyService = context.getServiceImpl(ITopologyService.class); |
| 256 | topology = topologyService.getTopology(); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 257 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 258 | setHostManagerProperties(context); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 259 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 260 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public void startUp(FloodlightModuleContext context) { |
| 263 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 264 | EXECUTOR_SERVICE.scheduleAtFixedRate(new HostCleaner(), |
| 265 | HOST_CLEANING_INITIAL_DELAY, cleanupSecondConfig, TimeUnit.SECONDS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 266 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 267 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 268 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 269 | public void deleteHost(Host host) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 270 | floodlightProvider.publishUpdate( |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 271 | new HostUpdate(host, HostUpdateType.DELETE)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 273 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 274 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 275 | public void deleteHostByMac(MACAddress mac) { |
| 276 | Host deleteHost = null; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 277 | topology.acquireReadLock(); |
| 278 | try { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 279 | net.onrc.onos.core.topology.Host host = topology.getHostByMac(mac); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 280 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 281 | for (Port switchPort : host.getAttachmentPoints()) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 282 | // We don't handle vlan now and multiple attachment points. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 283 | deleteHost = new Host(host.getMacAddress(), |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 284 | null, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 285 | switchPort.getDpid().value(), |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame^] | 286 | switchPort.getNumber().value(), |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 287 | new Date(host.getLastSeenTime())); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } finally { |
| 291 | topology.releaseReadLock(); |
| 292 | } |
| 293 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 294 | if (deleteHost != null) { |
| 295 | deleteHost(deleteHost); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 296 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 300 | public void addHost(Long mac, Host host) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 301 | floodlightProvider.publishUpdate( |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 302 | new HostUpdate(host, HostUpdateType.ADD)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 306 | public void addHostListener(IHostListener listener) { |
| 307 | hostListeners.add(listener); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 311 | public void removeHostListener(IHostListener listener) { |
| 312 | hostListeners.remove(listener); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 313 | } |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 314 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 315 | private void setHostManagerProperties(FloodlightModuleContext context) { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 316 | Map<String, String> configOptions = context.getConfigParams(this); |
| 317 | String cleanupsec = configOptions.get("cleanupsec"); |
| 318 | String agingmsec = configOptions.get("agingmsec"); |
| 319 | if (cleanupsec != null) { |
| 320 | cleanupSecondConfig = Integer.parseInt(cleanupsec); |
| 321 | log.debug("CLEANUP_SECOND is set to {}", cleanupSecondConfig); |
| 322 | } |
| 323 | |
| 324 | if (agingmsec != null) { |
| 325 | agingMillisecConfig = Integer.parseInt(agingmsec); |
| 326 | log.debug("AGEING_MILLSEC is set to {}", agingMillisecConfig); |
| 327 | } |
| 328 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 329 | } |