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