Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.devicemanager.internal; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 5 | import java.net.InetAddress; |
| 6 | import java.net.UnknownHostException; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 7 | import java.util.Arrays; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import net.floodlightcontroller.core.internal.TestDatabaseManager; |
| 11 | import net.floodlightcontroller.devicemanager.IDevice; |
| 12 | import net.floodlightcontroller.devicemanager.SwitchPort; |
| 13 | import net.floodlightcontroller.devicemanager.internal.Device; |
| 14 | import net.floodlightcontroller.packet.IPv4; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 15 | import net.onrc.onos.graph.GraphDBConnection; |
| 16 | import net.onrc.onos.graph.GraphDBOperation; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 19 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 20 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 23 | |
| 24 | import org.easymock.EasyMock; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 27 | import org.junit.Ignore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.openflow.util.HexString; |
| 31 | import org.powermock.api.easymock.PowerMock; |
| 32 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | import org.powermock.modules.junit4.PowerMockRunner; |
| 35 | |
| 36 | import com.thinkaurelius.titan.core.TitanFactory; |
| 37 | import com.thinkaurelius.titan.core.TitanGraph; |
| 38 | |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Jono, 11/4/2013 |
| 41 | * These tests are being ignored because they don't work because they |
| 42 | * rely on test functionality that was written ages ago and hasn't been |
| 43 | * updated as the database schema has evolved. |
| 44 | * These tests work by getting an in-memory Titan database and testing |
| 45 | * the DeviceStorageImpl on top of that. In this regard they're not really |
| 46 | * unit tests as they test the entire DB stack (i.e. GraphDBOperation and |
| 47 | * GraphDBConnection), not just DeviceStorageImpl. |
| 48 | * I've left them here as we may wish to resurrect this kind of |
| 49 | * integration testing of the DB layers in the future. |
| 50 | */ |
| 51 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 52 | //Add Powermock preparation |
| 53 | @RunWith(PowerMockRunner.class) |
| 54 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 55 | public class DeviceStorageImplTestBB { |
| 56 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 57 | |
| 58 | String conf; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 59 | String dbStore; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 60 | private GraphDBConnection conn = null; |
| 61 | private GraphDBOperation ope = null; |
| 62 | private TitanGraph titanGraph = null; |
| 63 | IDeviceStorage deviceImpl = null; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() throws Exception { |
| 67 | |
| 68 | deviceImpl = new DeviceStorageImpl(); |
| 69 | conf = "/dummy/path/to/db"; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 70 | dbStore = "dummyStore"; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 71 | |
| 72 | // Make mock cassandra DB |
| 73 | // Replace TitanFactory.open() to return mock DB |
| 74 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 75 | TestDatabaseManager.populateTestData(titanGraph); |
| 76 | PowerMock.mockStatic(TitanFactory.class); |
| 77 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 78 | PowerMock.replay(TitanFactory.class); |
| 79 | |
| 80 | conn = GraphDBConnection.getInstance(conf); |
| 81 | ope = new GraphDBOperation(conn); |
| 82 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 83 | deviceImpl.init(dbStore, conf); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @After |
| 87 | public void tearDown() throws Exception { |
| 88 | titanGraph.shutdown(); |
| 89 | TestDatabaseManager.deleteTestDatabase(); |
| 90 | |
| 91 | deviceImpl.close(); |
| 92 | deviceImpl = null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Desc: |
| 97 | * Test method for addDevice method. |
| 98 | * Codition: |
| 99 | * N/A |
| 100 | * Expect: |
| 101 | * Get proper IDeviceObject |
| 102 | * Check the IDeviceObject properties set |
| 103 | */ |
| 104 | @Test |
| 105 | public void testAddDevice() { |
| 106 | try |
| 107 | { |
| 108 | //Make mockDevice |
| 109 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 110 | // Mac addr for test device. |
| 111 | String macAddr = "99:99:99:99:99:99"; |
| 112 | // IP addr for test device |
| 113 | String ip = "192.168.100.1"; |
| 114 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 115 | Integer[] ipaddrs = {ipInt}; |
| 116 | // Mac addr for attached switch |
| 117 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 118 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 119 | // Port number for attached switch |
| 120 | short portNum = 2; |
| 121 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 122 | SwitchPort[] sps = {sp1}; |
| 123 | |
| 124 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 125 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 126 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 127 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 128 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 129 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 130 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 131 | |
| 132 | EasyMock.replay(mockDev); |
| 133 | |
| 134 | //Add the device |
| 135 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 136 | assertNotNull(obj); |
| 137 | |
| 138 | //Test to take a Device from DB correctly |
| 139 | IDeviceObject devObj1 = ope.searchDevice(macAddr); |
| 140 | assertEquals(macAddr, devObj1.getMACAddress()); |
| 141 | |
| 142 | //Test to take a attached sw from DB correctly |
| 143 | for(ISwitchObject sw1: devObj1.getSwitch()) |
| 144 | { |
| 145 | String swMacFromDB = sw1.getDPID(); |
| 146 | assertEquals(switchMacAddr, swMacFromDB); |
| 147 | } |
| 148 | |
| 149 | //Test to take a IP addr from DB |
| 150 | //TodoForGettingIPaddr. There may be bug in the test class. |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 151 | |
| 152 | //XXX not updated to new interface |
| 153 | //String ipFromDB = devObj1.getIPAddress(); |
| 154 | String ipFromDB = "foo"; |
| 155 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 156 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 157 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 158 | assertTrue(ipsList.contains(ip)); |
| 159 | |
| 160 | //Test to take a attached port from DB |
| 161 | for(IPortObject port : devObj1.getAttachedPorts()) |
| 162 | { |
| 163 | |
| 164 | //In this implementing, the object was not set the port. So it must be null. |
| 165 | if(port.getNumber() != null) |
| 166 | { |
| 167 | String portNumFromDB = port.getNumber().toString(); |
| 168 | assertEquals(String.valueOf(portNum), portNumFromDB); |
| 169 | } |
| 170 | } |
| 171 | } catch(Exception e) { |
| 172 | fail(e.getMessage()); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Desc: |
| 178 | * Test method for addDevice method. |
| 179 | * Condition: |
| 180 | * Already added device is existed. |
| 181 | * Expect: |
| 182 | * Get proper IDeviceObject still. |
| 183 | * Check the IDeviceObject properties set. |
| 184 | */ |
| 185 | @Test |
| 186 | public void testAddDeviceExisting() { |
| 187 | try |
| 188 | { |
| 189 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 190 | String macAddr = "99:99:99:99:99:99"; |
| 191 | String ip = "192.168.100.1"; |
| 192 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 193 | Integer[] ipaddrs = {ipInt}; |
| 194 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 195 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 196 | short portNum = 2; |
| 197 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 198 | SwitchPort[] sps = {sp1}; |
| 199 | |
| 200 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 201 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 202 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 203 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 204 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 205 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 206 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 207 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 208 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 209 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 210 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 211 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 212 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 213 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 214 | EasyMock.replay(mockDev); |
| 215 | |
| 216 | //Add the device |
| 217 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 218 | assertNotNull(obj); |
| 219 | |
| 220 | //Test to take a Device from DB correctly |
| 221 | IDeviceObject devObj1 = ope.searchDevice(macAddr); |
| 222 | assertEquals(macAddr, devObj1.getMACAddress()); |
| 223 | |
| 224 | //Add the same device |
| 225 | IDeviceObject obj2 = deviceImpl.addDevice(mockDev); |
| 226 | assertNotNull(obj2); |
| 227 | |
| 228 | IDeviceObject devObj2 = ope.searchDevice(macAddr); |
| 229 | assertEquals(macAddr, devObj2.getMACAddress()); |
| 230 | |
| 231 | //Test to take a attached port from DB |
| 232 | for(IPortObject port : devObj2.getAttachedPorts()) |
| 233 | { |
| 234 | //In this implementing, the object was not set the port. So it must be null. |
| 235 | if(port.getNumber() != null) |
| 236 | { |
| 237 | |
| 238 | String portNumFromDB = port.getNumber().toString(); |
Yuta HIGUCHI | b63f94f | 2013-10-14 10:01:45 -0700 | [diff] [blame] | 239 | assertEquals(String.valueOf(portNum), portNumFromDB); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 240 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 244 | //XXX not updated to new interface |
| 245 | //String ipFromDB = devObj2.getIPAddress(); |
| 246 | String ipFromDB = "foo"; |
| 247 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 248 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 249 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 250 | assertTrue(ipsList.contains(ip)); |
| 251 | |
| 252 | //Test to take a attached port from DB |
| 253 | for(IPortObject port : devObj2.getAttachedPorts()) |
| 254 | { |
| 255 | |
| 256 | //In this implementing, the object was not set the port. So it must be null. |
| 257 | if(port.getNumber() != null) |
| 258 | { |
| 259 | String portNumFromDB = port.getNumber().toString(); |
| 260 | assertEquals(String.valueOf(portNum), portNumFromDB); |
| 261 | } |
| 262 | } |
| 263 | } catch(Exception e) { |
| 264 | fail(e.getMessage()); |
| 265 | } |
| 266 | } |
| 267 | /** |
| 268 | * Desc: |
| 269 | * Test method for updateDevice method. |
| 270 | * Condition: |
| 271 | * The mac address and attachment point are the same. |
| 272 | * All of the other parameter are different. |
| 273 | * Expect: |
| 274 | * Changed parameters are set properly. |
| 275 | */ |
| 276 | //@Ignore |
| 277 | @Test |
| 278 | public void testUpdateDevice() { |
| 279 | try |
| 280 | { |
| 281 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 282 | String macAddr = "99:99:99:99:99:99"; |
| 283 | String ip = "192.168.100.1"; |
| 284 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 285 | Integer[] ipaddrs = {ipInt}; |
| 286 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 287 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 288 | short portNum = 2; |
| 289 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 290 | SwitchPort[] sps = {sp1}; |
| 291 | |
| 292 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 293 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 294 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 295 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 296 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 297 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 298 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 299 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 300 | EasyMock.replay(mockDev); |
| 301 | |
| 302 | //Dev2 (attached port is the same) |
| 303 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 304 | String macAddr2 = "99:aa:aa:aa:aa:aa"; |
| 305 | Integer ip2 = IPv4.toIPv4Address("192.168.100.2"); |
| 306 | Integer[] ipaddrs2 = {ip2}; |
| 307 | |
| 308 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 309 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 310 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 311 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 312 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 313 | EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps); |
| 314 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 315 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr2); |
| 316 | EasyMock.replay(mockDev2); |
| 317 | |
| 318 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 319 | assertNotNull(obj); |
| 320 | |
| 321 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 322 | assertEquals(macAddr, dev1.getMACAddress()); |
| 323 | |
| 324 | //update theDevice |
| 325 | deviceImpl.updateDevice(mockDev2); |
| 326 | IDeviceObject dev2 = ope.searchDevice(macAddr2); |
| 327 | assertEquals(macAddr2, dev2.getMACAddress()); |
| 328 | IPortObject iport = ope.searchPort(switchMacAddr, portNum); |
| 329 | |
| 330 | //Test to take a attached port from DB |
| 331 | for(IDeviceObject dev : iport.getDevices()) |
| 332 | { |
| 333 | String macAddrFromDB = dev.getMACAddress(); |
| 334 | if(macAddr2.equals(macAddrFromDB)){ |
| 335 | //Nothing to do |
| 336 | } |
| 337 | else{ |
| 338 | fail("notFoundTheDeviceOnThePort"); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | } catch(Exception e) { |
| 343 | fail(e.getMessage()); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Desc: |
| 349 | * Test method for testRemoveDevice method. |
| 350 | * Condition: |
| 351 | * 1. Unregistered IDeviceObject argument is put. |
| 352 | * Expect: |
| 353 | * 1. Nothing happen when unregistered IDeviceObject is put |
| 354 | * 2. IDeviceObject will be removed. |
| 355 | */ |
| 356 | //@Ignore |
| 357 | @Test |
| 358 | public void testRemoveDevice() { |
| 359 | try |
| 360 | { |
| 361 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 362 | String macAddr = "99:99:99:99:99:99"; |
| 363 | String ip = "192.168.100.1"; |
| 364 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 365 | Integer[] ipaddrs = {ipInt}; |
| 366 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 367 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 368 | short portNum = 2; |
| 369 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 370 | SwitchPort[] sps = {sp1}; |
| 371 | |
| 372 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 373 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 374 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 375 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 376 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 377 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 378 | |
| 379 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 380 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 381 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 382 | EasyMock.replay(mockDev); |
| 383 | |
| 384 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 385 | assertNotNull(obj); |
| 386 | |
| 387 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 388 | assertEquals(macAddr, dev1.getMACAddress()); |
| 389 | |
| 390 | deviceImpl.removeDevice(mockDev); |
| 391 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 392 | assertNull(dev); |
| 393 | |
| 394 | } catch(Exception e) { |
| 395 | fail(e.getMessage()); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Desc: |
| 401 | * Test method for getDeviceByMac |
| 402 | * Condition: |
| 403 | * 1. Unregistered mac address argument is set |
| 404 | * Expect: |
| 405 | * 1.Nothing happen when you put unregistered mac address |
| 406 | * 2.Get the proper IDeviceObject. |
| 407 | * 3.Check the IDeviceObject properties set. |
| 408 | */ |
| 409 | //@Ignore |
| 410 | @Test |
| 411 | public void testGetDeviceByMac() { |
| 412 | try |
| 413 | { |
| 414 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 415 | String macAddr = "99:99:99:99:99:99"; |
| 416 | String ip = "192.168.100.1"; |
| 417 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 418 | Integer[] ipaddrs = {ipInt}; |
| 419 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 420 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 421 | short portNum = 2; |
| 422 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 423 | SwitchPort[] sps = {sp1}; |
| 424 | |
| 425 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 426 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 427 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 428 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 429 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 430 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 431 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 432 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 433 | EasyMock.replay(mockDev); |
| 434 | |
| 435 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 436 | assertNotNull(obj); |
| 437 | |
| 438 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 439 | assertEquals(macAddr, dev1.getMACAddress()); |
| 440 | |
| 441 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 442 | assertNotNull(dev); |
| 443 | assertEquals(macAddr, dev.getMACAddress()); |
| 444 | |
| 445 | } catch(Exception e) { |
| 446 | fail(e.getMessage()); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Desc: |
| 452 | * Test method for getDeviceByIP method. |
| 453 | * Condition: |
| 454 | * 1. Unregistered ip address argument is set |
| 455 | * Expect: |
| 456 | * 1. Nothing happen when you put unregistered mac address |
| 457 | * 2. Get the proper IDeviceObject. |
| 458 | * 3. Check the IDeviceObject properties set. |
| 459 | */ |
| 460 | //@Ignore |
| 461 | @Test |
| 462 | public void testGetDeviceByIP() { |
| 463 | try |
| 464 | { |
| 465 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 466 | String macAddr = "99:99:99:99:99:99"; |
| 467 | String ip = "192.168.100.1"; |
| 468 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 469 | Integer[] ipaddrs = {ipInt}; |
| 470 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 471 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 472 | short portNum = 2; |
| 473 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 474 | SwitchPort[] sps = {sp1}; |
| 475 | |
| 476 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 477 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 478 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 479 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 480 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 481 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 482 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 483 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 484 | EasyMock.replay(mockDev); |
| 485 | |
| 486 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 487 | assertNotNull(obj); |
| 488 | |
| 489 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 490 | assertEquals(macAddr, dev1.getMACAddress()); |
| 491 | |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 492 | int ip_int = getPackedIPv4Address(ip); |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 493 | //XXX not updated to new interface |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 494 | IDeviceObject dev = deviceImpl.getDeviceByIP(ip_int); |
| 495 | //IDeviceObject dev = null; |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 496 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 497 | assertNotNull(dev); |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 498 | |
| 499 | //XXX not updated to new interface |
| 500 | //String ipFromDB = dev.getIPAddress(); |
| 501 | String ipFromDB = "foo"; |
| 502 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 503 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 504 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 505 | assertTrue(ipsList.contains(ip)); |
| 506 | |
| 507 | } catch(Exception e) { |
| 508 | fail(e.getMessage()); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Desc: |
| 514 | * Test method for testChangeDeviceAttachmentsIDevice |
| 515 | * Condition: |
| 516 | * 1. Unexisting attachment point argument is set |
| 517 | * Expect: |
| 518 | * 1. Unexisting attachment point is ignored, so nothing happen. |
| 519 | * 2. Change the attachment point. |
| 520 | */ |
| 521 | //@Ignore |
| 522 | @Test |
| 523 | public void testChangeDeviceAttachmentsIDevice() { |
| 524 | try |
| 525 | { |
| 526 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 527 | String macAddr = "99:99:99:99:99:99"; |
| 528 | String ip = "192.168.100.1"; |
| 529 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 530 | Integer[] ipaddrs = {ipInt}; |
| 531 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 532 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 533 | short portNum = 2; |
| 534 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 535 | SwitchPort[] sps = {sp1}; |
| 536 | |
| 537 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 538 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 539 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 540 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 541 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 542 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 543 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 544 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 545 | EasyMock.replay(mockDev); |
| 546 | |
| 547 | //Dev2 |
| 548 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 549 | String switchMacAddr2 = "00:00:00:00:00:00:0a:02"; |
| 550 | long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2); |
| 551 | short portNum2 = 2; |
| 552 | SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2); |
| 553 | SwitchPort sps2[] = {sp2}; |
| 554 | |
| 555 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 556 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 557 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 558 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 559 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs); |
| 560 | EasyMock.expect(mockDev2.getAttachmentPoints()).andReturn(sps2); |
| 561 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 562 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 563 | EasyMock.replay(mockDev2); |
| 564 | |
| 565 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 566 | assertNotNull(obj); |
| 567 | |
| 568 | deviceImpl.changeDeviceAttachments(mockDev2); |
| 569 | |
| 570 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 571 | assertNotNull(dev); |
| 572 | |
| 573 | for(ISwitchObject sw1: dev.getSwitch()) |
| 574 | { |
| 575 | String swMacFromDB = sw1.getDPID(); |
| 576 | assertEquals(switchMacAddr2, swMacFromDB); |
| 577 | } |
| 578 | } catch(Exception e) { |
| 579 | fail(e.getMessage()); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | //@Ignore |
| 584 | @Test |
| 585 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 586 | //It is tested by the testChangeDeviceAttachmentsIDevice |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Desc: |
| 591 | * Test method for testChangeDeviceIPv4Address |
| 592 | * Condition: |
| 593 | * N/A |
| 594 | * Expect: |
| 595 | * 1. Check correctly changed the ipadress |
| 596 | */ |
| 597 | //@Ignore |
| 598 | @Test |
| 599 | public void testChangeDeviceIPv4Address() { |
| 600 | try |
| 601 | { |
| 602 | //Dev1 |
| 603 | IDevice mockDev = EasyMock.createMock(Device.class); |
| 604 | String macAddr = "99:99:99:99:99:99"; |
| 605 | String ip = "192.168.100.1"; |
| 606 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 607 | Integer[] ipaddrs = {ipInt}; |
| 608 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 609 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 610 | short portNum = 2; |
| 611 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 612 | SwitchPort[] sps = {sp1}; |
| 613 | |
| 614 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 615 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 616 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 617 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 618 | EasyMock.expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 619 | EasyMock.expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 620 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 621 | EasyMock.expect(mockDev.getMACAddressString()).andReturn(macAddr); |
| 622 | EasyMock.replay(mockDev); |
| 623 | |
| 624 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 625 | assertNotNull(obj); |
| 626 | |
| 627 | IDevice mockDev2 = EasyMock.createMock(Device.class); |
| 628 | String ip2 = "192.168.100.2"; |
| 629 | Integer ipInt2 = IPv4.toIPv4Address(ip2); |
| 630 | Integer[] ipaddrs2 = {ipInt2}; |
| 631 | EasyMock.expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 632 | EasyMock.expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 633 | EasyMock.replay(mockDev2); |
| 634 | |
| 635 | IDeviceObject dev1 = ope.searchDevice(macAddr); |
| 636 | assertEquals(macAddr, dev1.getMACAddress()); |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 637 | |
| 638 | //XXX not updated to new interface |
| 639 | //String ipFromDB = dev1.getIPAddress(); |
| 640 | String ipFromDB = "foo"; |
| 641 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 642 | String[] ipsFromDB = ipFromDB.replace("[", "").replace("]", "").split(","); |
| 643 | List<String> ipsList = Arrays.asList(ipsFromDB); |
| 644 | assertTrue(ipsList.contains(ip)); |
| 645 | |
| 646 | deviceImpl.changeDeviceIPv4Address(mockDev2); |
| 647 | |
| 648 | IDeviceObject dev2 = ope.searchDevice(macAddr); |
| 649 | assertEquals(macAddr, dev2.getMACAddress()); |
Jonathan Hart | 26bfe5c | 2013-11-04 12:19:51 -0800 | [diff] [blame] | 650 | |
| 651 | //XXX not updated to new interface |
| 652 | //String ipFromDB2 = dev2.getIPAddress(); |
| 653 | String ipFromDB2 = "foo"; |
| 654 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 655 | String[] ipsFromDB2 = ipFromDB2.replace("[", "").replace("]", "").split(","); |
| 656 | List<String> ipsList2 = Arrays.asList(ipsFromDB2); |
| 657 | assertTrue(ipsList2.contains(ip2)); |
| 658 | } |
| 659 | catch(Exception e) { |
| 660 | fail(e.getMessage()); |
| 661 | } |
| 662 | } |
| 663 | |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 664 | int getPackedIPv4Address(String ip) throws UnknownHostException { |
| 665 | byte[] bytes = InetAddress.getByName(ip).getAddress(); |
| 666 | |
| 667 | int val = 0; |
| 668 | for (int i = 0; i < bytes.length; i++) { |
| 669 | val <<= 8; |
| 670 | val |= bytes[i] & 0xff; |
| 671 | } |
| 672 | return val; |
| 673 | } |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 674 | } |