blob: 67c7ec36c5fc15f0e92e8afd1458bd401ad4f0fb [file] [log] [blame]
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -07001package net.onrc.onos.ofcontroller.routing;
2
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 Radoslavove3e03dc2013-06-27 15:49:05 -070036public class TopoRouteServiceTest {
37 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
67 topologyManager = new TopologyManager();
68 topologyManager.setDbOperationHandler(oper);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070069 }
70
71 /**
72 * Cleanup after the tests.
73 */
74 @After
75 public void tearDown() throws Exception {
76 titanGraph.shutdown();
77 TestDatabaseManager.deleteTestDatabase();
78 }
79
80 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070081 * Test method TopologyManager.getTopoShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070082 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070083 * @see net.onrc.onos.ofcontroller.topology.TopologyManager#getTopoShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070084 */
85 @Test
86 public void test_getTopoShortestPath() {
87 DataPath dataPath = null;
88 String srcDpidStr = "00:00:00:00:00:00:0a:01";
89 String dstDpidStr = "00:00:00:00:00:00:0a:06";
90 short srcPortShort = 1;
91 short dstPortShort = 1;
92
93 //
94 // Initialize the source and destination points
95 //
96 Dpid srcDpid = new Dpid(srcDpidStr);
97 Port srcPort = new Port(srcPortShort);
98 Dpid dstDpid = new Dpid(dstDpidStr);
99 Port dstPort = new Port(dstPortShort);
100 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
101 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
102
103 //
104 // Test a valid Shortest-Path computation
105 //
106 Map<Long, ?> shortestPathTopo =
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700107 topologyManager.prepareShortestPathTopo();
108 dataPath = topologyManager.getTopoShortestPath(shortestPathTopo,
109 srcSwitchPort,
110 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700111 assertTrue(dataPath != null);
112 String dataPathSummaryStr = dataPath.dataPathSummary();
113 // System.out.println(dataPathSummaryStr);
114 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;";
115 assertEquals(dataPathSummaryStr, expectedResult);
116
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700117 // Test if we apply various Flow Path Flags
118 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;";
119 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
120 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
121 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
122 //
123 dataPath.applyFlowPathFlags(flowPathFlags2);
124 dataPathSummaryStr = dataPath.dataPathSummary();
125 assertEquals(dataPathSummaryStr, expectedResult2);
126 //
127 dataPath.applyFlowPathFlags(flowPathFlags3);
128 dataPathSummaryStr = dataPath.dataPathSummary();
129 assertEquals(dataPathSummaryStr, expectedResult3);
130
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700131 //
132 // Test Shortest-Path computation to non-existing destination
133 //
134 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
135 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
136 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700137 dataPath = topologyManager.getTopoShortestPath(shortestPathTopo,
138 srcSwitchPort,
139 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700140 assertTrue(dataPath == null);
141
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700142 topologyManager.dropShortestPathTopo(shortestPathTopo);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700143 }
144
145 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700146 * Test method TopologyManager.getShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700147 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700148 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#getShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700149 */
150 @Test
151 public void test_getShortestPath() {
152 DataPath dataPath = null;
153 String srcDpidStr = "00:00:00:00:00:00:0a:01";
154 String dstDpidStr = "00:00:00:00:00:00:0a:06";
155 short srcPortShort = 1;
156 short dstPortShort = 1;
157
158 //
159 // Initialize the source and destination points
160 //
161 Dpid srcDpid = new Dpid(srcDpidStr);
162 Port srcPort = new Port(srcPortShort);
163 Dpid dstDpid = new Dpid(dstDpidStr);
164 Port dstPort = new Port(dstPortShort);
165 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
166 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
167
168 //
169 // Test a valid Shortest-Path computation
170 //
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700171 dataPath = topologyManager.getShortestPath(srcSwitchPort,
172 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700173 assertTrue(dataPath != null);
174 String dataPathSummaryStr = dataPath.dataPathSummary();
175 // System.out.println(dataPathSummaryStr);
176 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;";
177 assertEquals(dataPathSummaryStr, expectedResult);
178
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700179 // Test if we apply various Flow Path Flags
180 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;";
181 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
182 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
183 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
184 //
185 dataPath.applyFlowPathFlags(flowPathFlags2);
186 dataPathSummaryStr = dataPath.dataPathSummary();
187 assertEquals(dataPathSummaryStr, expectedResult2);
188 //
189 dataPath.applyFlowPathFlags(flowPathFlags3);
190 dataPathSummaryStr = dataPath.dataPathSummary();
191 assertEquals(dataPathSummaryStr, expectedResult3);
192
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700193 //
194 // Test Shortest-Path computation to non-existing destination
195 //
196 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
197 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
198 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
199
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700200 dataPath = topologyManager.getShortestPath(srcSwitchPort,
201 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700202 assertTrue(dataPath == null);
203 }
204
205 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700206 * Test method TopologyManager.routeExists()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700207 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700208 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#routeExists
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700209 */
210 @Test
211 public void test_routeExists() {
212 Boolean result;
213 String srcDpidStr = "00:00:00:00:00:00:0a:01";
214 String dstDpidStr = "00:00:00:00:00:00:0a:06";
215 short srcPortShort = 1;
216 short dstPortShort = 1;
217
218 //
219 // Initialize the source and destination points
220 //
221 Dpid srcDpid = new Dpid(srcDpidStr);
222 Port srcPort = new Port(srcPortShort);
223 Dpid dstDpid = new Dpid(dstDpidStr);
224 Port dstPort = new Port(dstPortShort);
225 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
226 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
227
228 //
229 // Test a valid route
230 //
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700231 result = topologyManager.routeExists(srcSwitchPort, dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700232 assertTrue(result == true);
233
234 //
235 // Test a non-existing route
236 //
237 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
238 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
239 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700240 result = topologyManager.routeExists(srcSwitchPort,
241 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700242 assertTrue(result != true);
243 }
244}