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