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.ARP; |
| 31 | import net.onrc.onos.core.packet.DHCP; |
| 32 | import net.onrc.onos.core.packet.Ethernet; |
| 33 | import net.onrc.onos.core.packet.IPv4; |
| 34 | import net.onrc.onos.core.packet.UDP; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 36 | import net.onrc.onos.core.topology.NetworkGraph; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 37 | |
| 38 | import org.openflow.protocol.OFMessage; |
| 39 | import org.openflow.protocol.OFPacketIn; |
| 40 | import org.openflow.protocol.OFType; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 43 | |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 44 | public class OnosDeviceManager implements IFloodlightModule, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | IOFMessageListener, |
| 46 | IOnosDeviceService, |
| 47 | IEventChannelListener<Long, OnosDevice> { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 48 | private static final Logger log = LoggerFactory.getLogger(OnosDeviceManager.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | private static final int CLEANUP_SECOND = 60 * 60; |
| 50 | private static final int AGEING_MILLSEC = 60 * 60 * 1000; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | private CopyOnWriteArrayList<IOnosDeviceListener> deviceListeners; |
| 53 | private IFloodlightProviderService floodlightProvider; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 54 | private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | private IDatagridService datagrid; |
| 57 | private IEventChannel<Long, OnosDevice> eventChannel; |
| 58 | private static final String DEVICE_CHANNEL_NAME = "onos.device"; |
| 59 | private Map<Long, OnosDevice> mapDevice = new ConcurrentHashMap<Long, OnosDevice>(); |
| 60 | private INetworkGraphService networkGraphService; |
| 61 | private NetworkGraph networkGraph; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 62 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 63 | public enum OnosDeviceUpdateType { |
| 64 | ADD, DELETE, UPDATE; |
| 65 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 66 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | private class OnosDeviceUpdate implements IUpdate { |
| 68 | private OnosDevice device; |
| 69 | private OnosDeviceUpdateType type; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | public OnosDeviceUpdate(OnosDevice device, OnosDeviceUpdateType type) { |
| 72 | this.device = device; |
| 73 | this.type = type; |
| 74 | } |
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 | @Override |
| 77 | public void dispatch() { |
| 78 | if (type == OnosDeviceUpdateType.ADD) { |
| 79 | for (IOnosDeviceListener listener : deviceListeners) { |
| 80 | listener.onosDeviceAdded(device); |
| 81 | } |
| 82 | } else if (type == OnosDeviceUpdateType.DELETE) { |
| 83 | for (IOnosDeviceListener listener : deviceListeners) { |
| 84 | listener.onosDeviceRemoved(device); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 89 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public String getName() { |
| 92 | return "onosdevicemanager"; |
| 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 isCallbackOrderingPrereq(OFType type, String name) { |
| 97 | // We want link discovery to consume LLDP first otherwise we'll |
| 98 | // end up reading bad device info from LLDP packets |
| 99 | return type == OFType.PACKET_IN && "linkdiscovery".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 boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 104 | return type == OFType.PACKET_IN && |
| 105 | ("proxyarpmanager".equals(name) || "onosforwarding".equals(name)); |
| 106 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 107 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 110 | if (msg.getType().equals(OFType.PACKET_IN) && |
| 111 | (msg instanceof OFPacketIn)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | OFPacketIn pi = (OFPacketIn) msg; |
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 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 115 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | return processPacketIn(sw, pi, eth); |
| 118 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 119 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | return Command.CONTINUE; |
| 121 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 122 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | private Command processPacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
| 124 | long dpid = sw.getId(); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 125 | short portId = pi.getInPort(); |
| 126 | Long mac = eth.getSourceMAC().toLong(); |
| 127 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 128 | OnosDevice srcDevice = |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 129 | getSourceDeviceFromPacket(eth, dpid, portId); |
| 130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | if (srcDevice == null) { |
| 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 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 135 | //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] | 136 | OnosDevice exDev = mapDevice.get(mac); |
| 137 | if (exDev != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | if (exDev.equals(srcDevice)) { |
| 139 | //There is the same existing device. Update only ActiveSince time. |
| 140 | exDev.setLastSeenTimestamp(new Date()); |
| 141 | if (log.isTraceEnabled()) { |
| 142 | log.debug("In the datagrid, there is the same device." |
| 143 | + "Only update last seen time. dpid {}, port {}, mac {}, ip {}, lastSeenTime {}", |
| 144 | dpid, portId, srcDevice.getMacAddress(), srcDevice.getIpv4Address(), srcDevice.getLastSeenTimestamp().getTime()); |
| 145 | } |
| 146 | return Command.CONTINUE; |
| 147 | } else if (srcDevice.getIpv4Address() == null && |
| 148 | exDev.getSwitchDPID().equals(srcDevice.getSwitchDPID()) && |
| 149 | exDev.getSwitchPort() == srcDevice.getSwitchPort()) { |
| 150 | //Vlan should be handled based on the Onos spec. Until then, don't handle it. |
| 151 | //Device attachment point and mac address are the same |
| 152 | //but the packet does not have an ip address. |
| 153 | exDev.setLastSeenTimestamp(new Date()); |
| 154 | if (log.isTraceEnabled()) { |
| 155 | log.debug("In the datagrid, there is the same device with no ip." |
| 156 | + "Keep ip and update last seen time. dpid {}, port {}, mac {}, ip {} lastSeenTime {}", |
| 157 | dpid, portId, srcDevice.getMacAddress(), exDev.getIpv4Address(), srcDevice.getLastSeenTimestamp().getTime()); |
| 158 | } |
| 159 | return Command.CONTINUE; |
| 160 | } |
| 161 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 162 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 163 | //If the switch port we try to attach a new device already has a link, then stop adding device |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | if (networkGraph.getLink(dpid, (long) portId) != null) { |
| 165 | if (log.isTraceEnabled()) { |
| 166 | log.debug("Stop adding OnosDevice {} due to there is a link to: dpid {} port {}", |
| 167 | srcDevice.getMacAddress(), dpid, portId); |
| 168 | } |
| 169 | return Command.CONTINUE; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 170 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 171 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 172 | addOnosDevice(mac, srcDevice); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 173 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | if (log.isTraceEnabled()) { |
| 175 | log.debug("Add device info in the set. dpid {}, port {}, mac {}, ip {}, lastSeenTime {}", |
| 176 | dpid, portId, srcDevice.getMacAddress(), srcDevice.getIpv4Address(), srcDevice.getLastSeenTimestamp().getTime()); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 177 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 178 | return Command.CONTINUE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 179 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 180 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | //Thread to delete devices periodically. |
| 182 | //Remove all devices from the map first and then finally delete devices from the DB. |
| 183 | private class CleanDevice implements Runnable { |
| 184 | @Override |
| 185 | public void run() { |
| 186 | log.debug("called CleanDevice"); |
| 187 | try { |
| 188 | Set<OnosDevice> deleteSet = new HashSet<OnosDevice>(); |
| 189 | for (OnosDevice dev : mapDevice.values()) { |
| 190 | long now = new Date().getTime(); |
| 191 | if ((now - dev.getLastSeenTimestamp().getTime() > AGEING_MILLSEC)) { |
| 192 | if (log.isTraceEnabled()) { |
| 193 | log.debug("Remove device info in the datagrid. dpid {}, port {}, mac {}, ip {}, lastSeenTime {}, diff {}", |
| 194 | dev.getSwitchDPID(), dev.getSwitchPort(), dev.getMacAddress(), dev.getIpv4Address(), |
| 195 | dev.getLastSeenTimestamp().getTime(), now - dev.getLastSeenTimestamp().getTime()); |
| 196 | } |
| 197 | deleteSet.add(dev); |
| 198 | } |
| 199 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 200 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | for (OnosDevice dev : deleteSet) { |
| 202 | deleteOnosDevice(dev); |
| 203 | } |
| 204 | } catch (Exception e) { |
| 205 | log.error("Error:", e); |
| 206 | } |
| 207 | } |
| 208 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 209 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 210 | /** |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 211 | * Get IP address from packet if the packet is either an ARP |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 212 | * or a DHCP packet. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | * |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 214 | * @param eth |
| 215 | * @param dlAddr |
| 216 | * @return |
| 217 | */ |
| 218 | private int getSrcNwAddr(Ethernet eth, long dlAddr) { |
| 219 | if (eth.getPayload() instanceof ARP) { |
| 220 | ARP arp = (ARP) eth.getPayload(); |
| 221 | if ((arp.getProtocolType() == ARP.PROTO_TYPE_IP) && |
| 222 | (Ethernet.toLong(arp.getSenderHardwareAddress()) == dlAddr)) { |
| 223 | return IPv4.toIPv4Address(arp.getSenderProtocolAddress()); |
| 224 | } |
| 225 | } else if (eth.getPayload() instanceof IPv4) { |
| 226 | IPv4 ipv4 = (IPv4) eth.getPayload(); |
| 227 | if (ipv4.getPayload() instanceof UDP) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 228 | UDP udp = (UDP) ipv4.getPayload(); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 229 | if (udp.getPayload() instanceof DHCP) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | DHCP dhcp = (DHCP) udp.getPayload(); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 231 | if (dhcp.getOpCode() == DHCP.OPCODE_REPLY) { |
| 232 | return ipv4.getSourceAddress(); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Parse an entity from an {@link Ethernet} packet. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | * |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 243 | * @param eth the packet to parse |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 244 | * @param sw the switch on which the packet arrived |
| 245 | * @param pi the original packetin |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 246 | * @return the entity from the packet |
| 247 | */ |
| 248 | private OnosDevice getSourceDeviceFromPacket(Ethernet eth, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 249 | long swdpid, |
| 250 | short port) { |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 251 | byte[] dlAddrArr = eth.getSourceMACAddress(); |
| 252 | long dlAddr = Ethernet.toLong(dlAddrArr); |
| 253 | |
| 254 | // Ignore broadcast/multicast source |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 255 | if ((dlAddrArr[0] & 0x1) != 0) { |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 256 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 257 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 258 | |
| 259 | short vlan = eth.getVlanID(); |
| 260 | int nwSrc = getSrcNwAddr(eth, dlAddr); |
| 261 | return new OnosDevice(MACAddress.valueOf(dlAddr), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 262 | ((vlan >= 0) ? vlan : null), |
| 263 | ((nwSrc != 0) ? nwSrc : null), |
| 264 | swdpid, |
| 265 | port, |
| 266 | new Date()); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 269 | @Override |
| 270 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 271 | List<Class<? extends IFloodlightService>> services = |
| 272 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 273 | services.add(IOnosDeviceService.class); |
| 274 | return services; |
| 275 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 276 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 277 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 279 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 280 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 281 | impls.put(IOnosDeviceService.class, this); |
| 282 | return impls; |
| 283 | } |
| 284 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 285 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 286 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 287 | List<Class<? extends IFloodlightService>> dependencies = |
| 288 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 289 | dependencies.add(IFloodlightProviderService.class); |
| 290 | dependencies.add(INetworkGraphService.class); |
| 291 | dependencies.add(IDatagridService.class); |
| 292 | return dependencies; |
| 293 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 294 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public void init(FloodlightModuleContext context) |
| 297 | throws FloodlightModuleException { |
| 298 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 299 | EXECUTOR_SERVICE.scheduleAtFixedRate(new CleanDevice(), 30, CLEANUP_SECOND, TimeUnit.SECONDS); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 300 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 301 | deviceListeners = new CopyOnWriteArrayList<IOnosDeviceListener>(); |
| 302 | datagrid = context.getServiceImpl(IDatagridService.class); |
| 303 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
| 304 | networkGraph = networkGraphService.getNetworkGraph(); |
| 305 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 306 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 307 | @Override |
| 308 | public void startUp(FloodlightModuleContext context) { |
| 309 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 310 | eventChannel = datagrid.addListener(DEVICE_CHANNEL_NAME, this, |
| 311 | Long.class, |
| 312 | OnosDevice.class); |
| 313 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 314 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 315 | @Override |
| 316 | public void deleteOnosDevice(OnosDevice dev) { |
| 317 | Long mac = dev.getMacAddress().toLong(); |
| 318 | eventChannel.removeEntry(mac); |
| 319 | floodlightProvider.publishUpdate(new OnosDeviceUpdate(dev, OnosDeviceUpdateType.DELETE)); |
| 320 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 321 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 322 | @Override |
| 323 | public void deleteOnosDeviceByMac(MACAddress mac) { |
Pavlin Radoslavov | 6360779 | 2014-04-09 16:56:28 -0700 | [diff] [blame] | 324 | OnosDevice deleteDevice = mapDevice.get(mac.toLong()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 325 | deleteOnosDevice(deleteDevice); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void addOnosDevice(Long mac, OnosDevice dev) { |
| 330 | eventChannel.addEntry(mac, dev); |
| 331 | floodlightProvider.publishUpdate(new OnosDeviceUpdate(dev, OnosDeviceUpdateType.ADD)); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void entryAdded(OnosDevice dev) { |
| 336 | Long mac = dev.getMacAddress().toLong(); |
| 337 | mapDevice.put(mac, dev); |
| 338 | log.debug("Device added: device mac {}", mac); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void entryRemoved(OnosDevice dev) { |
| 343 | Long mac = dev.getMacAddress().toLong(); |
| 344 | mapDevice.remove(mac); |
| 345 | log.debug("Device removed: device mac {}", mac); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void entryUpdated(OnosDevice dev) { |
| 350 | Long mac = dev.getMacAddress().toLong(); |
| 351 | mapDevice.put(mac, dev); |
| 352 | log.debug("Device updated: device mac {}", mac); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void addOnosDeviceListener(IOnosDeviceListener listener) { |
| 357 | deviceListeners.add(listener); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void deleteOnosDeviceListener(IOnosDeviceListener listener) { |
| 362 | deviceListeners.remove(listener); |
| 363 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 364 | } |