blob: 6f4f850b12b8ee8749e878194ddc663b6763b846 [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;
37 private GraphDBConnection mockConn = null;
38 private GraphDBOperation mockOpe = null;
Teruab4b01a2013-06-20 10:09:57 -070039 ISwitchStorage swSt = null;
40
41 @Before
42 public void setUp() throws Exception {
43
44 swSt = new SwitchStorageImpl();
45 conf = "/dummy/path/to/db";
46
47 // Make mock cassandra DB
48 // Replace TitanFactory.open() to return mock DB
49
50 PowerMock.mockStatic(GraphDBConnection.class);
51 mockConn = createMock(GraphDBConnection.class);
52 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
53 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
54 PowerMock.replay(GraphDBConnection.class);
55
56 PowerMock.mockStatic(GraphDBOperation.class);
57 mockOpe = PowerMock.createStrictMock(GraphDBOperation.class);
58 PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
59 PowerMock.replay(GraphDBOperation.class);
60 // Replace the conf to dummy conf
61 // String conf = "/tmp/cassandra.titan";
62
63
64 }
65
66 @After
67 public void tearDown() throws Exception {
68 swSt.close();
69 swSt = null;
70
71 }
72
73 /**
74 * Desc:
75 * Test method for addSwitch method.
76 * Condition:
77 * Normal
78 * Expect:
79 * Call SwitchStorageImpl.addSwitch func with proper properties.
80 */
Teruab4b01a2013-06-20 10:09:57 -070081 @Test
82 public void testAddSwitch() {
83 String dpid = "00:00:00:00:00:00:0a:07";
84 String state = "ACTIVE";
85
86 //Mock Switch
87 ISwitchObject mockISw = createMock(ISwitchObject.class);
88 mockISw.setState(state);
89 replay(mockISw);
90
91 //Expectation of mock operation.
92 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
93 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
94 mockOpe.commit();
95 mockOpe.close();
96 replay(mockOpe);
97
98 swSt.init(conf);
99 swSt.addSwitch(dpid);
100 }
101
102 /**
103 * Desc:
104 * Test method for addSwitch method.
105 * Condition:
106 * The switch is already existing.
107 * Expect:
108 * Call SwitchStorageImpl.addSwitch func with proper properties.
109 */
110 //@Ignore
111 @Test
112 public void testAddSwitchExisting() {
113 String dpid = "00:00:00:00:00:00:0a:07";
114 String state = "ACTIVE";
115
116 //Mock Switch
117 ISwitchObject mockISw = createMock(ISwitchObject.class);
118 mockISw.setState(state);
119 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700120 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700121 replay(mockISw);
122
123 //Expectation of mock operation.
124 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
125 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
126 mockOpe.commit();
127 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
128 mockOpe.commit();
129 mockOpe.close();
130 replay(mockOpe);
131
132 swSt.init(conf);
133 swSt.addSwitch(dpid);
134 swSt.addSwitch(dpid);
135 }
136
137 /**
138 * Desc:
139 * Test method for addSwitch method.
140 * Condition:
141 * The switch construction is fail and return null
142 * Expect:
143 * Write the status as info log.
144 */
Teruab4b01a2013-06-20 10:09:57 -0700145 @Test
146 public void testAddSwitchAbnormal() {
147 String dpid = "00:00:00:00:00:00:0a:07";
148
149 //Expectation of mock operation.
150 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
151 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700152 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700153 mockOpe.close();
154 replay(mockOpe);
155
156 swSt.init(conf);
157 swSt.addSwitch(dpid);
158 }
159
160 /**
161 * Desc:
162 * Test method for addSwitch method.
163 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700164 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700165 * Expect:
166 * The rollback method is called.
167 */
168 //@Ignore
169 @Test
170 public void testAddSwitchException() {
171 String dpid = "00:00:00:00:00:00:0a:07";
172 String state = "ACTIVE";
173
174 //Mock Switch
175 ISwitchObject mockISw = createMock(ISwitchObject.class);
176 mockISw.setState(state);
177 replay(mockISw);
178
179 //Expectation of mock operation.
180 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
181 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
182 mockOpe.commit();
183 expectLastCall().andThrow(new RuntimeException());
184 mockOpe.rollback();
185 mockOpe.close();
186 replay(mockOpe);
187
188 swSt.init(conf);
189 swSt.addSwitch(dpid);
190 }
191
192 /**
193 * Desc:
194 * Test method for updateSwitch method.
195 * Condition:
196 * SwitchState : INACTIVE
197 * DMOPERATION : UPDATE
198 * Expect:
199 * Should call addSwitch function and commit.
200 */
201 //@Ignore
202 @Test
203 public void testUpdateUPDATE() {
204 String dpid = "00:00:00:00:00:00:0a:07";
205 SwitchState stateINACTIVE = SwitchState.INACTIVE;
206 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
207
208 //Mock Switch
209 ISwitchObject mockISw = createMock(ISwitchObject.class);
Teruab4b01a2013-06-20 10:09:57 -0700210 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700211 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700212 replay(mockISw);
213
214 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700215 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
216 mockOpe.commit();
217 mockOpe.close();
218 replay(mockOpe);
219
220 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700221 swSt.updateSwitch(dpid, stateINACTIVE, opUPDATE);
Teruab4b01a2013-06-20 10:09:57 -0700222 }
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());
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700245 replay(mockISw);
246
247 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700248 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
249 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700250 mockOpe.close();
251 replay(mockOpe);
252
253 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700254 swSt.updateSwitch(dpid, stateINACTIVE, opCREATE);
Teruab4b01a2013-06-20 10:09:57 -0700255 }
256
257 /**
258 * Desc:
259 * Test method for updateSwitch method.
260 * Condition:
261 * SwitchState : INACTIVE
262 * DMOPERATION : INSERT
263 * Expect:
264 * Should call addSwitch function and commit.
265 */
266 //@Ignore
267 @Test
268 public void testUpdateINSERT() {
269 String dpid = "00:00:00:00:00:00:0a:07";
270 SwitchState stateINACTIVE = SwitchState.INACTIVE;
271 DM_OPERATION opINSERT = DM_OPERATION.INSERT;
272
273 //Mock Switch
274 ISwitchObject mockISw = createMock(ISwitchObject.class);
275 mockISw.setState("ACTIVE");
276 mockISw.setState(stateINACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700277 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700278 replay(mockISw);
279
280 //Expectation of mock operation.
Teruab4b01a2013-06-20 10:09:57 -0700281 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
282 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700283 mockOpe.close();
284 replay(mockOpe);
285
286 swSt.init(conf);
Naoki Shiota987a5722013-10-23 11:59:36 -0700287 swSt.updateSwitch(dpid, stateINACTIVE, opINSERT);
Teruab4b01a2013-06-20 10:09:57 -0700288 }
289
290 /**
291 * Desc:
292 * Test method for updateSwitch method.
293 * Condition:
294 * SwitchState : ACTIVE
295 * DMOPERATION : DELETE
296 * Expect:
297 * Should call removeSwitch function and commit.
298 */
299 //@Ignore
300 @Test
301 public void testUpdateDELETE() {
302 String dpid = "00:00:00:00:00:00:0a:07";
303 SwitchState stateACTIVE = SwitchState.ACTIVE;
304 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
305
306 //Mock Switch
307 ISwitchObject mockISw = createMock(ISwitchObject.class);
308 mockISw.setState(stateACTIVE.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700309 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700310 replay(mockISw);
311
312 //Expectation of mock operation.
313 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
314 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
315 mockOpe.commit();
316 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
317 mockOpe.removeSwitch(mockISw);
318 mockOpe.commit();
319 mockOpe.close();
320 replay(mockOpe);
321
322 swSt.init(conf);
323 swSt.addSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700324 swSt.updateSwitch(dpid, stateACTIVE, opDELETE);
Teruab4b01a2013-06-20 10:09:57 -0700325 }
326
327 /**
328 * Desc:
329 * Test method for deleteSwitch method.
330 * Condition:
331 * The switch is existing.
332 * Expect:
333 * Should call removeSwitch function and commit.
334 */
335 //@Ignore
336 @Test
337 public void testDeleteSwitch() {
338 String dpid = "00:00:00:00:00:00:0a:07";
339 String state = "ACTIVE";
340
341 //Mock Switch
342 ISwitchObject mockISw = createMock(ISwitchObject.class);
343 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700344 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700345 replay(mockISw);
346
347 //Expectation of mock operation.
348 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
349 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
350 mockOpe.commit();
351 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
352 mockOpe.removeSwitch(mockISw);
353 mockOpe.commit();
354 mockOpe.close();
355 replay(mockOpe);
356
357 swSt.init(conf);
358 swSt.addSwitch(dpid);
359 swSt.deleteSwitch(dpid);
360
361 //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
362 //assertFalse(it.hasNext());
363 }
364
365 /**
366 * Desc:
367 * Test method for deleteSwitch method.
368 * Condition:
369 * The commit func throw exception.
370 * Expect:
371 * Should call rollback.
372 */
Teruab4b01a2013-06-20 10:09:57 -0700373 @Test
374 public void testDeleteSwitchException() {
375 String dpid = "00:00:00:00:00:00:0a:07";
376 String state = "ACTIVE";
Teruab4b01a2013-06-20 10:09:57 -0700377
378 //Mock Switch
379 ISwitchObject mockISw = createMock(ISwitchObject.class);
380 mockISw.setState(state);
Naoki Shiota987a5722013-10-23 11:59:36 -0700381 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700382 replay(mockISw);
383
384 //Expectation of mock operation.
385 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
386 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
387 mockOpe.commit();
388 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Yuta HIGUCHIb63f94f2013-10-14 10:01:45 -0700389 mockOpe.removeSwitch(mockISw);
390 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700391 expectLastCall().andThrow(new RuntimeException());
392 mockOpe.rollback();
393 mockOpe.close();
394 replay(mockOpe);
395
396 swSt.init(conf);
397 swSt.addSwitch(dpid);
398 swSt.deleteSwitch(dpid);
399 }
400
401 /**
402 * Desc:
403 * Test method for addPort method.
404 * Condition:
405 * port is existing.
406 * Expect:
407 * Should call addPort and commit.
408 */
409 //@Ignore
410 @Test
411 public void testAddPort() {
412 String dpid = "00:00:00:00:00:00:0a:01";
413 short portNumber = 5;
414 String state = "ACTIVE";
415 String name = "port 5 at SEA switch";
416
417 OFPhysicalPort portToAdd = new OFPhysicalPort();
418 portToAdd.setName(name);
419 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
420 portToAdd.setPortNumber(portNumber);
421 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
422
423 //Expectation of mock Port
424 IPortObject mockIPort = createMock(IPortObject.class);
425 mockIPort.setState(state);
426 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
427 mockIPort.setDesc(name);
428 replay(mockIPort);
429
430 //Expectation of mock Switch
431 ISwitchObject mockISw = createMock(ISwitchObject.class);
432 mockISw.setState(state);
433 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700434 expect(mockISw.getPort(anyShort())).andReturn(null);
435 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700436 replay(mockISw);
437
438 //Expectation of mock operation.
439 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
440 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
441 mockOpe.commit();
442 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700443// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700444 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700445 mockOpe.commit();
446 mockOpe.close();
447 replay(mockOpe);
448
449 swSt.init(conf);
450 swSt.addSwitch(dpid);
451 swSt.addPort(dpid, portToAdd);
452 }
453
454 /**
455 * Desc:
456 * Test method for addPort method.
457 * Condition:
458 * Port status is down.
459 * Expect:
460 * Should call removePort and commit.
461 */
462 //@Ignore
463 @Test
464 public void testAddPortWithPortLinkDown() {
465 String dpid = "00:00:00:00:00:00:0a:01";
466 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700467 String swState = "ACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700468// String portState = "INACTIVE";
469 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700470 String name = "port 5 at SEA switch";
471
472 OFPhysicalPort portToAdd = new OFPhysicalPort();
473 portToAdd.setName(name);
474 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
475 portToAdd.setPortNumber(portNumber);
476 portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
477
478 //Expectation of mock Port
479 IPortObject mockIPort = createMock(IPortObject.class);
Naoki Shiota987a5722013-10-23 11:59:36 -0700480 expect(mockIPort.getPortId()).andReturn(portId);
481// mockIPort.setState(portState);
482// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
483// mockIPort.setDesc(name);
Teruab4b01a2013-06-20 10:09:57 -0700484 replay(mockIPort);
485
486 //Expectation of mock Switch
487 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700488 mockISw.setState(swState);
489// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700490 expect(mockISw.getPort(anyShort())).andReturn(mockIPort);
491 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700492 replay(mockISw);
493
494 //Expectation of mock operation.
495 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
496 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700497 mockOpe.commit();
Teruab4b01a2013-06-20 10:09:57 -0700498 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700499// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
500 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700501 mockOpe.commit();
502 mockOpe.close();
503 replay(mockOpe);
504
505 swSt.init(conf);
506 swSt.addSwitch(dpid);
507 swSt.addPort(dpid, portToAdd);
508 }
509
510 /**
511 * Desc:
512 * Test method for addPort method.
513 * Condition:
514 * The switch is not existing.
515 * Expect:
516 * Nothing happens.
517 */
Teruab4b01a2013-06-20 10:09:57 -0700518 @Test
519 public void testAddPortAbnormalNoSwitch() {
520 String dpid = "00:00:00:00:00:00:0a:01";
521 short portNumber = 5;
Teruab4b01a2013-06-20 10:09:57 -0700522 String name = "port 5 at SEA switch";
523
524 OFPhysicalPort portToAdd = new OFPhysicalPort();
525 portToAdd.setName(name);
526 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
527 portToAdd.setPortNumber(portNumber);
528 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
529
530 //Expectation of mock Port
531 IPortObject mockIPort = createStrictMock(IPortObject.class);
532 replay(mockIPort);
533
534 //Expectation of mock Switch
535 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
Naoki Shiota987a5722013-10-23 11:59:36 -0700536 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700537 replay(mockISw);
538
539 //Expectation of mock operation.
540 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
541 mockOpe.close();
542 replay(mockOpe);
543
544 swSt.init(conf);
545 swSt.addPort(dpid, portToAdd);
546 }
547
548 /**
549 * Desc:
550 * Test method for addPort method.
551 * Condition:
552 * port is not existing.
553 * Expect:
554 * Should call addPort and commit.
555 */
556 //@Ignore
557 @Test
558 public void testAddPortAbnormalNoPort() {
559 String dpid = "00:00:00:00:00:00:0a:01";
560 short portNumber = 5;
561 String state = "ACTIVE";
562 String name = "port 5 at SEA switch";
563
564 OFPhysicalPort portToAdd = new OFPhysicalPort();
565 portToAdd.setName(name);
566 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
567 portToAdd.setPortNumber(portNumber);
568 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
569
570 //Expectation of mock Port
571 IPortObject mockIPort = createMock(IPortObject.class);
572 mockIPort.setState(state);
573 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
574 mockIPort.setDesc(name);
575 replay(mockIPort);
576
577 //Expectation of mock Switch
578 ISwitchObject mockISw = createMock(ISwitchObject.class);
579 mockISw.setState(state);
580 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700581 expect(mockISw.getPort(portNumber)).andReturn(null);
582 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700583 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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700590// 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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700634 expect(mockISw.getPort(portNumber)).andReturn(null);
635 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700636 replay(mockISw);
637
638 //Expectation of mock operation.
639 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
640 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
641 mockOpe.commit();
642 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700643// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700644 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700645 mockOpe.commit();
646 expectLastCall().andThrow(new RuntimeException());
647 mockOpe.rollback();
648 mockOpe.close();
649 replay(mockOpe);
650
651 swSt.init(conf);
652 swSt.addSwitch(dpid);
653 swSt.addPort(dpid, portToAdd);
654 }
655
656 /**
657 * Desc:
658 * Test method for deletePort method.
659 * Condition:
660 * port is existing.
661 * Expect:
662 * Should call removePort and commit.
663 */
664 //@Ignore
665 @Test
666 public void testDeletePort() {
667 String dpid = "00:00:00:00:00:00:0a:01";
668 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700669 String portState = "INACTIVE";
670 String swState = "ACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700671 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700672 String name = "port 5 at SEA switch";
673
674 OFPhysicalPort portToAdd = new OFPhysicalPort();
675 portToAdd.setName(name);
676 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
677 portToAdd.setPortNumber(portNumber);
678 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
679
680 //Expectation of mock Port
681 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700682 mockIPort.setState(swState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700683// mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
Teruab4b01a2013-06-20 10:09:57 -0700684 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700685 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700686 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700687 replay(mockIPort);
688
689 //Expectation of mock Switch
690 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700691 mockISw.setState(swState);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700692// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700693 expect(mockISw.getPort(portNumber)).andReturn(null);
694 mockISw.addPort(mockIPort);
695 expect(mockISw.getPort(portNumber)).andReturn(mockIPort);
696 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700697 replay(mockISw);
698
699 //Expectation of mock operation.
700 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
701 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
702 mockOpe.commit();
703 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700704// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700705 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700706 mockOpe.commit();
707 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700708// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
709 mockOpe.removePort(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700710 mockOpe.commit();
711 mockOpe.close();
712 replay(mockOpe);
713
714 swSt.init(conf);
715 swSt.addSwitch(dpid);
716 swSt.addPort(dpid, portToAdd);
717 swSt.deletePort(dpid, portNumber);
718 }
719
720 /**
721 * Desc:
722 * Test method for addPort method.
723 * Condition:
724 * commit throws the exception.
725 * Expect:
726 * Should call rollback.
727 */
728 //@Ignore
729 @Test
730 public void testDeletePortException() {
731 String dpid = "00:00:00:00:00:00:0a:01";
732 short portNumber = 5;
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700733 String swState = "ACTIVE";
734 String portState = "INACTIVE";
Naoki Shiota987a5722013-10-23 11:59:36 -0700735 String portId = "5";
Teruab4b01a2013-06-20 10:09:57 -0700736 String name = "port 5 at SEA switch";
737
738 OFPhysicalPort portToAdd = new OFPhysicalPort();
739 portToAdd.setName(name);
740 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
741 portToAdd.setPortNumber(portNumber);
742 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
743
744 //Expectation of mock Port
745 IPortObject mockIPort = createMock(IPortObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700746 mockIPort.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700747 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
748 mockIPort.setDesc(name);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700749 mockIPort.setState(portState);
Naoki Shiota987a5722013-10-23 11:59:36 -0700750 expect(mockIPort.getPortId()).andReturn(portId);
Teruab4b01a2013-06-20 10:09:57 -0700751 replay(mockIPort);
752
753 //Expectation of mock Switch
754 ISwitchObject mockISw = createMock(ISwitchObject.class);
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700755 mockISw.setState(swState);
Teruab4b01a2013-06-20 10:09:57 -0700756 mockISw.addPort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700757 expect(mockISw.getPort(portNumber)).andReturn(null);
758 expect(mockISw.getPort(portNumber)).andReturn(mockIPort).anyTimes();
Pankaj Berdeac54a4b2013-08-02 15:31:28 -0700759// mockISw.removePort(mockIPort);
Naoki Shiota987a5722013-10-23 11:59:36 -0700760
761 expect(mockISw.getDPID()).andReturn(dpid).anyTimes();
Teruab4b01a2013-06-20 10:09:57 -0700762 replay(mockISw);
763
764 //Expectation of mock operation.
765 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
766 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
767 mockOpe.commit();
768 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700769// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700770 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700771 mockOpe.commit();
772 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
Naoki Shiota987a5722013-10-23 11:59:36 -0700773// expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
774// mockOpe.commit(); // Cannot generate exception..need to revisit this test
775 mockOpe.removePort(mockIPort);
776 expectLastCall().andThrow(new RuntimeException());
777 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700778 mockOpe.close();
779 replay(mockOpe);
780
781 swSt.init(conf);
782 swSt.addSwitch(dpid);
783 swSt.addPort(dpid, portToAdd);
784 swSt.deletePort(dpid, portNumber);
785 }
786}