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