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;
19 import org.opendaylight.yangtools.yang.common.Uint32;
22 * Unit tests for OpenflowPortsUtil.
24 * @author: Kamal Rameshan (kramesha@cisco.com)
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;
33 * initiation before testing - once for all.
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);
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);
59 mapVersionToPorts = new HashMap<>();
60 mapVersionToPorts.put(OpenflowVersion.OF10, mapOF10Ports);
61 mapVersionToPorts.put(OpenflowVersion.OF13, mapOF13Ports);
66 * tearing down initiated values after all tests done.
69 public static void tearDownClass() {
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).longValue(),
86 OpenflowPortsUtil.getPortFromLogicalName(version, logicalName).toJava());
90 * test for method {@link OpenflowPortsUtil#getPortLogicalName(OpenflowVersion, Long)}.
93 public void testGetPortLogicalName() {
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());
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());
115 Assert.assertNull("Invalid port number should return a null",
116 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF10, 99999L));
118 Assert.assertNull("Invalid port number should return a null",
119 OpenflowPortsUtil.getPortLogicalName(OpenflowVersion.OF13, 99999L));
121 String name = OutputPortValues.INPORT.getName();
122 Assert.assertFalse(name.equals("a"));
128 * test for method {@link OpenflowPortsUtil#getPortFromLogicalName(OpenflowVersion, String)}.
131 public void testGetPortFromLogicalName() {
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());
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());
153 Assert.assertNull("Invalid port logical name should return a null",
154 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF10, "abc"));
156 Assert.assertNull("Invalid port logical name should return a null",
157 OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.OF13, "abc"));
162 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Uint32)} - OF-1.0.
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)));
175 * test for method {@link OpenflowPortsUtil#checkPortValidity(OpenflowVersion, Uint32)} - OF-1.3.
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)));
187 * test for method {@link OpenflowPortsUtil}.
190 public void testPortNumberToString() {
191 PortNumberUni portNumber;
193 portNumber = new PortNumberUni(Uint32.valueOf(42L));
194 Assert.assertEquals("42", OpenflowPortsUtil.portNumberToString(portNumber));
196 portNumber = new PortNumberUni(OutputPortValues.FLOOD.toString());
197 Assert.assertEquals("FLOOD", OpenflowPortsUtil.portNumberToString(portNumber));