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