Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import org.junit.Test; |
| 6 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 7 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 8 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 9 | import org.projectfloodlight.openflow.types.OFPort; |
| 10 | |
| 11 | /** |
| 12 | * Tests for {@link PortNumberUtils}. |
| 13 | */ |
| 14 | public class PortNumberUtilsTest { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Tests for {@link PortNumberUtils#of(OFVersion, int))}. |
| 19 | */ |
| 20 | @Test |
| 21 | public void testOfOFVersionInt() { |
| 22 | |
| 23 | assertEquals(PortNumber.uint32(0x1ffff), |
| 24 | PortNumberUtils.openFlow(OFVersion.OF_13, 0x1FFFF)); |
| 25 | |
| 26 | assertEquals(PortNumber.uint16((short) 0xabc), |
| 27 | PortNumberUtils.openFlow(OFVersion.OF_10, 0xabc)); |
| 28 | |
| 29 | try { |
| 30 | PortNumberUtils.openFlow(OFVersion.OF_10, 0x1FFFF); |
| 31 | fail("Should have thrown IllegalArgumentException"); |
| 32 | } catch (IllegalArgumentException e) { // CHECKSTYLE IGNORE THIS LINE |
| 33 | // should throw |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Tests for {@link PortNumberUtils#openFlow(OFPortDesc)}. |
| 39 | */ |
| 40 | @Test |
| 41 | public void testOfOFPortDesc() { |
| 42 | final OFPortDesc desc10 = OFFactories.getFactory(OFVersion.OF_10) |
| 43 | .buildPortDesc() |
| 44 | .setPortNo(OFPort.of(123)) |
| 45 | .build(); |
| 46 | |
| 47 | assertEquals(PortNumber.uint16((short) 123), |
| 48 | PortNumberUtils.openFlow(desc10)); |
| 49 | |
| 50 | final OFPortDesc desc13 = OFFactories.getFactory(OFVersion.OF_13) |
| 51 | .buildPortDesc() |
| 52 | .setPortNo(OFPort.of(0x1FFFF)) |
| 53 | .build(); |
| 54 | |
| 55 | assertEquals(PortNumber.uint32(0x1FFFF), |
| 56 | PortNumberUtils.openFlow(desc13)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests for {@link PortNumberUtils#toOF10(int)}. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testToOF10() { |
| 64 | assertEquals((short) 0, PortNumberUtils.toOF10(0)); |
| 65 | assertEquals((short) 1, PortNumberUtils.toOF10(1)); |
| 66 | assertEquals((short) 0xFF00, PortNumberUtils.toOF10(0xFF00)); |
| 67 | for (int i = 0xFF00 + 1; i < 0xFFf8; ++i) { |
| 68 | try { |
| 69 | PortNumberUtils.toOF10(i); |
| 70 | fail("Should have thrown IllegalArgumentException"); |
| 71 | } catch (IllegalArgumentException e) { // CHECKSTYLE IGNORE THIS LINE |
| 72 | // should throw |
| 73 | } |
| 74 | } |
| 75 | assertEquals((short) 0xFFf8, PortNumberUtils.toOF10(0xFFf8)); |
| 76 | assertEquals((short) 0xFFff, PortNumberUtils.toOF10(0xFFff)); |
| 77 | |
| 78 | |
| 79 | // OFPort#getPortNumber can return int value outside OF1.0 |
| 80 | // verifty that toOF10 converts them into valid OF1.0 range |
| 81 | assertEquals((short) 0xFF00, |
| 82 | PortNumberUtils.toOF10(OFPort.MAX.getPortNumber())); |
| 83 | assertEquals((short) 0xFFf8, |
| 84 | PortNumberUtils.toOF10(OFPort.IN_PORT.getPortNumber())); |
| 85 | assertEquals((short) 0xFFf9, |
| 86 | PortNumberUtils.toOF10(OFPort.TABLE.getPortNumber())); |
| 87 | assertEquals((short) 0xFFfa, |
| 88 | PortNumberUtils.toOF10(OFPort.NORMAL.getPortNumber())); |
| 89 | assertEquals((short) 0xFFfb, |
| 90 | PortNumberUtils.toOF10(OFPort.FLOOD.getPortNumber())); |
| 91 | assertEquals((short) 0xFFfc, |
| 92 | PortNumberUtils.toOF10(OFPort.ALL.getPortNumber())); |
| 93 | assertEquals((short) 0xFFfd, |
| 94 | PortNumberUtils.toOF10(OFPort.CONTROLLER.getPortNumber())); |
| 95 | assertEquals((short) 0xFFfe, |
| 96 | PortNumberUtils.toOF10(OFPort.LOCAL.getPortNumber())); |
| 97 | assertEquals((short) 0xFFff, // OFPP_NONE |
| 98 | PortNumberUtils.toOF10(OFPort.ANY.getPortNumber())); |
| 99 | } |
| 100 | } |