blob: a33df98049300441de8b3d5a83ff0f741d573a73 [file] [log] [blame]
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -07001package net.onrc.onos.ofcontroller.routing;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.junit.Assert.fail;
7
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
13
14import org.easymock.EasyMock;
15
16import org.junit.After;
17import org.junit.Before;
18import org.junit.Ignore;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21
22import org.powermock.api.easymock.PowerMock;
23import org.powermock.core.classloader.annotations.PrepareForTest;
24import org.powermock.modules.junit4.PowerMockRunner;
25
26import com.thinkaurelius.titan.core.TitanGraph;
27import com.thinkaurelius.titan.core.TitanFactory;
28import com.tinkerpop.blueprints.Vertex;
29import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
30import com.tinkerpop.gremlin.java.GremlinPipeline;
31import com.tinkerpop.pipes.PipeFunction;
32import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle;
33
34import javax.script.ScriptContext;
35import javax.script.ScriptEngine;
36import javax.script.ScriptException;
37
38import net.onrc.onos.graph.GraphDBConnection;
39import net.onrc.onos.graph.GraphDBOperation;
40import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
41import net.onrc.onos.ofcontroller.routing.TopoRouteService;
42import net.onrc.onos.ofcontroller.util.DataPath;
43import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070044import net.onrc.onos.ofcontroller.util.FlowPathFlags;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070045import net.onrc.onos.ofcontroller.util.Port;
46import net.onrc.onos.ofcontroller.util.SwitchPort;
47
48/**
49 * A class for testing the TopoRouteService class.
50 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070051 */
52@RunWith(PowerMockRunner.class)
53@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, TopoRouteService.class})
54public class TopoRouteServiceTest {
55 String conf;
56 private GraphDBConnection conn = null;
57 private GraphDBOperation oper = null;
58 private TitanGraph titanGraph = null;
59 private TopoRouteService topoRouteService = null;
60
61 /**
62 * Setup the tests.
63 */
64 @Before
65 public void setUp() throws Exception {
66 conf = "/dummy/path/to/db";
67
68 //
69 // Make mock database.
70 // Replace TitanFactory.open() to return the mock database.
71 //
72 titanGraph = TestDatabaseManager.getTestDatabase();
73 PowerMock.mockStatic(TitanFactory.class);
74 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
75 PowerMock.replay(TitanFactory.class);
76
77 // Create the connection to the database
78 conn = GraphDBConnection.getInstance(conf);
79 oper = new GraphDBOperation(conn);
80
81 // Populate the database
82 TestDatabaseManager.populateTestData(titanGraph);
83
84 // Prepare the TopoRouteService instance
85 topoRouteService = new TopoRouteService();
86 topoRouteService.setDbOperationHandler(oper);
87 }
88
89 /**
90 * Cleanup after the tests.
91 */
92 @After
93 public void tearDown() throws Exception {
94 titanGraph.shutdown();
95 TestDatabaseManager.deleteTestDatabase();
96 }
97
98 /**
99 * Test method TopoRouteService.getTopoShortestPath()
100 *
101 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#getTopoShortestPath
102 */
103 @Test
104 public void test_getTopoShortestPath() {
105 DataPath dataPath = null;
106 String srcDpidStr = "00:00:00:00:00:00:0a:01";
107 String dstDpidStr = "00:00:00:00:00:00:0a:06";
108 short srcPortShort = 1;
109 short dstPortShort = 1;
110
111 //
112 // Initialize the source and destination points
113 //
114 Dpid srcDpid = new Dpid(srcDpidStr);
115 Port srcPort = new Port(srcPortShort);
116 Dpid dstDpid = new Dpid(dstDpidStr);
117 Port dstPort = new Port(dstPortShort);
118 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
119 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
120
121 //
122 // Test a valid Shortest-Path computation
123 //
124 Map<Long, ?> shortestPathTopo =
125 topoRouteService.prepareShortestPathTopo();
126 dataPath = topoRouteService.getTopoShortestPath(shortestPathTopo,
127 srcSwitchPort,
128 dstSwitchPort);
129 assertTrue(dataPath != null);
130 String dataPathSummaryStr = dataPath.dataPathSummary();
131 // System.out.println(dataPathSummaryStr);
132 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;";
133 assertEquals(dataPathSummaryStr, expectedResult);
134
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700135 // Test if we apply various Flow Path Flags
136 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;";
137 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
138 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
139 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
140 //
141 dataPath.applyFlowPathFlags(flowPathFlags2);
142 dataPathSummaryStr = dataPath.dataPathSummary();
143 assertEquals(dataPathSummaryStr, expectedResult2);
144 //
145 dataPath.applyFlowPathFlags(flowPathFlags3);
146 dataPathSummaryStr = dataPath.dataPathSummary();
147 assertEquals(dataPathSummaryStr, expectedResult3);
148
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700149 //
150 // Test Shortest-Path computation to non-existing destination
151 //
152 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
153 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
154 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
155 dataPath = topoRouteService.getTopoShortestPath(shortestPathTopo,
156 srcSwitchPort,
157 noSuchDstSwitchPort);
158 assertTrue(dataPath == null);
159
160 topoRouteService.dropShortestPathTopo(shortestPathTopo);
161 }
162
163 /**
164 * Test method TopoRouteService.getShortestPath()
165 *
166 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#getShortestPath
167 */
168 @Test
169 public void test_getShortestPath() {
170 DataPath dataPath = null;
171 String srcDpidStr = "00:00:00:00:00:00:0a:01";
172 String dstDpidStr = "00:00:00:00:00:00:0a:06";
173 short srcPortShort = 1;
174 short dstPortShort = 1;
175
176 //
177 // Initialize the source and destination points
178 //
179 Dpid srcDpid = new Dpid(srcDpidStr);
180 Port srcPort = new Port(srcPortShort);
181 Dpid dstDpid = new Dpid(dstDpidStr);
182 Port dstPort = new Port(dstPortShort);
183 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
184 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
185
186 //
187 // Test a valid Shortest-Path computation
188 //
189 dataPath = topoRouteService.getShortestPath(srcSwitchPort,
190 dstSwitchPort);
191 assertTrue(dataPath != null);
192 String dataPathSummaryStr = dataPath.dataPathSummary();
193 // System.out.println(dataPathSummaryStr);
194 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;";
195 assertEquals(dataPathSummaryStr, expectedResult);
196
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700197 // Test if we apply various Flow Path Flags
198 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;";
199 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
200 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
201 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
202 //
203 dataPath.applyFlowPathFlags(flowPathFlags2);
204 dataPathSummaryStr = dataPath.dataPathSummary();
205 assertEquals(dataPathSummaryStr, expectedResult2);
206 //
207 dataPath.applyFlowPathFlags(flowPathFlags3);
208 dataPathSummaryStr = dataPath.dataPathSummary();
209 assertEquals(dataPathSummaryStr, expectedResult3);
210
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700211 //
212 // Test Shortest-Path computation to non-existing destination
213 //
214 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
215 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
216 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
217
218 dataPath = topoRouteService.getShortestPath(srcSwitchPort,
219 noSuchDstSwitchPort);
220 assertTrue(dataPath == null);
221 }
222
223 /**
224 * Test method TopoRouteService.routeExists()
225 *
226 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#routeExists
227 */
228 @Test
229 public void test_routeExists() {
230 Boolean result;
231 String srcDpidStr = "00:00:00:00:00:00:0a:01";
232 String dstDpidStr = "00:00:00:00:00:00:0a:06";
233 short srcPortShort = 1;
234 short dstPortShort = 1;
235
236 //
237 // Initialize the source and destination points
238 //
239 Dpid srcDpid = new Dpid(srcDpidStr);
240 Port srcPort = new Port(srcPortShort);
241 Dpid dstDpid = new Dpid(dstDpidStr);
242 Port dstPort = new Port(dstPortShort);
243 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
244 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
245
246 //
247 // Test a valid route
248 //
249 result = topoRouteService.routeExists(srcSwitchPort, dstSwitchPort);
250 assertTrue(result == true);
251
252 //
253 // Test a non-existing route
254 //
255 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
256 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
257 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
258 result = topoRouteService.routeExists(srcSwitchPort,
259 noSuchDstSwitchPort);
260 assertTrue(result != true);
261 }
262}