2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.util;
10 import java.util.HashMap;
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;
21 * @author: Kamal Rameshan (kramesha@cisco.com)
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;
30 * initiation before testing - once for all
33 public static void setupClass() {
34 OpenflowPortsUtil.init();
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);
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);
58 mapVersionToPorts = new HashMap<OpenflowVersion, Map<String, Long>>();
59 mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
60 mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
65 * tearing down initiated values after all tests done
68 public static void tearDownClass() {
69 OpenflowPortsUtil.close();
72 mapVersionToPorts.clear();
76 private static void matchGetLogicalName(final OpenflowVersion version, final String logicalName) {
77 Assert.assertEquals("Controller reserve port not matching to logical-name for "+ version,
79 OpenflowPortsUtil.getPortLogicalName(version, mapVersionToPorts.get(version).get(logicalName)));
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));
89 * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}
92 public void testGetPortLogicalName() {
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());
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());
114 Assert.assertNull("Invalid port number should return a null",
115 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
117 Assert.assertNull("Invalid port number should return a null",
118 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
123 * test for method {@link OpenflowPortsUtil#getPortFromLogicalName(OpenflowVersion, String)}
126 public void testGetPortFromLogicalName() {
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());
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());
148 Assert.assertNull("Invalid port logical name should return a null",
149 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
151 Assert.assertNull("Invalid port logical name should return a null",
152 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
157 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.0
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));
171 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Long)} - OF-1.3
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));
185 * test for method {@link OpenflowPortsUtil#portNumberToString(PortNumber)}
188 public void testPortNumberToString() {
189 PortNumberUni portNumber;
191 portNumber = new PortNumberUni(42L);
192 Assert.assertEquals("42", OpenflowPortsUtil.portNumberToString(portNumber));
194 portNumber = new PortNumberUni(OutputPortValues.FLOOD.toString());
195 Assert.assertEquals("FLOOD", OpenflowPortsUtil.portNumberToString(portNumber));
198 portNumber = new PortNumberUni((String) null);
199 Assert.fail("NPE was expected - due to value type");
200 } catch (Exception e) {
202 Assert.assertEquals(NullPointerException.class, e.getClass());