Merge "Removed unused private variable containerAwareRegistration - please review...
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / core / PathTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 /**
11  * @file   PathTest.java
12  *
13  * @brief  Unit Tests for Path element
14  *
15  * Unit Tests for Path element
16  */
17 package org.opendaylight.controller.sal.core;
18
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Arrays;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.opendaylight.controller.sal.core.ConstructionException;
25 import org.opendaylight.controller.sal.core.Edge;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.core.Path;
29
30 public class PathTest {
31     @Test
32     public void testPathValid() {
33         List<Edge> edges = null;
34         try {
35             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
36             NodeConnector c0 = new NodeConnector(
37                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
38                             (short) 0x4), n0);
39
40             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
41             NodeConnector c1 = new NodeConnector(
42                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
43                             (short) 0x1), n1);
44             NodeConnector c2 = new NodeConnector(
45                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
46                     new Short((short) 0xCAFE), n1);
47
48             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
49             NodeConnector c3 = new NodeConnector(
50                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
51                     new String("towardOF1"), n2);
52             NodeConnector c4 = new NodeConnector(
53                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
54                             "Gi1/0/1"), n2);
55
56             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
57             NodeConnector c5 = new NodeConnector(
58                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
59                             "Gi1/0/1"), n3);
60
61             Edge e0 = new Edge(c0, c1);
62             Edge e1 = new Edge(c1, c2);
63             Edge e2 = new Edge(c2, c3);
64             Edge e3 = new Edge(c3, c4);
65             Edge e4 = new Edge(c4, c5);
66             // actually need a LinkedList because we're going to .remove()
67             edges = new LinkedList<Edge>(Arrays.asList(e0, e1, e2, e3, e4));
68         } catch (ConstructionException e) {
69             // Exception is NOT expected if raised test will fail
70             Assert.assertTrue(false);
71         }
72
73         try {
74             Path res = new Path(edges);
75         } catch (ConstructionException e) {
76             // Exception is NOT expected if raised test will fail
77             Assert.assertTrue(false);
78         }
79
80         // Now lets disconnect on edge so to create a disconnected
81         // path, the constructor should catch that and should not
82         // create the path
83         edges.remove(2);
84
85         try {
86             Path res = new Path(edges);
87             // Exception is expected if not raised test will fail
88             Assert.assertTrue(false);
89         } catch (ConstructionException e) {
90         }
91     }
92
93     @Test
94     public void testPathComparison() {
95         List<Edge> edges1 = null;
96         Path path1 = null;
97         List<Edge> edges2 = null;
98         Path path2 = null;
99         List<Edge> edges3 = null;
100         Path path3 = null;
101
102         try {
103             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
104             NodeConnector c0 = new NodeConnector(
105                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
106                             (short) 0x4), n0);
107
108             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
109             NodeConnector c1 = new NodeConnector(
110                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
111                             (short) 0x1), n1);
112             NodeConnector c2 = new NodeConnector(
113                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
114                     new Short((short) 0xCAFE), n1);
115
116             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
117             NodeConnector c3 = new NodeConnector(
118                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
119                     new String("towardOF1"), n2);
120             NodeConnector c4 = new NodeConnector(
121                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
122                             "Gi1/0/1"), n2);
123
124             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
125             NodeConnector c5 = new NodeConnector(
126                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
127                             "Gi1/0/1"), n3);
128
129             Edge e0 = new Edge(c0, c1);
130             Edge e1 = new Edge(c1, c2);
131             Edge e2 = new Edge(c2, c3);
132             Edge e3 = new Edge(c3, c4);
133             Edge e4 = new Edge(c4, c5);
134             edges1 = Arrays.asList(e0, e1, e2, e3, e4);
135         } catch (ConstructionException e) {
136             // Exception is NOT expected if raised test will fail
137             Assert.assertTrue(false);
138         }
139
140         try {
141             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
142             NodeConnector c0 = new NodeConnector(
143                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
144                             (short) 0x4), n0);
145
146             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
147             NodeConnector c1 = new NodeConnector(
148                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
149                             (short) 0x1), n1);
150             NodeConnector c2 = new NodeConnector(
151                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
152                     new Short((short) 0xCAFE), n1);
153
154             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
155             NodeConnector c3 = new NodeConnector(
156                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
157                     new String("towardOF1"), n2);
158             NodeConnector c4 = new NodeConnector(
159                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
160                             "Gi1/0/1"), n2);
161
162             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
163             NodeConnector c5 = new NodeConnector(
164                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
165                             "Gi1/0/1"), n3);
166
167             Edge e0 = new Edge(c0, c1);
168             Edge e1 = new Edge(c1, c2);
169             Edge e2 = new Edge(c2, c3);
170             Edge e3 = new Edge(c3, c4);
171             Edge e4 = new Edge(c4, c5);
172             edges2 = Arrays.asList(e0, e1, e2, e3, e4);
173         } catch (ConstructionException e) {
174             // Exception is NOT expected if raised test will fail
175             Assert.assertTrue(false);
176         }
177
178         try {
179             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
180             NodeConnector c0 = new NodeConnector(
181                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
182                             (short) 0x5), n0);
183
184             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
185             NodeConnector c1 = new NodeConnector(
186                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
187                             (short) 0x1), n1);
188             NodeConnector c2 = new NodeConnector(
189                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
190                     new Short((short) 0xCAFE), n1);
191
192             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
193             NodeConnector c3 = new NodeConnector(
194                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
195                     new String("towardOF1"), n2);
196             NodeConnector c4 = new NodeConnector(
197                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
198                             "Gi1/0/1"), n2);
199
200             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
201             NodeConnector c5 = new NodeConnector(
202                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
203                             "Gi1/0/1"), n3);
204
205             Edge e0 = new Edge(c0, c1);
206             Edge e1 = new Edge(c1, c2);
207             Edge e2 = new Edge(c2, c3);
208             Edge e3 = new Edge(c3, c4);
209             Edge e4 = new Edge(c4, c5);
210             edges3 = Arrays.asList(e0, e1, e2, e3, e4);
211         } catch (ConstructionException e) {
212             // Exception is NOT expected if raised test will fail
213             Assert.assertTrue(false);
214         }
215
216         try {
217             path1 = new Path(edges1);
218             path2 = new Path(edges2);
219             path3 = new Path(edges3);
220
221             System.out.println("Path1: " + path1);
222             System.out.println("Path2: " + path2);
223             System.out.println("Path3: " + path3);
224
225             // Make sure the path are equals
226             Assert.assertTrue(path1.equals(path2));
227
228             // Make sure the path are marked as different
229             Assert.assertTrue(!path1.equals(path3));
230         } catch (ConstructionException e) {
231             // Exception is NOT expected if raised test will fail
232             Assert.assertTrue(false);
233         }
234     }
235
236     @Test
237     public void testPathEmpty() {
238         try {
239             Path path = new Path(new LinkedList());
240             // Exception is expected if not raised test will fail
241             Assert.assertTrue(false);
242         } catch (ConstructionException e) {
243         }
244     }
245
246     @Test
247     public void testPathOneElement() {
248         try {
249             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
250             NodeConnector c0 = new NodeConnector(
251                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
252                             (short) 0x5), n0);
253
254             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
255             NodeConnector c1 = new NodeConnector(
256                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
257                             (short) 0x1), n1);
258
259             Edge e0 = new Edge(c0, c1);
260
261             Path path = new Path(Arrays.asList(e0));
262         } catch (ConstructionException e) {
263             // Exception is NOT expected if raised test will fail
264             Assert.assertTrue(false);
265         }
266     }
267
268     @Test
269     public void testPathGetNodes() {
270         // Test on >2 edges paths
271         try {
272             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
273             NodeConnector c0 = new NodeConnector(
274                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
275                             (short) 0x4), n0);
276
277             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
278             NodeConnector c1 = new NodeConnector(
279                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
280                             (short) 0x1), n1);
281             NodeConnector c2 = new NodeConnector(
282                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
283                     new Short((short) 0xCAFE), n1);
284
285             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
286             NodeConnector c3 = new NodeConnector(
287                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
288                     new String("towardOF1"), n2);
289             NodeConnector c4 = new NodeConnector(
290                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
291                             "Gi1/0/1"), n2);
292
293             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
294             NodeConnector c5 = new NodeConnector(
295                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
296                             "Gi1/0/1"), n3);
297
298             Edge e0 = new Edge(c0, c1);
299             Edge e1 = new Edge(c1, c2);
300             Edge e2 = new Edge(c2, c3);
301             Edge e3 = new Edge(c3, c4);
302             Edge e4 = new Edge(c4, c5);
303             List<Edge> edges = Arrays.asList(e0, e1, e2, e3, e4);
304             Path path = new Path(edges);
305
306             // Test start node
307             Assert.assertTrue(path.getStartNode().equals(n0));
308
309             // Test end node
310             Assert.assertTrue(path.getEndNode().equals(n3));
311         } catch (ConstructionException e) {
312             // Exception is NOT expected if raised test will fail
313             Assert.assertTrue(false);
314         }
315
316         // Test on 1 edge path
317         try {
318             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
319             NodeConnector c0 = new NodeConnector(
320                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
321                             (short) 0x4), n0);
322
323             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
324             NodeConnector c1 = new NodeConnector(
325                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
326                             (short) 0x1), n1);
327
328             Edge e0 = new Edge(c0, c1);
329             List<Edge> edges = Arrays.asList(e0);
330             Path path = new Path(edges);
331
332             // Test start node
333             Assert.assertTrue(path.getStartNode().equals(n0));
334
335             // Test end node
336             Assert.assertTrue(path.getEndNode().equals(n1));
337         } catch (ConstructionException e) {
338             // Exception is NOT expected if raised test will fail
339             Assert.assertTrue(false);
340         }
341     }
342 }