Adopt 2023.03 Argon upstreams
[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 import org.opendaylight.yangtools.yang.common.Uint32;
20
21 /**
22  * Unit tests for OpenflowPortsUtil.
23  *
24  * @author: Kamal Rameshan (kramesha@cisco.com)
25  * @since : 6/2/14
26  */
27 public class OpenflowPortsUtilTest {
28     private static Map<String, Long> mapOF10Ports;
29     private static Map<String, Long> mapOF13Ports;
30     private static Map<OpenflowVersion, Map<String, Long>> mapVersionToPorts;
31
32     /**
33      * initiation before testing - once for all.
34      */
35     @BeforeClass
36     public static void setupClass() {
37         mapOF10Ports = new HashMap<>();
38         mapOF10Ports.put(OutputPortValues.MAX.getName(), 65280L);
39         mapOF10Ports.put(OutputPortValues.INPORT.getName(), 65528L);
40         mapOF10Ports.put(OutputPortValues.TABLE.getName(), 65529L);
41         mapOF10Ports.put(OutputPortValues.NORMAL.getName(), 65530L);
42         mapOF10Ports.put(OutputPortValues.FLOOD.getName(), 65531L);
43         mapOF10Ports.put(OutputPortValues.ALL.getName(), 65532L);
44         mapOF10Ports.put(OutputPortValues.CONTROLLER.getName(), 65533L);
45         mapOF10Ports.put(OutputPortValues.LOCAL.getName(), 65534L);
46         mapOF10Ports.put(OutputPortValues.NONE.getName(), 65535L);
47
48         mapOF13Ports = new HashMap<>();
49         mapOF13Ports.put(OutputPortValues.MAX.getName(), 4294967040L);
50         mapOF13Ports.put(OutputPortValues.INPORT.getName(), 4294967288L);
51         mapOF13Ports.put(OutputPortValues.TABLE.getName(), 4294967289L);
52         mapOF13Ports.put(OutputPortValues.NORMAL.getName(), 4294967290L);
53         mapOF13Ports.put(OutputPortValues.FLOOD.getName(), 4294967291L);
54         mapOF13Ports.put(OutputPortValues.ALL.getName(), 4294967292L);
55         mapOF13Ports.put(OutputPortValues.CONTROLLER.getName(), 4294967293L);
56         mapOF13Ports.put(OutputPortValues.LOCAL.getName(), 4294967294L);
57         mapOF13Ports.put(OutputPortValues.ANY.getName(), 4294967295L);
58
59         mapVersionToPorts = new HashMap<>();
60         mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
61         mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
62
63     }
64
65     /**
66      * tearing down initiated values after all tests done.
67      */
68     @AfterClass
69     public static void tearDownClass() {
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).longValue(),
86                 OpenflowPortsUtil.getPortFromLogicalName(version, logicalName).toJava());
87     }
88
89     /**
90      * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}.
91      */
92     @Test
93     public void testGetPortLogicalName() {
94
95         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.getName());
96         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.getName());
97         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.getName());
98         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.getName());
99         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.getName());
100         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.getName());
101         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.getName());
102         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.getName());
103         matchGetLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.getName());
104
105         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.getName());
106         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.getName());
107         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.getName());
108         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.getName());
109         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.getName());
110         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.getName());
111         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.getName());
112         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.getName());
113         matchGetLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.getName());
114
115         Assert.assertNull("Invalid port number should return a null",
116                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
117
118         Assert.assertNull("Invalid port number should return a null",
119                 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
120
121         String name = OutputPortValues.INPORT.getName();
122         Assert.assertFalse(name.equals("a"));
123     }
124
125
126
127     /**
128      * test for method {@link OpenflowPortsUtil#getPortFromLogicalName(OpenflowVersion, String)}.
129      */
130     @Test
131     public void testGetPortFromLogicalName() {
132
133         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.MAX.getName());
134         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.INPORT.getName());
135         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.TABLE.getName());
136         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NORMAL.getName());
137         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.FLOOD.getName());
138         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.ALL.getName());
139         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.CONTROLLER.getName());
140         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.LOCAL.getName());
141         matchGetPortfromLogicalName(OpenflowVersion.OF10, OutputPortValues.NONE.getName());
142
143         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.MAX.getName());
144         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.INPORT.getName());
145         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.TABLE.getName());
146         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.NORMAL.getName());
147         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.FLOOD.getName());
148         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ALL.getName());
149         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.CONTROLLER.getName());
150         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.LOCAL.getName());
151         matchGetPortfromLogicalName(OpenflowVersion.OF13, OutputPortValues.ANY.getName());
152
153         Assert.assertNull("Invalid port logical name should return a null",
154                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
155
156         Assert.assertNull("Invalid port logical name should return a null",
157                 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
158
159     }
160
161     /**
162      * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Uint32)} - OF-1.0.
163      */
164     @Test
165     public void testCheckPortValidity10() {
166         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0L)));
167         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0xFF00L)));
168         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0xFFF8L)));
169         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0xFFF0L)));
170         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0xFFFFL)));
171         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF10 , Uint32.valueOf(0x1FFFFL)));
172     }
173
174     /**
175      * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Uint32)} - OF-1.3.
176      */
177     @Test
178     public void testCheckPortValidity13() {
179         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , Uint32.valueOf(0L)));
180         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , Uint32.valueOf(0xFFFFFF00L)));
181         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , Uint32.valueOf(0xFFFFFFF8L)));
182         Assert.assertFalse(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , Uint32.valueOf(0xFFFFFFF0L)));
183         Assert.assertTrue(OpenflowPortsUtil.checkPortValidity(OpenflowVersion.OF13 , Uint32.valueOf(0xFFFFFFFFL)));
184     }
185
186     /**
187      * test for method {@link OpenflowPortsUtil}.
188      */
189     @Test
190     public void testPortNumberToString() {
191         PortNumberUni portNumber;
192
193         portNumber = new PortNumberUni(Uint32.valueOf(42L));
194         Assert.assertEquals("42", OpenflowPortsUtil.portNumberToString(portNumber));
195
196         portNumber = new PortNumberUni(OutputPortValues.FLOOD.toString());
197         Assert.assertEquals("FLOOD", OpenflowPortsUtil.portNumberToString(portNumber));
198     }
199 }