diff --git a/src/main/java/com/bigswitch/floodlight/vendor/OFBigSwitchVendorData.java b/src/main/java/com/bigswitch/floodlight/vendor/OFBigSwitchVendorData.java index e7d34258d077a9a6d13d6d7a928e664f23aaf18e..b5288f1381a5d5706a1813a4ff625fbcf94ffc3e 100644 --- a/src/main/java/com/bigswitch/floodlight/vendor/OFBigSwitchVendorData.java +++ b/src/main/java/com/bigswitch/floodlight/vendor/OFBigSwitchVendorData.java @@ -6,13 +6,13 @@ import org.openflow.protocol.vendor.OFVendorData; /** * Base class for vendor data corresponding to BigSwitch vendor extensions * BigSwitch vendor data always starts with a 4-byte integer data type value - * + * * @author Munish Mehta (munish.mehta@bigswitch.com) */ public class OFBigSwitchVendorData implements OFVendorData { - + public static final int BSN_VENDOR_ID = 0x005c16c7; - + /** * The value of the integer data type at the beginning of the vendor data */ @@ -26,7 +26,7 @@ public class OFBigSwitchVendorData implements OFVendorData { super(); this.dataType = dataType; } - + /** * Get the data type value at the beginning of the vendor data * @return @@ -74,4 +74,22 @@ public class OFBigSwitchVendorData implements OFVendorData { data.writeInt(dataType); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + dataType; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + OFBigSwitchVendorData other = (OFBigSwitchVendorData) obj; + if (dataType != other.dataType) return false; + return true; + } + } diff --git a/src/main/java/com/bigswitch/floodlight/vendor/OFBsnPktinSuppressionSetRequestVendorData.java b/src/main/java/com/bigswitch/floodlight/vendor/OFBsnPktinSuppressionSetRequestVendorData.java index 4f8a08e7624708560a34a0945b9d8a48e329d67d..da5c33db03397b80d540c6d1c71f2f37e774ac3b 100644 --- a/src/main/java/com/bigswitch/floodlight/vendor/OFBsnPktinSuppressionSetRequestVendorData.java +++ b/src/main/java/com/bigswitch/floodlight/vendor/OFBsnPktinSuppressionSetRequestVendorData.java @@ -121,4 +121,30 @@ public class OFBsnPktinSuppressionSetRequestVendorData data.writeShort(priority); data.writeLong(cookie); } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + (int) (cookie ^ (cookie >>> 32)); + result = prime * result + hardTimeout; + result = prime * result + idleTimeout; + result = prime * result + priority; + result = prime * result + (suppressionEnabled ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!super.equals(obj)) return false; + if (getClass() != obj.getClass()) return false; + OFBsnPktinSuppressionSetRequestVendorData other = (OFBsnPktinSuppressionSetRequestVendorData) obj; + if (cookie != other.cookie) return false; + if (hardTimeout != other.hardTimeout) return false; + if (idleTimeout != other.idleTimeout) return false; + if (priority != other.priority) return false; + if (suppressionEnabled != other.suppressionEnabled) return false; + return true; + } } diff --git a/src/test/java/com/bigswitch/floodlight/vendor/OFBsnPktinSupressionSetRequestVendorDataTest.java b/src/test/java/com/bigswitch/floodlight/vendor/OFBsnPktinSupressionSetRequestVendorDataTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2be162e1ca457c6f03e41ef03c9bbd84cb708f16 --- /dev/null +++ b/src/test/java/com/bigswitch/floodlight/vendor/OFBsnPktinSupressionSetRequestVendorDataTest.java @@ -0,0 +1,66 @@ +package com.bigswitch.floodlight.vendor; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class OFBsnPktinSupressionSetRequestVendorDataTest { + protected static byte[] expectedWireFormat = { + 0x00, 0x00, 0x00, 0x0b, // type == 11 + 0x01, // enabled + 0x00, // pad + 0x00, 0x5a, // idle timeout + (byte) 0xf0, (byte) 0xe0, // hard timeout + 0x12, 0x34, // priority + 0x33, 0x33, 0x66, 0x66, + 0x77, 0x77, (byte) 0x99, (byte) 0x99 // cookie + }; + + @Test + public void test() { + ChannelBuffer buf = ChannelBuffers.buffer(32); + + OFBsnPktinSuppressionSetRequestVendorData vendorData = + new OFBsnPktinSuppressionSetRequestVendorData( + true, + (short)0x5a, + (short)0xf0e0, + (short)0x1234, + 0x3333666677779999L); + assertEquals(11, vendorData.getDataType()); + + assertEquals(true, vendorData instanceof OFBigSwitchVendorData); + + vendorData.writeTo(buf); + + ChannelBuffer buf2 = buf.copy(); + assertEquals(20, buf.readableBytes()); + byte fromBuffer[] = new byte[20]; + buf.readBytes(fromBuffer); + assertArrayEquals(expectedWireFormat, fromBuffer); + + OFBsnPktinSuppressionSetRequestVendorData vendorData2 = + new OFBsnPktinSuppressionSetRequestVendorData(); + + assertEquals(11, vendorData2.getDataType()); + + vendorData2.setIdleTimeout((short)1); + assertEquals((short)1, vendorData2.getIdleTimeout()); + + vendorData2.setHardTimeout((short)2); + assertEquals((short)2, vendorData2.getHardTimeout()); + + vendorData2.setPriority((short)3); + assertEquals((short)3, vendorData2.getPriority()); + + vendorData2.setCookie(12345678901234L); + assertEquals(12345678901234L, vendorData2.getCookie()); + + vendorData2.readFrom(buf2, buf2.readableBytes()); + assertEquals(vendorData, vendorData2); + } + + +} diff --git a/src/test/java/net/floodlightcontroller/core/internal/OFSwitchImplTest.java b/src/test/java/net/floodlightcontroller/core/internal/OFSwitchImplTest.java index b8217b147c3eab234d78815dbb2b52dc941421a0..e59a9d060899e856b6564d48cbf8527c90316a8c 100644 --- a/src/test/java/net/floodlightcontroller/core/internal/OFSwitchImplTest.java +++ b/src/test/java/net/floodlightcontroller/core/internal/OFSwitchImplTest.java @@ -16,34 +16,67 @@ package net.floodlightcontroller.core.internal; +import net.floodlightcontroller.core.SwitchDriverSubHandshakeAlreadyStarted; +import net.floodlightcontroller.core.SwitchDriverSubHandshakeCompleted; +import net.floodlightcontroller.core.SwitchDriverSubHandshakeNotStarted; import net.floodlightcontroller.core.IFloodlightProviderService.Role; -import net.floodlightcontroller.test.FloodlightTestCase; import org.junit.Before; import org.junit.Test; +import org.openflow.protocol.OFMessage; +import org.openflow.protocol.OFType; +import org.openflow.protocol.factory.BasicFactory; -public class OFSwitchImplTest extends FloodlightTestCase { +import static org.junit.Assert.*; + +public class OFSwitchImplTest { protected OFSwitchImpl sw; - - - @Override + + @Before public void setUp() throws Exception { - super.setUp(); sw = new OFSwitchImpl(); - } - + } + @Test public void testSetHARoleReply() { sw.setHARole(Role.MASTER); assertEquals(Role.MASTER, sw.getHARole()); - + sw.setHARole(Role.EQUAL); assertEquals(Role.EQUAL, sw.getHARole()); - + sw.setHARole(Role.SLAVE); assertEquals(Role.SLAVE, sw.getHARole()); } - + + @Test + public void testSubHandshake() { + OFMessage m = BasicFactory.getInstance().getMessage(OFType.VENDOR); + // test execptions before handshake is started + try { + sw.processDriverHandshakeMessage(m); + fail("expected exception not thrown"); + } catch (SwitchDriverSubHandshakeNotStarted e) { /* expected */ } + try { + sw.isDriverHandshakeComplete(); + fail("expected exception not thrown"); + } catch (SwitchDriverSubHandshakeNotStarted e) { /* expected */ } + + // start the handshake -- it should immediately complete + sw.startDriverHandshake(); + assertTrue("Handshake should be complete", + sw.isDriverHandshakeComplete()); + + // test exceptions after handshake is completed + try { + sw.processDriverHandshakeMessage(m); + fail("expected exception not thrown"); + } catch (SwitchDriverSubHandshakeCompleted e) { /* expected */ } + try { + sw.startDriverHandshake(); + fail("Expected exception not thrown"); + } catch (SwitchDriverSubHandshakeAlreadyStarted e) { /* expected */ } + } }