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