blob: be9f34429cfa90a304bd144081103c3fc4f42b4b [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koidefe1d5d92014-02-26 20:09:48 -08002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
Jonathan Hartaa380972014-04-03 10:24:46 -07006import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hart472062d2014-04-03 10:56:48 -07007import net.onrc.onos.core.topology.LinkEvent;
8import net.onrc.onos.core.topology.Path;
Toshio Koidefe1d5d92014-02-26 20:09:48 -08009
10import org.junit.After;
11import org.junit.Before;
12import org.junit.Test;
13
14/**
15 * @author Toshio Koide (t-koide@onlab.us)
16 */
17public class ConstrainedBFSTreeTest {
18 static long LOCAL_PORT = 0xFFFEL;
19
20 @Before
21 public void setUp() throws Exception {
22 }
23
24 @After
25 public void tearDown() throws Exception {
26 }
27
28 @Test
29 public void testCreate() {
30 MockNetworkGraph graph = new MockNetworkGraph();
31 graph.createSampleTopology1();
32 ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L));
33 assertNotNull(tree);
34 }
35
36 @Test
37 public void testCreateConstrained() {
38 MockNetworkGraph graph = new MockNetworkGraph();
39 graph.createSampleTopology1();
40 PathIntentMap intents = new PathIntentMap();
41 ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 1000.0);
42 assertNotNull(tree);
43 }
44
45 @Test
46 public void testGetPath() {
47 MockNetworkGraph graph = new MockNetworkGraph();
48 graph.createSampleTopology1();
49 ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L));
50 Path path11 = tree.getPath(graph.getSwitch(1L));
51 Path path12 = tree.getPath(graph.getSwitch(2L));
52 Path path13 = tree.getPath(graph.getSwitch(3L));
53 Path path14 = tree.getPath(graph.getSwitch(4L));
54
55 assertNotNull(path11);
56 assertEquals(0, path11.size());
57
58 assertNotNull(path12);
59 assertEquals(1, path12.size());
60 assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path12.get(0));
61
62 assertNotNull(path13);
63 assertEquals(2, path13.size());
64 if (path13.get(0).getDst().getDpid() == 2L) {
65 assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path13.get(0));
66 assertEquals(new LinkEvent(graph.getLink(2L, 23L)), path13.get(1));
67 }
68 else {
69 assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path13.get(0));
70 assertEquals(new LinkEvent(graph.getLink(4L, 43L)), path13.get(1));
71 }
72
73 assertNotNull(path14);
74 assertEquals(1, path14.size());
75 assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path14.get(0));
76 }
77
78 @Test
79 public void testGetPathNull() {
80 MockNetworkGraph graph = new MockNetworkGraph();
81 graph.createSampleTopology1();
82 graph.removeLink(1L, 12L, 2L, 21L);
83 graph.removeLink(1L, 14L, 4L, 41L);
84 // now, there is no path from switch 1, but to switch1
85
86 ConstrainedBFSTree tree1 = new ConstrainedBFSTree(graph.getSwitch(1L));
87 Path path12 = tree1.getPath(graph.getSwitch(2L));
88 Path path13 = tree1.getPath(graph.getSwitch(3L));
89 Path path14 = tree1.getPath(graph.getSwitch(4L));
90
91 ConstrainedBFSTree tree2 = new ConstrainedBFSTree(graph.getSwitch(2L));
92 Path path21 = tree2.getPath(graph.getSwitch(1L));
93
94 assertNull(path12);
95 assertNull(path13);
96 assertNull(path14);
97 assertNotNull(path21);
98 assertEquals(1, path21.size());
99 assertEquals(new LinkEvent(graph.getLink(2L, 21L)), path21.get(0));
100 }
101
102 @Test
103 public void testGetConstrainedPath() {
104 MockNetworkGraph graph = new MockNetworkGraph();
105 graph.createSampleTopology1();
106 PathIntentMap intents = new PathIntentMap();
107 IntentOperationList intentOps = new IntentOperationList();
108
109 // create constrained shortest path intents that have the same source destination ports
110 ConstrainedShortestPathIntent intent1 = new ConstrainedShortestPathIntent(
111 "1", 1L, LOCAL_PORT, 0x111L, 2L, LOCAL_PORT, 0x222L, 600.0);
112 ConstrainedShortestPathIntent intent2 = new ConstrainedShortestPathIntent(
113 "2", 1L, LOCAL_PORT, 0x333L, 2L, LOCAL_PORT, 0x444L, 600.0);
114
115 // calculate path of the intent1
116 ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
117 Path path1 = tree.getPath(graph.getSwitch(2L));
118
119 assertNotNull(path1);
120 assertEquals(1, path1.size());
121 assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path1.get(0));
122
123 PathIntent pathIntent1 = new PathIntent("pi1", path1, 600.0, intent1);
124 intentOps.add(Operator.ADD, pathIntent1);
125 intents.executeOperations(intentOps);
126
127 // calculate path of the intent2
128 tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
129 Path path2 = tree.getPath(graph.getSwitch(2L));
130
131 assertNotNull(path2);
132 assertEquals(2, path2.size());
133 assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path2.get(0));
134 assertEquals(new LinkEvent(graph.getLink(4L, 42L)), path2.get(1));
135
136 PathIntent pathIntent2 = new PathIntent("pi2", path2, 600.0, intent2);
137 intentOps.add(Operator.ADD, pathIntent2);
138 intents.executeOperations(intentOps);
139
140 // calculate path of the intent3
141 tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
142 Path path3 = tree.getPath(graph.getSwitch(2L));
143
144 assertNull(path3);
145 }
146}