blob: 907866533dafb34af7fede5c7063668ad80ea5d3 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.core.internal;
2
3import static org.easymock.EasyMock.createMock;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.replay;
6import static org.easymock.EasyMock.verify;
Jonathan Harta88fd242014-04-03 11:24:54 -07007import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertNotSame;
9import static org.junit.Assert.assertSame;
10import static org.junit.Assert.assertTrue;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080011
12import java.io.IOException;
13import java.util.Collection;
14import java.util.LinkedList;
Jonathan Harta88fd242014-04-03 11:24:54 -070015
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080016import net.floodlightcontroller.core.IFloodlightProviderService.Role;
Jonathan Harta88fd242014-04-03 11:24:54 -070017import net.floodlightcontroller.core.IOFSwitch;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080018import net.floodlightcontroller.core.internal.RoleChanger.RoleChangeTask;
19
20import org.easymock.EasyMock;
21import org.jboss.netty.channel.Channel;
22import org.junit.Before;
HIGUCHI Yuta2f245d22013-06-28 10:24:34 -070023import org.junit.Ignore;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024import org.junit.Test;
25
26public class RoleChangerTest {
27 public RoleChanger roleChanger;
Ray Milkey269ffb92014-04-03 14:43:30 -070028
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029 @Before
30 public void setUp() throws Exception {
31 roleChanger = new RoleChanger();
32 }
Ray Milkey269ffb92014-04-03 14:43:30 -070033
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070035 * Send a role request for SLAVE to a switch that doesn't support it.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 * The connection should be closed.
37 */
38 @Test
39 public void testSendRoleRequestSlaveNotSupported() {
40 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -070041
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 // 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))
Ray Milkey269ffb92014-04-03 14:43:30 -070048 .andReturn(false);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049 expect(channel1.close()).andReturn(null);
50 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -070051
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 replay(sw1, channel1);
53 roleChanger.sendRoleRequest(switches, Role.SLAVE, 123456);
54 verify(sw1, channel1);
Ray Milkey269ffb92014-04-03 14:43:30 -070055
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 // sendRoleRequest needs to remove the switch from the list since
57 // it closed its connection
58 assertTrue(switches.isEmpty());
59 }
Ray Milkey269ffb92014-04-03 14:43:30 -070060
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080061 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070062 * Send a role request for MASTER to a switch that doesn't support it.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 * The connection should be closed.
64 */
65 @Test
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @Ignore
67 // FIXME: ONOS modified the behavior here to intentionally trigger OFS error.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 public void testSendRoleRequestMasterNotSupported() {
69 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -070070
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071 // a switch that doesn't support role requests
72 OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class);
73 // No support for NX_ROLE
74 expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -070075 .andReturn(false);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -070077
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 replay(sw1);
79 roleChanger.sendRoleRequest(switches, Role.MASTER, 123456);
80 verify(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -070081
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082 assertEquals(1, switches.size());
83 }
Ray Milkey269ffb92014-04-03 14:43:30 -070084
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070086 * Send a role request a switch that supports it and one that
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080087 * hasn't had a role request send to it yet
88 */
89 @Test
Ray Milkey269ffb92014-04-03 14:43:30 -070090 public void testSendRoleRequestErrorHandling() throws Exception {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -070092
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 // a switch that supports role requests
94 OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class);
95 // No support for NX_ROLE
96 expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -070097 .andReturn(true);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080098 expect(sw1.sendNxRoleRequest(Role.MASTER, 123456))
Ray Milkey269ffb92014-04-03 14:43:30 -070099 .andThrow(new IOException()).once();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800100 Channel channel1 = createMock(Channel.class);
101 expect(sw1.getChannel()).andReturn(channel1);
102 expect(channel1.close()).andReturn(null);
103 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700104
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 replay(sw1);
106 roleChanger.sendRoleRequest(switches, Role.MASTER, 123456);
107 verify(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700108
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 assertTrue(switches.isEmpty());
110 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700111
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800112 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 * Check error handling
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 * hasn't had a role request send to it yet
115 */
116 @Test
117 public void testSendRoleRequestSupported() throws Exception {
118 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700119
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800120 // a switch that supports role requests
121 OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class);
122 // No support for NX_ROLE
123 expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 .andReturn(true);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800125 expect(sw1.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once();
126 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700127
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800128 // a switch for which we don't have SUPPORTS_NX_ROLE yet
129 OFSwitchImpl sw2 = EasyMock.createMock(OFSwitchImpl.class);
130 // No support for NX_ROLE
131 expect(sw2.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 .andReturn(null);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 expect(sw2.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once();
134 switches.add(sw2);
Ray Milkey269ffb92014-04-03 14:43:30 -0700135
136
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800137 replay(sw1, sw2);
138 roleChanger.sendRoleRequest(switches, Role.MASTER, 123456);
139 verify(sw1, sw2);
Ray Milkey269ffb92014-04-03 14:43:30 -0700140
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800141 assertEquals(2, switches.size());
142 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700143
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 @Test
145 public void testVerifyRoleReplyReceived() {
146 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700147
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800148 // Add a switch that has received a role reply
149 OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class);
150 expect(sw1.checkFirstPendingRoleRequestCookie(123456))
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 .andReturn(false).once();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800152 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700153
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800154 // Add a switch that has not yet received a role reply
155 OFSwitchImpl sw2 = EasyMock.createMock(OFSwitchImpl.class);
156 expect(sw2.checkFirstPendingRoleRequestCookie(123456))
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 .andReturn(true).once();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158 Channel channel2 = createMock(Channel.class);
159 expect(sw2.getChannel()).andReturn(channel2);
160 expect(channel2.close()).andReturn(null);
161 switches.add(sw2);
Ray Milkey269ffb92014-04-03 14:43:30 -0700162
163
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 replay(sw1, sw2);
165 roleChanger.verifyRoleReplyReceived(switches, 123456);
166 verify(sw1, sw2);
Ray Milkey269ffb92014-04-03 14:43:30 -0700167
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800168 assertEquals(2, switches.size());
169 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700170
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800171 @Test
172 public void testRoleChangeTask() {
173 @SuppressWarnings("unchecked")
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 Collection<OFSwitchImpl> switches =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 EasyMock.createMock(Collection.class);
176 long now = System.nanoTime();
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 long dt1 = 10 * 1000 * 1000 * 1000L;
178 long dt2 = 20 * 1000 * 1000 * 1000L;
179 long dt3 = 15 * 1000 * 1000 * 1000L;
180 RoleChangeTask t1 = new RoleChangeTask(switches, null, now + dt1);
181 RoleChangeTask t2 = new RoleChangeTask(switches, null, now + dt2);
182 RoleChangeTask t3 = new RoleChangeTask(switches, null, now + dt3);
183
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800184 // FIXME: cannot test comparison against self. grrr
185 //assertTrue( t1.compareTo(t1) <= 0 );
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 assertTrue(t1.compareTo(t2) < 0);
187 assertTrue(t1.compareTo(t3) < 0);
188
189 assertTrue(t2.compareTo(t1) > 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 //assertTrue( t2.compareTo(t2) <= 0 );
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 assertTrue(t2.compareTo(t3) > 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800192 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700193
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800194 @Test
195 public void testSubmitRequest() throws Exception {
196 LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 roleChanger.timeout = 500 * 1000 * 1000; // 500 ms
198
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 // a switch that supports role requests
200 OFSwitchImpl sw1 = EasyMock.createStrictMock(OFSwitchImpl.class);
201 // No support for NX_ROLE
202 expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -0700203 .andReturn(true);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800204 expect(sw1.sendNxRoleRequest(EasyMock.same(Role.MASTER), EasyMock.anyLong()))
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 .andReturn(1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800206 expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE))
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 .andReturn(true);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800208 expect(sw1.sendNxRoleRequest(EasyMock.same(Role.SLAVE), EasyMock.anyLong()))
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 .andReturn(1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800210 // The following calls happen for timeout handling:
211 expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong()))
Ray Milkey269ffb92014-04-03 14:43:30 -0700212 .andReturn(false);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800213 expect(sw1.checkFirstPendingRoleRequestCookie(EasyMock.anyLong()))
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 .andReturn(false);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800215 switches.add(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700216
217
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800218 replay(sw1);
219 roleChanger.submitRequest(switches, Role.MASTER);
220 roleChanger.submitRequest(switches, Role.SLAVE);
221 // Wait until role request has been sent.
222 // TODO: need to get rid of this sleep somehow
223 Thread.sleep(100);
224 // Now there should be exactly one timeout task pending
225 assertEquals(2, roleChanger.pendingTasks.size());
226 // Make sure it's indeed a timeout task
Ray Milkey269ffb92014-04-03 14:43:30 -0700227 assertSame(RoleChanger.RoleChangeTask.Type.TIMEOUT,
228 roleChanger.pendingTasks.peek().type);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800229 // Check that RoleChanger indeed made a copy of switches collection
230 assertNotSame(switches, roleChanger.pendingTasks.peek().switches);
Ray Milkey269ffb92014-04-03 14:43:30 -0700231
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800232 // Wait until the timeout triggers
233 // TODO: get rid of this sleep too.
234 Thread.sleep(500);
235 assertEquals(0, roleChanger.pendingTasks.size());
236 verify(sw1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700237
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800238 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700239
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800240}