blob: e054e05a834afcac81e203ddd5368d1f6b206775 [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 -07005
6import org.easymock.EasyMock;
7
8import org.junit.After;
9import org.junit.Before;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070010import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import org.powermock.api.easymock.PowerMock;
14import org.powermock.core.classloader.annotations.PrepareForTest;
15import org.powermock.modules.junit4.PowerMockRunner;
16
17import com.thinkaurelius.titan.core.TitanGraph;
18import com.thinkaurelius.titan.core.TitanFactory;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070019import net.onrc.onos.graph.GraphDBConnection;
20import net.onrc.onos.graph.GraphDBOperation;
21import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070022import net.onrc.onos.ofcontroller.topology.TopologyManager;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070023import net.onrc.onos.ofcontroller.util.DataPath;
24import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070025import net.onrc.onos.ofcontroller.util.FlowPathFlags;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070026import net.onrc.onos.ofcontroller.util.Port;
27import net.onrc.onos.ofcontroller.util.SwitchPort;
28
29/**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070030 * A class for testing the TopologyManager class.
31 * @see net.onrc.onos.ofcontroller.topology.TopologyManager
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070032 */
33@RunWith(PowerMockRunner.class)
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070034@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, TopologyManager.class})
Pavlin Radoslavov8e7ba4b2013-10-16 04:04:27 -070035public class TopologyManagerTest {
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070036 String conf;
37 private GraphDBConnection conn = null;
38 private GraphDBOperation oper = null;
39 private TitanGraph titanGraph = null;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070040 private TopologyManager topologyManager = null;
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070041
42 /**
43 * Setup the tests.
44 */
45 @Before
46 public void setUp() throws Exception {
47 conf = "/dummy/path/to/db";
48
49 //
50 // Make mock database.
51 // Replace TitanFactory.open() to return the mock database.
52 //
53 titanGraph = TestDatabaseManager.getTestDatabase();
54 PowerMock.mockStatic(TitanFactory.class);
55 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
56 PowerMock.replay(TitanFactory.class);
57
58 // Create the connection to the database
59 conn = GraphDBConnection.getInstance(conf);
60 oper = new GraphDBOperation(conn);
61
62 // Populate the database
63 TestDatabaseManager.populateTestData(titanGraph);
64
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070065 // Prepare the TopologyManager instance
Pavlin Radoslavov0367d352013-10-19 11:04:43 -070066 topologyManager = new TopologyManager(oper);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070067 }
68
69 /**
70 * Cleanup after the tests.
71 */
72 @After
73 public void tearDown() throws Exception {
74 titanGraph.shutdown();
75 TestDatabaseManager.deleteTestDatabase();
76 }
77
78 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070079 * Test method TopologyManager.getTopologyShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070080 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070081 * @see net.onrc.onos.ofcontroller.topology.TopologyManager#getTopologyShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070082 */
83 @Test
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070084 public void test_getTopologyShortestPath() {
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -070085 DataPath dataPath = null;
86 String srcDpidStr = "00:00:00:00:00:00:0a:01";
87 String dstDpidStr = "00:00:00:00:00:00:0a:06";
88 short srcPortShort = 1;
89 short dstPortShort = 1;
90
91 //
92 // Initialize the source and destination points
93 //
94 Dpid srcDpid = new Dpid(srcDpidStr);
95 Port srcPort = new Port(srcPortShort);
96 Dpid dstDpid = new Dpid(dstDpidStr);
97 Port dstPort = new Port(dstPortShort);
98 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
99 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
100
101 //
102 // Test a valid Shortest-Path computation
103 //
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700104 Topology topology = topologyManager.newDatabaseTopology();
105 dataPath = topologyManager.getTopologyShortestPath(topology,
106 srcSwitchPort,
107 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700108 assertTrue(dataPath != null);
109 String dataPathSummaryStr = dataPath.dataPathSummary();
110 // System.out.println(dataPathSummaryStr);
111 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;";
112 assertEquals(dataPathSummaryStr, expectedResult);
113
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700114 // Test if we apply various Flow Path Flags
115 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;";
116 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
117 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
118 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
119 //
120 dataPath.applyFlowPathFlags(flowPathFlags2);
121 dataPathSummaryStr = dataPath.dataPathSummary();
122 assertEquals(dataPathSummaryStr, expectedResult2);
123 //
124 dataPath.applyFlowPathFlags(flowPathFlags3);
125 dataPathSummaryStr = dataPath.dataPathSummary();
126 assertEquals(dataPathSummaryStr, expectedResult3);
127
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700128 //
129 // Test Shortest-Path computation to non-existing destination
130 //
131 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
132 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
133 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700134 dataPath = topologyManager.getTopologyShortestPath(topology,
135 srcSwitchPort,
136 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700137 assertTrue(dataPath == null);
138
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700139 topologyManager.dropTopology(topology);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700140 }
141
142 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700143 * Test method TopologyManager.getDatabaseShortestPath()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700144 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700145 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#getDatabaseShortestPath
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700146 */
147 @Test
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700148 public void test_getDatabaseShortestPath() {
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700149 DataPath dataPath = null;
150 String srcDpidStr = "00:00:00:00:00:00:0a:01";
151 String dstDpidStr = "00:00:00:00:00:00:0a:06";
152 short srcPortShort = 1;
153 short dstPortShort = 1;
154
155 //
156 // Initialize the source and destination points
157 //
158 Dpid srcDpid = new Dpid(srcDpidStr);
159 Port srcPort = new Port(srcPortShort);
160 Dpid dstDpid = new Dpid(dstDpidStr);
161 Port dstPort = new Port(dstPortShort);
162 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
163 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
164
165 //
166 // Test a valid Shortest-Path computation
167 //
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700168 dataPath = topologyManager.getDatabaseShortestPath(srcSwitchPort,
169 dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700170 assertTrue(dataPath != null);
171 String dataPathSummaryStr = dataPath.dataPathSummary();
172 // System.out.println(dataPathSummaryStr);
173 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;";
174 assertEquals(dataPathSummaryStr, expectedResult);
175
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700176 // Test if we apply various Flow Path Flags
177 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;";
178 String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;";
179 FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY");
180 FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY");
181 //
182 dataPath.applyFlowPathFlags(flowPathFlags2);
183 dataPathSummaryStr = dataPath.dataPathSummary();
184 assertEquals(dataPathSummaryStr, expectedResult2);
185 //
186 dataPath.applyFlowPathFlags(flowPathFlags3);
187 dataPathSummaryStr = dataPath.dataPathSummary();
188 assertEquals(dataPathSummaryStr, expectedResult3);
189
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700190 //
191 // Test Shortest-Path computation to non-existing destination
192 //
193 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
194 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
195 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
196
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700197 dataPath = topologyManager.getDatabaseShortestPath(srcSwitchPort,
198 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700199 assertTrue(dataPath == null);
200 }
201
202 /**
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700203 * Test method TopologyManager.routeExists()
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700204 *
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700205 * @see net.onrc.onos.ofcontroller.routing.TopologyManager#routeExists
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700206 */
207 @Test
208 public void test_routeExists() {
209 Boolean result;
210 String srcDpidStr = "00:00:00:00:00:00:0a:01";
211 String dstDpidStr = "00:00:00:00:00:00:0a:06";
212 short srcPortShort = 1;
213 short dstPortShort = 1;
214
215 //
216 // Initialize the source and destination points
217 //
218 Dpid srcDpid = new Dpid(srcDpidStr);
219 Port srcPort = new Port(srcPortShort);
220 Dpid dstDpid = new Dpid(dstDpidStr);
221 Port dstPort = new Port(dstPortShort);
222 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
223 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
224
225 //
226 // Test a valid route
227 //
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700228 result = topologyManager.routeExists(srcSwitchPort, dstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700229 assertTrue(result == true);
230
231 //
232 // Test a non-existing route
233 //
234 String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06";
235 Dpid noSuchDstDpid = new Dpid(noSuchDpidStr);
236 SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -0700237 result = topologyManager.routeExists(srcSwitchPort,
238 noSuchDstSwitchPort);
Pavlin Radoslavove3e03dc2013-06-27 15:49:05 -0700239 assertTrue(result != true);
240 }
241}