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