1 package org.opendaylight.openflowplugin.openflow.md.util;
3 import java.util.HashMap;
6 import org.junit.AfterClass;
7 import org.junit.Assert;
8 import org.junit.BeforeClass;
10 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.CommonPort;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
16 * @author: Kamal Rameshan (kramesha@cisco.com)
19 public class OpenflowPortsUtilTest {
20 private static Map<String, Long> mapOF10Ports;
21 private static Map<String, Long> mapOF13Ports;
22 private static Map<OpenflowVersion, Map<String, Long>> mapVersionToPorts;
25 * initiation before testing - once for all
28 public static void setupClass() {
29 OpenflowPortsUtil.init();
31 mapOF10Ports = new HashMap<String, Long>();
32 mapOF10Ports.put(OutputPortValues.MAX.toString(), 65280L);
33 mapOF10Ports.put(OutputPortValues.INPORT.toString(), 65528L);
34 mapOF10Ports.put(OutputPortValues.TABLE.toString(), 65529L);
35 mapOF10Ports.put(OutputPortValues.NORMAL.toString(), 65530L);
36 mapOF10Ports.put(OutputPortValues.FLOOD.toString(), 65531L);
37 mapOF10Ports.put(OutputPortValues.ALL.toString(), 65532L);
38 mapOF10Ports.put(OutputPortValues.CONTROLLER.toString(), 65533L);
39 mapOF10Ports.put(OutputPortValues.LOCAL.toString(), 65534L);
40 mapOF10Ports.put(OutputPortValues.NONE.toString(), 65535L);
42 mapOF13Ports = new HashMap<String, Long>();
43 mapOF13Ports.put(OutputPortValues.MAX.toString(), 4294967040L);
44 mapOF13Ports.put(OutputPortValues.INPORT.toString(), 4294967288L);
45 mapOF13Ports.put(OutputPortValues.TABLE.toString(), 4294967289L);
46 mapOF13Ports.put(OutputPortValues.NORMAL.toString(), 4294967290L);
47 mapOF13Ports.put(OutputPortValues.FLOOD.toString(), 4294967291L);
48 mapOF13Ports.put(OutputPortValues.ALL.toString(), 4294967292L);
49 mapOF13Ports.put(OutputPortValues.CONTROLLER.toString(), 4294967293L);
50 mapOF13Ports.put(OutputPortValues.LOCAL.toString(), 4294967294L);
51 mapOF13Ports.put(OutputPortValues.ANY.toString(), 4294967295L);
53 mapVersionToPorts = new HashMap<OpenflowVersion, Map<String, Long>>();
54 mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
55 mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
60 * tearing down initiated values after all tests done
63 public static void tearDownClass() {
64 OpenflowPortsUtil.close();
67 mapVersionToPorts.clear();
71 private static void matchGetLogicalName(OpenflowVersion version, String logicalName) {
72 Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
74 OpenflowPortsUtil.getPortLogicalName(version, mapVersionToPorts.get(version).get(logicalName)));
78 private static void matchGetPortfromLogicalName(OpenflowVersion version, String logicalName) {
79 Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
80 mapVersionToPorts.get(version).get(logicalName), OpenflowPortsUtil.getPortFromLogicalName(version, logicalName));
84 * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}
87 public void testGetPortLogicalName() {
89 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
90 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
91 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
92 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
93 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
94 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
95 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
96 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
97 matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
99 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
100 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
101 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
102 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
103 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
104 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
105 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
106 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
107 matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
109 Assert.assertNull("Invalid port number should return a null",
110 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
112 Assert.assertNull("Invalid port number should return a null",
113 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
118 * test for method {@link OpenflowPortsUtil#getPortFromLogicalName(OpenflowVersion, String)}
121 public void testGetPortFromLogicalName() {
123 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
124 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
125 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
126 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
127 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
128 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
129 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
130 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
131 matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
133 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
134 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
135 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
136 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
137 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
138 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
139 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
140 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
141 matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
143 Assert.assertNull("Invalid port logical name should return a null",
144 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
146 Assert.assertNull("Invalid port logical name should return a null",
147 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
152 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.0
155 public void testCheckPortValidity10() {
156 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , -1L));
157 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0L));
158 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFF00L));
159 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFF8L));
160 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFF0L));
161 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFFFL));
162 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0x1FFFFL));
166 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.3
169 public void testCheckPortValidity13() {
170 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , -1L));
171 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0L));
172 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFF00L));
173 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFF8L));
174 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFF0L));
175 Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFFFL));
176 Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0x1FFFFFFFFL));
180 * test for method {@link OpenflowPortsUtil#portNumberToString(PortNumber)}
183 public void testPortNumberToString() {
184 PortNumberUni portNumber;
186 portNumber = new PortNumberUni(42L);
187 Assert.assertEquals("42", OpenflowPortsUtil.portNumberToString(portNumber));
189 portNumber = new PortNumberUni(OutputPortValues.FLOOD.toString());
190 Assert.assertEquals("FLOOD", OpenflowPortsUtil.portNumberToString(portNumber));
193 portNumber = new PortNumberUni((String) null);
194 Assert.fail("NPE was expected - due to value type");
195 } catch (Exception e) {
197 Assert.assertEquals(NullPointerException.class, e.getClass());