Merge "Bug-835:Reserve ports should be logical ports"
[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.inventory.rev130819.NodeConnectorId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 /**
26  * test of {@link NodeMapping} utility class
27  */
28 public class NodeMappingTest {
29
30     /**
31      * Test method for
32      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADMacAddress(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId)}
33      * .
34      */
35     @Test
36     public void testToADMacAddress() {
37         NodeId[] nodeIds = new NodeId[] {
38                 // 0x0000|0000 0000002a (answer to the ultimate question of life, universe and everything)
39                 new NodeId("42"),
40                 // 0x7fff|ffff ffffffff (max long -> 2**63 - 1)
41                 new NodeId("9223372036854775807"),
42                 // 0x7fff|7fff ffffffff
43                 new NodeId("9223231299366420479"),
44                 // 0x8fff|7fff ffffffff (more than biggest positive long)
45                 new NodeId("10376152803973267455"),
46                 // 0xca13|764a e9ace65a (BUG-770)
47                 new NodeId("14561112084339025498")
48         };
49
50         byte[][] expectedMacs = new byte[][] {
51                 {0, 0, 0, 0, 0, 0x2a},
52                 {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff},
53                 {(byte) 0x7f, (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) 0x76, (byte) 0x4a, (byte) 0xe9, (byte) 0xac, (byte) 0xe6, (byte) 0x5a}
56         };
57
58         Assert.assertEquals(expectedMacs.length, nodeIds.length);
59
60         for (int i = 0; i < expectedMacs.length; i++) {
61             NodeId nodeId = nodeIds[i];
62             MacAddress mac = NodeMapping.toADMacAddress(nodeId);
63             Assert.assertArrayEquals(expectedMacs[i], mac.getMacAddress());
64         }
65     }
66
67     /**
68      * Test method for
69      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toAdNodeId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId)}
70      * .
71      */
72     @Test
73     public void testToAdNodeId() {
74         NodeId observed;
75         observed = NodeMapping.toAdNodeId(null);
76         Assert.assertNull(observed);
77
78         observed = NodeMapping.toAdNodeId(new NodeConnectorId("openflow:5:2"));
79         Assert.assertEquals("openflow:5", observed.getValue());
80     }
81
82     /**
83      * Test method for
84      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNode(NodeId)}
85      * .
86      */
87     @Test
88     public void testToAdNode1() {
89         org.opendaylight.controller.sal.core.Node observed;
90         try {
91             observed = NodeMapping.toADNode((NodeId) null);
92         } catch (NullPointerException | ConstructionException e) {
93             //expected
94         }
95
96         NodeId nodeId = new NodeId("openflow:1");
97         try {
98             observed = NodeMapping.toADNode(nodeId);
99             Assert.assertEquals("OF|00:00:00:00:00:00:00:01", observed.toString());
100         } catch (ConstructionException e) {
101             Assert.fail("should succeed to construct Node: "+e.getMessage());
102         }
103     }
104
105     /**
106      * Test method for
107      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorType(NodeConnectorId, NodeId)}
108      * .
109      */
110     @Test
111     public void testToNodeConnectorType() {
112         NodeConnectorId ncId;
113         NodeId nodeId = buildNodeId("1");
114
115         ncId = buildNodeConnectorId("1", "42");
116         Assert.assertEquals(NodeConnectorIDType.OPENFLOW, NodeMapping.toNodeConnectorType(ncId, nodeId ));
117
118         ncId = buildNodeConnectorId("1", "4294967293");
119         Assert.assertEquals(NodeConnectorIDType.CONTROLLER, NodeMapping.toNodeConnectorType(ncId, nodeId ));
120
121         ncId = buildNodeConnectorId("1", "4294967290");
122         Assert.assertEquals(NodeConnectorIDType.HWPATH, NodeMapping.toNodeConnectorType(ncId, nodeId ));
123
124         ncId = buildNodeConnectorId("1", "4294967294");
125         Assert.assertEquals(NodeConnectorIDType.SWSTACK, NodeMapping.toNodeConnectorType(ncId, nodeId ));
126     }
127
128     /**
129      * Test method for
130      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNodeConnectorId(NodeConnectorId, NodeId)}
131      * .
132      */
133     @Test
134     public void testToAdNodeConnectorId() {
135         NodeConnectorId nodeConnectorId = buildNodeConnectorId("1", "2");
136         NodeId nodeId = buildNodeId("1");
137         Assert.assertEquals(Short.valueOf((short) 2), NodeMapping.toADNodeConnectorId(nodeConnectorId , nodeId));
138     }
139
140     /**
141      * Test method for
142      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeRef(Node)}
143      * .
144      * @throws ConstructionException
145      */
146     @Test
147     public void testToNodeRef() throws ConstructionException {
148         org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
149         InstanceIdentifier<?> nodePath = NodeMapping.toNodeRef(node).getValue();
150
151         String observedId = nodePath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
152         Assert.assertEquals("openflow:42", observedId);
153     }
154
155     /**
156      * Test method for
157      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorRef(org.opendaylight.controller.sal.core.NodeConnector)}
158      * .
159      * @throws ConstructionException
160      */
161     @Test
162     public void testToNodeConnectorRef() throws ConstructionException {
163         org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
164         org.opendaylight.controller.sal.core.NodeConnector nodeConnector =
165                 new org.opendaylight.controller.sal.core.NodeConnector(
166                         NodeConnectorIDType.OPENFLOW, (short) 1, node);
167
168         InstanceIdentifier<?> nodeConnectorPath = NodeMapping.toNodeConnectorRef(nodeConnector ).getValue();
169         String observedNodeId = nodeConnectorPath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
170         Assert.assertEquals("openflow:42", observedNodeId);
171
172         String observedNodeConnectorId = nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
173         Assert.assertEquals("openflow:1", observedNodeConnectorId);
174     }
175
176     /**
177      * @param nodeId
178      * @param portId
179      * @return nodeConnectorId
180      */
181     public static NodeConnectorId buildNodeConnectorId(String nodeId, String portId) {
182         return new NodeConnectorId(NodeConnectorIDType.OPENFLOW+"|" + nodeId + ":" + portId);
183     }
184
185     /**
186      * @param id
187      * @return nodeId
188      */
189     public static NodeId buildNodeId(String id) {
190         return new NodeId(NodeConnectorIDType.OPENFLOW+"|" + id);
191     }
192 }