Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core.internal; |
| 2 | |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 5 | import net.floodlightcontroller.core.internal.TestDatabaseManager; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 6 | import net.onrc.onos.graph.GraphDBConnection; |
| 7 | import net.onrc.onos.graph.GraphDBOperation; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 8 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 12 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 13 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
| 14 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 15 | import org.easymock.EasyMock; |
| 16 | import org.junit.After; |
| 17 | import org.junit.Before; |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 18 | import org.junit.Ignore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | import org.openflow.protocol.OFPhysicalPort; |
| 22 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
| 23 | import org.powermock.api.easymock.PowerMock; |
| 24 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 25 | import org.powermock.modules.junit4.PowerMockRunner; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.thinkaurelius.titan.core.TitanFactory; |
| 29 | import com.thinkaurelius.titan.core.TitanGraph; |
| 30 | |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 31 | /* |
| 32 | * Jono, 11/4/2013 |
| 33 | * These tests are being ignored because they don't work because they |
| 34 | * rely on test functionality that was written ages ago and hasn't been |
| 35 | * updated as the database schema has evolved. |
| 36 | * These tests work by getting an in-memory Titan database and testing |
| 37 | * the SwitchStorageImpl on top of that. In this regard they're not really |
| 38 | * unit tests as they test the entire DB stack (i.e. GraphDBOperation and |
| 39 | * GraphDBConnection), not just SwitchStorageImpl. |
| 40 | * I've left them here as we may wish to resurrect this kind of |
| 41 | * integration testing of the DB layers in the future. |
| 42 | */ |
| 43 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 44 | //Add Powermock preparation |
| 45 | @RunWith(PowerMockRunner.class) |
| 46 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 47 | public class SwitchStorageImplTestBB { |
| 48 | |
| 49 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 50 | |
| 51 | String conf; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 52 | String dbStore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 53 | private GraphDBConnection conn = null; |
| 54 | private GraphDBOperation ope = null; |
| 55 | private TitanGraph titanGraph = null; |
| 56 | ISwitchStorage swSt = null; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | |
| 61 | swSt = new SwitchStorageImpl(); |
| 62 | conf = "/dummy/path/to/db"; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 63 | dbStore ="dummyStore"; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 64 | |
| 65 | // Make mock cassandra DB |
| 66 | // Replace TitanFactory.open() to return mock DB |
| 67 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 68 | TestDatabaseManager.populateTestData(titanGraph); |
| 69 | PowerMock.mockStatic(TitanFactory.class); |
| 70 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 71 | PowerMock.replay(TitanFactory.class); |
| 72 | |
| 73 | conn = GraphDBConnection.getInstance(conf); |
| 74 | ope = new GraphDBOperation(conn); |
| 75 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 76 | swSt.init(dbStore, conf); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @After |
| 80 | public void tearDown() throws Exception { |
| 81 | |
| 82 | titanGraph.shutdown(); |
| 83 | TestDatabaseManager.deleteTestDatabase(); |
| 84 | |
| 85 | swSt.close(); |
| 86 | swSt = null; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Desc: |
| 91 | * Test method for addSwitch method. |
| 92 | * Condition: |
| 93 | * Normal |
| 94 | * Expect: |
| 95 | * 1. Switch should be generated. |
| 96 | * 2. The status of switch should be ACTIVE |
| 97 | */ |
| 98 | //@Ignore |
| 99 | @Test |
| 100 | public void testAddSwitch() { |
| 101 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 102 | |
| 103 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 104 | assertTrue(sw == null); |
| 105 | swSt.addSwitch(dpid); |
| 106 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 107 | assertTrue(sw2 != null); |
| 108 | assertEquals(sw2.getState(), "ACTIVE"); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Desc: |
| 113 | * Test method for addSwitch method. |
| 114 | * Condition: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 115 | * The existing switch status is INACTIVE. |
| 116 | * The switch is already existing. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 117 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 118 | * 1. After add the same switch, the status of switch should be ACTIVE |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 119 | */ |
| 120 | //@Ignore |
| 121 | @Test |
| 122 | public void testAddSwitchExisting() { |
| 123 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 124 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 125 | swSt.updateSwitch(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 126 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 127 | assertTrue(sw != null); |
| 128 | assertEquals(sw.getState(), SwitchState.INACTIVE.toString()); |
| 129 | swSt.addSwitch(dpid); |
| 130 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 131 | assertTrue(sw2 != null); |
| 132 | assertEquals(sw2.getState(), SwitchState.ACTIVE.toString()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Desc: |
| 137 | * Test method for testUpdate method. |
| 138 | * Condition: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 139 | * The switch is not existing. |
| 140 | * The status of added switch is INACTIVE. |
| 141 | * DM_OPERATION is CREATE. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 142 | * Expect: |
| 143 | * 1. Switch should be created. |
| 144 | * 2. The status of switch should be INACTIVE. |
| 145 | */ |
| 146 | //@Ignore |
| 147 | @Test |
| 148 | public void testUpdate() { |
| 149 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 150 | SwitchState state = ISwitchStorage.SwitchState.INACTIVE; |
| 151 | DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE; |
| 152 | |
| 153 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 154 | assertTrue(sw == null); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 155 | swSt.updateSwitch(dpid, state, dmope); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 156 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 157 | assertTrue(sw2 != null); |
| 158 | assertEquals(sw2.getState(), state.toString()); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Desc: |
| 163 | * Test method for testUpdate method. |
| 164 | * Condition: |
| 165 | * The switch is existing. |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 166 | * The status of added switch is ACTIVE. |
| 167 | * DM_OPERATION is DELETE. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 168 | * Expect: |
| 169 | * 1. Switch should be deleted. |
| 170 | */ |
| 171 | //@Ignore |
| 172 | @Test |
| 173 | public void testUpdateWithDELETE() { |
| 174 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 175 | SwitchState state = ISwitchStorage.SwitchState.ACTIVE; |
| 176 | DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE; |
| 177 | |
| 178 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 179 | assertTrue(sw != null); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 180 | swSt.updateSwitch(dpid, state, dmope); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 181 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 182 | assertTrue(sw2 == null); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Desc: |
| 187 | * Test method for delete switch method. |
| 188 | * Condition: |
| 189 | * The switch is existing. |
| 190 | * Expect: |
| 191 | * 1. Switch should be deleted. |
| 192 | */ |
| 193 | //@Ignore |
| 194 | @Test |
| 195 | public void testDeleteSwitch() { |
| 196 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 197 | |
| 198 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 199 | assertTrue(sw != null); |
| 200 | swSt.deleteSwitch(dpid); |
| 201 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 202 | assertTrue(sw2 == null); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Desc: |
| 207 | * Test method for delete switch method. |
| 208 | * Condition: |
| 209 | * The switch is not existing. |
| 210 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 211 | * Nothing happens. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 212 | */ |
| 213 | //@Ignore |
| 214 | @Test |
| 215 | public void testDeleteNonExistingSwitch() { |
| 216 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 217 | |
| 218 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 219 | assertTrue(sw == null); |
| 220 | swSt.deleteSwitch(dpid); |
| 221 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 222 | assertTrue(sw2 == null); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Desc: |
| 227 | * Test method for delete port method. |
| 228 | * Condition: |
| 229 | * The port is existing. |
| 230 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 231 | * Deleted the port. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 232 | */ |
| 233 | //@Ignore |
| 234 | @Test |
| 235 | public void testDeletePort() { |
| 236 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 237 | short portNumber = 3; |
| 238 | |
| 239 | IPortObject portObj1 = ope.searchPort(dpid, portNumber); |
| 240 | assertTrue(portObj1 != null); |
| 241 | swSt.deletePort(dpid, portNumber); |
| 242 | IPortObject portObj2 = ope.searchPort(dpid, portNumber); |
| 243 | assertTrue(portObj2 == null); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Desc: |
| 248 | * Test method for delete port method. |
| 249 | * Condition: |
| 250 | * The port is not existing. |
| 251 | * Expect: |
| 252 | * Nothing happens. |
| 253 | */ |
| 254 | //@Ignore |
| 255 | @Test |
| 256 | public void testDeleteNonExistingPort() { |
| 257 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 258 | short portNumber = 4; |
| 259 | |
| 260 | IPortObject portObj1 = ope.searchPort(dpid, portNumber); |
| 261 | assertTrue(portObj1 == null); |
| 262 | swSt.deletePort(dpid, portNumber); |
| 263 | IPortObject portObj2 = ope.searchPort(dpid, portNumber); |
| 264 | assertTrue(portObj2 == null); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Desc: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 269 | * Test method for add port method. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 270 | * Condition: |
| 271 | * The port is not existing. |
| 272 | * Expect: |
| 273 | * The port should be added. |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 274 | * The desc of IPortObject is the same as the name of OFPhysicalPort. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 275 | */ |
| 276 | //@Ignore |
| 277 | @Test |
| 278 | public void testAddPort() { |
| 279 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 280 | short portNumber = 4; |
| 281 | String name = "port 4 at ATL Switch"; |
| 282 | int state = OFPortState.OFPPS_STP_FORWARD.getValue(); |
| 283 | OFPhysicalPort port = new OFPhysicalPort(); |
| 284 | port.setPortNumber(portNumber); |
| 285 | port.setName(name); |
| 286 | port.setState(state); |
| 287 | |
| 288 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 289 | assertTrue(sw != null); |
| 290 | swSt.addPort(dpid, port); |
| 291 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 292 | assertTrue(portObj != null); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 293 | assertEquals(portObj.getDesc(), name); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Desc: |
| 298 | * Test method for add method. |
| 299 | * Condition: |
| 300 | * The port is existing. |
| 301 | * Expect: |
| 302 | * Nothing happens. |
| 303 | */ |
| 304 | //@Ignore |
| 305 | @Test |
| 306 | public void testAddExistingPort() { |
| 307 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 308 | short portNumber = 3; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 309 | String name = "xxx"; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 310 | int state = OFPortState.OFPPS_STP_FORWARD.getValue(); |
| 311 | OFPhysicalPort port = new OFPhysicalPort(); |
| 312 | port.setPortNumber(portNumber); |
| 313 | port.setName(name); |
| 314 | port.setState(state); |
| 315 | |
| 316 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 317 | assertTrue(sw != null); |
| 318 | swSt.addPort(dpid, port); |
| 319 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 320 | assertTrue(portObj != null); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Desc: |
| 325 | * Test method for add method. |
| 326 | * Condition: |
| 327 | * The port status is down. |
| 328 | * Expect: |
| 329 | * Delete the port. |
| 330 | */ |
| 331 | //@Ignore |
| 332 | @Test |
| 333 | public void testAddDownPort() { |
| 334 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 335 | short portNumber = 3; |
| 336 | String name = "port 3 at ATL Switch"; |
| 337 | int state = OFPortState.OFPPS_LINK_DOWN.getValue(); |
| 338 | OFPhysicalPort port = new OFPhysicalPort(); |
| 339 | port.setPortNumber(portNumber); |
| 340 | port.setName(name); |
| 341 | port.setState(state); |
| 342 | |
| 343 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 344 | assertTrue(sw != null); |
| 345 | swSt.addPort(dpid, port); |
| 346 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 347 | assertTrue(portObj == null); |
| 348 | } |
| 349 | } |