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