BUG-1866: sal-compatibility does not register some services
[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         final String nodeUriPrefix = "opendaylight-inventory:nodes/node/";
106         nodeId = new NodeId(nodeUriPrefix + "iosv-2");
107         try {
108             observed = NodeMapping.toADNode(nodeId);
109             Assert.assertEquals("PR|opendaylight-inventory:nodes/node/iosv-2", observed.toString());
110         } catch (ConstructionException e) {
111             Assert.fail("should succeed to construct Node: "+e.getMessage());
112         }
113
114     }
115
116     /**
117      * Test method for
118      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorType(NodeConnectorId, NodeId)}
119      * .
120      */
121     @Test
122     public void testToNodeConnectorType() {
123         NodeConnectorId ncId;
124         NodeId nodeId = buildNodeId("1");
125
126         ncId = buildNodeConnectorId("1", "42");
127         Assert.assertEquals(NodeConnectorIDType.OPENFLOW, NodeMapping.toNodeConnectorType(ncId, nodeId ));
128
129         ncId = buildNodeConnectorId("1", OutputPortValues.CONTROLLER.toString());
130         Assert.assertEquals(NodeConnectorIDType.CONTROLLER, NodeMapping.toNodeConnectorType(ncId, nodeId ));
131
132         ncId = buildNodeConnectorId("1", OutputPortValues.NORMAL.toString());
133         Assert.assertEquals(NodeConnectorIDType.HWPATH, NodeMapping.toNodeConnectorType(ncId, nodeId ));
134
135         ncId = buildNodeConnectorId("1", OutputPortValues.LOCAL.toString());
136         Assert.assertEquals(NodeConnectorIDType.SWSTACK, NodeMapping.toNodeConnectorType(ncId, nodeId ));
137     }
138
139     /**
140      * Test method for
141      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADNodeConnectorId(NodeConnectorId, NodeId)}
142      * .
143      */
144     @Test
145     public void testToAdNodeConnectorId() {
146         NodeConnectorId nodeConnectorId = buildNodeConnectorId("1", "2");
147         NodeId nodeId = buildNodeId("1");
148         Assert.assertEquals(Short.valueOf((short) 2), NodeMapping.toADNodeConnectorId(nodeConnectorId , nodeId));
149     }
150
151     /**
152      * Test method for
153      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeRef(Node)}
154      * .
155      * @throws ConstructionException
156      */
157     @Test
158     public void testToNodeRef() throws ConstructionException {
159         org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
160         InstanceIdentifier<?> nodePath = NodeMapping.toNodeRef(node).getValue();
161
162         String observedId = nodePath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
163         Assert.assertEquals("openflow:42", observedId);
164     }
165
166     /**
167      * Test method for
168      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeConnectorRef(org.opendaylight.controller.sal.core.NodeConnector)}
169      * .
170      * @throws ConstructionException
171      */
172     @Test
173     public void testToNodeConnectorRef() throws ConstructionException {
174         org.opendaylight.controller.sal.core.Node node = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
175         org.opendaylight.controller.sal.core.NodeConnector nodeConnector =
176                 new org.opendaylight.controller.sal.core.NodeConnector(
177                         NodeConnectorIDType.OPENFLOW, (short) 1, node);
178
179         InstanceIdentifier<?> nodeConnectorPath = NodeMapping.toNodeConnectorRef(nodeConnector ).getValue();
180         String observedNodeId = nodeConnectorPath.firstKeyOf(Node.class, NodeKey.class).getId().getValue();
181         Assert.assertEquals("openflow:42", observedNodeId);
182
183         String observedNodeConnectorId = nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue();
184         Assert.assertEquals("openflow:1", observedNodeConnectorId);
185     }
186
187     /**
188      * Test method for
189      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#openflowFullNodeIdToLong(String)}
190      * .
191      * @throws ConstructionException
192      */
193     @Test
194     public void testOpenflowFullNodeIdToLong() throws ConstructionException {
195         Assert.assertEquals(42L, NodeMapping.openflowFullNodeIdToLong("42").longValue());
196         Assert.assertEquals(0xCC4E241C4A000000L, NodeMapping.openflowFullNodeIdToLong("14721743935839928320").longValue());
197     }
198
199     /**
200      * Test method for
201      * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toNodeKey(org.opendaylight.controller.sal.core.Node)}
202      * .
203      * @throws ConstructionException
204      */
205     @Test
206     public void testToNodeKey() throws ConstructionException {
207         org.opendaylight.controller.sal.core.Node aDNode = new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, 42L);
208         NodeKey nodeKey = NodeMapping.toNodeKey(aDNode);
209         Assert.assertEquals("openflow:42", nodeKey.getId().getValue());
210     }
211
212     /**
213      * @param nodeId
214      * @param portId
215      * @return nodeConnectorId
216      */
217     public static NodeConnectorId buildNodeConnectorId(String nodeId, String portId) {
218         return new NodeConnectorId(NodeConnectorIDType.OPENFLOW+"|" + nodeId + ":" + portId);
219     }
220
221     /**
222      * @param id
223      * @return nodeId
224      */
225     public static NodeId buildNodeId(String id) {
226         return new NodeId(NodeConnectorIDType.OPENFLOW+"|" + id);
227     }
228 }