Merge "Cleanup: Removed unused code"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / test / java / org / opendaylight / controller / sal / compatibility / test / NodeMappingTest.java
1 /**
2  * Copyright (c) 2013 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.controller.sal.compatibility.test;
9
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;
25
26 /**
27  * test of {@link NodeMapping} utility class
28  */
29 public class NodeMappingTest {
30
31     /**
32      * Test method for
33      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADMacAddress(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId)}
34      * .
35      */
36     @Test
37     public void testToADMacAddress() {
38         NodeId[] nodeIds = new NodeId[] {
39                 // 0x0000|0000 0000002a (answer to the ultimate question of life, universe and everything)
40                 new NodeId("42"),
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")
49         };
50
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}
57         };
58
59         Assert.assertEquals(expectedMacs.length, nodeIds.length);
60
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());
65         }
66     }
67
68     /**
69      * Test method for
70      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toAdNodeId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId)}
71      * .
72      */
73     @Test
74     public void testToAdNodeId() {
75         NodeId observed;
76         observed = NodeMapping.toAdNodeId(null);
77         Assert.assertNull(observed);
78
79         observed = NodeMapping.toAdNodeId(new NodeConnectorId("openflow:5:2"));
80         Assert.assertEquals("openflow:5", observed.getValue());
81     }
82
83     /**
84      * Test method for
85      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNode(NodeId)}
86      * .
87      */
88     @Test
89     public void testToAdNode1() {
90         org.opendaylight.controller.sal.core.Node observed;
91         try {
92             observed = NodeMapping.toADNode((NodeId) null);
93         } catch (NullPointerException | ConstructionException e) {
94             //expected
95         }
96
97         NodeId nodeId = new NodeId("openflow:1");
98         try {
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());
103         }
104     }
105
106     /**
107      * Test method for
108      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorType(NodeConnectorId, NodeId)}
109      * .
110      */
111     @Test
112     public void testToNodeConnectorType() {
113         NodeConnectorId ncId;
114         NodeId nodeId = buildNodeId("1");
115
116         ncId = buildNodeConnectorId("1", "42");
117         Assert.assertEquals(NodeConnectorIDType.OPENFLOW, NodeMapping.toNodeConnectorType(ncId, nodeId ));
118
119         ncId = buildNodeConnectorId("1", OutputPortValues.CONTROLLER.toString());
120         Assert.assertEquals(NodeConnectorIDType.CONTROLLER, NodeMapping.toNodeConnectorType(ncId, nodeId ));
121
122         ncId = buildNodeConnectorId("1", OutputPortValues.NORMAL.toString());
123         Assert.assertEquals(NodeConnectorIDType.HWPATH, NodeMapping.toNodeConnectorType(ncId, nodeId ));
124
125         ncId = buildNodeConnectorId("1", OutputPortValues.LOCAL.toString());
126         Assert.assertEquals(NodeConnectorIDType.SWSTACK, NodeMapping.toNodeConnectorType(ncId, nodeId ));
127     }
128
129     /**
130      * Test method for
131      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNodeConnectorId(NodeConnectorId, NodeId)}
132      * .
133      */
134     @Test
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));
139     }
140
141     /**
142      * Test method for
143      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeRef(Node)}
144      * .
145      * @throws ConstructionException
146      */
147     @Test
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();
151
152         String observedId = nodePath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
153         Assert.assertEquals("openflow:42", observedId);
154     }
155
156     /**
157      * Test method for
158      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorRef(org.opendaylight.controller.sal.core.NodeConnector)}
159      * .
160      * @throws ConstructionException
161      */
162     @Test
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);
168
169         InstanceIdentifier<?> nodeConnectorPath = NodeMapping.toNodeConnectorRef(nodeConnector ).getValue();
170         String observedNodeId = nodeConnectorPath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
171         Assert.assertEquals("openflow:42", observedNodeId);
172
173         String observedNodeConnectorId = nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
174         Assert.assertEquals("openflow:1", observedNodeConnectorId);
175     }
176
177     /**
178      * Test method for
179      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#openflowFullNodeIdToLong(String)}
180      * .
181      * @throws ConstructionException
182      */
183     @Test
184     public void testOpenflowFullNodeIdToLong() throws ConstructionException {
185         Assert.assertEquals(42L, NodeMapping.openflowFullNodeIdToLong("42").longValue());
186         Assert.assertEquals(0xCC4E241C4A000000L, NodeMapping.openflowFullNodeIdToLong("14721743935839928320").longValue());
187     }
188
189     /**
190      * @param nodeId
191      * @param portId
192      * @return nodeConnectorId
193      */
194     public static NodeConnectorId buildNodeConnectorId(String nodeId, String portId) {
195         return new NodeConnectorId(NodeConnectorIDType.OPENFLOW+"|" + nodeId + ":" + portId);
196     }
197
198     /**
199      * @param id
200      * @return nodeId
201      */
202     public static NodeId buildNodeId(String id) {
203         return new NodeId(NodeConnectorIDType.OPENFLOW+"|" + id);
204     }
205 }