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