5cb08f8bb902f85dd1fce9168c46b4d1f14c9620
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / util / OpenflowPortsUtilTest.java
1 package org.opendaylight.openflowplugin.openflow.md.util;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.junit.AfterClass;
7 import org.junit.Assert;
8 import org.junit.BeforeClass;
9 import org.junit.Test;
10 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
13
14 /**
15  * @author: Kamal Rameshan (kramesha@cisco.com)
16  * @since : 6/2/14
17  */
18 public class OpenflowPortsUtilTest {
19     private static Map<String, Long> mapOF10Ports;
20     private static Map<String, Long> mapOF13Ports;
21     private static Map<OpenflowVersion, Map<String, Long>> mapVersionToPorts;
22
23     /**
24      * initiation before testing - once for all
25      */
26     @BeforeClass
27     public static void setupClass() {
28         OpenflowPortsUtil.init();
29
30         mapOF10Ports = new HashMap<String, Long>();
31         mapOF10Ports.put(OutputPortValues.MAX.toString(), 65280L);
32         mapOF10Ports.put(OutputPortValues.INPORT.toString(), 65528L);
33         mapOF10Ports.put(OutputPortValues.TABLE.toString(), 65529L);
34         mapOF10Ports.put(OutputPortValues.NORMAL.toString(), 65530L);
35         mapOF10Ports.put(OutputPortValues.FLOOD.toString(), 65531L);
36         mapOF10Ports.put(OutputPortValues.ALL.toString(), 65532L);
37         mapOF10Ports.put(OutputPortValues.CONTROLLER.toString(), 65533L);
38         mapOF10Ports.put(OutputPortValues.LOCAL.toString(), 65534L);
39         mapOF10Ports.put(OutputPortValues.NONE.toString(), 65535L);
40
41         mapOF13Ports = new HashMap<String, Long>();
42         mapOF13Ports.put(OutputPortValues.MAX.toString(), 4294967040L);
43         mapOF13Ports.put(OutputPortValues.INPORT.toString(), 4294967288L);
44         mapOF13Ports.put(OutputPortValues.TABLE.toString(), 4294967289L);
45         mapOF13Ports.put(OutputPortValues.NORMAL.toString(), 4294967290L);
46         mapOF13Ports.put(OutputPortValues.FLOOD.toString(), 4294967291L);
47         mapOF13Ports.put(OutputPortValues.ALL.toString(), 4294967292L);
48         mapOF13Ports.put(OutputPortValues.CONTROLLER.toString(), 4294967293L);
49         mapOF13Ports.put(OutputPortValues.LOCAL.toString(), 4294967294L);
50         mapOF13Ports.put(OutputPortValues.ANY.toString(), 4294967295L);
51
52         mapVersionToPorts = new HashMap<OpenflowVersion, Map<String, Long>>();
53         mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
54         mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
55
56     }
57
58     /**
59      * tearing down initiated values after all tests done
60      */
61     @AfterClass
62     public static void tearDownClass() {
63         OpenflowPortsUtil.close();
64         mapOF10Ports.clear();
65         mapOF13Ports.clear();
66         mapVersionToPorts.clear();
67     }
68
69     //helper
70     private static void matchGetLogicalName(OpenflowVersion version, String logicalName) {
71         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
72                 logicalName,
73                 OpenflowPortsUtil.getPortLogicalName(version, mapVersionToPorts.get(version).get(logicalName)));
74     }
75
76     //helper
77     private static void matchGetPortfromLogicalName(OpenflowVersion version, String logicalName) {
78         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
79                 mapVersionToPorts.get(version).get(logicalName), OpenflowPortsUtil.getPortFromLogicalName(version, logicalName));
80     }
81
82     /**
83      * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}
84      */
85     @Test
86     public void testGetPortLogicalName() {
87
88         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
89         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
90         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
91         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
92         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
93         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
94         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
95         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
96         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
97
98         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
99         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
100         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
101         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
102         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
103         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
104         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
105         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
106         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
107
108         Assert.assertNull("Invalid port number should return a null",
109                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
110
111         Assert.assertNull("Invalid port number should return a null",
112                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
113     }
114
115
116     /**
117      * test for method {@link OpenflowPortsUtil#getPortFromLogicalName(OpenflowVersion, String)}
118      */
119     @Test
120     public void testGetPortFromLogicalName() {
121
122         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
123         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
124         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
125         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
126         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
127         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
128         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
129         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
130         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
131
132         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
133         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
134         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
135         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
136         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
137         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
138         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
139         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
140         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
141
142         Assert.assertNull("Invalid port logical name should return a null",
143                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
144
145         Assert.assertNull("Invalid port logical name should return a null",
146                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
147
148     }
149
150     /**
151      * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.0
152      */
153     @Test
154     public void testCheckPortValidity10() {
155         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , -1L));
156         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0L));
157         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFF00L));
158         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFF8L));
159         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFF0L));
160         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0xFFFFL));
161         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , 0x1FFFFL));
162     }
163
164     /**
165      * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.3
166      */
167     @Test
168     public void testCheckPortValidity13() {
169         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , -1L));
170         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0L));
171         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFF00L));
172         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFF8L));
173         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFF0L));
174         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0xFFFFFFFFL));
175         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , 0x1FFFFFFFFL));
176     }
177
178     /**
179      * test for method {@link OpenflowPortsUtil#portNumberToString(PortNumber)}
180      */
181     @Test
182     public void testPortNumberToString() {
183         PortNumberUni portNumber;
184         
185         portNumber = new PortNumberUni(42L);
186         Assert.assertEquals("42", OpenflowPortsUtil.portNumberToString(portNumber));
187         
188         portNumber = new PortNumberUni(OutputPortValues.FLOOD.toString());
189         Assert.assertEquals("FLOOD", OpenflowPortsUtil.portNumberToString(portNumber));
190         
191         try {
192             portNumber = new PortNumberUni((String) null);
193             Assert.fail("NPE was expected - due to value type");
194         } catch (Exception e) {
195             // expected
196             Assert.assertEquals(NullPointerException.class, e.getClass());
197         }
198     }
199
200 }