blob: e6d7d16bc7205c2fe535e7ad2d19dbfb47b81284 [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;
44import net.onrc.onos.ofcontroller.util.Port;
45import net.onrc.onos.ofcontroller.util.SwitchPort;
46
47/**
48 * A class for testing the TopoRouteService class.
49 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService
50 * @author Pavlin Radoslavov (pavlin@onlab.us)
51 */
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
135 //
136 // Test Shortest-Path computation to non-existing destination
137 //
138 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
139 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
140 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
141 dataPath = topoRouteService.getTopoShortestPath(shortestPathTopo,
142 srcSwitchPort,
143 noSuchDstSwitchPort);
144 assertTrue(dataPath == null);
145
146 topoRouteService.dropShortestPathTopo(shortestPathTopo);
147 }
148
149 /**
150 * Test method TopoRouteService.getShortestPath()
151 *
152 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#getShortestPath
153 */
154 @Test
155 public void test_getShortestPath() {
156 DataPath dataPath = null;
157 String srcDpidStr = "00:00:00:00:00:00:0a:01";
158 String dstDpidStr = "00:00:00:00:00:00:0a:06";
159 short srcPortShort = 1;
160 short dstPortShort = 1;
161
162 //
163 // Initialize the source and destination points
164 //
165 Dpid srcDpid = new Dpid(srcDpidStr);
166 Port srcPort = new Port(srcPortShort);
167 Dpid dstDpid = new Dpid(dstDpidStr);
168 Port dstPort = new Port(dstPortShort);
169 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
170 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
171
172 //
173 // Test a valid Shortest-Path computation
174 //
175 dataPath = topoRouteService.getShortestPath(srcSwitchPort,
176 dstSwitchPort);
177 assertTrue(dataPath != null);
178 String dataPathSummaryStr = dataPath.dataPathSummary();
179 // System.out.println(dataPathSummaryStr);
180 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;";
181 assertEquals(dataPathSummaryStr, expectedResult);
182
183 //
184 // Test Shortest-Path computation to non-existing destination
185 //
186 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
187 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
188 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
189
190 dataPath = topoRouteService.getShortestPath(srcSwitchPort,
191 noSuchDstSwitchPort);
192 assertTrue(dataPath == null);
193 }
194
195 /**
196 * Test method TopoRouteService.routeExists()
197 *
198 * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#routeExists
199 */
200 @Test
201 public void test_routeExists() {
202 Boolean result;
203 String srcDpidStr = "00:00:00:00:00:00:0a:01";
204 String dstDpidStr = "00:00:00:00:00:00:0a:06";
205 short srcPortShort = 1;
206 short dstPortShort = 1;
207
208 //
209 // Initialize the source and destination points
210 //
211 Dpid srcDpid = new Dpid(srcDpidStr);
212 Port srcPort = new Port(srcPortShort);
213 Dpid dstDpid = new Dpid(dstDpidStr);
214 Port dstPort = new Port(dstPortShort);
215 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
216 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
217
218 //
219 // Test a valid route
220 //
221 result = topoRouteService.routeExists(srcSwitchPort, dstSwitchPort);
222 assertTrue(result == true);
223
224 //
225 // Test a non-existing route
226 //
227 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
228 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
229 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
230 result = topoRouteService.routeExists(srcSwitchPort,
231 noSuchDstSwitchPort);
232 assertTrue(result != true);
233 }
234}