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