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