blob: 585a8fde21a77eb182a0fc8a0736f27c7ac22c5f [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.core.internal;
2
3import static org.junit.Assert.*;
4
5import net.floodlightcontroller.core.internal.TestDatabaseManager;
Pankaj Berde38646d62013-06-21 11:34:04 -07006import net.onrc.onos.graph.GraphDBConnection;
7import net.onrc.onos.graph.GraphDBOperation;
Teruab4b01a2013-06-20 10:09:57 -07008import net.onrc.onos.ofcontroller.core.ISwitchStorage;
9import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
10import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070011import net.onrc.onos.ofcontroller.core.INetMapStorage;
12import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
15import org.easymock.EasyMock;
16import org.junit.After;
17import org.junit.Before;
Teruab4b01a2013-06-20 10:09:57 -070018import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.openflow.protocol.OFPhysicalPort;
21import org.openflow.protocol.OFPhysicalPort.OFPortState;
22import org.powermock.api.easymock.PowerMock;
23import org.powermock.core.classloader.annotations.PrepareForTest;
24import org.powermock.modules.junit4.PowerMockRunner;
25import org.slf4j.LoggerFactory;
26
27import com.thinkaurelius.titan.core.TitanFactory;
28import com.thinkaurelius.titan.core.TitanGraph;
29
30//Add Powermock preparation
31@RunWith(PowerMockRunner.class)
32@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
33public class SwitchStorageImplTestBB {
34
35 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
36
37 String conf;
38 private GraphDBConnection conn = null;
39 private GraphDBOperation ope = 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 titanGraph = TestDatabaseManager.getTestDatabase();
52 TestDatabaseManager.populateTestData(titanGraph);
53 PowerMock.mockStatic(TitanFactory.class);
54 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
55 PowerMock.replay(TitanFactory.class);
56
57 conn = GraphDBConnection.getInstance(conf);
58 ope = new GraphDBOperation(conn);
59
60 swSt.init(conf);
61 }
62
63 @After
64 public void tearDown() throws Exception {
65
66 titanGraph.shutdown();
67 TestDatabaseManager.deleteTestDatabase();
68
69 swSt.close();
70 swSt = null;
71 }
72
73 /**
74 * Desc:
75 * Test method for addSwitch method.
76 * Condition:
77 * Normal
78 * Expect:
79 * 1. Switch should be generated.
80 * 2. The status of switch should be ACTIVE
81 */
82 //@Ignore
83 @Test
84 public void testAddSwitch() {
85 String dpid = "00:00:00:00:00:00:0a:07";
86
87 ISwitchObject sw = ope.searchSwitch(dpid);
88 assertTrue(sw == null);
89 swSt.addSwitch(dpid);
90 ISwitchObject sw2 = ope.searchSwitch(dpid);
91 assertTrue(sw2 != null);
92 assertEquals(sw2.getState(), "ACTIVE");
93 }
94
95 /**
96 * Desc:
97 * Test method for addSwitch method.
98 * Condition:
Teru4fd58642013-06-21 07:54:49 -070099 * The existing switch status is INACTIVE.
100 * The switch is already existing.
Teruab4b01a2013-06-20 10:09:57 -0700101 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700102 * 1. After add the same switch, the status of switch should be ACTIVE
Teruab4b01a2013-06-20 10:09:57 -0700103 */
104 //@Ignore
105 @Test
106 public void testAddSwitchExisting() {
107 String dpid = "00:00:00:00:00:00:0a:06";
108
Naoki Shiota987a5722013-10-23 11:59:36 -0700109 swSt.updateSwitch(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE);
Teruab4b01a2013-06-20 10:09:57 -0700110 ISwitchObject sw = ope.searchSwitch(dpid);
111 assertTrue(sw != null);
112 assertEquals(sw.getState(), SwitchState.INACTIVE.toString());
113 swSt.addSwitch(dpid);
114 ISwitchObject sw2 = ope.searchSwitch(dpid);
115 assertTrue(sw2 != null);
116 assertEquals(sw2.getState(), SwitchState.ACTIVE.toString());
117 }
118
119 /**
120 * Desc:
121 * Test method for testUpdate method.
122 * Condition:
Teru4fd58642013-06-21 07:54:49 -0700123 * The switch is not existing.
124 * The status of added switch is INACTIVE.
125 * DM_OPERATION is CREATE.
Teruab4b01a2013-06-20 10:09:57 -0700126 * Expect:
127 * 1. Switch should be created.
128 * 2. The status of switch should be INACTIVE.
129 */
130 //@Ignore
131 @Test
132 public void testUpdate() {
133 String dpid = "00:00:00:00:00:00:0a:07";
134 SwitchState state = ISwitchStorage.SwitchState.INACTIVE;
135 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE;
136
137 ISwitchObject sw = ope.searchSwitch(dpid);
138 assertTrue(sw == null);
Naoki Shiota987a5722013-10-23 11:59:36 -0700139 swSt.updateSwitch(dpid, state, dmope);
Teruab4b01a2013-06-20 10:09:57 -0700140 ISwitchObject sw2 = ope.searchSwitch(dpid);
141 assertTrue(sw2 != null);
142 assertEquals(sw2.getState(), state.toString());
143 }
144
145 /**
146 * Desc:
147 * Test method for testUpdate method.
148 * Condition:
149 * The switch is existing.
Teru4fd58642013-06-21 07:54:49 -0700150 * The status of added switch is ACTIVE.
151 * DM_OPERATION is DELETE.
Teruab4b01a2013-06-20 10:09:57 -0700152 * Expect:
153 * 1. Switch should be deleted.
154 */
155 //@Ignore
156 @Test
157 public void testUpdateWithDELETE() {
158 String dpid = "00:00:00:00:00:00:0a:06";
159 SwitchState state = ISwitchStorage.SwitchState.ACTIVE;
160 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE;
161
162 ISwitchObject sw = ope.searchSwitch(dpid);
163 assertTrue(sw != null);
Naoki Shiota987a5722013-10-23 11:59:36 -0700164 swSt.updateSwitch(dpid, state, dmope);
Teruab4b01a2013-06-20 10:09:57 -0700165 ISwitchObject sw2 = ope.searchSwitch(dpid);
166 assertTrue(sw2 == null);
167 }
168
169 /**
170 * Desc:
171 * Test method for delete switch method.
172 * Condition:
173 * The switch is existing.
174 * Expect:
175 * 1. Switch should be deleted.
176 */
177 //@Ignore
178 @Test
179 public void testDeleteSwitch() {
180 String dpid = "00:00:00:00:00:00:0a:06";
181
182 ISwitchObject sw = ope.searchSwitch(dpid);
183 assertTrue(sw != null);
184 swSt.deleteSwitch(dpid);
185 ISwitchObject sw2 = ope.searchSwitch(dpid);
186 assertTrue(sw2 == null);
187 }
188
189 /**
190 * Desc:
191 * Test method for delete switch method.
192 * Condition:
193 * The switch is not existing.
194 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700195 * Nothing happens.
Teruab4b01a2013-06-20 10:09:57 -0700196 */
197 //@Ignore
198 @Test
199 public void testDeleteNonExistingSwitch() {
200 String dpid = "00:00:00:00:00:00:0a:07";
201
202 ISwitchObject sw = ope.searchSwitch(dpid);
203 assertTrue(sw == null);
204 swSt.deleteSwitch(dpid);
205 ISwitchObject sw2 = ope.searchSwitch(dpid);
206 assertTrue(sw2 == null);
207 }
208
209 /**
210 * Desc:
211 * Test method for delete port method.
212 * Condition:
213 * The port is existing.
214 * Expect:
Teru4fd58642013-06-21 07:54:49 -0700215 * Deleted the port.
Teruab4b01a2013-06-20 10:09:57 -0700216 */
217 //@Ignore
218 @Test
219 public void testDeletePort() {
220 String dpid = "00:00:00:00:00:00:0a:06";
221 short portNumber = 3;
222
223 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
224 assertTrue(portObj1 != null);
225 swSt.deletePort(dpid, portNumber);
226 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
227 assertTrue(portObj2 == null);
228 }
229
230 /**
231 * Desc:
232 * Test method for delete port method.
233 * Condition:
234 * The port is not existing.
235 * Expect:
236 * Nothing happens.
237 */
238 //@Ignore
239 @Test
240 public void testDeleteNonExistingPort() {
241 String dpid = "00:00:00:00:00:00:0a:06";
242 short portNumber = 4;
243
244 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
245 assertTrue(portObj1 == null);
246 swSt.deletePort(dpid, portNumber);
247 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
248 assertTrue(portObj2 == null);
249 }
250
251 /**
252 * Desc:
Teru4fd58642013-06-21 07:54:49 -0700253 * Test method for add port method.
Teruab4b01a2013-06-20 10:09:57 -0700254 * Condition:
255 * The port is not existing.
256 * Expect:
257 * The port should be added.
Teru4fd58642013-06-21 07:54:49 -0700258 * The desc of IPortObject is the same as the name of OFPhysicalPort.
Teruab4b01a2013-06-20 10:09:57 -0700259 */
260 //@Ignore
261 @Test
262 public void testAddPort() {
263 String dpid = "00:00:00:00:00:00:0a:06";
264 short portNumber = 4;
265 String name = "port 4 at ATL Switch";
266 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
267 OFPhysicalPort port = new OFPhysicalPort();
268 port.setPortNumber(portNumber);
269 port.setName(name);
270 port.setState(state);
271
272 ISwitchObject sw = ope.searchSwitch(dpid);
273 assertTrue(sw != null);
274 swSt.addPort(dpid, port);
275 IPortObject portObj = ope.searchPort(dpid, portNumber);
276 assertTrue(portObj != null);
Teru4fd58642013-06-21 07:54:49 -0700277 assertEquals(portObj.getDesc(), name);
Teruab4b01a2013-06-20 10:09:57 -0700278 }
279
280 /**
281 * Desc:
282 * Test method for add method.
283 * Condition:
284 * The port is existing.
285 * Expect:
286 * Nothing happens.
287 */
288 //@Ignore
289 @Test
290 public void testAddExistingPort() {
291 String dpid = "00:00:00:00:00:00:0a:06";
292 short portNumber = 3;
Teru4fd58642013-06-21 07:54:49 -0700293 String name = "xxx";
Teruab4b01a2013-06-20 10:09:57 -0700294 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
295 OFPhysicalPort port = new OFPhysicalPort();
296 port.setPortNumber(portNumber);
297 port.setName(name);
298 port.setState(state);
299
300 ISwitchObject sw = ope.searchSwitch(dpid);
301 assertTrue(sw != null);
302 swSt.addPort(dpid, port);
303 IPortObject portObj = ope.searchPort(dpid, portNumber);
304 assertTrue(portObj != null);
305 }
306
307 /**
308 * Desc:
309 * Test method for add method.
310 * Condition:
311 * The port status is down.
312 * Expect:
313 * Delete the port.
314 */
315 //@Ignore
316 @Test
317 public void testAddDownPort() {
318 String dpid = "00:00:00:00:00:00:0a:06";
319 short portNumber = 3;
320 String name = "port 3 at ATL Switch";
321 int state = OFPortState.OFPPS_LINK_DOWN.getValue();
322 OFPhysicalPort port = new OFPhysicalPort();
323 port.setPortNumber(portNumber);
324 port.setName(name);
325 port.setState(state);
326
327 ISwitchObject sw = ope.searchSwitch(dpid);
328 assertTrue(sw != null);
329 swSt.addPort(dpid, port);
330 IPortObject portObj = ope.searchPort(dpid, portNumber);
331 assertTrue(portObj == null);
332 }
333}