blob: 1922cfecdbeb1245f201e4ad622a491851ecf2e2 [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.core.internal;
2
3import static org.easymock.EasyMock.*;
4
Pankaj Berde38646d62013-06-21 11:34:04 -07005import net.onrc.onos.graph.GraphDBConnection;
6import net.onrc.onos.graph.GraphDBOperation;
Teruab4b01a2013-06-20 10:09:57 -07007import net.onrc.onos.ofcontroller.core.ISwitchStorage;
8import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
9import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070010import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
13import org.easymock.EasyMock;
14import org.junit.After;
15import org.junit.Before;
16import org.junit.Ignore;
17import 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;
27import com.thinkaurelius.titan.core.TitanGraph;
28
29//Add Powermock preparation
30@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;
37 private GraphDBConnection mockConn = null;
38 private GraphDBOperation mockOpe = null;
39 private GraphDBOperation realOpe = null;
40 private TitanGraph titanGraph = null;
41 ISwitchStorage swSt = null;
42
43 @Before
44 public void setUp() throws Exception {
45
46 swSt = new SwitchStorageImpl();
47 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
100 swSt.init(conf);
101 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);
122 replay(mockISw);
123
124 //Expectation of mock operation.
125 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
126 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
127 mockOpe.commit();
128 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
129 mockOpe.commit();
130 mockOpe.close();
131 replay(mockOpe);
132
133 swSt.init(conf);
134 swSt.addSwitch(dpid);
135 swSt.addSwitch(dpid);
136 }
137
138 /**
139 * Desc:
140 * Test method for addSwitch method.
141 * Condition:
142 * The switch construction is fail and return null
143 * Expect:
144 * Write the status as info log.
145 */
Teruab4b01a2013-06-20 10:09:57 -0700146 @Test
147 public void testAddSwitchAbnormal() {
148 String dpid = "00:00:00:00:00:00:0a:07";
149
150 //Expectation of mock operation.
151 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
152 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700153 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700154 mockOpe.close();
155 replay(mockOpe);
156
157 swSt.init(conf);
158 swSt.addSwitch(dpid);
159 }
160
161 /**
162 * Desc:
163 * Test method for addSwitch method.
164 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700165 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700166 * Expect:
167 * The rollback method is called.
168 */
169 //@Ignore
170 @Test
171 public void testAddSwitchException() {
172 String dpid = "00:00:00:00:00:00:0a:07";
173 String state = "ACTIVE";
174
175 //Mock Switch
176 ISwitchObject mockISw = createMock(ISwitchObject.class);
177 mockISw.setState(state);
178 replay(mockISw);
179
180 //Expectation of mock operation.
181 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
182 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
183 mockOpe.commit();
184 expectLastCall().andThrow(new RuntimeException());
185 mockOpe.rollback();
186 mockOpe.close();
187 replay(mockOpe);
188
189 swSt.init(conf);
190 swSt.addSwitch(dpid);
191 }
192
193 /**
194 * Desc:
195 * Test method for updateSwitch method.
196 * Condition:
197 * SwitchState : INACTIVE
198 * DMOPERATION : UPDATE
199 * Expect:
200 * Should call addSwitch function and commit.
201 */
202 //@Ignore
203 @Test
204 public void testUpdateUPDATE() {
205 String dpid = "00:00:00:00:00:00:0a:07";
206 SwitchState stateINACTIVE = SwitchState.INACTIVE;
207 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
208
209 //Mock Switch
210 ISwitchObject mockISw = createMock(ISwitchObject.class);
211 mockISw.setState("ACTIVE");
212 mockISw.setState(stateINACTIVE.toString());
213 replay(mockISw);
214
215 //Expectation of mock operation.
216 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
217 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
218 mockOpe.commit();
219 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
220 mockOpe.commit();
221 mockOpe.close();
222 replay(mockOpe);
223
224 swSt.init(conf);
225 swSt.update(dpid, stateINACTIVE, opUPDATE);
226 }
227
228 /**
229 * Desc:
230 * Test method for updateSwitch method.
231 * Condition:
232 * SwitchState : INACTIVE
233 * DMOPERATION : CREATE
234 * Expect:
235 * Should call addSwitch function and commit.
236 */
237 //@Ignore
238 @Test
239 public void testUpdateCREATE() {
240 String dpid = "00:00:00:00:00:00:0a:07";
241 SwitchState stateINACTIVE = SwitchState.INACTIVE;
242 DM_OPERATION opCREATE = DM_OPERATION.CREATE;
243
244 //Mock Switch
245 ISwitchObject mockISw = createMock(ISwitchObject.class);
246 mockISw.setState("ACTIVE");
247 mockISw.setState(stateINACTIVE.toString());
248 replay(mockISw);
249
250 //Expectation of mock operation.
251 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
252 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
253 mockOpe.commit();
254 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
255 mockOpe.commit();
256 mockOpe.close();
257 replay(mockOpe);
258
259 swSt.init(conf);
260 swSt.update(dpid, stateINACTIVE, opCREATE);
261 }
262
263 /**
264 * Desc:
265 * Test method for updateSwitch method.
266 * Condition:
267 * SwitchState : INACTIVE
268 * DMOPERATION : INSERT
269 * Expect:
270 * Should call addSwitch function and commit.
271 */
272 //@Ignore
273 @Test
274 public void testUpdateINSERT() {
275 String dpid = "00:00:00:00:00:00:0a:07";
276 SwitchState stateINACTIVE = SwitchState.INACTIVE;
277 DM_OPERATION opINSERT = DM_OPERATION.INSERT;
278
279 //Mock Switch
280 ISwitchObject mockISw = createMock(ISwitchObject.class);
281 mockISw.setState("ACTIVE");
282 mockISw.setState(stateINACTIVE.toString());
283 replay(mockISw);
284
285 //Expectation of mock operation.
286 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
287 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
288 mockOpe.commit();
289 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
290 mockOpe.commit();
291 mockOpe.close();
292 replay(mockOpe);
293
294 swSt.init(conf);
295 swSt.update(dpid, stateINACTIVE, opINSERT);
296 }
297
298 /**
299 * Desc:
300 * Test method for updateSwitch method.
301 * Condition:
302 * SwitchState : ACTIVE
303 * DMOPERATION : DELETE
304 * Expect:
305 * Should call removeSwitch function and commit.
306 */
307 //@Ignore
308 @Test
309 public void testUpdateDELETE() {
310 String dpid = "00:00:00:00:00:00:0a:07";
311 SwitchState stateACTIVE = SwitchState.ACTIVE;
312 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
313
314 //Mock Switch
315 ISwitchObject mockISw = createMock(ISwitchObject.class);
316 mockISw.setState(stateACTIVE.toString());
317 replay(mockISw);
318
319 //Expectation of mock operation.
320 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
321 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
322 mockOpe.commit();
323 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
324 mockOpe.removeSwitch(mockISw);
325 mockOpe.commit();
326 mockOpe.close();
327 replay(mockOpe);
328
329 swSt.init(conf);
330 swSt.addSwitch(dpid);
331 swSt.update(dpid, stateACTIVE, opDELETE);
332 }
333
334 /**
335 * Desc:
336 * Test method for deleteSwitch method.
337 * Condition:
338 * The switch is existing.
339 * Expect:
340 * Should call removeSwitch function and commit.
341 */
342 //@Ignore
343 @Test
344 public void testDeleteSwitch() {
345 String dpid = "00:00:00:00:00:00:0a:07";
346 String state = "ACTIVE";
347
348 //Mock Switch
349 ISwitchObject mockISw = createMock(ISwitchObject.class);
350 mockISw.setState(state);
351 replay(mockISw);
352
353 //Expectation of mock operation.
354 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
355 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
356 mockOpe.commit();
357 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
358 mockOpe.removeSwitch(mockISw);
359 mockOpe.commit();
360 mockOpe.close();
361 replay(mockOpe);
362
363 swSt.init(conf);
364 swSt.addSwitch(dpid);
365 swSt.deleteSwitch(dpid);
366
367 //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
368 //assertFalse(it.hasNext());
369 }
370
371 /**
372 * Desc:
373 * Test method for deleteSwitch method.
374 * Condition:
375 * The commit func throw exception.
376 * Expect:
377 * Should call rollback.
378 */
379 //@Ignore
380 @Test
381 public void testDeleteSwitchException() {
382 String dpid = "00:00:00:00:00:00:0a:07";
383 String state = "ACTIVE";
384 String type = "";
385
386 //Mock Switch
387 ISwitchObject mockISw = createMock(ISwitchObject.class);
388 mockISw.setState(state);
389 replay(mockISw);
390
391 //Expectation of mock operation.
392 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
393 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
394 mockOpe.commit();
395 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
396 mockOpe.removeSwitch(mockISw);
397 mockOpe.commit();
398 expectLastCall().andThrow(new RuntimeException());
399 mockOpe.rollback();
400 mockOpe.close();
401 replay(mockOpe);
402
403 swSt.init(conf);
404 swSt.addSwitch(dpid);
405 swSt.deleteSwitch(dpid);
406 }
407
408 /**
409 * Desc:
410 * Test method for addPort method.
411 * Condition:
412 * port is existing.
413 * Expect:
414 * Should call addPort and commit.
415 */
416 //@Ignore
417 @Test
418 public void testAddPort() {
419 String dpid = "00:00:00:00:00:00:0a:01";
420 short portNumber = 5;
421 String state = "ACTIVE";
422 String name = "port 5 at SEA switch";
423
424 OFPhysicalPort portToAdd = new OFPhysicalPort();
425 portToAdd.setName(name);
426 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
427 portToAdd.setPortNumber(portNumber);
428 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
429
430 //Expectation of mock Port
431 IPortObject mockIPort = createMock(IPortObject.class);
432 mockIPort.setState(state);
433 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
434 mockIPort.setDesc(name);
435 replay(mockIPort);
436
437 //Expectation of mock Switch
438 ISwitchObject mockISw = createMock(ISwitchObject.class);
439 mockISw.setState(state);
440 mockISw.addPort(mockIPort);
441 replay(mockISw);
442
443 //Expectation of mock operation.
444 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
445 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
446 mockOpe.commit();
447 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
448 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700449 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700450 mockOpe.commit();
451 mockOpe.close();
452 replay(mockOpe);
453
454 swSt.init(conf);
455 swSt.addSwitch(dpid);
456 swSt.addPort(dpid, portToAdd);
457 }
458
459 /**
460 * Desc:
461 * Test method for addPort method.
462 * Condition:
463 * Port status is down.
464 * Expect:
465 * Should call removePort and commit.
466 */
467 //@Ignore
468 @Test
469 public void testAddPortWithPortLinkDown() {
470 String dpid = "00:00:00:00:00:00:0a:01";
471 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700472 String swState = "ACTIVE";
473 String portState = "INACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700474 String name = "port 5 at SEA switch";
475
476 OFPhysicalPort portToAdd = new OFPhysicalPort();
477 portToAdd.setName(name);
478 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
479 portToAdd.setPortNumber(portNumber);
480 portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
481
482 //Expectation of mock Port
483 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700484 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700485 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
486 mockIPort.setDesc(name);
487 replay(mockIPort);
488
489 //Expectation of mock Switch
490 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700491 mockISw.setState(swState);
492// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700493 replay(mockISw);
494
495 //Expectation of mock operation.
496 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
497 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
498 mockOpe.commit();
499 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
500 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700501// mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700502 mockOpe.commit();
503 mockOpe.close();
504 replay(mockOpe);
505
506 swSt.init(conf);
507 swSt.addSwitch(dpid);
508 swSt.addPort(dpid, portToAdd);
509 }
510
511 /**
512 * Desc:
513 * Test method for addPort method.
514 * Condition:
515 * The switch is not existing.
516 * Expect:
517 * Nothing happens.
518 */
519 //@Ignore
520 @Test
521 public void testAddPortAbnormalNoSwitch() {
522 String dpid = "00:00:00:00:00:00:0a:01";
523 short portNumber = 5;
524 String state = "ACTIVE";
525 String name = "port 5 at SEA switch";
526
527 OFPhysicalPort portToAdd = new OFPhysicalPort();
528 portToAdd.setName(name);
529 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
530 portToAdd.setPortNumber(portNumber);
531 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
532
533 //Expectation of mock Port
534 IPortObject mockIPort = createStrictMock(IPortObject.class);
535 replay(mockIPort);
536
537 //Expectation of mock Switch
538 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
539 replay(mockISw);
540
541 //Expectation of mock operation.
542 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
543 mockOpe.close();
544 replay(mockOpe);
545
546 swSt.init(conf);
547 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);
583 replay(mockISw);
584
585 //Expectation of mock operation.
586 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
587 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
588 mockOpe.commit();
589 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
590 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700591 expect(mockOpe.newPort(dpid, portNumber)).andReturn(null);
Teruab4b01a2013-06-20 10:09:57 -0700592 mockOpe.rollback();
593 mockOpe.close();
594 replay(mockOpe);
595
596 swSt.init(conf);
597 swSt.addSwitch(dpid);
598 swSt.addPort(dpid, portToAdd);
599 }
600
601 /**
602 * Desc:
603 * Test method for addPort method.
604 * Condition:
605 * commit throw the exception.
606 * Expect:
607 * Should call rollback.
608 */
609 //@Ignore
610 @Test
611 public void testAddPortWithException() {
612 String dpid = "00:00:00:00:00:00:0a:01";
613 short portNumber = 5;
614 String state = "ACTIVE";
615 String name = "port 5 at SEA switch";
616
617 OFPhysicalPort portToAdd = new OFPhysicalPort();
618 portToAdd.setName(name);
619 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
620 portToAdd.setPortNumber(portNumber);
621 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
622
623 //Expectation of mock Port
624 IPortObject mockIPort = createMock(IPortObject.class);
625 mockIPort.setState(state);
626 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
627 mockIPort.setDesc(name);
628 replay(mockIPort);
629
630 //Expectation of mock Switch
631 ISwitchObject mockISw = createMock(ISwitchObject.class);
632 mockISw.setState(state);
633 mockISw.addPort(mockIPort);
634 replay(mockISw);
635
636 //Expectation of mock operation.
637 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
638 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
639 mockOpe.commit();
640 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
641 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700642 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700643 mockOpe.commit();
644 expectLastCall().andThrow(new RuntimeException());
645 mockOpe.rollback();
646 mockOpe.close();
647 replay(mockOpe);
648
649 swSt.init(conf);
650 swSt.addSwitch(dpid);
651 swSt.addPort(dpid, portToAdd);
652 }
653
654 /**
655 * Desc:
656 * Test method for deletePort method.
657 * Condition:
658 * port is existing.
659 * Expect:
660 * Should call removePort and commit.
661 */
662 //@Ignore
663 @Test
664 public void testDeletePort() {
665 String dpid = "00:00:00:00:00:00:0a:01";
666 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700667 String portState = "INACTIVE";
668 String swState = "ACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700669 String name = "port 5 at SEA switch";
670
671 OFPhysicalPort portToAdd = new OFPhysicalPort();
672 portToAdd.setName(name);
673 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
674 portToAdd.setPortNumber(portNumber);
675 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
676
677 //Expectation of mock Port
678 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700679 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700680 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
681 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700682 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700683 replay(mockIPort);
684
685 //Expectation of mock Switch
686 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700687 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700688 mockISw.addPort(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700689// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700690 replay(mockISw);
691
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700692
693
Teruab4b01a2013-06-20 10:09:57 -0700694 //Expectation of mock operation.
695 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
696 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
697 mockOpe.commit();
698 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
699 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700700 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700701 mockOpe.commit();
702 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
703 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700704// mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700705 mockOpe.commit();
706 mockOpe.close();
707 replay(mockOpe);
708
709 swSt.init(conf);
710 swSt.addSwitch(dpid);
711 swSt.addPort(dpid, portToAdd);
712 swSt.deletePort(dpid, portNumber);
713 }
714
715 /**
716 * Desc:
717 * Test method for addPort method.
718 * Condition:
719 * commit throws the exception.
720 * Expect:
721 * Should call rollback.
722 */
723 //@Ignore
724 @Test
725 public void testDeletePortException() {
726 String dpid = "00:00:00:00:00:00:0a:01";
727 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700728 String swState = "ACTIVE";
729 String portState = "INACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700730 String name = "port 5 at SEA switch";
731
732 OFPhysicalPort portToAdd = new OFPhysicalPort();
733 portToAdd.setName(name);
734 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
735 portToAdd.setPortNumber(portNumber);
736 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
737
738 //Expectation of mock Port
739 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700740 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700741 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
742 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700743 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700744 replay(mockIPort);
745
746 //Expectation of mock Switch
747 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700748 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700749 mockISw.addPort(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700750// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700751 replay(mockISw);
752
753 //Expectation of mock operation.
754 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
755 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
756 mockOpe.commit();
757 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
758 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700759 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700760 mockOpe.commit();
761 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
762 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700763 mockOpe.commit(); // Cannot generate exception..need to revisit this test
764// mockOpe.removePort(mockIPort);
765// expectLastCall().andThrow(new RuntimeException());
766// mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700767 mockOpe.close();
768 replay(mockOpe);
769
770 swSt.init(conf);
771 swSt.addSwitch(dpid);
772 swSt.addPort(dpid, portToAdd);
773 swSt.deletePort(dpid, portNumber);
774 }
775}