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