blob: 152d2f0e9966bb658a04b3d276e993405dd9bab7 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.core.internal;
2
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.easymock.EasyMock.capture;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.expect;
6import static org.easymock.EasyMock.replay;
7import static org.easymock.EasyMock.reset;
8import static org.easymock.EasyMock.verify;
9
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080010import java.net.InetSocketAddress;
11import java.net.SocketAddress;
12import java.util.List;
13
14import net.floodlightcontroller.core.IFloodlightProviderService.Role;
15import net.floodlightcontroller.core.IOFSwitch;
16import net.floodlightcontroller.core.internal.OFSwitchImpl.PendingRoleRequestEntry;
17import net.floodlightcontroller.core.test.MockFloodlightProvider;
18import net.floodlightcontroller.test.FloodlightTestCase;
19
20import org.easymock.Capture;
21import org.jboss.netty.channel.Channel;
22import org.junit.Before;
23import org.junit.Test;
24import org.openflow.protocol.OFMessage;
25import org.openflow.protocol.OFType;
26import org.openflow.protocol.OFVendor;
27import org.openflow.protocol.vendor.OFVendorData;
28import org.openflow.vendor.nicira.OFNiciraVendorData;
29import org.openflow.vendor.nicira.OFRoleRequestVendorData;
30import org.openflow.vendor.nicira.OFRoleVendorData;
31
32public class OFSwitchImplTest extends FloodlightTestCase {
33 protected OFSwitchImpl sw;
34
35
36 @Before
37 public void setUp() throws Exception {
38 sw = new OFSwitchImpl();
39 Channel ch = createMock(Channel.class);
40 SocketAddress sa = new InetSocketAddress(42);
41 expect(ch.getRemoteAddress()).andReturn(sa).anyTimes();
42 sw.setChannel(ch);
43 MockFloodlightProvider floodlightProvider = new MockFloodlightProvider();
44 sw.setFloodlightProvider(floodlightProvider);
45 }
46
47
48 public void doSendNxRoleRequest(Role role, int nx_role) throws Exception {
49 long cookie = System.nanoTime();
50
51 // verify that the correct OFMessage is sent
52 Capture<List<OFMessage>> msgCapture = new Capture<List<OFMessage>>();
53 expect(sw.channel.write(capture(msgCapture))).andReturn(null);
54 replay(sw.channel);
55 int xid = sw.sendNxRoleRequest(role, cookie);
56 verify(sw.channel);
57 List<OFMessage> msgList = msgCapture.getValue();
58 assertEquals(1, msgList.size());
59 OFMessage msg = msgList.get(0);
60 assertEquals("Transaction Ids must match", xid, msg.getXid());
61 assertTrue("Message must be an OFVendor type", msg instanceof OFVendor);
62 assertEquals(OFType.VENDOR, msg.getType());
63 OFVendor vendorMsg = (OFVendor)msg;
64 assertEquals("Vendor message must be vendor Nicira",
65 OFNiciraVendorData.NX_VENDOR_ID, vendorMsg.getVendor());
66 OFVendorData vendorData = vendorMsg.getVendorData();
67 assertTrue("Vendor Data must be an OFRoleRequestVendorData",
68 vendorData instanceof OFRoleRequestVendorData);
69 OFRoleRequestVendorData roleRequest = (OFRoleRequestVendorData)vendorData;
70 assertEquals(nx_role, roleRequest.getRole());
71
72 // Now verify that we've added the pending request correctly
73 // to the pending queue
74 assertEquals(1, sw.pendingRoleRequests.size());
75 PendingRoleRequestEntry pendingRoleRequest = sw.pendingRoleRequests.poll();
76 assertEquals(msg.getXid(), pendingRoleRequest.xid);
77 assertEquals(role, pendingRoleRequest.role);
78 assertEquals(cookie, pendingRoleRequest.cookie);
79 reset(sw.channel);
80 }
81
82 @Test
83 public void testSendNxRoleRequest() throws Exception {
84 doSendNxRoleRequest(Role.MASTER, OFRoleVendorData.NX_ROLE_MASTER);
85 doSendNxRoleRequest(Role.SLAVE, OFRoleVendorData.NX_ROLE_SLAVE);
86 doSendNxRoleRequest(Role.EQUAL, OFRoleVendorData.NX_ROLE_OTHER);
87 }
88
89
90 @Test
91 public void testDeliverRoleReplyOk() {
92 // test normal case
93 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
94 (int)System.currentTimeMillis(), // arbitrary xid
95 Role.MASTER,
96 System.nanoTime() // arbitrary cookie
97 );
98 sw.pendingRoleRequests.add(pending);
99 replay(sw.channel);
100 sw.deliverRoleReply(pending.xid, pending.role);
101 verify(sw.channel);
102 assertEquals(true, sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE));
103 assertEquals(pending.role, sw.role);
104 assertEquals(0, sw.pendingRoleRequests.size());
105 }
106
107 @Test
108 public void testDeliverRoleReplyOkRepeated() {
109 // test normal case. Not the first role reply
110 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
111 (int)System.currentTimeMillis(), // arbitrary xid
112 Role.MASTER,
113 System.nanoTime() // arbitrary cookie
114 );
115 sw.setAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE, true);
116 sw.pendingRoleRequests.add(pending);
117 replay(sw.channel);
118 sw.deliverRoleReply(pending.xid, pending.role);
119 verify(sw.channel);
120 assertEquals(true, sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE));
121 assertEquals(pending.role, sw.role);
122 assertEquals(0, sw.pendingRoleRequests.size());
123 }
124
125 @Test
126 public void testDeliverRoleReplyNonePending() {
127 // nothing pending
128 expect(sw.channel.close()).andReturn(null);
129 replay(sw.channel);
130 sw.deliverRoleReply(1, Role.MASTER);
131 verify(sw.channel);
132 assertEquals(0, sw.pendingRoleRequests.size());
133 }
134
135 @Test
136 public void testDeliverRoleReplyWrongXid() {
137 // wrong xid received
138 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
139 (int)System.currentTimeMillis(), // arbitrary xid
140 Role.MASTER,
141 System.nanoTime() // arbitrary cookie
142 );
143 sw.pendingRoleRequests.add(pending);
144 expect(sw.channel.close()).andReturn(null);
145 replay(sw.channel);
146 sw.deliverRoleReply(pending.xid+1, pending.role);
147 verify(sw.channel);
148 assertEquals(null, sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE));
149 assertEquals(0, sw.pendingRoleRequests.size());
150 }
151
152 @Test
153 public void testDeliverRoleReplyWrongRole() {
154 // correct xid but incorrect role received
155 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
156 (int)System.currentTimeMillis(), // arbitrary xid
157 Role.MASTER,
158 System.nanoTime() // arbitrary cookie
159 );
160 sw.pendingRoleRequests.add(pending);
161 expect(sw.channel.close()).andReturn(null);
162 replay(sw.channel);
163 sw.deliverRoleReply(pending.xid, Role.SLAVE);
164 verify(sw.channel);
165 assertEquals(null, sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE));
166 assertEquals(0, sw.pendingRoleRequests.size());
167 }
168
169 @Test
170 public void testCheckFirstPendingRoleRequestXid() {
171 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
172 54321, Role.MASTER, 232323);
173 replay(sw.channel); // we don't expect any invocations
174 sw.pendingRoleRequests.add(pending);
175 assertEquals(true, sw.checkFirstPendingRoleRequestXid(54321));
176 assertEquals(false, sw.checkFirstPendingRoleRequestXid(0));
177 sw.pendingRoleRequests.clear();
178 assertEquals(false, sw.checkFirstPendingRoleRequestXid(54321));
179 verify(sw.channel);
180 }
181
182 @Test
183 public void testCheckFirstPendingRoleRequestCookie() {
184 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
185 54321, Role.MASTER, 232323);
186 replay(sw.channel); // we don't expect any invocations
187 sw.pendingRoleRequests.add(pending);
188 assertEquals(true, sw.checkFirstPendingRoleRequestCookie(232323));
189 assertEquals(false, sw.checkFirstPendingRoleRequestCookie(0));
190 sw.pendingRoleRequests.clear();
191 assertEquals(false, sw.checkFirstPendingRoleRequestCookie(232323));
192 verify(sw.channel);
193 }
194
195 @Test
196 public void testDeliverRoleRequestNotSupported () {
197 // normal case. xid is pending
198 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
199 (int)System.currentTimeMillis(), // arbitrary xid
200 Role.MASTER,
201 System.nanoTime() // arbitrary cookie
202 );
203 sw.role = Role.SLAVE;
204 sw.pendingRoleRequests.add(pending);
205 replay(sw.channel);
HIGUCHI Yutaeae374d2013-06-17 10:39:42 -0700206 sw.deliverRoleRequestNotSupportedEx(pending.xid);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800207 verify(sw.channel);
208 assertEquals(false, sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE));
209 assertEquals(null, sw.role);
210 assertEquals(0, sw.pendingRoleRequests.size());
211 }
212
213 @Test
214 public void testDeliverRoleRequestNotSupportedNonePending() {
215 // nothing pending
216 sw.role = Role.SLAVE;
217 expect(sw.channel.close()).andReturn(null);
218 replay(sw.channel);
HIGUCHI Yutaeae374d2013-06-17 10:39:42 -0700219 sw.deliverRoleRequestNotSupportedEx(1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800220 verify(sw.channel);
221 assertEquals(null, sw.role);
222 assertEquals(0, sw.pendingRoleRequests.size());
223 }
224
225 @Test
226 public void testDeliverRoleRequestNotSupportedWrongXid() {
227 // wrong xid received
228 PendingRoleRequestEntry pending = new PendingRoleRequestEntry(
229 (int)System.currentTimeMillis(), // arbitrary xid
230 Role.MASTER,
231 System.nanoTime() // arbitrary cookie
232 );
233 sw.role = Role.SLAVE;
234 sw.pendingRoleRequests.add(pending);
235 expect(sw.channel.close()).andReturn(null);
236 replay(sw.channel);
HIGUCHI Yutaeae374d2013-06-17 10:39:42 -0700237 sw.deliverRoleRequestNotSupportedEx(pending.xid+1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800238 verify(sw.channel);
239 assertEquals(null, sw.role);
240 assertEquals(0, sw.pendingRoleRequests.size());
241 }
242}