blob: 53a5b12778d8b7c4135724e20f16e3f904be5cb2 [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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700118 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700119 replay(mockISw);
120
121 //Expectation of mock operation.
122 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
123 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
124 mockOpe.commit();
125 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
126 mockOpe.commit();
127 mockOpe.close();
128 replay(mockOpe);
129
130 swSt.init(conf);
131 swSt.addSwitch(dpid);
132 swSt.addSwitch(dpid);
133 }
134
135 /**
136 * Desc:
137 * Test method for addSwitch method.
138 * Condition:
139 * The switch construction is fail and return null
140 * Expect:
141 * Write the status as info log.
142 */
Teruab4b01a2013-06-20 10:09:57 -0700143 @Test
144 public void testAddSwitchAbnormal() {
145 String dpid = "00:00:00:00:00:00:0a:07";
146
147 //Expectation of mock operation.
148 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
149 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700150 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700151 mockOpe.close();
152 replay(mockOpe);
153
154 swSt.init(conf);
155 swSt.addSwitch(dpid);
156 }
157
158 /**
159 * Desc:
160 * Test method for addSwitch method.
161 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700162 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700163 * Expect:
164 * The rollback method is called.
165 */
166 //@Ignore
167 @Test
168 public void testAddSwitchException() {
169 String dpid = "00:00:00:00:00:00:0a:07";
170 String state = "ACTIVE";
171
172 //Mock Switch
173 ISwitchObject mockISw = createMock(ISwitchObject.class);
174 mockISw.setState(state);
175 replay(mockISw);
176
177 //Expectation of mock operation.
178 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
179 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
180 mockOpe.commit();
181 expectLastCall().andThrow(new RuntimeException());
182 mockOpe.rollback();
183 mockOpe.close();
184 replay(mockOpe);
185
186 swSt.init(conf);
187 swSt.addSwitch(dpid);
188 }
189
190 /**
191 * Desc:
192 * Test method for updateSwitch method.
193 * Condition:
194 * SwitchState : INACTIVE
195 * DMOPERATION : UPDATE
196 * Expect:
197 * Should call addSwitch function and commit.
198 */
199 //@Ignore
200 @Test
201 public void testUpdateUPDATE() {
202 String dpid = "00:00:00:00:00:00:0a:07";
203 SwitchState stateINACTIVE = SwitchState.INACTIVE;
204 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
205
206 //Mock Switch
207 ISwitchObject mockISw = createMock(ISwitchObject.class);
Teruab4b01a2013-06-20 10:09:57 -0700208 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700209 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700210 replay(mockISw);
211
212 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700213 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
214 mockOpe.commit();
215 mockOpe.close();
216 replay(mockOpe);
217
218 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700219 swSt.updateSwitch(dpid, stateINACTIVE, opUPDATE);
Teruab4b01a2013-06-20 10:09:57 -0700220 }
221
222 /**
223 * Desc:
224 * Test method for updateSwitch method.
225 * Condition:
226 * SwitchState : INACTIVE
227 * DMOPERATION : CREATE
228 * Expect:
229 * Should call addSwitch function and commit.
230 */
231 //@Ignore
232 @Test
233 public void testUpdateCREATE() {
234 String dpid = "00:00:00:00:00:00:0a:07";
235 SwitchState stateINACTIVE = SwitchState.INACTIVE;
236 DM_OPERATION opCREATE = DM_OPERATION.CREATE;
237
238 //Mock Switch
239 ISwitchObject mockISw = createMock(ISwitchObject.class);
240 mockISw.setState("ACTIVE");
241 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700242 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700243 replay(mockISw);
244
245 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700246 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
247 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700248 mockOpe.close();
249 replay(mockOpe);
250
251 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700252 swSt.updateSwitch(dpid, stateINACTIVE, opCREATE);
Teruab4b01a2013-06-20 10:09:57 -0700253 }
254
255 /**
256 * Desc:
257 * Test method for updateSwitch method.
258 * Condition:
259 * SwitchState : INACTIVE
260 * DMOPERATION : INSERT
261 * Expect:
262 * Should call addSwitch function and commit.
263 */
264 //@Ignore
265 @Test
266 public void testUpdateINSERT() {
267 String dpid = "00:00:00:00:00:00:0a:07";
268 SwitchState stateINACTIVE = SwitchState.INACTIVE;
269 DM_OPERATION opINSERT = DM_OPERATION.INSERT;
270
271 //Mock Switch
272 ISwitchObject mockISw = createMock(ISwitchObject.class);
273 mockISw.setState("ACTIVE");
274 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700275 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700276 replay(mockISw);
277
278 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700279 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
280 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700281 mockOpe.close();
282 replay(mockOpe);
283
284 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700285 swSt.updateSwitch(dpid, stateINACTIVE, opINSERT);
Teruab4b01a2013-06-20 10:09:57 -0700286 }
287
288 /**
289 * Desc:
290 * Test method for updateSwitch method.
291 * Condition:
292 * SwitchState : ACTIVE
293 * DMOPERATION : DELETE
294 * Expect:
295 * Should call removeSwitch function and commit.
296 */
297 //@Ignore
298 @Test
299 public void testUpdateDELETE() {
300 String dpid = "00:00:00:00:00:00:0a:07";
301 SwitchState stateACTIVE = SwitchState.ACTIVE;
302 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
303
304 //Mock Switch
305 ISwitchObject mockISw = createMock(ISwitchObject.class);
306 mockISw.setState(stateACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700307 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700308 replay(mockISw);
309
310 //Expectation of mock operation.
311 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
312 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
313 mockOpe.commit();
314 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
315 mockOpe.removeSwitch(mockISw);
316 mockOpe.commit();
317 mockOpe.close();
318 replay(mockOpe);
319
320 swSt.init(conf);
321 swSt.addSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700322 swSt.updateSwitch(dpid, stateACTIVE, opDELETE);
Teruab4b01a2013-06-20 10:09:57 -0700323 }
324
325 /**
326 * Desc:
327 * Test method for deleteSwitch method.
328 * Condition:
329 * The switch is existing.
330 * Expect:
331 * Should call removeSwitch function and commit.
332 */
333 //@Ignore
334 @Test
335 public void testDeleteSwitch() {
336 String dpid = "00:00:00:00:00:00:0a:07";
337 String state = "ACTIVE";
338
339 //Mock Switch
340 ISwitchObject mockISw = createMock(ISwitchObject.class);
341 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700342 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700343 replay(mockISw);
344
345 //Expectation of mock operation.
346 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
347 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
348 mockOpe.commit();
349 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
350 mockOpe.removeSwitch(mockISw);
351 mockOpe.commit();
352 mockOpe.close();
353 replay(mockOpe);
354
355 swSt.init(conf);
356 swSt.addSwitch(dpid);
357 swSt.deleteSwitch(dpid);
358
359 //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
360 //assertFalse(it.hasNext());
361 }
362
363 /**
364 * Desc:
365 * Test method for deleteSwitch method.
366 * Condition:
367 * The commit func throw exception.
368 * Expect:
369 * Should call rollback.
370 */
Teruab4b01a2013-06-20 10:09:57 -0700371 @Test
372 public void testDeleteSwitchException() {
373 String dpid = "00:00:00:00:00:00:0a:07";
374 String state = "ACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700375
376 //Mock Switch
377 ISwitchObject mockISw = createMock(ISwitchObject.class);
378 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700379 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700380 replay(mockISw);
381
382 //Expectation of mock operation.
383 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
384 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
385 mockOpe.commit();
386 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Yuta HIGUCHIb63f94f2013-10-14 10:01:45 -0700387 mockOpe.removeSwitch(mockISw);
388 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700389 expectLastCall().andThrow(new RuntimeException());
390 mockOpe.rollback();
391 mockOpe.close();
392 replay(mockOpe);
393
394 swSt.init(conf);
395 swSt.addSwitch(dpid);
396 swSt.deleteSwitch(dpid);
397 }
398
399 /**
400 * Desc:
401 * Test method for addPort method.
402 * Condition:
403 * port is existing.
404 * Expect:
405 * Should call addPort and commit.
406 */
407 //@Ignore
408 @Test
409 public void testAddPort() {
410 String dpid = "00:00:00:00:00:00:0a:01";
411 short portNumber = 5;
412 String state = "ACTIVE";
413 String name = "port 5 at SEA switch";
414
415 OFPhysicalPort portToAdd = new OFPhysicalPort();
416 portToAdd.setName(name);
417 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
418 portToAdd.setPortNumber(portNumber);
419 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
420
421 //Expectation of mock Port
422 IPortObject mockIPort = createMock(IPortObject.class);
423 mockIPort.setState(state);
424 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
425 mockIPort.setDesc(name);
426 replay(mockIPort);
427
428 //Expectation of mock Switch
429 ISwitchObject mockISw = createMock(ISwitchObject.class);
430 mockISw.setState(state);
431 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700432 expect(mockISw.getPort(anyShort())).andReturn(null);
433 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700434 replay(mockISw);
435
436 //Expectation of mock operation.
437 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
438 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
439 mockOpe.commit();
440 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700441// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700442 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700443 mockOpe.commit();
444 mockOpe.close();
445 replay(mockOpe);
446
447 swSt.init(conf);
448 swSt.addSwitch(dpid);
449 swSt.addPort(dpid, portToAdd);
450 }
451
452 /**
453 * Desc:
454 * Test method for addPort method.
455 * Condition:
456 * Port status is down.
457 * Expect:
458 * Should call removePort and commit.
459 */
460 //@Ignore
461 @Test
462 public void testAddPortWithPortLinkDown() {
463 String dpid = "00:00:00:00:00:00:0a:01";
464 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700465 String swState = "ACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700466// String portState = "INACTIVE";
467 String portId = "5";
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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700478 expect(mockIPort.getPortId()).andReturn(portId);
479// mockIPort.setState(portState);
480// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
481// mockIPort.setDesc(name);
Teruab4b01a2013-06-20 10:09:57 -0700482 replay(mockIPort);
483
484 //Expectation of mock Switch
485 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700486 mockISw.setState(swState);
487// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700488 expect(mockISw.getPort(anyShort())).andReturn(mockIPort);
489 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700490 replay(mockISw);
491
492 //Expectation of mock operation.
493 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
494 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700495 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700496 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700497// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
498 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700499 mockOpe.commit();
500 mockOpe.close();
501 replay(mockOpe);
502
503 swSt.init(conf);
504 swSt.addSwitch(dpid);
505 swSt.addPort(dpid, portToAdd);
506 }
507
508 /**
509 * Desc:
510 * Test method for addPort method.
511 * Condition:
512 * The switch is not existing.
513 * Expect:
514 * Nothing happens.
515 */
Teruab4b01a2013-06-20 10:09:57 -0700516 @Test
517 public void testAddPortAbnormalNoSwitch() {
518 String dpid = "00:00:00:00:00:00:0a:01";
519 short portNumber = 5;
Teruab4b01a2013-06-20 10:09:57 -0700520 String name = "port 5 at SEA switch";
521
522 OFPhysicalPort portToAdd = new OFPhysicalPort();
523 portToAdd.setName(name);
524 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
525 portToAdd.setPortNumber(portNumber);
526 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
527
528 //Expectation of mock Port
529 IPortObject mockIPort = createStrictMock(IPortObject.class);
530 replay(mockIPort);
531
532 //Expectation of mock Switch
533 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
Naoki Shiota987a5722013-10-23 11:59:36 -0700534 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700535 replay(mockISw);
536
537 //Expectation of mock operation.
538 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
539 mockOpe.close();
540 replay(mockOpe);
541
542 swSt.init(conf);
543 swSt.addPort(dpid, portToAdd);
544 }
545
546 /**
547 * Desc:
548 * Test method for addPort method.
549 * Condition:
550 * port is not existing.
551 * Expect:
552 * Should call addPort and commit.
553 */
554 //@Ignore
555 @Test
556 public void testAddPortAbnormalNoPort() {
557 String dpid = "00:00:00:00:00:00:0a:01";
558 short portNumber = 5;
559 String state = "ACTIVE";
560 String name = "port 5 at SEA switch";
561
562 OFPhysicalPort portToAdd = new OFPhysicalPort();
563 portToAdd.setName(name);
564 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
565 portToAdd.setPortNumber(portNumber);
566 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
567
568 //Expectation of mock Port
569 IPortObject mockIPort = createMock(IPortObject.class);
570 mockIPort.setState(state);
571 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
572 mockIPort.setDesc(name);
573 replay(mockIPort);
574
575 //Expectation of mock Switch
576 ISwitchObject mockISw = createMock(ISwitchObject.class);
577 mockISw.setState(state);
578 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700579 expect(mockISw.getPort(portNumber)).andReturn(null);
580 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700581 replay(mockISw);
582
583 //Expectation of mock operation.
584 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
585 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
586 mockOpe.commit();
587 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700588// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700589 expect(mockOpe.newPort(dpid, portNumber)).andReturn(null);
Teruab4b01a2013-06-20 10:09:57 -0700590 mockOpe.rollback();
591 mockOpe.close();
592 replay(mockOpe);
593
594 swSt.init(conf);
595 swSt.addSwitch(dpid);
596 swSt.addPort(dpid, portToAdd);
597 }
598
599 /**
600 * Desc:
601 * Test method for addPort method.
602 * Condition:
603 * commit throw the exception.
604 * Expect:
605 * Should call rollback.
606 */
607 //@Ignore
608 @Test
609 public void testAddPortWithException() {
610 String dpid = "00:00:00:00:00:00:0a:01";
611 short portNumber = 5;
612 String state = "ACTIVE";
613 String name = "port 5 at SEA switch";
614
615 OFPhysicalPort portToAdd = new OFPhysicalPort();
616 portToAdd.setName(name);
617 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
618 portToAdd.setPortNumber(portNumber);
619 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
620
621 //Expectation of mock Port
622 IPortObject mockIPort = createMock(IPortObject.class);
623 mockIPort.setState(state);
624 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
625 mockIPort.setDesc(name);
626 replay(mockIPort);
627
628 //Expectation of mock Switch
629 ISwitchObject mockISw = createMock(ISwitchObject.class);
630 mockISw.setState(state);
631 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700632 expect(mockISw.getPort(portNumber)).andReturn(null);
633 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700634 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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700641// 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";
Naoki Shiota987a5722013-10-23 11:59:36 -0700669 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700670 String name = "port 5 at SEA switch";
671
672 OFPhysicalPort portToAdd = new OFPhysicalPort();
673 portToAdd.setName(name);
674 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
675 portToAdd.setPortNumber(portNumber);
676 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
677
678 //Expectation of mock Port
679 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700680 mockIPort.setState(swState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700681// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
Teruab4b01a2013-06-20 10:09:57 -0700682 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700683 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700684 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700685 replay(mockIPort);
686
687 //Expectation of mock Switch
688 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700689 mockISw.setState(swState);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700690// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700691 expect(mockISw.getPort(portNumber)).andReturn(null);
692 mockISw.addPort(mockIPort);
693 expect(mockISw.getPort(portNumber)).andReturn(mockIPort);
694 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700695 replay(mockISw);
696
697 //Expectation of mock operation.
698 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
699 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
700 mockOpe.commit();
701 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700702// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700703 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700704 mockOpe.commit();
705 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700706// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
707 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700708 mockOpe.commit();
709 mockOpe.close();
710 replay(mockOpe);
711
712 swSt.init(conf);
713 swSt.addSwitch(dpid);
714 swSt.addPort(dpid, portToAdd);
715 swSt.deletePort(dpid, portNumber);
716 }
717
718 /**
719 * Desc:
720 * Test method for addPort method.
721 * Condition:
722 * commit throws the exception.
723 * Expect:
724 * Should call rollback.
725 */
726 //@Ignore
727 @Test
728 public void testDeletePortException() {
729 String dpid = "00:00:00:00:00:00:0a:01";
730 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700731 String swState = "ACTIVE";
732 String portState = "INACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700733 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700734 String name = "port 5 at SEA switch";
735
736 OFPhysicalPort portToAdd = new OFPhysicalPort();
737 portToAdd.setName(name);
738 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
739 portToAdd.setPortNumber(portNumber);
740 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
741
742 //Expectation of mock Port
743 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700744 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700745 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
746 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700747 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700748 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700749 replay(mockIPort);
750
751 //Expectation of mock Switch
752 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700753 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700754 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700755 expect(mockISw.getPort(portNumber)).andReturn(null);
756 expect(mockISw.getPort(portNumber)).andReturn(mockIPort).anyTimes();
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700757// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700758
759 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700760 replay(mockISw);
761
762 //Expectation of mock operation.
763 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
764 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
765 mockOpe.commit();
766 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700767// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700768 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700769 mockOpe.commit();
770 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700771// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
772// mockOpe.commit(); // Cannot generate exception..need to revisit this test
773 mockOpe.removePort(mockIPort);
774 expectLastCall().andThrow(new RuntimeException());
775 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700776 mockOpe.close();
777 replay(mockOpe);
778
779 swSt.init(conf);
780 swSt.addSwitch(dpid);
781 swSt.addPort(dpid, portToAdd);
782 swSt.deletePort(dpid, portNumber);
783 }
784}