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