blob: fdc13db806a341af2fdcc805c5fce6d3de9674b2 [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.core.internal;
2
3import static org.easymock.EasyMock.*;
Pankaj Berde38646d62013-06-21 11:34:04 -07004import net.onrc.onos.graph.GraphDBConnection;
5import net.onrc.onos.graph.GraphDBOperation;
Teruab4b01a2013-06-20 10:09:57 -07006import net.onrc.onos.ofcontroller.core.ISwitchStorage;
7import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
8import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -07009import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hartfb698512013-11-19 17:11:29 -080012
Teruab4b01a2013-06-20 10:09:57 -070013import org.easymock.EasyMock;
14import org.junit.After;
15import org.junit.Before;
Jonathan Hartfb698512013-11-19 17:11:29 -080016import org.junit.Ignore;
Teruab4b01a2013-06-20 10:09:57 -070017import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.openflow.protocol.OFPhysicalPort;
20import org.openflow.protocol.OFPhysicalPort.OFPortState;
21import org.powermock.api.easymock.PowerMock;
22import org.powermock.core.classloader.annotations.PrepareForTest;
23import org.powermock.modules.junit4.PowerMockRunner;
24import org.slf4j.LoggerFactory;
25
26import com.thinkaurelius.titan.core.TitanFactory;
Teruab4b01a2013-06-20 10:09:57 -070027
28//Add Powermock preparation
Jonathan Hartfb698512013-11-19 17:11:29 -080029@Ignore //TODO broken 11/19/13, should fix
Teruab4b01a2013-06-20 10:09:57 -070030@RunWith(PowerMockRunner.class)
31@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
32public class SwitchStorageImplTest {
33
34 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
35
36 String conf;
yoshi2fd4c7e2013-11-22 15:47:55 -080037 String dbStore;
Teruab4b01a2013-06-20 10:09:57 -070038 private GraphDBConnection mockConn = null;
39 private GraphDBOperation mockOpe = null;
Teruab4b01a2013-06-20 10:09:57 -070040 ISwitchStorage swSt = null;
41
42 @Before
43 public void setUp() throws Exception {
44
45 swSt = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -080046 dbStore = "dummyStore";
Teruab4b01a2013-06-20 10:09:57 -070047 conf = "/dummy/path/to/db";
48
49 // Make mock cassandra DB
50 // Replace TitanFactory.open() to return mock DB
51
52 PowerMock.mockStatic(GraphDBConnection.class);
53 mockConn = createMock(GraphDBConnection.class);
54 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
55 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
56 PowerMock.replay(GraphDBConnection.class);
57
58 PowerMock.mockStatic(GraphDBOperation.class);
59 mockOpe = PowerMock.createStrictMock(GraphDBOperation.class);
60 PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
61 PowerMock.replay(GraphDBOperation.class);
62 // Replace the conf to dummy conf
63 // String conf = "/tmp/cassandra.titan";
64
65
66 }
67
68 @After
69 public void tearDown() throws Exception {
70 swSt.close();
71 swSt = null;
72
73 }
74
75 /**
76 * Desc:
77 * Test method for addSwitch method.
78 * Condition:
79 * Normal
80 * Expect:
81 * Call SwitchStorageImpl.addSwitch func with proper properties.
82 */
Teruab4b01a2013-06-20 10:09:57 -070083 @Test
84 public void testAddSwitch() {
85 String dpid = "00:00:00:00:00:00:0a:07";
86 String state = "ACTIVE";
87
88 //Mock Switch
89 ISwitchObject mockISw = createMock(ISwitchObject.class);
90 mockISw.setState(state);
91 replay(mockISw);
92
93 //Expectation of mock operation.
94 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
95 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
96 mockOpe.commit();
97 mockOpe.close();
98 replay(mockOpe);
99
yoshi2fd4c7e2013-11-22 15:47:55 -0800100 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700101 swSt.addSwitch(dpid);
102 }
103
104 /**
105 * Desc:
106 * Test method for addSwitch method.
107 * Condition:
108 * The switch is already existing.
109 * Expect:
110 * Call SwitchStorageImpl.addSwitch func with proper properties.
111 */
112 //@Ignore
113 @Test
114 public void testAddSwitchExisting() {
115 String dpid = "00:00:00:00:00:00:0a:07";
116 String state = "ACTIVE";
117
118 //Mock Switch
119 ISwitchObject mockISw = createMock(ISwitchObject.class);
120 mockISw.setState(state);
121 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700122 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700123 replay(mockISw);
124
125 //Expectation of mock operation.
126 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
127 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
128 mockOpe.commit();
129 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
130 mockOpe.commit();
131 mockOpe.close();
132 replay(mockOpe);
133
yoshi2fd4c7e2013-11-22 15:47:55 -0800134 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700135 swSt.addSwitch(dpid);
136 swSt.addSwitch(dpid);
137 }
138
139 /**
140 * Desc:
141 * Test method for addSwitch method.
142 * Condition:
143 * The switch construction is fail and return null
144 * Expect:
145 * Write the status as info log.
146 */
Teruab4b01a2013-06-20 10:09:57 -0700147 @Test
148 public void testAddSwitchAbnormal() {
149 String dpid = "00:00:00:00:00:00:0a:07";
150
151 //Expectation of mock operation.
152 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
153 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700154 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700155 mockOpe.close();
156 replay(mockOpe);
157
yoshi2fd4c7e2013-11-22 15:47:55 -0800158 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700159 swSt.addSwitch(dpid);
160 }
161
162 /**
163 * Desc:
164 * Test method for addSwitch method.
165 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700166 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700167 * Expect:
168 * The rollback method is called.
169 */
170 //@Ignore
171 @Test
172 public void testAddSwitchException() {
173 String dpid = "00:00:00:00:00:00:0a:07";
174 String state = "ACTIVE";
175
176 //Mock Switch
177 ISwitchObject mockISw = createMock(ISwitchObject.class);
178 mockISw.setState(state);
179 replay(mockISw);
180
181 //Expectation of mock operation.
182 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
183 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
184 mockOpe.commit();
185 expectLastCall().andThrow(new RuntimeException());
186 mockOpe.rollback();
187 mockOpe.close();
188 replay(mockOpe);
189
yoshi2fd4c7e2013-11-22 15:47:55 -0800190 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700191 swSt.addSwitch(dpid);
192 }
193
194 /**
195 * Desc:
196 * Test method for updateSwitch method.
197 * Condition:
198 * SwitchState : INACTIVE
199 * DMOPERATION : UPDATE
200 * Expect:
201 * Should call addSwitch function and commit.
202 */
203 //@Ignore
204 @Test
205 public void testUpdateUPDATE() {
206 String dpid = "00:00:00:00:00:00:0a:07";
207 SwitchState stateINACTIVE = SwitchState.INACTIVE;
208 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
209
210 //Mock Switch
211 ISwitchObject mockISw = createMock(ISwitchObject.class);
Teruab4b01a2013-06-20 10:09:57 -0700212 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700213 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700214 replay(mockISw);
215
216 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700217 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
218 mockOpe.commit();
219 mockOpe.close();
220 replay(mockOpe);
221
yoshi2fd4c7e2013-11-22 15:47:55 -0800222 swSt.init(dbStore, conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700223 swSt.updateSwitch(dpid, stateINACTIVE, opUPDATE);
Teruab4b01a2013-06-20 10:09:57 -0700224 }
225
226 /**
227 * Desc:
228 * Test method for updateSwitch method.
229 * Condition:
230 * SwitchState : INACTIVE
231 * DMOPERATION : CREATE
232 * Expect:
233 * Should call addSwitch function and commit.
234 */
235 //@Ignore
236 @Test
237 public void testUpdateCREATE() {
238 String dpid = "00:00:00:00:00:00:0a:07";
239 SwitchState stateINACTIVE = SwitchState.INACTIVE;
240 DM_OPERATION opCREATE = DM_OPERATION.CREATE;
241
242 //Mock Switch
243 ISwitchObject mockISw = createMock(ISwitchObject.class);
244 mockISw.setState("ACTIVE");
245 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700246 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700247 replay(mockISw);
248
249 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700250 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
251 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700252 mockOpe.close();
253 replay(mockOpe);
254
yoshi2fd4c7e2013-11-22 15:47:55 -0800255 swSt.init(dbStore, conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700256 swSt.updateSwitch(dpid, stateINACTIVE, opCREATE);
Teruab4b01a2013-06-20 10:09:57 -0700257 }
258
259 /**
260 * Desc:
261 * Test method for updateSwitch method.
262 * Condition:
263 * SwitchState : INACTIVE
264 * DMOPERATION : INSERT
265 * Expect:
266 * Should call addSwitch function and commit.
267 */
268 //@Ignore
269 @Test
270 public void testUpdateINSERT() {
271 String dpid = "00:00:00:00:00:00:0a:07";
272 SwitchState stateINACTIVE = SwitchState.INACTIVE;
273 DM_OPERATION opINSERT = DM_OPERATION.INSERT;
274
275 //Mock Switch
276 ISwitchObject mockISw = createMock(ISwitchObject.class);
277 mockISw.setState("ACTIVE");
278 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700279 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700280 replay(mockISw);
281
282 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700283 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
284 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700285 mockOpe.close();
286 replay(mockOpe);
287
yoshi2fd4c7e2013-11-22 15:47:55 -0800288 swSt.init(dbStore, conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700289 swSt.updateSwitch(dpid, stateINACTIVE, opINSERT);
Teruab4b01a2013-06-20 10:09:57 -0700290 }
291
292 /**
293 * Desc:
294 * Test method for updateSwitch method.
295 * Condition:
296 * SwitchState : ACTIVE
297 * DMOPERATION : DELETE
298 * Expect:
299 * Should call removeSwitch function and commit.
300 */
301 //@Ignore
302 @Test
303 public void testUpdateDELETE() {
304 String dpid = "00:00:00:00:00:00:0a:07";
305 SwitchState stateACTIVE = SwitchState.ACTIVE;
306 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
307
308 //Mock Switch
309 ISwitchObject mockISw = createMock(ISwitchObject.class);
310 mockISw.setState(stateACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700311 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700312 replay(mockISw);
313
314 //Expectation of mock operation.
315 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
316 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
317 mockOpe.commit();
318 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
319 mockOpe.removeSwitch(mockISw);
320 mockOpe.commit();
321 mockOpe.close();
322 replay(mockOpe);
323
yoshi2fd4c7e2013-11-22 15:47:55 -0800324 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700325 swSt.addSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700326 swSt.updateSwitch(dpid, stateACTIVE, opDELETE);
Teruab4b01a2013-06-20 10:09:57 -0700327 }
328
329 /**
330 * Desc:
331 * Test method for deleteSwitch method.
332 * Condition:
333 * The switch is existing.
334 * Expect:
335 * Should call removeSwitch function and commit.
336 */
337 //@Ignore
338 @Test
339 public void testDeleteSwitch() {
340 String dpid = "00:00:00:00:00:00:0a:07";
341 String state = "ACTIVE";
342
343 //Mock Switch
344 ISwitchObject mockISw = createMock(ISwitchObject.class);
345 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700346 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700347 replay(mockISw);
348
349 //Expectation of mock operation.
350 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
351 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
352 mockOpe.commit();
353 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
354 mockOpe.removeSwitch(mockISw);
355 mockOpe.commit();
356 mockOpe.close();
357 replay(mockOpe);
358
yoshi2fd4c7e2013-11-22 15:47:55 -0800359 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700360 swSt.addSwitch(dpid);
361 swSt.deleteSwitch(dpid);
362
363 //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
364 //assertFalse(it.hasNext());
365 }
366
367 /**
368 * Desc:
369 * Test method for deleteSwitch method.
370 * Condition:
371 * The commit func throw exception.
372 * Expect:
373 * Should call rollback.
374 */
Teruab4b01a2013-06-20 10:09:57 -0700375 @Test
376 public void testDeleteSwitchException() {
377 String dpid = "00:00:00:00:00:00:0a:07";
378 String state = "ACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700379
380 //Mock Switch
381 ISwitchObject mockISw = createMock(ISwitchObject.class);
382 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700383 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700384 replay(mockISw);
385
386 //Expectation of mock operation.
387 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
388 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
389 mockOpe.commit();
390 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Yuta HIGUCHIb63f94f2013-10-14 10:01:45 -0700391 mockOpe.removeSwitch(mockISw);
392 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700393 expectLastCall().andThrow(new RuntimeException());
394 mockOpe.rollback();
395 mockOpe.close();
396 replay(mockOpe);
397
yoshi2fd4c7e2013-11-22 15:47:55 -0800398 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700399 swSt.addSwitch(dpid);
400 swSt.deleteSwitch(dpid);
401 }
402
403 /**
404 * Desc:
405 * Test method for addPort method.
406 * Condition:
407 * port is existing.
408 * Expect:
409 * Should call addPort and commit.
410 */
411 //@Ignore
412 @Test
413 public void testAddPort() {
414 String dpid = "00:00:00:00:00:00:0a:01";
415 short portNumber = 5;
416 String state = "ACTIVE";
417 String name = "port 5 at SEA switch";
418
419 OFPhysicalPort portToAdd = new OFPhysicalPort();
420 portToAdd.setName(name);
421 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
422 portToAdd.setPortNumber(portNumber);
423 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
424
425 //Expectation of mock Port
426 IPortObject mockIPort = createMock(IPortObject.class);
427 mockIPort.setState(state);
428 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
429 mockIPort.setDesc(name);
430 replay(mockIPort);
431
432 //Expectation of mock Switch
433 ISwitchObject mockISw = createMock(ISwitchObject.class);
434 mockISw.setState(state);
435 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700436 expect(mockISw.getPort(anyShort())).andReturn(null);
437 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700438 replay(mockISw);
439
440 //Expectation of mock operation.
441 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
442 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
443 mockOpe.commit();
444 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700445// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700446 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700447 mockOpe.commit();
448 mockOpe.close();
449 replay(mockOpe);
450
yoshi2fd4c7e2013-11-22 15:47:55 -0800451 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700452 swSt.addSwitch(dpid);
453 swSt.addPort(dpid, portToAdd);
454 }
455
456 /**
457 * Desc:
458 * Test method for addPort method.
459 * Condition:
460 * Port status is down.
461 * Expect:
462 * Should call removePort and commit.
463 */
464 //@Ignore
465 @Test
466 public void testAddPortWithPortLinkDown() {
467 String dpid = "00:00:00:00:00:00:0a:01";
468 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700469 String swState = "ACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700470// String portState = "INACTIVE";
471 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700472 String name = "port 5 at SEA switch";
473
474 OFPhysicalPort portToAdd = new OFPhysicalPort();
475 portToAdd.setName(name);
476 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
477 portToAdd.setPortNumber(portNumber);
478 portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
479
480 //Expectation of mock Port
481 IPortObject mockIPort = createMock(IPortObject.class);
Naoki Shiota987a5722013-10-23 11:59:36 -0700482 expect(mockIPort.getPortId()).andReturn(portId);
483// mockIPort.setState(portState);
484// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
485// mockIPort.setDesc(name);
Teruab4b01a2013-06-20 10:09:57 -0700486 replay(mockIPort);
487
488 //Expectation of mock Switch
489 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700490 mockISw.setState(swState);
491// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700492 expect(mockISw.getPort(anyShort())).andReturn(mockIPort);
493 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700494 replay(mockISw);
495
496 //Expectation of mock operation.
497 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
498 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700499 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700500 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700501// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
502 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700503 mockOpe.commit();
504 mockOpe.close();
505 replay(mockOpe);
506
yoshi2fd4c7e2013-11-22 15:47:55 -0800507 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700508 swSt.addSwitch(dpid);
509 swSt.addPort(dpid, portToAdd);
510 }
511
512 /**
513 * Desc:
514 * Test method for addPort method.
515 * Condition:
516 * The switch is not existing.
517 * Expect:
518 * Nothing happens.
519 */
Teruab4b01a2013-06-20 10:09:57 -0700520 @Test
521 public void testAddPortAbnormalNoSwitch() {
522 String dpid = "00:00:00:00:00:00:0a:01";
523 short portNumber = 5;
Teruab4b01a2013-06-20 10:09:57 -0700524 String name = "port 5 at SEA switch";
525
526 OFPhysicalPort portToAdd = new OFPhysicalPort();
527 portToAdd.setName(name);
528 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
529 portToAdd.setPortNumber(portNumber);
530 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
531
532 //Expectation of mock Port
533 IPortObject mockIPort = createStrictMock(IPortObject.class);
534 replay(mockIPort);
535
536 //Expectation of mock Switch
537 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
Naoki Shiota987a5722013-10-23 11:59:36 -0700538 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700539 replay(mockISw);
540
541 //Expectation of mock operation.
542 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
543 mockOpe.close();
544 replay(mockOpe);
545
yoshi2fd4c7e2013-11-22 15:47:55 -0800546 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700547 swSt.addPort(dpid, portToAdd);
548 }
549
550 /**
551 * Desc:
552 * Test method for addPort method.
553 * Condition:
554 * port is not existing.
555 * Expect:
556 * Should call addPort and commit.
557 */
558 //@Ignore
559 @Test
560 public void testAddPortAbnormalNoPort() {
561 String dpid = "00:00:00:00:00:00:0a:01";
562 short portNumber = 5;
563 String state = "ACTIVE";
564 String name = "port 5 at SEA switch";
565
566 OFPhysicalPort portToAdd = new OFPhysicalPort();
567 portToAdd.setName(name);
568 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
569 portToAdd.setPortNumber(portNumber);
570 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
571
572 //Expectation of mock Port
573 IPortObject mockIPort = createMock(IPortObject.class);
574 mockIPort.setState(state);
575 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
576 mockIPort.setDesc(name);
577 replay(mockIPort);
578
579 //Expectation of mock Switch
580 ISwitchObject mockISw = createMock(ISwitchObject.class);
581 mockISw.setState(state);
582 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700583 expect(mockISw.getPort(portNumber)).andReturn(null);
584 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700585 replay(mockISw);
586
587 //Expectation of mock operation.
588 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
589 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
590 mockOpe.commit();
591 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700592// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700593 expect(mockOpe.newPort(dpid, portNumber)).andReturn(null);
Teruab4b01a2013-06-20 10:09:57 -0700594 mockOpe.rollback();
595 mockOpe.close();
596 replay(mockOpe);
597
yoshi2fd4c7e2013-11-22 15:47:55 -0800598 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700599 swSt.addSwitch(dpid);
600 swSt.addPort(dpid, portToAdd);
601 }
602
603 /**
604 * Desc:
605 * Test method for addPort method.
606 * Condition:
607 * commit throw the exception.
608 * Expect:
609 * Should call rollback.
610 */
611 //@Ignore
612 @Test
613 public void testAddPortWithException() {
614 String dpid = "00:00:00:00:00:00:0a:01";
615 short portNumber = 5;
616 String state = "ACTIVE";
617 String name = "port 5 at SEA switch";
618
619 OFPhysicalPort portToAdd = new OFPhysicalPort();
620 portToAdd.setName(name);
621 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
622 portToAdd.setPortNumber(portNumber);
623 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
624
625 //Expectation of mock Port
626 IPortObject mockIPort = createMock(IPortObject.class);
627 mockIPort.setState(state);
628 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
629 mockIPort.setDesc(name);
630 replay(mockIPort);
631
632 //Expectation of mock Switch
633 ISwitchObject mockISw = createMock(ISwitchObject.class);
634 mockISw.setState(state);
635 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700636 expect(mockISw.getPort(portNumber)).andReturn(null);
637 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700638 replay(mockISw);
639
640 //Expectation of mock operation.
641 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
642 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
643 mockOpe.commit();
644 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700645// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700646 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700647 mockOpe.commit();
648 expectLastCall().andThrow(new RuntimeException());
649 mockOpe.rollback();
650 mockOpe.close();
651 replay(mockOpe);
652
yoshi2fd4c7e2013-11-22 15:47:55 -0800653 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700654 swSt.addSwitch(dpid);
655 swSt.addPort(dpid, portToAdd);
656 }
657
658 /**
659 * Desc:
660 * Test method for deletePort method.
661 * Condition:
662 * port is existing.
663 * Expect:
664 * Should call removePort and commit.
665 */
666 //@Ignore
667 @Test
668 public void testDeletePort() {
669 String dpid = "00:00:00:00:00:00:0a:01";
670 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700671 String portState = "INACTIVE";
672 String swState = "ACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700673 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700674 String name = "port 5 at SEA switch";
675
676 OFPhysicalPort portToAdd = new OFPhysicalPort();
677 portToAdd.setName(name);
678 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
679 portToAdd.setPortNumber(portNumber);
680 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
681
682 //Expectation of mock Port
683 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700684 mockIPort.setState(swState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700685// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
Teruab4b01a2013-06-20 10:09:57 -0700686 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700687 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700688 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700689 replay(mockIPort);
690
691 //Expectation of mock Switch
692 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700693 mockISw.setState(swState);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700694// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700695 expect(mockISw.getPort(portNumber)).andReturn(null);
696 mockISw.addPort(mockIPort);
697 expect(mockISw.getPort(portNumber)).andReturn(mockIPort);
698 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700699 replay(mockISw);
700
701 //Expectation of mock operation.
702 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
703 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
704 mockOpe.commit();
705 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700706// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700707 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700708 mockOpe.commit();
709 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700710// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
711 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700712 mockOpe.commit();
713 mockOpe.close();
714 replay(mockOpe);
715
yoshi2fd4c7e2013-11-22 15:47:55 -0800716 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700717 swSt.addSwitch(dpid);
718 swSt.addPort(dpid, portToAdd);
719 swSt.deletePort(dpid, portNumber);
720 }
721
722 /**
723 * Desc:
724 * Test method for addPort method.
725 * Condition:
726 * commit throws the exception.
727 * Expect:
728 * Should call rollback.
729 */
730 //@Ignore
731 @Test
732 public void testDeletePortException() {
733 String dpid = "00:00:00:00:00:00:0a:01";
734 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700735 String swState = "ACTIVE";
736 String portState = "INACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700737 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700738 String name = "port 5 at SEA switch";
739
740 OFPhysicalPort portToAdd = new OFPhysicalPort();
741 portToAdd.setName(name);
742 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
743 portToAdd.setPortNumber(portNumber);
744 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
745
746 //Expectation of mock Port
747 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700748 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700749 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
750 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700751 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700752 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700753 replay(mockIPort);
754
755 //Expectation of mock Switch
756 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700757 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700758 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700759 expect(mockISw.getPort(portNumber)).andReturn(null);
760 expect(mockISw.getPort(portNumber)).andReturn(mockIPort).anyTimes();
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700761// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700762
763 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700764 replay(mockISw);
765
766 //Expectation of mock operation.
767 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
768 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
769 mockOpe.commit();
770 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700771// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700772 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700773 mockOpe.commit();
774 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700775// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
776// mockOpe.commit(); // Cannot generate exception..need to revisit this test
777 mockOpe.removePort(mockIPort);
778 expectLastCall().andThrow(new RuntimeException());
779 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700780 mockOpe.close();
781 replay(mockOpe);
782
yoshi2fd4c7e2013-11-22 15:47:55 -0800783 swSt.init(dbStore, conf);
Teruab4b01a2013-06-20 10:09:57 -0700784 swSt.addSwitch(dpid);
785 swSt.addPort(dpid, portToAdd);
786 swSt.deletePort(dpid, portNumber);
787 }
788}