1737475ab175dd8026bba2de52ded078bca4c2bd
[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         OpenflowPortsUtil.init();
35
36         mapOF10Ports = new HashMap<String, Long>();
37         mapOF10Ports.put(OutputPortValues.MAX.toString(), 65280L);
38         mapOF10Ports.put(OutputPortValues.INPORT.toString(), 65528L);
39         mapOF10Ports.put(OutputPortValues.TABLE.toString(), 65529L);
40         mapOF10Ports.put(OutputPortValues.NORMAL.toString(), 65530L);
41         mapOF10Ports.put(OutputPortValues.FLOOD.toString(), 65531L);
42         mapOF10Ports.put(OutputPortValues.ALL.toString(), 65532L);
43         mapOF10Ports.put(OutputPortValues.CONTROLLER.toString(), 65533L);
44         mapOF10Ports.put(OutputPortValues.LOCAL.toString(), 65534L);
45         mapOF10Ports.put(OutputPortValues.NONE.toString(), 65535L);
46
47         mapOF13Ports = new HashMap<String, Long>();
48         mapOF13Ports.put(OutputPortValues.MAX.toString(), 4294967040L);
49         mapOF13Ports.put(OutputPortValues.INPORT.toString(), 4294967288L);
50         mapOF13Ports.put(OutputPortValues.TABLE.toString(), 4294967289L);
51         mapOF13Ports.put(OutputPortValues.NORMAL.toString(), 4294967290L);
52         mapOF13Ports.put(OutputPortValues.FLOOD.toString(), 4294967291L);
53         mapOF13Ports.put(OutputPortValues.ALL.toString(), 4294967292L);
54         mapOF13Ports.put(OutputPortValues.CONTROLLER.toString(), 4294967293L);
55         mapOF13Ports.put(OutputPortValues.LOCAL.toString(), 4294967294L);
56         mapOF13Ports.put(OutputPortValues.ANY.toString(), 4294967295L);
57
58         mapVersionToPorts = new HashMap<OpenflowVersion, Map<String, Long>>();
59         mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
60         mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
61
62     }
63
64     /**
65      * tearing down initiated values after all tests done
66      */
67     @AfterClass
68     public static void tearDownClass() {
69         OpenflowPortsUtil.close();
70         mapOF10Ports.clear();
71         mapOF13Ports.clear();
72         mapVersionToPorts.clear();
73     }
74
75     //helper
76     private static void matchGetLogicalName(final OpenflowVersion version, final String logicalName) {
77         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
78                 logicalName,
79                 OpenflowPortsUtil.getPortLogicalName(version, mapVersionToPorts.get(version).get(logicalName)));
80     }
81
82     //helper
83     private static void matchGetPortfromLogicalName(final OpenflowVersion version, final String logicalName) {
84         Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
85                 mapVersionToPorts.get(version).get(logicalName), OpenflowPortsUtil.getPortFromLogicalName(version, logicalName));
86     }
87
88     /**
89      * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}
90      */
91     @Test
92     public void testGetPortLogicalName() {
93
94         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.toString());
95         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
96         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
97         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
98         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
99         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
100         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
101         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
102         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
103
104         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
105         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
106         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
107         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
108         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
109         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
110         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
111         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
112         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
113
114         Assert.assertNull("Invalid port number should return a null",
115                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
116
117         Assert.assertNull("Invalid port number should return a null",
118                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
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.toString());
129         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.toString());
130         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.toString());
131         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.toString());
132         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.toString());
133         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.toString());
134         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.toString());
135         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.toString());
136         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.toString());
137
138         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.toString());
139         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.toString());
140         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.toString());
141         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.toString());
142         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.toString());
143         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.toString());
144         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.toString());
145         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.toString());
146         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.toString());
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#portNumberToString(PortNumber)}
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         try {
198             portNumber = new PortNumberUni((String) null);
199             Assert.fail("NPE was expected - due to value type");
200         } catch (Exception e) {
201             // expected
202             Assert.assertEquals(NullPointerException.class, e.getClass());
203         }
204     }
205
206 }