blob: 8bdee72946af8f4d73af11d894d6777c822e87da [file] [log] [blame]
Pavlin Radoslavov8e7ba4b2013-10-16 04:04:27 -07001package net.onrc.onos.ofcontroller.topology;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -07002
3import static org.junit.Assert.assertEquals;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -07004import static org.junit.Assert.assertTrue;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -07005import java.util.Map;
6
7import org.easymock.EasyMock;
8
9import org.junit.After;
10import org.junit.Before;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070011import org.junit.Test;
12import org.junit.runner.RunWith;
13
14import org.powermock.api.easymock.PowerMock;
15import org.powermock.core.classloader.annotations.PrepareForTest;
16import org.powermock.modules.junit4.PowerMockRunner;
17
18import com.thinkaurelius.titan.core.TitanGraph;
19import com.thinkaurelius.titan.core.TitanFactory;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070020import net.onrc.onos.graph.GraphDBConnection;
21import net.onrc.onos.graph.GraphDBOperation;
22import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070023import net.onrc.onos.ofcontroller.topology.TopologyManager;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070024import net.onrc.onos.ofcontroller.util.DataPath;
25import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070026import net.onrc.onos.ofcontroller.util.FlowPathFlags;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070027import net.onrc.onos.ofcontroller.util.Port;
28import net.onrc.onos.ofcontroller.util.SwitchPort;
29
30/**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070031 * A class for testing the TopologyManager class.
32 * @see net.onrc.onos.ofcontroller.topology.TopologyManager
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070033 */
34@RunWith(PowerMockRunner.class)
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070035@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, TopologyManager.class})
Pavlin Radoslavov8e7ba4b2013-10-16 04:04:27 -070036public class TopologyManagerTest {
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070037 String conf;
38 private GraphDBConnection conn = null;
39 private GraphDBOperation oper = null;
40 private TitanGraph titanGraph = null;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070041 private TopologyManager topologyManager = null;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070042
43 /**
44 * Setup the tests.
45 */
46 @Before
47 public void setUp() throws Exception {
48 conf = "/dummy/path/to/db";
49
50 //
51 // Make mock database.
52 // Replace TitanFactory.open() to return the mock database.
53 //
54 titanGraph = TestDatabaseManager.getTestDatabase();
55 PowerMock.mockStatic(TitanFactory.class);
56 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
57 PowerMock.replay(TitanFactory.class);
58
59 // Create the connection to the database
60 conn = GraphDBConnection.getInstance(conf);
61 oper = new GraphDBOperation(conn);
62
63 // Populate the database
64 TestDatabaseManager.populateTestData(titanGraph);
65
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070066 // Prepare the TopologyManager instance
Pavlin Radoslavov0367d352013-10-19 11:04:43 -070067 topologyManager = new TopologyManager(oper);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070068 }
69
70 /**
71 * Cleanup after the tests.
72 */
73 @After
74 public void tearDown() throws Exception {
75 titanGraph.shutdown();
76 TestDatabaseManager.deleteTestDatabase();
77 }
78
79 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070080 * Test method TopologyManager.getTopoShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070081 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070082 * @see net.onrc.onos.ofcontroller.topology.TopologyManager#getTopoShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070083 */
84 @Test
85 public void test_getTopoShortestPath() {
86 DataPath dataPath = null;
87 String srcDpidStr = "00:00:00:00:00:00:0a:01";
88 String dstDpidStr = "00:00:00:00:00:00:0a:06";
89 short srcPortShort = 1;
90 short dstPortShort = 1;
91
92 //
93 // Initialize the source and destination points
94 //
95 Dpid srcDpid = new Dpid(srcDpidStr);
96 Port srcPort = new Port(srcPortShort);
97 Dpid dstDpid = new Dpid(dstDpidStr);
98 Port dstPort = new Port(dstPortShort);
99 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
100 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
101
102 //
103 // Test a valid Shortest-Path computation
104 //
105 Map<Long, ?> shortestPathTopo =
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700106 topologyManager.prepareShortestPathTopo();
107 dataPath = topologyManager.getTopoShortestPath(shortestPathTopo,
108 srcSwitchPort,
109 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700110 assertTrue(dataPath != null);
111 String dataPathSummaryStr = dataPath.dataPathSummary();
112 // System.out.println(dataPathSummaryStr);
113 String expectedResult = "1/00:00:00:00:00:00:0a:01/2;1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;";
114 assertEquals(dataPathSummaryStr, expectedResult);
115
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700116 // Test if we apply various Flow Path Flags
117 String expectedResult2 = "1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;";
118 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
119 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
120 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
121 //
122 dataPath.applyFlowPathFlags(flowPathFlags2);
123 dataPathSummaryStr = dataPath.dataPathSummary();
124 assertEquals(dataPathSummaryStr, expectedResult2);
125 //
126 dataPath.applyFlowPathFlags(flowPathFlags3);
127 dataPathSummaryStr = dataPath.dataPathSummary();
128 assertEquals(dataPathSummaryStr, expectedResult3);
129
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700130 //
131 // Test Shortest-Path computation to non-existing destination
132 //
133 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
134 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
135 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700136 dataPath = topologyManager.getTopoShortestPath(shortestPathTopo,
137 srcSwitchPort,
138 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700139 assertTrue(dataPath == null);
140
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700141 topologyManager.dropShortestPathTopo(shortestPathTopo);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700142 }
143
144 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700145 * Test method TopologyManager.getShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700146 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700147 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#getShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700148 */
149 @Test
150 public void test_getShortestPath() {
151 DataPath dataPath = null;
152 String srcDpidStr = "00:00:00:00:00:00:0a:01";
153 String dstDpidStr = "00:00:00:00:00:00:0a:06";
154 short srcPortShort = 1;
155 short dstPortShort = 1;
156
157 //
158 // Initialize the source and destination points
159 //
160 Dpid srcDpid = new Dpid(srcDpidStr);
161 Port srcPort = new Port(srcPortShort);
162 Dpid dstDpid = new Dpid(dstDpidStr);
163 Port dstPort = new Port(dstPortShort);
164 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
165 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
166
167 //
168 // Test a valid Shortest-Path computation
169 //
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700170 dataPath = topologyManager.getShortestPath(srcSwitchPort,
171 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700172 assertTrue(dataPath != null);
173 String dataPathSummaryStr = dataPath.dataPathSummary();
174 // System.out.println(dataPathSummaryStr);
175 String expectedResult = "1/00:00:00:00:00:00:0a:01/2;1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;";
176 assertEquals(dataPathSummaryStr, expectedResult);
177
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700178 // Test if we apply various Flow Path Flags
179 String expectedResult2 = "1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;";
180 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
181 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
182 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
183 //
184 dataPath.applyFlowPathFlags(flowPathFlags2);
185 dataPathSummaryStr = dataPath.dataPathSummary();
186 assertEquals(dataPathSummaryStr, expectedResult2);
187 //
188 dataPath.applyFlowPathFlags(flowPathFlags3);
189 dataPathSummaryStr = dataPath.dataPathSummary();
190 assertEquals(dataPathSummaryStr, expectedResult3);
191
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700192 //
193 // Test Shortest-Path computation to non-existing destination
194 //
195 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
196 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
197 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
198
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700199 dataPath = topologyManager.getShortestPath(srcSwitchPort,
200 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700201 assertTrue(dataPath == null);
202 }
203
204 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700205 * Test method TopologyManager.routeExists()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700206 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700207 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#routeExists
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700208 */
209 @Test
210 public void test_routeExists() {
211 Boolean result;
212 String srcDpidStr = "00:00:00:00:00:00:0a:01";
213 String dstDpidStr = "00:00:00:00:00:00:0a:06";
214 short srcPortShort = 1;
215 short dstPortShort = 1;
216
217 //
218 // Initialize the source and destination points
219 //
220 Dpid srcDpid = new Dpid(srcDpidStr);
221 Port srcPort = new Port(srcPortShort);
222 Dpid dstDpid = new Dpid(dstDpidStr);
223 Port dstPort = new Port(dstPortShort);
224 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
225 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
226
227 //
228 // Test a valid route
229 //
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700230 result = topologyManager.routeExists(srcSwitchPort, dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700231 assertTrue(result == true);
232
233 //
234 // Test a non-existing route
235 //
236 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
237 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
238 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700239 result = topologyManager.routeExists(srcSwitchPort,
240 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700241 assertTrue(result != true);
242 }
243}