blob: 78594b515ca1af23ac9b1a42b329aac810e7085e [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;
6import net.onrc.onos.ofcontroller.core.ISwitchStorage;
7import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
8import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
9import net.onrc.onos.util.GraphDBConnection;
10import net.onrc.onos.util.GraphDBOperation;
11import 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;
18import org.junit.Ignore;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.openflow.protocol.OFPhysicalPort;
22import org.openflow.protocol.OFPhysicalPort.OFPortState;
23import org.powermock.api.easymock.PowerMock;
24import org.powermock.core.classloader.annotations.PrepareForTest;
25import org.powermock.modules.junit4.PowerMockRunner;
26import org.slf4j.LoggerFactory;
27
28import com.thinkaurelius.titan.core.TitanFactory;
29import com.thinkaurelius.titan.core.TitanGraph;
30
31//Add Powermock preparation
32@RunWith(PowerMockRunner.class)
33@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
34public class SwitchStorageImplTestBB {
35
36 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
37
38 String conf;
39 private GraphDBConnection conn = null;
40 private GraphDBOperation ope = null;
41 private TitanGraph titanGraph = null;
42 ISwitchStorage swSt = null;
43
44 @Before
45 public void setUp() throws Exception {
46
47 swSt = new SwitchStorageImpl();
48 conf = "/dummy/path/to/db";
49
50 // Make mock cassandra DB
51 // Replace TitanFactory.open() to return mock DB
52 titanGraph = TestDatabaseManager.getTestDatabase();
53 TestDatabaseManager.populateTestData(titanGraph);
54 PowerMock.mockStatic(TitanFactory.class);
55 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
56 PowerMock.replay(TitanFactory.class);
57
58 conn = GraphDBConnection.getInstance(conf);
59 ope = new GraphDBOperation(conn);
60
61 swSt.init(conf);
62 }
63
64 @After
65 public void tearDown() throws Exception {
66
67 titanGraph.shutdown();
68 TestDatabaseManager.deleteTestDatabase();
69
70 swSt.close();
71 swSt = null;
72 }
73
74 /**
75 * Desc:
76 * Test method for addSwitch method.
77 * Condition:
78 * Normal
79 * Expect:
80 * 1. Switch should be generated.
81 * 2. The status of switch should be ACTIVE
82 */
83 //@Ignore
84 @Test
85 public void testAddSwitch() {
86 String dpid = "00:00:00:00:00:00:0a:07";
87
88 ISwitchObject sw = ope.searchSwitch(dpid);
89 assertTrue(sw == null);
90 swSt.addSwitch(dpid);
91 ISwitchObject sw2 = ope.searchSwitch(dpid);
92 assertTrue(sw2 != null);
93 assertEquals(sw2.getState(), "ACTIVE");
94 }
95
96 /**
97 * Desc:
98 * Test method for addSwitch method.
99 * Condition:
100 * The switch status is INACTIVE.
101 * Expect:
102 * 1. Switch should be existing.
103 * 2. The status of switch should be ACTIVE
104 */
105 //@Ignore
106 @Test
107 public void testAddSwitchExisting() {
108 String dpid = "00:00:00:00:00:00:0a:06";
109
110 swSt.update(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE);
111 ISwitchObject sw = ope.searchSwitch(dpid);
112 assertTrue(sw != null);
113 assertEquals(sw.getState(), SwitchState.INACTIVE.toString());
114 swSt.addSwitch(dpid);
115 ISwitchObject sw2 = ope.searchSwitch(dpid);
116 assertTrue(sw2 != null);
117 assertEquals(sw2.getState(), SwitchState.ACTIVE.toString());
118 }
119
120 /**
121 * Desc:
122 * Test method for testUpdate method.
123 * Condition:
124 * We will create a swith with this method.
125 * Expect:
126 * 1. Switch should be created.
127 * 2. The status of switch should be INACTIVE.
128 */
129 //@Ignore
130 @Test
131 public void testUpdate() {
132 String dpid = "00:00:00:00:00:00:0a:07";
133 SwitchState state = ISwitchStorage.SwitchState.INACTIVE;
134 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE;
135
136 ISwitchObject sw = ope.searchSwitch(dpid);
137 assertTrue(sw == null);
138 swSt.update(dpid, state, dmope);
139 ISwitchObject sw2 = ope.searchSwitch(dpid);
140 assertTrue(sw2 != null);
141 assertEquals(sw2.getState(), state.toString());
142 }
143
144 /**
145 * Desc:
146 * Test method for testUpdate method.
147 * Condition:
148 * The switch is existing.
149 * Expect:
150 * 1. Switch should be deleted.
151 */
152 //@Ignore
153 @Test
154 public void testUpdateWithDELETE() {
155 String dpid = "00:00:00:00:00:00:0a:06";
156 SwitchState state = ISwitchStorage.SwitchState.ACTIVE;
157 DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE;
158
159 ISwitchObject sw = ope.searchSwitch(dpid);
160 assertTrue(sw != null);
161 swSt.update(dpid, state, dmope);
162 ISwitchObject sw2 = ope.searchSwitch(dpid);
163 assertTrue(sw2 == null);
164 }
165
166 /**
167 * Desc:
168 * Test method for delete switch method.
169 * Condition:
170 * The switch is existing.
171 * Expect:
172 * 1. Switch should be deleted.
173 */
174 //@Ignore
175 @Test
176 public void testDeleteSwitch() {
177 String dpid = "00:00:00:00:00:00:0a:06";
178
179 ISwitchObject sw = ope.searchSwitch(dpid);
180 assertTrue(sw != null);
181 swSt.deleteSwitch(dpid);
182 ISwitchObject sw2 = ope.searchSwitch(dpid);
183 assertTrue(sw2 == null);
184 }
185
186 /**
187 * Desc:
188 * Test method for delete switch method.
189 * Condition:
190 * The switch is not existing.
191 * Expect:
192 * Nothing happends.
193 */
194 //@Ignore
195 @Test
196 public void testDeleteNonExistingSwitch() {
197 String dpid = "00:00:00:00:00:00:0a:07";
198
199 ISwitchObject sw = ope.searchSwitch(dpid);
200 assertTrue(sw == null);
201 swSt.deleteSwitch(dpid);
202 ISwitchObject sw2 = ope.searchSwitch(dpid);
203 assertTrue(sw2 == null);
204 }
205
206 /**
207 * Desc:
208 * Test method for delete port method.
209 * Condition:
210 * The port is existing.
211 * Expect:
212 * Delete the port.
213 */
214 //@Ignore
215 @Test
216 public void testDeletePort() {
217 String dpid = "00:00:00:00:00:00:0a:06";
218 short portNumber = 3;
219
220 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
221 assertTrue(portObj1 != null);
222 swSt.deletePort(dpid, portNumber);
223 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
224 assertTrue(portObj2 == null);
225 }
226
227 /**
228 * Desc:
229 * Test method for delete port method.
230 * Condition:
231 * The port is not existing.
232 * Expect:
233 * Nothing happens.
234 */
235 //@Ignore
236 @Test
237 public void testDeleteNonExistingPort() {
238 String dpid = "00:00:00:00:00:00:0a:06";
239 short portNumber = 4;
240
241 IPortObject portObj1 = ope.searchPort(dpid, portNumber);
242 assertTrue(portObj1 == null);
243 swSt.deletePort(dpid, portNumber);
244 IPortObject portObj2 = ope.searchPort(dpid, portNumber);
245 assertTrue(portObj2 == null);
246 }
247
248 /**
249 * Desc:
250 * Test method for add method.
251 * Condition:
252 * The port is not existing.
253 * Expect:
254 * The port should be added.
255 */
256 //@Ignore
257 @Test
258 public void testAddPort() {
259 String dpid = "00:00:00:00:00:00:0a:06";
260 short portNumber = 4;
261 String name = "port 4 at ATL Switch";
262 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
263 OFPhysicalPort port = new OFPhysicalPort();
264 port.setPortNumber(portNumber);
265 port.setName(name);
266 port.setState(state);
267
268 ISwitchObject sw = ope.searchSwitch(dpid);
269 assertTrue(sw != null);
270 swSt.addPort(dpid, port);
271 IPortObject portObj = ope.searchPort(dpid, portNumber);
272 assertTrue(portObj != null);
273 }
274
275 /**
276 * Desc:
277 * Test method for add method.
278 * Condition:
279 * The port is existing.
280 * Expect:
281 * Nothing happens.
282 */
283 //@Ignore
284 @Test
285 public void testAddExistingPort() {
286 String dpid = "00:00:00:00:00:00:0a:06";
287 short portNumber = 3;
288 String name = "port 3 at ATL Switch";
289 int state = OFPortState.OFPPS_STP_FORWARD.getValue();
290 OFPhysicalPort port = new OFPhysicalPort();
291 port.setPortNumber(portNumber);
292 port.setName(name);
293 port.setState(state);
294
295 ISwitchObject sw = ope.searchSwitch(dpid);
296 assertTrue(sw != null);
297 swSt.addPort(dpid, port);
298 IPortObject portObj = ope.searchPort(dpid, portNumber);
299 assertTrue(portObj != null);
300 }
301
302 /**
303 * Desc:
304 * Test method for add method.
305 * Condition:
306 * The port status is down.
307 * Expect:
308 * Delete the port.
309 */
310 //@Ignore
311 @Test
312 public void testAddDownPort() {
313 String dpid = "00:00:00:00:00:00:0a:06";
314 short portNumber = 3;
315 String name = "port 3 at ATL Switch";
316 int state = OFPortState.OFPPS_LINK_DOWN.getValue();
317 OFPhysicalPort port = new OFPhysicalPort();
318 port.setPortNumber(portNumber);
319 port.setName(name);
320 port.setState(state);
321
322 ISwitchObject sw = ope.searchSwitch(dpid);
323 assertTrue(sw != null);
324 swSt.addPort(dpid, port);
325 IPortObject portObj = ope.searchPort(dpid, portNumber);
326 assertTrue(portObj == null);
327 }
328}