blob: 24c17f4c06062ad3bd77a3838f9c751563e7d733 [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;
Teruab4b01a2013-06-20 10:09:57 -070016import org.junit.Test;
17import org.junit.runner.RunWith;
18import org.openflow.protocol.OFPhysicalPort;
19import org.openflow.protocol.OFPhysicalPort.OFPortState;
20import org.powermock.api.easymock.PowerMock;
21import org.powermock.core.classloader.annotations.PrepareForTest;
22import org.powermock.modules.junit4.PowerMockRunner;
23import org.slf4j.LoggerFactory;
24
25import com.thinkaurelius.titan.core.TitanFactory;
Teruab4b01a2013-06-20 10:09:57 -070026
27//Add Powermock preparation
28@RunWith(PowerMockRunner.class)
29@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
30public class SwitchStorageImplTest {
31
32 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
33
34 String conf;
35 private GraphDBConnection mockConn = null;
36 private GraphDBOperation mockOpe = null;
Teruab4b01a2013-06-20 10:09:57 -070037 ISwitchStorage swSt = null;
38
39 @Before
40 public void setUp() throws Exception {
41
42 swSt = new SwitchStorageImpl();
43 conf = "/dummy/path/to/db";
44
45 // Make mock cassandra DB
46 // Replace TitanFactory.open() to return mock DB
47
48 PowerMock.mockStatic(GraphDBConnection.class);
49 mockConn = createMock(GraphDBConnection.class);
50 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
51 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
52 PowerMock.replay(GraphDBConnection.class);
53
54 PowerMock.mockStatic(GraphDBOperation.class);
55 mockOpe = PowerMock.createStrictMock(GraphDBOperation.class);
56 PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
57 PowerMock.replay(GraphDBOperation.class);
58 // Replace the conf to dummy conf
59 // String conf = "/tmp/cassandra.titan";
60
61
62 }
63
64 @After
65 public void tearDown() throws Exception {
66 swSt.close();
67 swSt = null;
68
69 }
70
71 /**
72 * Desc:
73 * Test method for addSwitch method.
74 * Condition:
75 * Normal
76 * Expect:
77 * Call SwitchStorageImpl.addSwitch func with proper properties.
78 */
Teruab4b01a2013-06-20 10:09:57 -070079 @Test
80 public void testAddSwitch() {
81 String dpid = "00:00:00:00:00:00:0a:07";
82 String state = "ACTIVE";
83
84 //Mock Switch
85 ISwitchObject mockISw = createMock(ISwitchObject.class);
86 mockISw.setState(state);
87 replay(mockISw);
88
89 //Expectation of mock operation.
90 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
91 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
92 mockOpe.commit();
93 mockOpe.close();
94 replay(mockOpe);
95
96 swSt.init(conf);
97 swSt.addSwitch(dpid);
98 }
99
100 /**
101 * Desc:
102 * Test method for addSwitch method.
103 * Condition:
104 * The switch is already existing.
105 * Expect:
106 * Call SwitchStorageImpl.addSwitch func with proper properties.
107 */
108 //@Ignore
109 @Test
110 public void testAddSwitchExisting() {
111 String dpid = "00:00:00:00:00:00:0a:07";
112 String state = "ACTIVE";
113
114 //Mock Switch
115 ISwitchObject mockISw = createMock(ISwitchObject.class);
116 mockISw.setState(state);
117 mockISw.setState(state);
118 replay(mockISw);
119
120 //Expectation of mock operation.
121 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
122 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
123 mockOpe.commit();
124 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
125 mockOpe.commit();
126 mockOpe.close();
127 replay(mockOpe);
128
129 swSt.init(conf);
130 swSt.addSwitch(dpid);
131 swSt.addSwitch(dpid);
132 }
133
134 /**
135 * Desc:
136 * Test method for addSwitch method.
137 * Condition:
138 * The switch construction is fail and return null
139 * Expect:
140 * Write the status as info log.
141 */
Teruab4b01a2013-06-20 10:09:57 -0700142 @Test
143 public void testAddSwitchAbnormal() {
144 String dpid = "00:00:00:00:00:00:0a:07";
145
146 //Expectation of mock operation.
147 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
148 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700149 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700150 mockOpe.close();
151 replay(mockOpe);
152
153 swSt.init(conf);
154 swSt.addSwitch(dpid);
155 }
156
157 /**
158 * Desc:
159 * Test method for addSwitch method.
160 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700161 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700162 * Expect:
163 * The rollback method is called.
164 */
165 //@Ignore
166 @Test
167 public void testAddSwitchException() {
168 String dpid = "00:00:00:00:00:00:0a:07";
169 String state = "ACTIVE";
170
171 //Mock Switch
172 ISwitchObject mockISw = createMock(ISwitchObject.class);
173 mockISw.setState(state);
174 replay(mockISw);
175
176 //Expectation of mock operation.
177 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
178 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
179 mockOpe.commit();
180 expectLastCall().andThrow(new RuntimeException());
181 mockOpe.rollback();
182 mockOpe.close();
183 replay(mockOpe);
184
185 swSt.init(conf);
186 swSt.addSwitch(dpid);
187 }
188
189 /**
190 * Desc:
191 * Test method for updateSwitch method.
192 * Condition:
193 * SwitchState : INACTIVE
194 * DMOPERATION : UPDATE
195 * Expect:
196 * Should call addSwitch function and commit.
197 */
198 //@Ignore
199 @Test
200 public void testUpdateUPDATE() {
201 String dpid = "00:00:00:00:00:00:0a:07";
202 SwitchState stateINACTIVE = SwitchState.INACTIVE;
203 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
204
205 //Mock Switch
206 ISwitchObject mockISw = createMock(ISwitchObject.class);
207 mockISw.setState("ACTIVE");
208 mockISw.setState(stateINACTIVE.toString());
209 replay(mockISw);
210
211 //Expectation of mock operation.
212 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
213 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
214 mockOpe.commit();
215 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
216 mockOpe.commit();
217 mockOpe.close();
218 replay(mockOpe);
219
220 swSt.init(conf);
221 swSt.update(dpid, stateINACTIVE, opUPDATE);
222 }
223
224 /**
225 * Desc:
226 * Test method for updateSwitch method.
227 * Condition:
228 * SwitchState : INACTIVE
229 * DMOPERATION : CREATE
230 * Expect:
231 * Should call addSwitch function and commit.
232 */
233 //@Ignore
234 @Test
235 public void testUpdateCREATE() {
236 String dpid = "00:00:00:00:00:00:0a:07";
237 SwitchState stateINACTIVE = SwitchState.INACTIVE;
238 DM_OPERATION opCREATE = DM_OPERATION.CREATE;
239
240 //Mock Switch
241 ISwitchObject mockISw = createMock(ISwitchObject.class);
242 mockISw.setState("ACTIVE");
243 mockISw.setState(stateINACTIVE.toString());
244 replay(mockISw);
245
246 //Expectation of mock operation.
247 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
248 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
249 mockOpe.commit();
250 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
251 mockOpe.commit();
252 mockOpe.close();
253 replay(mockOpe);
254
255 swSt.init(conf);
256 swSt.update(dpid, stateINACTIVE, opCREATE);
257 }
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());
279 replay(mockISw);
280
281 //Expectation of mock operation.
282 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
283 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
284 mockOpe.commit();
285 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
286 mockOpe.commit();
287 mockOpe.close();
288 replay(mockOpe);
289
290 swSt.init(conf);
291 swSt.update(dpid, stateINACTIVE, opINSERT);
292 }
293
294 /**
295 * Desc:
296 * Test method for updateSwitch method.
297 * Condition:
298 * SwitchState : ACTIVE
299 * DMOPERATION : DELETE
300 * Expect:
301 * Should call removeSwitch function and commit.
302 */
303 //@Ignore
304 @Test
305 public void testUpdateDELETE() {
306 String dpid = "00:00:00:00:00:00:0a:07";
307 SwitchState stateACTIVE = SwitchState.ACTIVE;
308 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
309
310 //Mock Switch
311 ISwitchObject mockISw = createMock(ISwitchObject.class);
312 mockISw.setState(stateACTIVE.toString());
313 replay(mockISw);
314
315 //Expectation of mock operation.
316 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
317 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
318 mockOpe.commit();
319 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
320 mockOpe.removeSwitch(mockISw);
321 mockOpe.commit();
322 mockOpe.close();
323 replay(mockOpe);
324
325 swSt.init(conf);
326 swSt.addSwitch(dpid);
327 swSt.update(dpid, stateACTIVE, opDELETE);
328 }
329
330 /**
331 * Desc:
332 * Test method for deleteSwitch method.
333 * Condition:
334 * The switch is existing.
335 * Expect:
336 * Should call removeSwitch function and commit.
337 */
338 //@Ignore
339 @Test
340 public void testDeleteSwitch() {
341 String dpid = "00:00:00:00:00:00:0a:07";
342 String state = "ACTIVE";
343
344 //Mock Switch
345 ISwitchObject mockISw = createMock(ISwitchObject.class);
346 mockISw.setState(state);
347 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
359 swSt.init(conf);
360 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);
383 replay(mockISw);
384
385 //Expectation of mock operation.
386 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
387 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
388 mockOpe.commit();
389 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Yuta HIGUCHIb63f94f2013-10-14 10:01:45 -0700390 mockOpe.removeSwitch(mockISw);
391 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700392 expectLastCall().andThrow(new RuntimeException());
393 mockOpe.rollback();
394 mockOpe.close();
395 replay(mockOpe);
396
397 swSt.init(conf);
398 swSt.addSwitch(dpid);
399 swSt.deleteSwitch(dpid);
400 }
401
402 /**
403 * Desc:
404 * Test method for addPort method.
405 * Condition:
406 * port is existing.
407 * Expect:
408 * Should call addPort and commit.
409 */
410 //@Ignore
411 @Test
412 public void testAddPort() {
413 String dpid = "00:00:00:00:00:00:0a:01";
414 short portNumber = 5;
415 String state = "ACTIVE";
416 String name = "port 5 at SEA switch";
417
418 OFPhysicalPort portToAdd = new OFPhysicalPort();
419 portToAdd.setName(name);
420 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
421 portToAdd.setPortNumber(portNumber);
422 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
423
424 //Expectation of mock Port
425 IPortObject mockIPort = createMock(IPortObject.class);
426 mockIPort.setState(state);
427 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
428 mockIPort.setDesc(name);
429 replay(mockIPort);
430
431 //Expectation of mock Switch
432 ISwitchObject mockISw = createMock(ISwitchObject.class);
433 mockISw.setState(state);
434 mockISw.addPort(mockIPort);
435 replay(mockISw);
436
437 //Expectation of mock operation.
438 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
439 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
440 mockOpe.commit();
441 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
442 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700443 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700444 mockOpe.commit();
445 mockOpe.close();
446 replay(mockOpe);
447
448 swSt.init(conf);
449 swSt.addSwitch(dpid);
450 swSt.addPort(dpid, portToAdd);
451 }
452
453 /**
454 * Desc:
455 * Test method for addPort method.
456 * Condition:
457 * Port status is down.
458 * Expect:
459 * Should call removePort and commit.
460 */
461 //@Ignore
462 @Test
463 public void testAddPortWithPortLinkDown() {
464 String dpid = "00:00:00:00:00:00:0a:01";
465 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700466 String swState = "ACTIVE";
467 String portState = "INACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700468 String name = "port 5 at SEA switch";
469
470 OFPhysicalPort portToAdd = new OFPhysicalPort();
471 portToAdd.setName(name);
472 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
473 portToAdd.setPortNumber(portNumber);
474 portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
475
476 //Expectation of mock Port
477 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700478 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700479 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
480 mockIPort.setDesc(name);
481 replay(mockIPort);
482
483 //Expectation of mock Switch
484 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700485 mockISw.setState(swState);
486// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700487 replay(mockISw);
488
489 //Expectation of mock operation.
490 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
491 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
492 mockOpe.commit();
493 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
494 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700495// mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700496 mockOpe.commit();
497 mockOpe.close();
498 replay(mockOpe);
499
500 swSt.init(conf);
501 swSt.addSwitch(dpid);
502 swSt.addPort(dpid, portToAdd);
503 }
504
505 /**
506 * Desc:
507 * Test method for addPort method.
508 * Condition:
509 * The switch is not existing.
510 * Expect:
511 * Nothing happens.
512 */
Teruab4b01a2013-06-20 10:09:57 -0700513 @Test
514 public void testAddPortAbnormalNoSwitch() {
515 String dpid = "00:00:00:00:00:00:0a:01";
516 short portNumber = 5;
Teruab4b01a2013-06-20 10:09:57 -0700517 String name = "port 5 at SEA switch";
518
519 OFPhysicalPort portToAdd = new OFPhysicalPort();
520 portToAdd.setName(name);
521 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
522 portToAdd.setPortNumber(portNumber);
523 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
524
525 //Expectation of mock Port
526 IPortObject mockIPort = createStrictMock(IPortObject.class);
527 replay(mockIPort);
528
529 //Expectation of mock Switch
530 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
531 replay(mockISw);
532
533 //Expectation of mock operation.
534 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
535 mockOpe.close();
536 replay(mockOpe);
537
538 swSt.init(conf);
539 swSt.addPort(dpid, portToAdd);
540 }
541
542 /**
543 * Desc:
544 * Test method for addPort method.
545 * Condition:
546 * port is not existing.
547 * Expect:
548 * Should call addPort and commit.
549 */
550 //@Ignore
551 @Test
552 public void testAddPortAbnormalNoPort() {
553 String dpid = "00:00:00:00:00:00:0a:01";
554 short portNumber = 5;
555 String state = "ACTIVE";
556 String name = "port 5 at SEA switch";
557
558 OFPhysicalPort portToAdd = new OFPhysicalPort();
559 portToAdd.setName(name);
560 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
561 portToAdd.setPortNumber(portNumber);
562 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
563
564 //Expectation of mock Port
565 IPortObject mockIPort = createMock(IPortObject.class);
566 mockIPort.setState(state);
567 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
568 mockIPort.setDesc(name);
569 replay(mockIPort);
570
571 //Expectation of mock Switch
572 ISwitchObject mockISw = createMock(ISwitchObject.class);
573 mockISw.setState(state);
574 mockISw.addPort(mockIPort);
575 replay(mockISw);
576
577 //Expectation of mock operation.
578 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
579 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
580 mockOpe.commit();
581 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
582 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700583 expect(mockOpe.newPort(dpid, portNumber)).andReturn(null);
Teruab4b01a2013-06-20 10:09:57 -0700584 mockOpe.rollback();
585 mockOpe.close();
586 replay(mockOpe);
587
588 swSt.init(conf);
589 swSt.addSwitch(dpid);
590 swSt.addPort(dpid, portToAdd);
591 }
592
593 /**
594 * Desc:
595 * Test method for addPort method.
596 * Condition:
597 * commit throw the exception.
598 * Expect:
599 * Should call rollback.
600 */
601 //@Ignore
602 @Test
603 public void testAddPortWithException() {
604 String dpid = "00:00:00:00:00:00:0a:01";
605 short portNumber = 5;
606 String state = "ACTIVE";
607 String name = "port 5 at SEA switch";
608
609 OFPhysicalPort portToAdd = new OFPhysicalPort();
610 portToAdd.setName(name);
611 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
612 portToAdd.setPortNumber(portNumber);
613 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
614
615 //Expectation of mock Port
616 IPortObject mockIPort = createMock(IPortObject.class);
617 mockIPort.setState(state);
618 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
619 mockIPort.setDesc(name);
620 replay(mockIPort);
621
622 //Expectation of mock Switch
623 ISwitchObject mockISw = createMock(ISwitchObject.class);
624 mockISw.setState(state);
625 mockISw.addPort(mockIPort);
626 replay(mockISw);
627
628 //Expectation of mock operation.
629 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
630 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
631 mockOpe.commit();
632 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
633 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700634 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700635 mockOpe.commit();
636 expectLastCall().andThrow(new RuntimeException());
637 mockOpe.rollback();
638 mockOpe.close();
639 replay(mockOpe);
640
641 swSt.init(conf);
642 swSt.addSwitch(dpid);
643 swSt.addPort(dpid, portToAdd);
644 }
645
646 /**
647 * Desc:
648 * Test method for deletePort method.
649 * Condition:
650 * port is existing.
651 * Expect:
652 * Should call removePort and commit.
653 */
654 //@Ignore
655 @Test
656 public void testDeletePort() {
657 String dpid = "00:00:00:00:00:00:0a:01";
658 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700659 String portState = "INACTIVE";
660 String swState = "ACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700661 String name = "port 5 at SEA switch";
662
663 OFPhysicalPort portToAdd = new OFPhysicalPort();
664 portToAdd.setName(name);
665 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
666 portToAdd.setPortNumber(portNumber);
667 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
668
669 //Expectation of mock Port
670 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700671 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700672 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
673 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700674 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700675 replay(mockIPort);
676
677 //Expectation of mock Switch
678 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700679 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700680 mockISw.addPort(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700681// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700682 replay(mockISw);
683
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700684
685
Teruab4b01a2013-06-20 10:09:57 -0700686 //Expectation of mock operation.
687 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
688 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
689 mockOpe.commit();
690 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
691 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700692 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700693 mockOpe.commit();
694 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
695 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700696// mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700697 mockOpe.commit();
698 mockOpe.close();
699 replay(mockOpe);
700
701 swSt.init(conf);
702 swSt.addSwitch(dpid);
703 swSt.addPort(dpid, portToAdd);
704 swSt.deletePort(dpid, portNumber);
705 }
706
707 /**
708 * Desc:
709 * Test method for addPort method.
710 * Condition:
711 * commit throws the exception.
712 * Expect:
713 * Should call rollback.
714 */
715 //@Ignore
716 @Test
717 public void testDeletePortException() {
718 String dpid = "00:00:00:00:00:00:0a:01";
719 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700720 String swState = "ACTIVE";
721 String portState = "INACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700722 String name = "port 5 at SEA switch";
723
724 OFPhysicalPort portToAdd = new OFPhysicalPort();
725 portToAdd.setName(name);
726 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
727 portToAdd.setPortNumber(portNumber);
728 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
729
730 //Expectation of mock Port
731 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700732 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700733 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
734 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700735 mockIPort.setState(portState);
Teruab4b01a2013-06-20 10:09:57 -0700736 replay(mockIPort);
737
738 //Expectation of mock Switch
739 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700740 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700741 mockISw.addPort(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700742// mockISw.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700743 replay(mockISw);
744
745 //Expectation of mock operation.
746 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
747 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
748 mockOpe.commit();
749 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
750 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700751 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700752 mockOpe.commit();
753 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
754 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700755 mockOpe.commit(); // Cannot generate exception..need to revisit this test
756// mockOpe.removePort(mockIPort);
757// expectLastCall().andThrow(new RuntimeException());
758// mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700759 mockOpe.close();
760 replay(mockOpe);
761
762 swSt.init(conf);
763 swSt.addSwitch(dpid);
764 swSt.addPort(dpid, portToAdd);
765 swSt.deletePort(dpid, portNumber);
766 }
767}