Initial opendaylight infrastructure commit!!
[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             edges = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
67         } catch (ConstructionException e) {
68             // Exception is NOT expected if raised test will fail
69             Assert.assertTrue(false);
70         }
71
72         try {
73             Path res = new Path(edges);
74         } catch (ConstructionException e) {
75             // Exception is NOT expected if raised test will fail
76             Assert.assertTrue(false);
77         }
78
79         // Now lets disconnect on edge so to create a disconnected
80         // path, the constructor should catch that and should not
81         // create the path
82         edges.remove(2);
83
84         try {
85             Path res = new Path(edges);
86             // Exception is expected if not raised test will fail
87             Assert.assertTrue(false);
88         } catch (ConstructionException e) {
89         }
90     }
91
92     @Test
93     public void testPathComparison() {
94         List<Edge> edges1 = null;
95         Path path1 = null;
96         List<Edge> edges2 = null;
97         Path path2 = null;
98         List<Edge> edges3 = null;
99         Path path3 = null;
100
101         try {
102             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
103             NodeConnector c0 = new NodeConnector(
104                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
105                             (short) 0x4), n0);
106
107             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
108             NodeConnector c1 = new NodeConnector(
109                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
110                             (short) 0x1), n1);
111             NodeConnector c2 = new NodeConnector(
112                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
113                     new Short((short) 0xCAFE), n1);
114
115             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
116             NodeConnector c3 = new NodeConnector(
117                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
118                     new String("towardOF1"), n2);
119             NodeConnector c4 = new NodeConnector(
120                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
121                             "Gi1/0/1"), n2);
122
123             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
124             NodeConnector c5 = new NodeConnector(
125                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
126                             "Gi1/0/1"), n3);
127
128             Edge e0 = new Edge(c0, c1);
129             Edge e1 = new Edge(c1, c2);
130             Edge e2 = new Edge(c2, c3);
131             Edge e3 = new Edge(c3, c4);
132             Edge e4 = new Edge(c4, c5);
133             edges1 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
134         } catch (ConstructionException e) {
135             // Exception is NOT expected if raised test will fail
136             Assert.assertTrue(false);
137         }
138
139         try {
140             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
141             NodeConnector c0 = new NodeConnector(
142                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
143                             (short) 0x4), n0);
144
145             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
146             NodeConnector c1 = new NodeConnector(
147                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
148                             (short) 0x1), n1);
149             NodeConnector c2 = new NodeConnector(
150                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
151                     new Short((short) 0xCAFE), n1);
152
153             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
154             NodeConnector c3 = new NodeConnector(
155                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
156                     new String("towardOF1"), n2);
157             NodeConnector c4 = new NodeConnector(
158                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
159                             "Gi1/0/1"), n2);
160
161             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
162             NodeConnector c5 = new NodeConnector(
163                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
164                             "Gi1/0/1"), n3);
165
166             Edge e0 = new Edge(c0, c1);
167             Edge e1 = new Edge(c1, c2);
168             Edge e2 = new Edge(c2, c3);
169             Edge e3 = new Edge(c3, c4);
170             Edge e4 = new Edge(c4, c5);
171             edges2 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
172         } catch (ConstructionException e) {
173             // Exception is NOT expected if raised test will fail
174             Assert.assertTrue(false);
175         }
176
177         try {
178             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
179             NodeConnector c0 = new NodeConnector(
180                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
181                             (short) 0x5), n0);
182
183             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
184             NodeConnector c1 = new NodeConnector(
185                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
186                             (short) 0x1), n1);
187             NodeConnector c2 = new NodeConnector(
188                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
189                     new Short((short) 0xCAFE), n1);
190
191             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
192             NodeConnector c3 = new NodeConnector(
193                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
194                     new String("towardOF1"), n2);
195             NodeConnector c4 = new NodeConnector(
196                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
197                             "Gi1/0/1"), n2);
198
199             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
200             NodeConnector c5 = new NodeConnector(
201                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
202                             "Gi1/0/1"), n3);
203
204             Edge e0 = new Edge(c0, c1);
205             Edge e1 = new Edge(c1, c2);
206             Edge e2 = new Edge(c2, c3);
207             Edge e3 = new Edge(c3, c4);
208             Edge e4 = new Edge(c4, c5);
209             edges3 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
210         } catch (ConstructionException e) {
211             // Exception is NOT expected if raised test will fail
212             Assert.assertTrue(false);
213         }
214
215         try {
216             path1 = new Path(edges1);
217             path2 = new Path(edges2);
218             path3 = new Path(edges3);
219
220             System.out.println("Path1: " + path1);
221             System.out.println("Path2: " + path2);
222             System.out.println("Path3: " + path3);
223
224             // Make sure the path are equals
225             Assert.assertTrue(path1.equals(path2));
226
227             // Make sure the path are marked as different
228             Assert.assertTrue(!path1.equals(path3));
229         } catch (ConstructionException e) {
230             // Exception is NOT expected if raised test will fail
231             Assert.assertTrue(false);
232         }
233     }
234
235     @Test
236     public void testPathEmpty() {
237         try {
238             Path path = new Path(new LinkedList());
239             // Exception is expected if not raised test will fail
240             Assert.assertTrue(false);
241         } catch (ConstructionException e) {
242         }
243     }
244
245     @Test
246     public void testPathOneElement() {
247         try {
248             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
249             NodeConnector c0 = new NodeConnector(
250                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
251                             (short) 0x5), n0);
252
253             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
254             NodeConnector c1 = new NodeConnector(
255                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
256                             (short) 0x1), n1);
257
258             Edge e0 = new Edge(c0, c1);
259
260             Path path = new Path(new LinkedList(Arrays.asList(e0)));
261         } catch (ConstructionException e) {
262             // Exception is NOT expected if raised test will fail
263             Assert.assertTrue(false);
264         }
265     }
266
267     @Test
268     public void testPathGetNodes() {
269         // Test on >2 edges paths
270         try {
271             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
272             NodeConnector c0 = new NodeConnector(
273                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
274                             (short) 0x4), n0);
275
276             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
277             NodeConnector c1 = new NodeConnector(
278                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
279                             (short) 0x1), n1);
280             NodeConnector c2 = new NodeConnector(
281                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
282                     new Short((short) 0xCAFE), n1);
283
284             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
285             NodeConnector c3 = new NodeConnector(
286                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
287                     new String("towardOF1"), n2);
288             NodeConnector c4 = new NodeConnector(
289                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
290                             "Gi1/0/1"), n2);
291
292             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
293             NodeConnector c5 = new NodeConnector(
294                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
295                             "Gi1/0/1"), n3);
296
297             Edge e0 = new Edge(c0, c1);
298             Edge e1 = new Edge(c1, c2);
299             Edge e2 = new Edge(c2, c3);
300             Edge e3 = new Edge(c3, c4);
301             Edge e4 = new Edge(c4, c5);
302             List<Edge> edges = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
303             Path path = new Path(edges);
304
305             // Test start node
306             Assert.assertTrue(path.getStartNode().equals(n0));
307
308             // Test end node
309             Assert.assertTrue(path.getEndNode().equals(n3));
310         } catch (ConstructionException e) {
311             // Exception is NOT expected if raised test will fail
312             Assert.assertTrue(false);
313         }
314
315         // Test on 1 edge path
316         try {
317             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
318             NodeConnector c0 = new NodeConnector(
319                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
320                             (short) 0x4), n0);
321
322             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
323             NodeConnector c1 = new NodeConnector(
324                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
325                             (short) 0x1), n1);
326
327             Edge e0 = new Edge(c0, c1);
328             List<Edge> edges = new LinkedList(Arrays.asList(e0));
329             Path path = new Path(edges);
330
331             // Test start node
332             Assert.assertTrue(path.getStartNode().equals(n0));
333
334             // Test end node
335             Assert.assertTrue(path.getEndNode().equals(n1));
336         } catch (ConstructionException e) {
337             // Exception is NOT expected if raised test will fail
338             Assert.assertTrue(false);
339         }
340     }
341 }