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