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