Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.internal; |
| 2 | |
| 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.easymock.EasyMock.verify; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 7 | import static org.junit.Assert.assertEquals; |
| 8 | import static org.junit.Assert.assertNotSame; |
| 9 | import static org.junit.Assert.assertSame; |
| 10 | import static org.junit.Assert.assertTrue; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 11 | |
| 12 | import java.io.IOException; |
| 13 | import java.util.Collection; |
| 14 | import java.util.LinkedList; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 15 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 17 | import net.floodlightcontroller.core.IOFSwitch; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.core.internal.RoleChanger.RoleChangeTask; |
| 19 | |
| 20 | import org.easymock.EasyMock; |
| 21 | import org.jboss.netty.channel.Channel; |
| 22 | import org.junit.Before; |
HIGUCHI Yuta | 2f245d2 | 2013-06-28 10:24:34 -0700 | [diff] [blame] | 23 | import org.junit.Ignore; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | |
| 26 | public class RoleChangerTest { |
| 27 | public RoleChanger roleChanger; |
| 28 | |
| 29 | @Before |
| 30 | public void setUp() throws Exception { |
| 31 | roleChanger = new RoleChanger(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Send a role request for SLAVE to a switch that doesn't support it. |
| 36 | * The connection should be closed. |
| 37 | */ |
| 38 | @Test |
| 39 | public void testSendRoleRequestSlaveNotSupported() { |
| 40 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 41 | |
| 42 | // a switch that doesn't support role requests |
| 43 | OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); |
| 44 | Channel channel1 = createMock(Channel.class); |
| 45 | expect(sw1.getChannel()).andReturn(channel1); |
| 46 | // No support for NX_ROLE |
| 47 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 48 | .andReturn(false); |
| 49 | expect(channel1.close()).andReturn(null); |
| 50 | switches.add(sw1); |
| 51 | |
| 52 | replay(sw1, channel1); |
| 53 | roleChanger.sendRoleRequest(switches, Role.SLAVE, 123456); |
| 54 | verify(sw1, channel1); |
| 55 | |
| 56 | // sendRoleRequest needs to remove the switch from the list since |
| 57 | // it closed its connection |
| 58 | assertTrue(switches.isEmpty()); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Send a role request for MASTER to a switch that doesn't support it. |
| 63 | * The connection should be closed. |
| 64 | */ |
| 65 | @Test |
HIGUCHI Yuta | 2f245d2 | 2013-06-28 10:24:34 -0700 | [diff] [blame] | 66 | @Ignore // FIXME: ONOS modified the behavior here to intentionally trigger OFS error. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | public void testSendRoleRequestMasterNotSupported() { |
| 68 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 69 | |
| 70 | // a switch that doesn't support role requests |
| 71 | OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); |
| 72 | // No support for NX_ROLE |
| 73 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 74 | .andReturn(false); |
| 75 | switches.add(sw1); |
| 76 | |
| 77 | replay(sw1); |
| 78 | roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); |
| 79 | verify(sw1); |
| 80 | |
| 81 | assertEquals(1, switches.size()); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Send a role request a switch that supports it and one that |
| 86 | * hasn't had a role request send to it yet |
| 87 | */ |
| 88 | @Test |
| 89 | public void testSendRoleRequestErrorHandling () throws Exception { |
| 90 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 91 | |
| 92 | // a switch that supports role requests |
| 93 | OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); |
| 94 | // No support for NX_ROLE |
| 95 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 96 | .andReturn(true); |
| 97 | expect(sw1.sendNxRoleRequest(Role.MASTER, 123456)) |
| 98 | .andThrow(new IOException()).once(); |
| 99 | Channel channel1 = createMock(Channel.class); |
| 100 | expect(sw1.getChannel()).andReturn(channel1); |
| 101 | expect(channel1.close()).andReturn(null); |
| 102 | switches.add(sw1); |
| 103 | |
| 104 | replay(sw1); |
| 105 | roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); |
| 106 | verify(sw1); |
| 107 | |
| 108 | assertTrue(switches.isEmpty()); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check error handling |
| 113 | * hasn't had a role request send to it yet |
| 114 | */ |
| 115 | @Test |
| 116 | public void testSendRoleRequestSupported() throws Exception { |
| 117 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 118 | |
| 119 | // a switch that supports role requests |
| 120 | OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); |
| 121 | // No support for NX_ROLE |
| 122 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 123 | .andReturn(true); |
| 124 | expect(sw1.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once(); |
| 125 | switches.add(sw1); |
| 126 | |
| 127 | // a switch for which we don't have SUPPORTS_NX_ROLE yet |
| 128 | OFSwitchImpl sw2 = EasyMock.createMock(OFSwitchImpl.class); |
| 129 | // No support for NX_ROLE |
| 130 | expect(sw2.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 131 | .andReturn(null); |
| 132 | expect(sw2.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once(); |
| 133 | switches.add(sw2); |
| 134 | |
| 135 | |
| 136 | replay(sw1, sw2); |
| 137 | roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); |
| 138 | verify(sw1, sw2); |
| 139 | |
| 140 | assertEquals(2, switches.size()); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testVerifyRoleReplyReceived() { |
| 145 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 146 | |
| 147 | // Add a switch that has received a role reply |
| 148 | OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); |
| 149 | expect(sw1.checkFirstPendingRoleRequestCookie(123456)) |
| 150 | .andReturn(false).once(); |
| 151 | switches.add(sw1); |
| 152 | |
| 153 | // Add a switch that has not yet received a role reply |
| 154 | OFSwitchImpl sw2 = EasyMock.createMock(OFSwitchImpl.class); |
| 155 | expect(sw2.checkFirstPendingRoleRequestCookie(123456)) |
| 156 | .andReturn(true).once(); |
| 157 | Channel channel2 = createMock(Channel.class); |
| 158 | expect(sw2.getChannel()).andReturn(channel2); |
| 159 | expect(channel2.close()).andReturn(null); |
| 160 | switches.add(sw2); |
| 161 | |
| 162 | |
| 163 | replay(sw1, sw2); |
| 164 | roleChanger.verifyRoleReplyReceived(switches, 123456); |
| 165 | verify(sw1, sw2); |
| 166 | |
| 167 | assertEquals(2, switches.size()); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testRoleChangeTask() { |
| 172 | @SuppressWarnings("unchecked") |
| 173 | Collection<OFSwitchImpl> switches = |
| 174 | EasyMock.createMock(Collection.class); |
| 175 | long now = System.nanoTime(); |
| 176 | long dt1 = 10 * 1000*1000*1000L; |
| 177 | long dt2 = 20 * 1000*1000*1000L; |
| 178 | long dt3 = 15 * 1000*1000*1000L; |
| 179 | RoleChangeTask t1 = new RoleChangeTask(switches, null, now+dt1); |
| 180 | RoleChangeTask t2 = new RoleChangeTask(switches, null, now+dt2); |
| 181 | RoleChangeTask t3 = new RoleChangeTask(switches, null, now+dt3); |
| 182 | |
| 183 | // FIXME: cannot test comparison against self. grrr |
| 184 | //assertTrue( t1.compareTo(t1) <= 0 ); |
| 185 | assertTrue( t1.compareTo(t2) < 0 ); |
| 186 | assertTrue( t1.compareTo(t3) < 0 ); |
| 187 | |
| 188 | assertTrue( t2.compareTo(t1) > 0 ); |
| 189 | //assertTrue( t2.compareTo(t2) <= 0 ); |
| 190 | assertTrue( t2.compareTo(t3) > 0 ); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testSubmitRequest() throws Exception { |
| 195 | LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); |
| 196 | roleChanger.timeout = 500*1000*1000; // 500 ms |
| 197 | |
| 198 | // a switch that supports role requests |
| 199 | OFSwitchImpl sw1 = EasyMock.createStrictMock(OFSwitchImpl.class); |
| 200 | // No support for NX_ROLE |
| 201 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 202 | .andReturn(true); |
| 203 | expect(sw1.sendNxRoleRequest(EasyMock.same(Role.MASTER), EasyMock.anyLong())) |
| 204 | .andReturn(1); |
| 205 | expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 206 | .andReturn(true); |
| 207 | expect(sw1.sendNxRoleRequest(EasyMock.same(Role.SLAVE), EasyMock.anyLong())) |
| 208 | .andReturn(1); |
| 209 | // The following calls happen for timeout handling: |
| 210 | expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong())) |
| 211 | .andReturn(false); |
| 212 | expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong())) |
| 213 | .andReturn(false); |
| 214 | switches.add(sw1); |
| 215 | |
| 216 | |
| 217 | replay(sw1); |
| 218 | roleChanger.submitRequest(switches, Role.MASTER); |
| 219 | roleChanger.submitRequest(switches, Role.SLAVE); |
| 220 | // Wait until role request has been sent. |
| 221 | // TODO: need to get rid of this sleep somehow |
| 222 | Thread.sleep(100); |
| 223 | // Now there should be exactly one timeout task pending |
| 224 | assertEquals(2, roleChanger.pendingTasks.size()); |
| 225 | // Make sure it's indeed a timeout task |
| 226 | assertSame(RoleChanger.RoleChangeTask.Type.TIMEOUT, |
| 227 | roleChanger.pendingTasks.peek().type); |
| 228 | // Check that RoleChanger indeed made a copy of switches collection |
| 229 | assertNotSame(switches, roleChanger.pendingTasks.peek().switches); |
| 230 | |
| 231 | // Wait until the timeout triggers |
| 232 | // TODO: get rid of this sleep too. |
| 233 | Thread.sleep(500); |
| 234 | assertEquals(0, roleChanger.pendingTasks.size()); |
| 235 | verify(sw1); |
| 236 | |
| 237 | } |
| 238 | |
| 239 | } |