a9921f8ee3c066bc5650a060a6f41c8f7642dd87
[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.rev131026.OutputPortValues;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 /**
13  * @author: Kamal Rameshan (kramesha@cisco.com)
14  * @since : 6/2/14
15  */
16 public class OpenflowPortsUtilTest {
17     private static Map<String, Long> mapOF10Ports;
18     private static Map<String, Long> mapOF13Ports;
19     private static Map<OpenflowVersion, Map<String, Long>> mapVersionToPorts;
20
21     @BeforeClass
22     public static void setupClass() {
23         OpenflowPortsUtil.init();
24
25         mapOF10Ports = new HashMap<String, Long>();
26         mapOF10Ports.put(OutputPortValues.MAX.toString(), 65280L);
27         mapOF10Ports.put(OutputPortValues.INPORT.toString(), 65528L);
28         mapOF10Ports.put(OutputPortValues.TABLE.toString(), 65529L);
29         mapOF10Ports.put(OutputPortValues.NORMAL.toString(), 65530L);
30         mapOF10Ports.put(OutputPortValues.FLOOD.toString(), 65531L);
31         mapOF10Ports.put(OutputPortValues.ALL.toString(), 65532L);
32         mapOF10Ports.put(OutputPortValues.CONTROLLER.toString(), 65533L);
33         mapOF10Ports.put(OutputPortValues.LOCAL.toString(), 65534L);
34         mapOF10Ports.put(OutputPortValues.NONE.toString(), 65535L);
35
36         mapOF13Ports = new HashMap<String, Long>();
37         mapOF13Ports.put(OutputPortValues.MAX.toString(), 4294967040L);
38         mapOF13Ports.put(OutputPortValues.INPORT.toString(), 4294967288L);
39         mapOF13Ports.put(OutputPortValues.TABLE.toString(), 4294967289L);
40         mapOF13Ports.put(OutputPortValues.NORMAL.toString(), 4294967290L);
41         mapOF13Ports.put(OutputPortValues.FLOOD.toString(), 4294967291L);
42         mapOF13Ports.put(OutputPortValues.ALL.toString(), 4294967292L);
43         mapOF13Ports.put(OutputPortValues.CONTROLLER.toString(), 4294967293L);
44         mapOF13Ports.put(OutputPortValues.LOCAL.toString(), 4294967294L);
45         mapOF13Ports.put(OutputPortValues.ANY.toString(), 4294967295L);
46
47         mapVersionToPorts = new HashMap<OpenflowVersion, Map<String, Long>>();
48         mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
49         mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
50
51     }
52
53     @AfterClass
54     public static void tearDownClass() {
55         OpenflowPortsUtil.close();
56         mapOF10Ports.clear();
57         mapOF13Ports.clear();
58         mapVersionToPorts.clear();
59     }
60
61     //helper
62     private void matchGetLogicalName(OpenflowVersion version, String logicalName) {
63         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
64                 logicalName,
65                 OpenflowPortsUtil.getPortLogicalName(version, mapVersionToPorts.get(version).get(logicalName)));
66     }
67
68     //helper
69     private void matchGetPortfromLogicalName(OpenflowVersion version, String logicalName) {
70         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
71                 mapVersionToPorts.get(version).get(logicalName), OpenflowPortsUtil.getPortFromLogicalName(version, logicalName));
72     }
73
74     @Test
75     public void testGetPortLogicalName() {
76
77         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
78         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
79         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
80         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
81         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
82         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
83         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
84         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
85         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
86
87         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
88         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
89         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
90         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
91         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
92         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
93         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
94         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
95         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
96
97         Assert.assertNull("Invalid port number should return a null",
98                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
99
100         Assert.assertNull("Invalid port number should return a null",
101                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
102     }
103
104
105     @Test
106     public void testGetPortFromLogicalName() {
107
108         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
109         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
110         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
111         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
112         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
113         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
114         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
115         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
116         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
117
118         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
119         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
120         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
121         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
122         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
123         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
124         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
125         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
126         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
127
128         Assert.assertNull("Invalid port logical name should return a null",
129                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
130
131         Assert.assertNull("Invalid port logical name should return a null",
132                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
133
134     }
135
136 }