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