Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.hostmanager; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 2 | |
| 3 | import static org.easymock.EasyMock.anyObject; |
| 4 | import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.createNiceMock; |
| 6 | import static org.easymock.EasyMock.eq; |
| 7 | import static org.easymock.EasyMock.expect; |
| 8 | import static org.easymock.EasyMock.expectLastCall; |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 9 | import static org.easymock.EasyMock.isA; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 10 | import static org.easymock.EasyMock.replay; |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 11 | import static org.easymock.EasyMock.verify; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 12 | |
| 13 | import java.util.Date; |
| 14 | |
| 15 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 16 | import net.floodlightcontroller.core.IListener.Command; |
| 17 | import net.floodlightcontroller.core.IOFSwitch; |
| 18 | import net.floodlightcontroller.core.IUpdate; |
| 19 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.test.FloodlightTestCase; |
| 22 | import net.floodlightcontroller.util.MACAddress; |
| 23 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 24 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 25 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.packet.ARP; |
| 27 | import net.onrc.onos.core.packet.DHCP; |
| 28 | import net.onrc.onos.core.packet.Data; |
| 29 | import net.onrc.onos.core.packet.Ethernet; |
| 30 | import net.onrc.onos.core.packet.IPacket; |
| 31 | import net.onrc.onos.core.packet.IPv4; |
| 32 | import net.onrc.onos.core.packet.UDP; |
| 33 | import net.onrc.onos.core.registry.IControllerRegistryService; |
| 34 | import net.onrc.onos.core.topology.ITopologyListener; |
| 35 | import net.onrc.onos.core.topology.ITopologyService; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.topology.MockTopology; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.util.Dpid; |
| 38 | import net.onrc.onos.core.util.PortNumber; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 39 | |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 44 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 45 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 46 | import org.projectfloodlight.openflow.protocol.OFPacketInReason; |
| 47 | import org.projectfloodlight.openflow.protocol.OFType; |
| 48 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 49 | import org.projectfloodlight.openflow.types.OFPort; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 52 | * Unit tests for the Host Manager module (HostManger). |
| 53 | * These test cases check the result of add/delete host and |
| 54 | * verify the result of processPacketIn by injecting fake packets. |
| 55 | * floodlightProvider, datagridService, topologyService, |
| 56 | * controllerRegistryService, eventChannel are mocked out. |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 57 | */ |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 58 | public class HostManagerTest extends FloodlightTestCase { |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 59 | private IPacket pkt0, pkt1, pkt2, pkt3, pkt4; |
| 60 | private IOFSwitch sw1; |
| 61 | private long sw1Dpid; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 62 | private long sw1DevPort, sw1DevPort2; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 63 | private HostManager hostManager; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 64 | private OFPacketIn pktIn, pktIn2; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 65 | private FloodlightModuleContext modContext; |
| 66 | private ITopologyService networkGraphService; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 67 | private IEventChannel<Long, Host> eventChannel; |
| 68 | private IFloodlightProviderService floodlightProvider; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 69 | private Date lastSeenTimestamp; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 70 | private OFFactory ofact; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | @Before |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 74 | @SuppressWarnings("unchecked") |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 75 | public void setUp() throws Exception { |
| 76 | super.setUp(); |
| 77 | MockTopology topology = new MockTopology(); |
| 78 | IDatagridService datagridService; |
| 79 | IControllerRegistryService controllerRegistryService; |
| 80 | |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 81 | topology.createSampleTopology2(); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 82 | modContext = new FloodlightModuleContext(); |
| 83 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 84 | floodlightProvider = createMock(IFloodlightProviderService.class); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 85 | datagridService = createMock(IDatagridService.class); |
| 86 | networkGraphService = createMock(ITopologyService.class); |
| 87 | controllerRegistryService = createMock(IControllerRegistryService.class); |
| 88 | eventChannel = createMock(IEventChannel.class); |
| 89 | expect(networkGraphService.getTopology()).andReturn(topology).anyTimes(); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 90 | networkGraphService.addListener(anyObject(ITopologyListener.class), eq(false)); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 91 | expectLastCall(); |
| 92 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 93 | expect(datagridService.createChannel("onos.host", Long.class, Host.class)) |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 94 | .andReturn(eventChannel).once(); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 95 | expect(topology.getOutgoingLink(new Dpid(1L), PortNumber.uint16((short) 100))).andReturn(null).anyTimes(); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 96 | expect(datagridService.addListener( |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 97 | eq("onos.host"), |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 98 | anyObject(IEventChannelListener.class), |
| 99 | eq(Long.class), |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 100 | eq(Host.class))) |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 101 | .andReturn(eventChannel).once(); |
| 102 | |
| 103 | replay(datagridService); |
| 104 | replay(networkGraphService); |
| 105 | replay(controllerRegistryService); |
| 106 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 107 | modContext.addService(IDatagridService.class, datagridService); |
| 108 | modContext.addService(ITopologyService.class, networkGraphService); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 109 | modContext.addService(IFloodlightProviderService.class, floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 110 | modContext.getServiceImpl(IFloodlightProviderService.class); |
| 111 | sw1Dpid = 1L; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 112 | ofact = OFFactories.getFactory(OFVersion.OF_10); |
| 113 | |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 114 | sw1 = createMockSwitch(sw1Dpid); |
| 115 | replay(sw1); |
| 116 | |
| 117 | sw1DevPort = 100; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 118 | sw1DevPort2 = 12L; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 119 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 120 | hostManager = new HostManager(); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 121 | /* |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 122 | * Broadcast source address |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 123 | */ |
| 124 | this.pkt0 = new Ethernet() |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 125 | .setDestinationMACAddress("00:44:33:22:11:33") |
| 126 | .setSourceMACAddress("FF:FF:FF:FF:FF:FF") |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 127 | .setEtherType(Ethernet.TYPE_IPV4) |
| 128 | .setPayload( |
| 129 | new IPv4() |
| 130 | .setTtl((byte) 128) |
| 131 | .setSourceAddress("192.168.10.1") |
| 132 | .setDestinationAddress("192.168.255.255") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 133 | .setPayload(getUDP(new Data(new byte[]{0x01})))); |
| 134 | |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 135 | /* |
| 136 | * Normal IPv4 packet |
| 137 | */ |
| 138 | this.pkt1 = new Ethernet() |
| 139 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 140 | .setSourceMACAddress("00:44:33:22:11:00") |
| 141 | .setEtherType(Ethernet.TYPE_IPV4) |
| 142 | .setPayload( |
| 143 | new IPv4() |
| 144 | .setTtl((byte) 128) |
| 145 | .setSourceAddress("192.168.1.1") |
| 146 | .setDestinationAddress("192.168.1.2") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 147 | .setPayload(getUDP(new Data(new byte[]{0x01})))); |
| 148 | |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Same MAC header as pkt1,but not IP address set |
| 151 | */ |
| 152 | this.pkt2 = new Ethernet() |
| 153 | .setSourceMACAddress("00:44:33:22:11:01") |
| 154 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 155 | .setEtherType(Ethernet.TYPE_IPV4) |
| 156 | .setPayload( |
| 157 | new IPv4() |
| 158 | .setTtl((byte) 128) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 159 | .setPayload(getUDP(new Data(new byte[]{0x01})))); |
| 160 | |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 161 | /* |
| 162 | * DHCP packet |
| 163 | */ |
| 164 | this.pkt3 = new Ethernet() |
| 165 | .setSourceMACAddress("00:44:33:22:11:01") |
| 166 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 167 | .setEtherType(Ethernet.TYPE_IPV4) |
| 168 | .setPayload( |
| 169 | new IPv4() |
| 170 | .setTtl((byte) 128) |
| 171 | .setSourceAddress("192.168.1.1") |
| 172 | .setDestinationAddress("192.168.1.2") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 173 | .setPayload(getUDP( |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 174 | new DHCP() |
| 175 | .setOpCode(DHCP.OPCODE_REPLY) |
| 176 | .setHardwareType(DHCP.HWTYPE_ETHERNET) |
| 177 | .setHardwareAddressLength((byte) 6) |
| 178 | .setHops((byte) 0) |
| 179 | .setTransactionId(0x00003d1d) |
| 180 | .setSeconds((short) 0) |
| 181 | .setFlags((short) 0) |
| 182 | .setClientIPAddress(0) |
| 183 | .setYourIPAddress(0) |
| 184 | .setServerIPAddress(0) |
| 185 | .setGatewayIPAddress(0)))); |
| 186 | /* |
| 187 | * ARP packet |
| 188 | */ |
| 189 | this.pkt4 = new Ethernet() |
| 190 | .setSourceMACAddress("00:44:33:22:11:01") |
| 191 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 192 | .setEtherType(Ethernet.TYPE_ARP) |
| 193 | .setPayload( |
| 194 | new ARP() |
| 195 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 196 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 197 | .setHardwareAddressLength((byte) 6) |
| 198 | .setProtocolAddressLength((byte) 4) |
| 199 | .setOpCode(ARP.OP_REPLY) |
| 200 | .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01")) |
| 201 | .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1")) |
| 202 | .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55")) |
| 203 | .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2"))); |
| 204 | |
| 205 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 206 | this.pktIn = getPacketIn((short) sw1DevPort); |
| 207 | this.pktIn2 = getPacketIn((short) sw1DevPort2); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 208 | |
| 209 | lastSeenTimestamp = new Date(1); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | @After |
| 214 | public void tearDown() throws Exception { |
| 215 | } |
| 216 | |
| 217 | public IOFSwitch createMockSwitch(Long id) { |
| 218 | IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class); |
| 219 | expect(mockSwitch.getId()).andReturn(id).anyTimes(); |
| 220 | return mockSwitch; |
| 221 | } |
| 222 | |
| 223 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 224 | * Test set operation on lastSeenTimstamp field in Host. |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 225 | */ |
| 226 | @Test |
| 227 | public void testSetLastSeenTimestamp() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 228 | Ethernet eth = (Ethernet) pkt1; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 229 | Host srcHost = hostManager.getSourceHostFromPacket(eth, sw1Dpid, sw1DevPort); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 230 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 231 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 232 | isA(HostManager.class)); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 233 | srcHost.setLastSeenTimestamp(lastSeenTimestamp); |
| 234 | assertEquals(lastSeenTimestamp, srcHost.getLastSeenTimestamp()); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 235 | } |
| 236 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 237 | * test the functionality to get the source host from Packet header |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 238 | * information. |
| 239 | */ |
| 240 | @Test |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 241 | public void testGetSourceHostFromPacket() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 242 | byte[] address = new byte[] {0x00, 0x44, 0x33, 0x22, 0x11, 0x01}; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 243 | MACAddress srcMac = new MACAddress(address); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 244 | Host host1 = new Host(srcMac, |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 245 | null, |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 246 | sw1Dpid, |
| 247 | sw1DevPort, |
| 248 | null); |
| 249 | |
| 250 | /* |
| 251 | * test DHCP packet case |
| 252 | */ |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 253 | Ethernet eth = (Ethernet) pkt3; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 254 | Host host2 = hostManager.getSourceHostFromPacket(eth, sw1Dpid, sw1DevPort); |
| 255 | assertEquals(host1, host2); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * test ARP packet case |
| 259 | */ |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 260 | eth = (Ethernet) pkt4; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 261 | host2 = hostManager.getSourceHostFromPacket(eth, sw1Dpid, sw1DevPort); |
| 262 | assertEquals(host1, host2); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 266 | * This test will invoke addHost to add a new host through Packet pkt1. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 267 | * @throws FloodlightModuleException |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 268 | */ |
| 269 | @Test |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 270 | public void testProcessPacketInAddNewHost() throws FloodlightModuleException { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 271 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 272 | isA(HostManager.class)); |
| 273 | expectLastCall(); |
| 274 | floodlightProvider.publishUpdate(isA(IUpdate.class)); |
| 275 | expectLastCall(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 276 | replay(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 277 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 278 | hostManager.init(modContext); |
| 279 | hostManager.startUp(modContext); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 280 | Command cmd = hostManager.processPacketIn( |
| 281 | sw1, pktIn, (Ethernet) pkt1, (short) sw1DevPort); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 282 | assertEquals(Command.CONTINUE, cmd); |
| 283 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 284 | verify(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 288 | * Test ProcessPacket function. |
| 289 | * @throws FloodlightModuleException |
| 290 | */ |
| 291 | @Test |
| 292 | public void testProcessPacketInHasLink() throws FloodlightModuleException { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 293 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 294 | isA(HostManager.class)); |
| 295 | expectLastCall(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 296 | replay(floodlightProvider); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 297 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 298 | hostManager.init(modContext); |
| 299 | hostManager.startUp(modContext); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 300 | Command cmd = hostManager.processPacketIn( |
| 301 | sw1, pktIn2, (Ethernet) pkt1, (short) sw1DevPort2); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 302 | assertEquals(Command.CONTINUE, cmd); |
| 303 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 304 | verify(floodlightProvider); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Test return Command.STOP path in processPacketIn method by injecting a broadcast packet. |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 309 | */ |
| 310 | @Test |
| 311 | public void testProcessPacketInStop() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 312 | Command cmd = hostManager.processPacketIn( |
| 313 | sw1, pktIn, (Ethernet) pkt0, (short) sw1DevPort); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 314 | assertEquals(Command.STOP, cmd); |
| 315 | } |
| 316 | |
| 317 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 318 | * Test add a host from the information from packet. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 319 | * @throws FloodlightModuleException |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 320 | */ |
| 321 | @Test |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 322 | public void testAddHost() throws FloodlightModuleException { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 323 | Ethernet eth = (Ethernet) pkt1; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 324 | Long longmac = eth.getSourceMAC().toLong(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 325 | Host srcHost = hostManager.getSourceHostFromPacket(eth, sw1Dpid, sw1DevPort); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 326 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 327 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 328 | isA(HostManager.class)); |
| 329 | expectLastCall(); |
| 330 | floodlightProvider.publishUpdate(isA(IUpdate.class)); |
| 331 | expectLastCall(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 332 | replay(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 333 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 334 | hostManager.init(modContext); |
| 335 | hostManager.startUp(modContext); |
| 336 | hostManager.addHost(longmac, srcHost); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 337 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 338 | verify(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 342 | * Test delete a host. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 343 | * @throws FloodlightModuleException |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 344 | */ |
| 345 | @Test |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 346 | public void testDeleteHost() throws FloodlightModuleException { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 347 | Ethernet eth = (Ethernet) pkt1; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 348 | Host srcHost = hostManager.getSourceHostFromPacket(eth, sw1Dpid, sw1DevPort); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 349 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 350 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 351 | isA(HostManager.class)); |
| 352 | expectLastCall(); |
| 353 | floodlightProvider.publishUpdate(isA(IUpdate.class)); |
| 354 | expectLastCall(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 355 | replay(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 356 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 357 | hostManager.init(modContext); |
| 358 | hostManager.startUp(modContext); |
| 359 | hostManager.deleteHost(srcHost); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 360 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 361 | verify(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 365 | * Test delete a host by using its source mac address. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 366 | * @throws FloodlightModuleException |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 367 | */ |
| 368 | @Test |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 369 | public void testDeleteHostByMac() throws FloodlightModuleException { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 370 | Ethernet eth = (Ethernet) pkt1; |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 371 | MACAddress mac = eth.getSourceMAC(); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 372 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 373 | floodlightProvider.addOFMessageListener(eq(OFType.PACKET_IN), |
| 374 | isA(HostManager.class)); |
| 375 | expectLastCall(); |
| 376 | floodlightProvider.publishUpdate(isA(IUpdate.class)); |
| 377 | expectLastCall(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 378 | replay(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 379 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 380 | hostManager.init(modContext); |
| 381 | hostManager.startUp(modContext); |
| 382 | hostManager.deleteHostByMac(mac); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 383 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 384 | verify(floodlightProvider); |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 385 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 386 | |
| 387 | /** |
| 388 | * Helper for building PacketIns. Defaults to a flowtable miss. |
| 389 | * @param inport the inport field value |
| 390 | * @return a PacketIn |
| 391 | */ |
| 392 | private OFPacketIn getPacketIn(int inport) { |
| 393 | return ofact.buildPacketIn() |
| 394 | .setInPort(OFPort.of(inport)) |
| 395 | .setReason(OFPacketInReason.NO_MATCH) |
| 396 | .build(); |
| 397 | } |
| 398 | |
| 399 | private UDP getUDP(IPacket payload) { |
| 400 | return (UDP) new UDP() |
| 401 | .setSourcePort((short) 5000) |
| 402 | .setDestinationPort((short) 5001) |
| 403 | .setPayload(payload); |
| 404 | } |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 405 | } |