blob: 57aefd3378255a18da98a79cacfc6b6a8a7f1d86 [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.core.internal;
2
3import static org.junit.Assert.*;
4
5import net.floodlightcontroller.core.internal.TestDatabaseManager;
Pankaj Berde38646d62013-06-21 11:34:04 -07006import net.onrc.onos.graph.GraphDBConnection;
7import net.onrc.onos.graph.GraphDBOperation;
Teruab4b01a2013-06-20 10:09:57 -07008import net.onrc.onos.ofcontroller.core.ISwitchStorage;
9import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
10import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070011import net.onrc.onos.ofcontroller.core.INetMapStorage;
12import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
15import org.easymock.EasyMock;
16import org.junit.After;
17import org.junit.Before;
18import org.junit.Ignore;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.openflow.protocol.OFPhysicalPort;
22import org.openflow.protocol.OFPhysicalPort.OFPortState;
23import org.powermock.api.easymock.PowerMock;
24import org.powermock.core.classloader.annotations.PrepareForTest;
25import org.powermock.modules.junit4.PowerMockRunner;
26import org.slf4j.LoggerFactory;
27
28import com.thinkaurelius.titan.core.TitanFactory;
29import com.thinkaurelius.titan.core.TitanGraph;
30
31//Add Powermock preparation
32@RunWith(PowerMockRunner.class)
33@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
34public class SwitchStorageImplTestBB {
35
36 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
37
38 String conf;
39 private GraphDBConnection conn = null;
40 private GraphDBOperation ope = null;
41 private TitanGraph titanGraph = null;
42 ISwitchStorage swSt = null;
43
44 @Before
45 public void setUp() throws Exception {
46
47 swSt = new SwitchStorageImpl();
48 conf = "/dummy/path/to/db";
49
50 // Make mock cassandra DB
51 // Replace TitanFactory.open() to return mock DB
52 titanGraph = TestDatabaseManager.getTestDatabase();
53 TestDatabaseManager.populateTestData(titanGraph);
54 PowerMock.mockStatic(TitanFactory.class);
55 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
56 PowerMock.replay(TitanFactory.class);
57
58 conn = GraphDBConnection.getInstance(conf);
59 ope = new GraphDBOperation(conn);
60
61 swSt.init(conf);
62 }
63
64 @After
65 public void tearDown() throws Exception {
66
67 titanGraph.shutdown();
68 TestDatabaseManager.deleteTestDatabase();
69
70 swSt.close();
71 swSt = null;
72 }
73
74 /**
75 * Desc:
76 * Test method for addSwitch method.
77 * Condition:
78 * Normal
79 * Expect:
80 * 1. Switch should be generated.
81 * 2. The status of switch should be ACTIVE
82 */
83 //@Ignore
84 @Test
85 public void testAddSwitch() {
86 String dpid = "00:00:00:00:00:00:0a:07";
87
88 ISwitchObject sw = ope.searchSwitch(dpid);
89 assertTrue(sw == null);
90 swSt.addSwitch(dpid);
91 ISwitchObject sw2 = ope.searchSwitch(dpid);
92 assertTrue(sw2 != null);
93 assertEquals(sw2.getState(), "ACTIVE");
94 }
95
96 /**
97 * Desc:
98 * Test method for addSwitch method.
99 * Condition:
Teru4fd58642013-06-21 07:54:49 -0700100 * The existing switch status is INACTIVE.
101 * The switch is already existing.
Teruab4b01a2013-06-20 10:09:57 -0700102 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700103 * 1. After add the same switch, the status of switch should be ACTIVE
Teruab4b01a2013-06-20 10:09:57 -0700104 */
105 //@Ignore
106 @Test
107 public void testAddSwitchExisting() {
108 String dpid = "00:00:00:00:00:00:0a:06";
109
110 swSt.update(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE);
111 ISwitchObject sw = ope.searchSwitch(dpid);
112 assertTrue(sw != null);
113 assertEquals(sw.getState(), SwitchState.INACTIVE.toString());
114 swSt.addSwitch(dpid);
115 ISwitchObject sw2 = ope.searchSwitch(dpid);
116 assertTrue(sw2 != null);
117 assertEquals(sw2.getState(), SwitchState.ACTIVE.toString());
118 }
119
120 /**
121 * Desc:
122 * Test method for testUpdate method.
123 * Condition:
Teru4fd58642013-06-21 07:54:49 -0700124 * The switch is not existing.
125 * The status of added switch is INACTIVE.
126 * DM_OPERATION is CREATE.
Teruab4b01a2013-06-20 10:09:57 -0700127 * Expect:
128 * 1. Switch should be created.
129 * 2. The status of switch should be INACTIVE.
130 */
131 //@Ignore
132 @Test
133 public void testUpdate() {
134 String dpid = "00:00:00:00:00:00:0a:07";
135 SwitchState state = ISwitchStorage.SwitchState.INACTIVE;
136 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE;
137
138 ISwitchObject sw = ope.searchSwitch(dpid);
139 assertTrue(sw == null);
140 swSt.update(dpid, state, dmope);
141 ISwitchObject sw2 = ope.searchSwitch(dpid);
142 assertTrue(sw2 != null);
143 assertEquals(sw2.getState(), state.toString());
144 }
145
146 /**
147 * Desc:
148 * Test method for testUpdate method.
149 * Condition:
150 * The switch is existing.
Teru4fd58642013-06-21 07:54:49 -0700151 * The status of added switch is ACTIVE.
152 * DM_OPERATION is DELETE.
Teruab4b01a2013-06-20 10:09:57 -0700153 * Expect:
154 * 1. Switch should be deleted.
155 */
156 //@Ignore
157 @Test
158 public void testUpdateWithDELETE() {
159 String dpid = "00:00:00:00:00:00:0a:06";
160 SwitchState state = ISwitchStorage.SwitchState.ACTIVE;
161 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE;
162
163 ISwitchObject sw = ope.searchSwitch(dpid);
164 assertTrue(sw != null);
165 swSt.update(dpid, state, dmope);
166 ISwitchObject sw2 = ope.searchSwitch(dpid);
167 assertTrue(sw2 == null);
168 }
169
170 /**
171 * Desc:
172 * Test method for delete switch method.
173 * Condition:
174 * The switch is existing.
175 * Expect:
176 * 1. Switch should be deleted.
177 */
178 //@Ignore
179 @Test
180 public void testDeleteSwitch() {
181 String dpid = "00:00:00:00:00:00:0a:06";
182
183 ISwitchObject sw = ope.searchSwitch(dpid);
184 assertTrue(sw != null);
185 swSt.deleteSwitch(dpid);
186 ISwitchObject sw2 = ope.searchSwitch(dpid);
187 assertTrue(sw2 == null);
188 }
189
190 /**
191 * Desc:
192 * Test method for delete switch method.
193 * Condition:
194 * The switch is not existing.
195 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700196 * Nothing happens.
Teruab4b01a2013-06-20 10:09:57 -0700197 */
198 //@Ignore
199 @Test
200 public void testDeleteNonExistingSwitch() {
201 String dpid = "00:00:00:00:00:00:0a:07";
202
203 ISwitchObject sw = ope.searchSwitch(dpid);
204 assertTrue(sw == null);
205 swSt.deleteSwitch(dpid);
206 ISwitchObject sw2 = ope.searchSwitch(dpid);
207 assertTrue(sw2 == null);
208 }
209
210 /**
211 * Desc:
212 * Test method for delete port method.
213 * Condition:
214 * The port is existing.
215 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700216 * Deleted the port.
Teruab4b01a2013-06-20 10:09:57 -0700217 */
218 //@Ignore
219 @Test
220 public void testDeletePort() {
221 String dpid = "00:00:00:00:00:00:0a:06";
222 short portNumber = 3;
223
224 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
225 assertTrue(portObj1 != null);
226 swSt.deletePort(dpid, portNumber);
227 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
228 assertTrue(portObj2 == null);
229 }
230
231 /**
232 * Desc:
233 * Test method for delete port method.
234 * Condition:
235 * The port is not existing.
236 * Expect:
237 * Nothing happens.
238 */
239 //@Ignore
240 @Test
241 public void testDeleteNonExistingPort() {
242 String dpid = "00:00:00:00:00:00:0a:06";
243 short portNumber = 4;
244
245 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
246 assertTrue(portObj1 == null);
247 swSt.deletePort(dpid, portNumber);
248 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
249 assertTrue(portObj2 == null);
250 }
251
252 /**
253 * Desc:
Teru4fd58642013-06-21 07:54:49 -0700254 * Test method for add port method.
Teruab4b01a2013-06-20 10:09:57 -0700255 * Condition:
256 * The port is not existing.
257 * Expect:
258 * The port should be added.
Teru4fd58642013-06-21 07:54:49 -0700259 * The desc of IPortObject is the same as the name of OFPhysicalPort.
Teruab4b01a2013-06-20 10:09:57 -0700260 */
261 //@Ignore
262 @Test
263 public void testAddPort() {
264 String dpid = "00:00:00:00:00:00:0a:06";
265 short portNumber = 4;
266 String name = "port 4 at ATL Switch";
267 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
268 OFPhysicalPort port = new OFPhysicalPort();
269 port.setPortNumber(portNumber);
270 port.setName(name);
271 port.setState(state);
272
273 ISwitchObject sw = ope.searchSwitch(dpid);
274 assertTrue(sw != null);
275 swSt.addPort(dpid, port);
276 IPortObject portObj = ope.searchPort(dpid, portNumber);
277 assertTrue(portObj != null);
Teru4fd58642013-06-21 07:54:49 -0700278 assertEquals(portObj.getDesc(), name);
Teruab4b01a2013-06-20 10:09:57 -0700279 }
280
281 /**
282 * Desc:
283 * Test method for add method.
284 * Condition:
285 * The port is existing.
286 * Expect:
287 * Nothing happens.
288 */
289 //@Ignore
290 @Test
291 public void testAddExistingPort() {
292 String dpid = "00:00:00:00:00:00:0a:06";
293 short portNumber = 3;
Teru4fd58642013-06-21 07:54:49 -0700294 String name = "xxx";
Teruab4b01a2013-06-20 10:09:57 -0700295 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
296 OFPhysicalPort port = new OFPhysicalPort();
297 port.setPortNumber(portNumber);
298 port.setName(name);
299 port.setState(state);
300
301 ISwitchObject sw = ope.searchSwitch(dpid);
302 assertTrue(sw != null);
303 swSt.addPort(dpid, port);
304 IPortObject portObj = ope.searchPort(dpid, portNumber);
305 assertTrue(portObj != null);
306 }
307
308 /**
309 * Desc:
310 * Test method for add method.
311 * Condition:
312 * The port status is down.
313 * Expect:
314 * Delete the port.
315 */
316 //@Ignore
317 @Test
318 public void testAddDownPort() {
319 String dpid = "00:00:00:00:00:00:0a:06";
320 short portNumber = 3;
321 String name = "port 3 at ATL Switch";
322 int state = OFPortState.OFPPS_LINK_DOWN.getValue();
323 OFPhysicalPort port = new OFPhysicalPort();
324 port.setPortNumber(portNumber);
325 port.setName(name);
326 port.setState(state);
327
328 ISwitchObject sw = ope.searchSwitch(dpid);
329 assertTrue(sw != null);
330 swSt.addPort(dpid, port);
331 IPortObject portObj = ope.searchPort(dpid, portNumber);
332 assertTrue(portObj == null);
333 }
334}