blob: 2ddab3d499e6c56e7b289bc056baac3efbb02c31 [file] [log] [blame]
Teru68076a42013-06-27 14:57:49 -07001package net.onrc.onos.ofcontroller.core;
2
3import static org.junit.Assert.*;
4
5import net.onrc.onos.graph.GraphDBConnection;
6import net.onrc.onos.graph.GraphDBOperation;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
8import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
12import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
13import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
14import net.onrc.onos.ofcontroller.flowmanager.FlowManager;
15import net.onrc.onos.ofcontroller.util.FlowId;
16import net.onrc.onos.ofcontroller.util.FlowPath;
17
18import org.easymock.EasyMock;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.openflow.protocol.OFPhysicalPort.OFPortState;
24import org.powermock.api.easymock.PowerMock;
25import org.powermock.core.classloader.annotations.PrepareForTest;
26import org.slf4j.LoggerFactory;
27import org.powermock.modules.junit4.PowerMockRunner;
28
29import com.thinkaurelius.titan.core.TitanFactory;
30import com.thinkaurelius.titan.core.TitanGraph;
31import java.util.ArrayList;
32import java.util.HashMap;
33
34//Add Powermock preparation
35@RunWith(PowerMockRunner.class)
36@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
37public class INetMapTopologyObjectsIPortObjectTest {
38
39 //The test network is ./titan/schema/test-network.xml
40
41 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
42
43 String conf;
44 private GraphDBConnection conn = null;
45 private GraphDBOperation ope = null;
46 private TitanGraph titanGraph = null;
47
48 private ISwitchObject swObj;
49 private IPortObject portObj;
50 private IPortObject portObj2;
51 String dpid;
52 Short number;
53 Short number2;
54
55 private ISwitchObject swObjParty;
56 private IPortObject portObjParty1;
57 private IPortObject portObjParty2;
58 String dpidParty;
59 Short numberParty1;
60 Short numberParty2;
61
62
63 @Before
64 public void setUp() throws Exception {
65 conf = "/dummy/path/to/db";
66
67 // Make mock cassandra DB
68 // Replace TitanFactory.open() to return mock DB
69 TestDatabaseManager.deleteTestDatabase();
70 titanGraph = TestDatabaseManager.getTestDatabase();
71 //TestDatabaseManager.populateTestData(titanGraph);
72 PowerMock.mockStatic(TitanFactory.class);
73 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
74 PowerMock.replay(TitanFactory.class);
75
76 conn = GraphDBConnection.getInstance(conf);
77 ope = new GraphDBOperation(conn);
78
79 dpid = "00:00:00:00:00:00:0a:07";
80 number = 1;
81 number2 = 2;
82 swObj = ope.newSwitch(dpid);
83 portObj = ope.newPort(dpid, number);
84 portObj2 = ope.newPort(dpid, number2);
85
86 swObj.addPort(portObj);
87 swObj.addPort(portObj2);
88
89 dpidParty = "00:00:00:00:00:00:0a:08";
90 numberParty1 = 1;
91 numberParty2 = 2;
92 swObjParty = ope.newSwitch(dpidParty);
93 portObjParty1 = ope.newPort(dpidParty, numberParty1);
94 portObjParty2 = ope.newPort(dpidParty, numberParty2);
95 swObjParty.addPort(portObjParty1);
96 swObjParty.addPort(portObjParty2);
97 }
98
99 @After
100 public void tearDown() throws Exception {
101 titanGraph.shutdown();
102 TestDatabaseManager.deleteTestDatabase();
103 }
104
105 /**
106 * Desc:
107 * Test method for set and get port number property.
108 * Condition:
109 * N/A
110 * Expect:
111 * 1. Should set the port number.
112 * 2. Should get the port number.
113 */
114 @Test
115 public void testSetGetNumber() {
116 assertEquals(portObj.getNumber(), number);
117 Short testedNumber = 4;
118 portObj.setNumber(testedNumber);
119 assertEquals(portObj.getNumber(), testedNumber);
120 }
121
122 /**
123 * Desc:
124 * Test method for set and get port id property.
125 * Condition:
126 * N/A
127 * Expect:
128 * 1. Should set the port id.
129 * 2. Should get the port id.
130 */
131 @Test
132 public void testSetGetPortId() {
133 String portId = "test1";
134 portObj.setPortId(portId);
135 assertEquals(portObj.getPortId(), portId);
136 }
137
138 /**
139 * Desc:
140 * Test method for set and get port desc property.
141 * Condition:
142 * N/A
143 * Expect:
144 * 1. Should set the port desc.
145 * 2. Should get the port desc.
146 */
147 @Test
148 public void testSetGetDesc() {
149 String testedDesc = "port 4 at ATL Switch";
150 portObj.setDesc(testedDesc);
151 assertEquals(portObj.getDesc(), testedDesc);
152 }
153
154
155 /**
156 * Desc:
157 * Test method for set and get port status property.
158 * Condition:
159 * N/A
160 * Expect:
161 * 1. Should set the port status.
162 * 2. Should get the port status.
163 */
164 @Test
165 public void testSetGetPortState() {
166 Integer portState = OFPortState.OFPPS_STP_FORWARD.getValue();
167 portObj.setPortState(portState);
168 assertEquals(portObj.getPortState(), portState);
169 }
170
171 /**
172 * Desc:
173 * Test method for get switch object.
174 * Condition:
175 * N/A
176 * Expect:
177 * 1. Should get the switch status.
178 */
179 @Test
180 public void testGetSwitch() {
181 ISwitchObject sw = portObj.getSwitch();
182 assertEquals(sw.getDPID(), dpid);
183 }
184
185 private boolean checkIDeviceObject(IPortObject IportObj, String mac)
186 {
187 HashMap<String, IDeviceObject> devList = new HashMap<String, IDeviceObject>();
188 for(IDeviceObject IdevObj : IportObj.getDevices())
189 {
190 devList.put(IdevObj.getMACAddress(), IdevObj);
191 }
192 return devList.containsKey(mac);
193 }
194
195 /**
196 * Desc:
197 * Test method for set and remove device object.
198 * Condition:
199 * N/A
200 * Expect:
201 * 1. Should set the device object.
202 * 2. SHould remove the device object.
203 */
204 @Test
205 public void testSetAndRemoveDevice() {
206 IDeviceObject devObj = ope.newDevice();
207 String devMac = "00:00:00:00:00:11";
208 devObj.setMACAddress(devMac);
209
210 boolean b = checkIDeviceObject(portObj, devMac);
211 assertTrue(!b);
212 portObj.setDevice(devObj);
213 boolean b2 = checkIDeviceObject(portObj, devMac);
214 assertTrue(b2);
215
216 portObj.removeDevice(devObj);
217 boolean b3 = checkIDeviceObject(portObj, devMac);
218 assertTrue(!b3);
219
220 }
221
222 /**
223 * Desc:
224 * Test method for get devices object.
225 * Condition:
226 * N/A
227 * Expect:
228 * 1. Should get the device objects.
229 */
230 @Test
231 public void testGetDevices() {
232 IDeviceObject devObj = ope.newDevice();
233 String devMac = "58:55:ca:c4:1b:a0";
234 devObj.setMACAddress(devMac);
235
236 portObj.setDevice(devObj);
237 boolean b = checkIDeviceObject(portObj, devMac);
238 assertTrue(b);
239 }
240
241 /**
242 * Desc:
243 * Test method for set, get and remove linked port.
244 * Condition:
245 * N/A
246 * Expect:
247 * 1. Should set the linked objects.
248 * 2. Should get the linked objects.
249 * 3. SHould remove the liked objects.
250 */
251 @Test
252 public void testSetGetRemoveLinkedPorts() {
253 String dpidParty = "00:00:00:00:00:00:00:08";
254 ISwitchObject swParty = ope.newSwitch(dpidParty);
255 Short poShort = 1;
256 IPortObject poParty = ope.newPort(dpidParty, poShort);
257 swParty.addPort(poParty);
258
259 portObj.setLinkPort(poParty);
260
261 ArrayList<IPortObject> iPortList = new ArrayList<IPortObject>();
262 for(IPortObject port : portObj.getLinkedPorts()) {
263 iPortList.add(port);
264 }
265 assertTrue(iPortList.contains(poParty));
266
267 portObj.removeLink(poParty);
268
269 ArrayList<IPortObject> iPortList2 = new ArrayList<IPortObject>();
270 for(IPortObject port : portObj.getLinkedPorts()) {
271 iPortList2.add(port);
272 }
273
274 assertTrue(!iPortList2.contains(poParty));
275 }
276
277 /**
278 * Desc:
279 * Test method for get inbound flowEntry
280 * Condition:
281 * N/A
282 * Expect:
283 * 1. Should get the inbound flowEntry.
284 */
285 @Test
286 public void testGetInFlowEntries() {
287
288 portObj.setLinkPort(portObj2);
289
290 IFlowPath flowPathObj = ope.newFlowPath();
291
292 String flowEId = "1";
293 IFlowEntry flowEntryObj = ope.newFlowEntry();
294 flowEntryObj.setFlowEntryId(flowEId);
295 flowEntryObj.setInPort(portObj);
296 flowEntryObj.setOutPort(portObj2);
297 flowEntryObj.setSwitch(swObj);
298 flowEntryObj.setFlow(flowPathObj);
299
300 String flowEId2 = "2";
301 IFlowEntry flowEntryObj2 = ope.newFlowEntry();
302 flowEntryObj2.setFlowEntryId(flowEId2);
303 flowEntryObj2.setInPort(portObjParty1);
304 flowEntryObj2.setOutPort(portObjParty2);
305 flowEntryObj.setSwitch(swObjParty);
306 flowEntryObj2.setFlow(flowPathObj);
307
308 HashMap<String, IFlowEntry> flowEntryList = new HashMap<String, IFlowEntry>();
309 for(IFlowEntry flowEnt : portObj.getInFlowEntries())
310 {
311 flowEntryList.put(flowEnt.getFlowEntryId(), flowEnt);
312 }
313
314 assertTrue(flowEntryList.containsValue(flowEntryObj));
315
316 }
317
318 /**
319 * Desc:
320 * Test method for get outbound flowEntry
321 * Condition:
322 * N/A
323 * Expect:
324 * 1. Should get the outbound flowEntry.
325 */
326 @Test
327 public void testGetOutFlowEntries() {
328
329 portObj.setLinkPort(portObj2);
330
331 IFlowPath flowPathObj = ope.newFlowPath();
332
333 String flowEId = "1";
334 IFlowEntry flowEntryObj = ope.newFlowEntry();
335 flowEntryObj.setFlowEntryId(flowEId);
336 flowEntryObj.setInPort(portObj);
337 flowEntryObj.setOutPort(portObj2);
338 flowEntryObj.setSwitch(swObj);
339 flowEntryObj.setFlow(flowPathObj);
340
341 String flowEId2 = "2";
342 IFlowEntry flowEntryObj2 = ope.newFlowEntry();
343 flowEntryObj2.setFlowEntryId(flowEId2);
344 flowEntryObj2.setInPort(portObjParty1);
345 flowEntryObj2.setOutPort(portObjParty2);
346 flowEntryObj.setSwitch(swObjParty);
347 flowEntryObj2.setFlow(flowPathObj);
348
349 HashMap<String, IFlowEntry> flowEntryList = new HashMap<String, IFlowEntry>();
350 for(IFlowEntry flowEnt : portObj2.getOutFlowEntries())
351 {
352 flowEntryList.put(flowEnt.getFlowEntryId(), flowEnt);
353 }
354
355 assertTrue(flowEntryList.containsValue(flowEntryObj));
356
357 }
358
359}