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; |
| 52 | private GraphDBConnection conn = null; |
| 53 | private GraphDBOperation ope = null; |
| 54 | private TitanGraph titanGraph = null; |
| 55 | ISwitchStorage swSt = null; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() throws Exception { |
| 59 | |
| 60 | swSt = new SwitchStorageImpl(); |
| 61 | conf = "/dummy/path/to/db"; |
| 62 | |
| 63 | // Make mock cassandra DB |
| 64 | // Replace TitanFactory.open() to return mock DB |
| 65 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 66 | TestDatabaseManager.populateTestData(titanGraph); |
| 67 | PowerMock.mockStatic(TitanFactory.class); |
| 68 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 69 | PowerMock.replay(TitanFactory.class); |
| 70 | |
| 71 | conn = GraphDBConnection.getInstance(conf); |
| 72 | ope = new GraphDBOperation(conn); |
| 73 | |
| 74 | swSt.init(conf); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() throws Exception { |
| 79 | |
| 80 | titanGraph.shutdown(); |
| 81 | TestDatabaseManager.deleteTestDatabase(); |
| 82 | |
| 83 | swSt.close(); |
| 84 | swSt = null; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Desc: |
| 89 | * Test method for addSwitch method. |
| 90 | * Condition: |
| 91 | * Normal |
| 92 | * Expect: |
| 93 | * 1. Switch should be generated. |
| 94 | * 2. The status of switch should be ACTIVE |
| 95 | */ |
| 96 | //@Ignore |
| 97 | @Test |
| 98 | public void testAddSwitch() { |
| 99 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 100 | |
| 101 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 102 | assertTrue(sw == null); |
| 103 | swSt.addSwitch(dpid); |
| 104 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 105 | assertTrue(sw2 != null); |
| 106 | assertEquals(sw2.getState(), "ACTIVE"); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Desc: |
| 111 | * Test method for addSwitch method. |
| 112 | * Condition: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 113 | * The existing switch status is INACTIVE. |
| 114 | * The switch is already existing. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 115 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 116 | * 1. After add the same switch, the status of switch should be ACTIVE |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 117 | */ |
| 118 | //@Ignore |
| 119 | @Test |
| 120 | public void testAddSwitchExisting() { |
| 121 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 122 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 123 | swSt.updateSwitch(dpid, SwitchState.INACTIVE, DM_OPERATION.UPDATE); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 124 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 125 | assertTrue(sw != null); |
| 126 | assertEquals(sw.getState(), SwitchState.INACTIVE.toString()); |
| 127 | swSt.addSwitch(dpid); |
| 128 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 129 | assertTrue(sw2 != null); |
| 130 | assertEquals(sw2.getState(), SwitchState.ACTIVE.toString()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Desc: |
| 135 | * Test method for testUpdate method. |
| 136 | * Condition: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 137 | * The switch is not existing. |
| 138 | * The status of added switch is INACTIVE. |
| 139 | * DM_OPERATION is CREATE. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 140 | * Expect: |
| 141 | * 1. Switch should be created. |
| 142 | * 2. The status of switch should be INACTIVE. |
| 143 | */ |
| 144 | //@Ignore |
| 145 | @Test |
| 146 | public void testUpdate() { |
| 147 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 148 | SwitchState state = ISwitchStorage.SwitchState.INACTIVE; |
| 149 | DM_OPERATION dmope = INetMapStorage.DM_OPERATION.CREATE; |
| 150 | |
| 151 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 152 | assertTrue(sw == null); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 153 | swSt.updateSwitch(dpid, state, dmope); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 154 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 155 | assertTrue(sw2 != null); |
| 156 | assertEquals(sw2.getState(), state.toString()); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Desc: |
| 161 | * Test method for testUpdate method. |
| 162 | * Condition: |
| 163 | * The switch is existing. |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 164 | * The status of added switch is ACTIVE. |
| 165 | * DM_OPERATION is DELETE. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 166 | * Expect: |
| 167 | * 1. Switch should be deleted. |
| 168 | */ |
| 169 | //@Ignore |
| 170 | @Test |
| 171 | public void testUpdateWithDELETE() { |
| 172 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 173 | SwitchState state = ISwitchStorage.SwitchState.ACTIVE; |
| 174 | DM_OPERATION dmope = INetMapStorage.DM_OPERATION.DELETE; |
| 175 | |
| 176 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 177 | assertTrue(sw != null); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 178 | swSt.updateSwitch(dpid, state, dmope); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 179 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 180 | assertTrue(sw2 == null); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Desc: |
| 185 | * Test method for delete switch method. |
| 186 | * Condition: |
| 187 | * The switch is existing. |
| 188 | * Expect: |
| 189 | * 1. Switch should be deleted. |
| 190 | */ |
| 191 | //@Ignore |
| 192 | @Test |
| 193 | public void testDeleteSwitch() { |
| 194 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 195 | |
| 196 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 197 | assertTrue(sw != null); |
| 198 | swSt.deleteSwitch(dpid); |
| 199 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 200 | assertTrue(sw2 == null); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Desc: |
| 205 | * Test method for delete switch method. |
| 206 | * Condition: |
| 207 | * The switch is not existing. |
| 208 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 209 | * Nothing happens. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 210 | */ |
| 211 | //@Ignore |
| 212 | @Test |
| 213 | public void testDeleteNonExistingSwitch() { |
| 214 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 215 | |
| 216 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 217 | assertTrue(sw == null); |
| 218 | swSt.deleteSwitch(dpid); |
| 219 | ISwitchObject sw2 = ope.searchSwitch(dpid); |
| 220 | assertTrue(sw2 == null); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Desc: |
| 225 | * Test method for delete port method. |
| 226 | * Condition: |
| 227 | * The port is existing. |
| 228 | * Expect: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 229 | * Deleted the port. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 230 | */ |
| 231 | //@Ignore |
| 232 | @Test |
| 233 | public void testDeletePort() { |
| 234 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 235 | short portNumber = 3; |
| 236 | |
| 237 | IPortObject portObj1 = ope.searchPort(dpid, portNumber); |
| 238 | assertTrue(portObj1 != null); |
| 239 | swSt.deletePort(dpid, portNumber); |
| 240 | IPortObject portObj2 = ope.searchPort(dpid, portNumber); |
| 241 | assertTrue(portObj2 == null); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Desc: |
| 246 | * Test method for delete port method. |
| 247 | * Condition: |
| 248 | * The port is not existing. |
| 249 | * Expect: |
| 250 | * Nothing happens. |
| 251 | */ |
| 252 | //@Ignore |
| 253 | @Test |
| 254 | public void testDeleteNonExistingPort() { |
| 255 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 256 | short portNumber = 4; |
| 257 | |
| 258 | IPortObject portObj1 = ope.searchPort(dpid, portNumber); |
| 259 | assertTrue(portObj1 == null); |
| 260 | swSt.deletePort(dpid, portNumber); |
| 261 | IPortObject portObj2 = ope.searchPort(dpid, portNumber); |
| 262 | assertTrue(portObj2 == null); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Desc: |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 267 | * Test method for add port method. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 268 | * Condition: |
| 269 | * The port is not existing. |
| 270 | * Expect: |
| 271 | * The port should be added. |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 272 | * The desc of IPortObject is the same as the name of OFPhysicalPort. |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 273 | */ |
| 274 | //@Ignore |
| 275 | @Test |
| 276 | public void testAddPort() { |
| 277 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 278 | short portNumber = 4; |
| 279 | String name = "port 4 at ATL Switch"; |
| 280 | int state = OFPortState.OFPPS_STP_FORWARD.getValue(); |
| 281 | OFPhysicalPort port = new OFPhysicalPort(); |
| 282 | port.setPortNumber(portNumber); |
| 283 | port.setName(name); |
| 284 | port.setState(state); |
| 285 | |
| 286 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 287 | assertTrue(sw != null); |
| 288 | swSt.addPort(dpid, port); |
| 289 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 290 | assertTrue(portObj != null); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 291 | assertEquals(portObj.getDesc(), name); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Desc: |
| 296 | * Test method for add method. |
| 297 | * Condition: |
| 298 | * The port is existing. |
| 299 | * Expect: |
| 300 | * Nothing happens. |
| 301 | */ |
| 302 | //@Ignore |
| 303 | @Test |
| 304 | public void testAddExistingPort() { |
| 305 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 306 | short portNumber = 3; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 307 | String name = "xxx"; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 308 | int state = OFPortState.OFPPS_STP_FORWARD.getValue(); |
| 309 | OFPhysicalPort port = new OFPhysicalPort(); |
| 310 | port.setPortNumber(portNumber); |
| 311 | port.setName(name); |
| 312 | port.setState(state); |
| 313 | |
| 314 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 315 | assertTrue(sw != null); |
| 316 | swSt.addPort(dpid, port); |
| 317 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 318 | assertTrue(portObj != null); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Desc: |
| 323 | * Test method for add method. |
| 324 | * Condition: |
| 325 | * The port status is down. |
| 326 | * Expect: |
| 327 | * Delete the port. |
| 328 | */ |
| 329 | //@Ignore |
| 330 | @Test |
| 331 | public void testAddDownPort() { |
| 332 | String dpid = "00:00:00:00:00:00:0a:06"; |
| 333 | short portNumber = 3; |
| 334 | String name = "port 3 at ATL Switch"; |
| 335 | int state = OFPortState.OFPPS_LINK_DOWN.getValue(); |
| 336 | OFPhysicalPort port = new OFPhysicalPort(); |
| 337 | port.setPortNumber(portNumber); |
| 338 | port.setName(name); |
| 339 | port.setState(state); |
| 340 | |
| 341 | ISwitchObject sw = ope.searchSwitch(dpid); |
| 342 | assertTrue(sw != null); |
| 343 | swSt.addPort(dpid, port); |
| 344 | IPortObject portObj = ope.searchPort(dpid, portNumber); |
| 345 | assertTrue(portObj == null); |
| 346 | } |
| 347 | } |