2 * Copyright (c) 2013 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.controller.sal.compatibility.test;
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.compatibility.NodeMapping;
13 import org.opendaylight.controller.sal.core.ConstructionException;
14 import org.opendaylight.controller.sal.core.MacAddress;
15 import org.opendaylight.controller.sal.core.Node.NodeIDType;
16 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 * test of {@link NodeMapping} utility class
29 public class NodeMappingTest {
33 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADMacAddress(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId)}
37 public void testToADMacAddress() {
38 NodeId[] nodeIds = new NodeId[] {
39 // 0x0000|0000 0000002a (answer to the ultimate question of life, universe and everything)
41 // 0x7fff|ffff ffffffff (max long -> 2**63 - 1)
42 new NodeId("9223372036854775807"),
43 // 0x7fff|7fff ffffffff
44 new NodeId("9223231299366420479"),
45 // 0x8fff|7fff ffffffff (more than biggest positive long)
46 new NodeId("10376152803973267455"),
47 // 0xca13|764a e9ace65a (BUG-770)
48 new NodeId("14561112084339025498")
51 byte[][] expectedMacs = new byte[][] {
52 {0, 0, 0, 0, 0, 0x2a},
53 {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff},
54 {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff},
55 {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff},
56 {(byte) 0x76, (byte) 0x4a, (byte) 0xe9, (byte) 0xac, (byte) 0xe6, (byte) 0x5a}
59 Assert.assertEquals(expectedMacs.length, nodeIds.length);
61 for (int i = 0; i < expectedMacs.length; i++) {
62 NodeId nodeId = nodeIds[i];
63 MacAddress mac = NodeMapping.toADMacAddress(nodeId);
64 Assert.assertArrayEquals(expectedMacs[i], mac.getMacAddress());
70 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toAdNodeId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId)}
74 public void testToAdNodeId() {
76 observed = NodeMapping.toAdNodeId(null);
77 Assert.assertNull(observed);
79 observed = NodeMapping.toAdNodeId(new NodeConnectorId("openflow:5:2"));
80 Assert.assertEquals("openflow:5", observed.getValue());
85 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNode(NodeId)}
89 public void testToAdNode1() {
90 org.opendaylight.controller.sal.core.Node observed;
92 observed = NodeMapping.toADNode((NodeId) null);
93 } catch (NullPointerException | ConstructionException e) {
97 NodeId nodeId = new NodeId("openflow:1");
99 observed = NodeMapping.toADNode(nodeId);
100 Assert.assertEquals("OF|00:00:00:00:00:00:00:01", observed.toString());
101 } catch (ConstructionException e) {
102 Assert.fail("should succeed to construct Node: "+e.getMessage());
108 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorType(NodeConnectorId, NodeId)}
112 public void testToNodeConnectorType() {
113 NodeConnectorId ncId;
114 NodeId nodeId = buildNodeId("1");
116 ncId = buildNodeConnectorId("1", "42");
117 Assert.assertEquals(NodeConnectorIDType.OPENFLOW, NodeMapping.toNodeConnectorType(ncId, nodeId ));
119 ncId = buildNodeConnectorId("1", OutputPortValues.CONTROLLER.toString());
120 Assert.assertEquals(NodeConnectorIDType.CONTROLLER, NodeMapping.toNodeConnectorType(ncId, nodeId ));
122 ncId = buildNodeConnectorId("1", OutputPortValues.NORMAL.toString());
123 Assert.assertEquals(NodeConnectorIDType.HWPATH, NodeMapping.toNodeConnectorType(ncId, nodeId ));
125 ncId = buildNodeConnectorId("1", OutputPortValues.LOCAL.toString());
126 Assert.assertEquals(NodeConnectorIDType.SWSTACK, NodeMapping.toNodeConnectorType(ncId, nodeId ));
131 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNodeConnectorId(NodeConnectorId, NodeId)}
135 public void testToAdNodeConnectorId() {
136 NodeConnectorId nodeConnectorId = buildNodeConnectorId("1", "2");
137 NodeId nodeId = buildNodeId("1");
138 Assert.assertEquals(Short.valueOf((short) 2), NodeMapping.toADNodeConnectorId(nodeConnectorId , nodeId));
143 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeRef(Node)}
145 * @throws ConstructionException
148 public void testToNodeRef() throws ConstructionException {
149 org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
150 InstanceIdentifier<?> nodePath = NodeMapping.toNodeRef(node).getValue();
152 String observedId = nodePath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
153 Assert.assertEquals("openflow:42", observedId);
158 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorRef(org.opendaylight.controller.sal.core.NodeConnector)}
160 * @throws ConstructionException
163 public void testToNodeConnectorRef() throws ConstructionException {
164 org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
165 org.opendaylight.controller.sal.core.NodeConnector nodeConnector =
166 new org.opendaylight.controller.sal.core.NodeConnector(
167 NodeConnectorIDType.OPENFLOW, (short) 1, node);
169 InstanceIdentifier<?> nodeConnectorPath = NodeMapping.toNodeConnectorRef(nodeConnector ).getValue();
170 String observedNodeId = nodeConnectorPath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
171 Assert.assertEquals("openflow:42", observedNodeId);
173 String observedNodeConnectorId = nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
174 Assert.assertEquals("openflow:1", observedNodeConnectorId);
179 * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#openflowFullNodeIdToLong(String)}
181 * @throws ConstructionException
184 public void testOpenflowFullNodeIdToLong() throws ConstructionException {
185 Assert.assertEquals(42L, NodeMapping.openflowFullNodeIdToLong("42").longValue());
186 Assert.assertEquals(0xCC4E241C4A000000L, NodeMapping.openflowFullNodeIdToLong("14721743935839928320").longValue());
192 * @return nodeConnectorId
194 public static NodeConnectorId buildNodeConnectorId(String nodeId, String portId) {
195 return new NodeConnectorId(NodeConnectorIDType.OPENFLOW+"|" + nodeId + ":" + portId);
202 public static NodeId buildNodeId(String id) {
203 return new NodeId(NodeConnectorIDType.OPENFLOW+"|" + id);