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