Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.devicemanager.internal; |
| 2 | |
HIGUCHI Yuta | 5fdd915 | 2013-06-17 15:48:09 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.easymock.EasyMock.verify; |
| 7 | import static org.junit.Assert.assertNotNull; |
| 8 | import static org.junit.Assert.assertNull; |
| 9 | import static org.junit.Assert.fail; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.List; |
| 13 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.devicemanager.IDevice; |
| 15 | import net.floodlightcontroller.devicemanager.SwitchPort; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 16 | import net.floodlightcontroller.packet.IPv4; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 17 | import net.onrc.onos.graph.GraphDBConnection; |
| 18 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 5fdd915 | 2013-06-17 15:48:09 -0700 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 20 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 21 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
| 22 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.devicemanager.internal.Device; |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 24 | import org.easymock.EasyMock; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [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.powermock.modules.junit4.PowerMockRunner; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | import com.thinkaurelius.titan.core.TitanFactory; |
| 37 | |
| 38 | //Add Powermock preparation |
| 39 | @RunWith(PowerMockRunner.class) |
| 40 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class}) |
| 41 | public class DeviceStorageImplTest{ //extends FloodlightTestCase{ |
| 42 | |
| 43 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 44 | |
| 45 | String conf; |
| 46 | DeviceStorageImpl deviceImpl; |
| 47 | private GraphDBConnection mockConn; |
| 48 | private GraphDBOperation mockOpe; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() throws Exception { |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 52 | deviceImpl = new DeviceStorageImpl(); |
| 53 | conf = "/dummy/path/to/db"; |
| 54 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 55 | PowerMock.mockStatic(GraphDBConnection.class); |
| 56 | mockConn = createMock(GraphDBConnection.class); |
| 57 | PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class)); |
| 58 | EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn); |
| 59 | PowerMock.replay(GraphDBConnection.class); |
| 60 | |
| 61 | //PowerMock.mockStatic(GraphDBOperation.class); |
| 62 | mockOpe = PowerMock.createMock(GraphDBOperation.class); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 63 | PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe); |
| 64 | mockOpe.close(); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 65 | PowerMock.replay(GraphDBOperation.class); |
| 66 | // Replace the conf to dummy conf |
| 67 | // String conf = "/tmp/cassandra.titan"; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 68 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 69 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 70 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @After |
| 74 | public void tearDown() throws Exception { |
| 75 | deviceImpl.close(); |
| 76 | deviceImpl = null; |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 77 | |
| 78 | verify(mockOpe); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private String makeIPStringFromArray(Integer[] ipaddresses){ |
| 82 | String multiIntString = ""; |
| 83 | for(Integer intValue : ipaddresses) |
| 84 | { |
| 85 | if (multiIntString == null || multiIntString.isEmpty()){ |
| 86 | multiIntString = "[" + IPv4.fromIPv4Address(intValue); |
| 87 | } |
| 88 | else{ |
| 89 | multiIntString += "," + IPv4.fromIPv4Address(intValue); |
| 90 | } |
| 91 | } |
| 92 | return multiIntString + "]"; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Desc: |
| 98 | * Test method for addDevice method. |
| 99 | * Codition: |
| 100 | * N/A |
| 101 | * Expect: |
| 102 | * Get proper IDeviceObject |
| 103 | */ |
| 104 | //@Ignore |
| 105 | @Test |
| 106 | public void testAddNewDevice() { |
| 107 | try |
| 108 | { |
| 109 | //Make mockDevice |
| 110 | IDevice mockDev = createMock(Device.class); |
| 111 | // Mac addr for test device. |
| 112 | String macAddr = "99:99:99:99:99:99"; |
| 113 | // IP addr for test device |
| 114 | String ip = "192.168.100.1"; |
| 115 | Integer[] ipaddrs = {IPv4.toIPv4Address(ip)}; |
| 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 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 125 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).anyTimes(); |
| 126 | expect(mockDev.getAttachmentPoints()).andReturn(sps).anyTimes(); |
| 127 | replay(mockDev); |
| 128 | |
| 129 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 130 | IPortObject mockIPort = createMock(IPortObject.class); |
| 131 | mockIPort.setNumber(portNum); |
| 132 | mockIPort.setType("port"); |
| 133 | String iPortDesc = "port 1 at SEA Switch"; |
| 134 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 135 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 136 | replay(mockIPort); |
| 137 | |
| 138 | //Make Iterator for mockIport |
| 139 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 140 | portList.add(mockIPort); |
| 141 | |
| 142 | //Expectation for mockIDeviceObject |
| 143 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 144 | expect(mockIDev.getAttachedPorts()).andReturn(portList); |
| 145 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 146 | mockIDev.setMACAddress(macAddr); |
| 147 | mockIDev.setType("device"); |
| 148 | mockIDev.setState("ACTIVE"); |
| 149 | replay(mockIDev); |
| 150 | |
| 151 | //Expectation for mockOpe |
| 152 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 153 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 154 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 155 | mockOpe.commit(); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 156 | replay(mockOpe); |
| 157 | |
| 158 | deviceImpl.init(conf); |
| 159 | |
| 160 | //Add the device |
| 161 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 162 | assertNotNull(obj); |
| 163 | |
| 164 | verify(mockIDev); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 165 | |
| 166 | } catch(Exception e) { |
| 167 | fail(e.getMessage()); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Desc: |
| 173 | * Test method for addDevice method. |
| 174 | * Condition: |
| 175 | * Already added device is existed. |
| 176 | * Expect: |
| 177 | * Get proper IDeviceObject still. |
| 178 | * Check the IDeviceObject properties set expectedly. |
| 179 | */ |
| 180 | //@Ignore |
| 181 | @Test |
| 182 | public void testAddDeviceExisting() { |
| 183 | try |
| 184 | { |
| 185 | IDevice mockDev = createMock(Device.class); |
| 186 | String macAddr = "99:99:99:99:99:99"; |
| 187 | String ip = "192.168.100.1"; |
| 188 | Integer[] ipaddrs = {IPv4.toIPv4Address(ip)}; |
| 189 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 190 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 191 | short portNum = 2; |
| 192 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 193 | SwitchPort[] sps = {sp1}; |
| 194 | |
| 195 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 196 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).times(2); |
| 197 | expect(mockDev.getAttachmentPoints()).andReturn(sps).times(2); |
| 198 | replay(mockDev); |
| 199 | |
| 200 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 201 | IPortObject mockIPort = createMock(IPortObject.class); |
| 202 | mockIPort.setNumber(portNum); |
| 203 | mockIPort.setType("port"); |
| 204 | String iPortDesc = "port 1 at SEA Switch"; |
| 205 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 206 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 207 | replay(mockIPort); |
| 208 | |
| 209 | //Make Iterator for mockIport |
| 210 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 211 | portList.add(mockIPort); |
| 212 | |
| 213 | //Expectation for mockIDeviceObject |
| 214 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 215 | expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes(); |
| 216 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 217 | mockIDev.setMACAddress(macAddr); |
| 218 | mockIDev.setType("device"); |
| 219 | mockIDev.setState("ACTIVE"); |
| 220 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 221 | mockIDev.setMACAddress(macAddr); |
| 222 | mockIDev.setType("device"); |
| 223 | mockIDev.setState("ACTIVE"); |
| 224 | replay(mockIDev); |
| 225 | |
| 226 | //Expectation for mockOpe |
| 227 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 228 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 229 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 230 | mockOpe.commit(); |
| 231 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 232 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 233 | mockOpe.commit(); |
| 234 | replay(mockOpe); |
| 235 | |
| 236 | deviceImpl.init(conf); |
| 237 | |
| 238 | //Add the device |
| 239 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 240 | assertNotNull(obj); |
| 241 | |
| 242 | //Add the same device |
| 243 | IDeviceObject obj2 = deviceImpl.addDevice(mockDev); |
| 244 | assertNotNull(obj2); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 245 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 246 | } catch(Exception e) { |
| 247 | fail(e.getMessage()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Desc: |
| 253 | * Test method for updateDevice method. |
| 254 | * Condition: |
| 255 | * The mac address and attachment point are the same. |
| 256 | * All of the other parameter are different. |
| 257 | * Expect: |
| 258 | * Changed parameters are set expectedly. |
| 259 | */ |
| 260 | //@Ignore |
| 261 | @Test |
| 262 | public void testUpdateDevice() { |
| 263 | try |
| 264 | { |
| 265 | IDevice mockDev = createMock(Device.class); |
| 266 | String macAddr = "99:99:99:99:99:99"; |
| 267 | String ip = "192.168.100.1"; |
| 268 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 269 | Integer[] ipaddrs = {ipInt}; |
| 270 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 271 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 272 | short portNum = 2; |
| 273 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 274 | SwitchPort[] sps = {sp1}; |
| 275 | |
| 276 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 277 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 278 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 279 | replay(mockDev); |
| 280 | |
| 281 | //Dev2 (attached port is the same) |
| 282 | IDevice mockDev2 = createMock(Device.class); |
| 283 | String ip2 = "192.168.100.2"; |
| 284 | Integer ipInt2 = IPv4.toIPv4Address(ip2); |
| 285 | Integer[] ipaddrs2 = {ipInt2}; |
| 286 | |
| 287 | expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 288 | expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 289 | expect(mockDev2.getAttachmentPoints()).andReturn(sps); |
| 290 | replay(mockDev2); |
| 291 | |
| 292 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 293 | IPortObject mockIPort = createMock(IPortObject.class); |
| 294 | mockIPort.setNumber(portNum); |
| 295 | mockIPort.setType("port"); |
| 296 | String iPortDesc = "port 1 at SEA Switch"; |
| 297 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 298 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 299 | replay(mockIPort); |
| 300 | |
| 301 | //Make Iterator for mockIport |
| 302 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 303 | portList.add(mockIPort); |
| 304 | |
| 305 | //Expectation for mockIDeviceObject |
| 306 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 307 | expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes(); |
| 308 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 309 | mockIDev.setMACAddress(macAddr); |
| 310 | mockIDev.setType("device"); |
| 311 | mockIDev.setState("ACTIVE"); |
| 312 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2)); |
| 313 | mockIDev.setMACAddress(macAddr); |
| 314 | mockIDev.setType("device"); |
| 315 | mockIDev.setState("ACTIVE"); |
| 316 | replay(mockIDev); |
| 317 | |
| 318 | //Expectation for mockOpe |
| 319 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 320 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 321 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 322 | mockOpe.commit(); |
| 323 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 324 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 325 | mockOpe.commit(); |
| 326 | replay(mockOpe); |
| 327 | |
| 328 | deviceImpl.init(conf); |
| 329 | |
| 330 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 331 | assertNotNull(obj); |
| 332 | |
| 333 | //update theDevice |
| 334 | IDeviceObject obj2 = deviceImpl.updateDevice(mockDev2); |
| 335 | assertNotNull(obj2); |
| 336 | |
| 337 | verify(mockIDev); |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 338 | |
| 339 | } catch(Exception e) { |
| 340 | fail(e.getMessage()); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Desc: |
| 346 | * Test method for testRemoveDevice method. |
| 347 | * Condition: |
| 348 | * 1. Unregistered IDeviceObject argument is put. |
| 349 | * Expect: |
| 350 | * 1. Nothing happen when unregistered IDeviceObject is put |
| 351 | * 2. IDeviceObject will be removed. |
| 352 | */ |
| 353 | //@Ignore |
| 354 | @Test |
| 355 | public void testRemoveDevice() { |
| 356 | try |
| 357 | { |
| 358 | IDevice mockDev = createMock(Device.class); |
| 359 | String macAddr = "99:99:99:99:99:99"; |
| 360 | String ip = "192.168.100.1"; |
| 361 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 362 | Integer[] ipaddrs = {ipInt}; |
| 363 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 364 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 365 | short portNum = 2; |
| 366 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 367 | SwitchPort[] sps = {sp1}; |
| 368 | |
| 369 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 370 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 371 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 372 | replay(mockDev); |
| 373 | |
| 374 | //Dev2 (attached port is the same) |
| 375 | IDevice mockDev2 = createMock(Device.class); |
| 376 | String macAddr2 = "33:33:33:33:33:33"; |
| 377 | expect(mockDev2.getMACAddressString()).andReturn(macAddr2).anyTimes(); |
| 378 | expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs); |
| 379 | expect(mockDev2.getAttachmentPoints()).andReturn(sps); |
| 380 | replay(mockDev2); |
| 381 | |
| 382 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 383 | IPortObject mockIPort = createMock(IPortObject.class); |
| 384 | mockIPort.setNumber(portNum); |
| 385 | mockIPort.setType("port"); |
| 386 | String iPortDesc = "port 1 at SEA Switch"; |
| 387 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 388 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 389 | replay(mockIPort); |
| 390 | |
| 391 | //Make Iterator for mockIport |
| 392 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 393 | portList.add(mockIPort); |
| 394 | |
| 395 | //Expectation for mockIDeviceObject |
| 396 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 397 | expect(mockIDev.getAttachedPorts()).andReturn(portList); |
| 398 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 399 | mockIDev.setMACAddress(macAddr); |
| 400 | mockIDev.setType("device"); |
| 401 | mockIDev.setState("ACTIVE"); |
| 402 | replay(mockIDev); |
| 403 | |
| 404 | //Expectation for mockOpe |
| 405 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 406 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 407 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 408 | mockOpe.commit(); |
| 409 | expect(mockOpe.searchDevice(macAddr2)).andReturn(null); |
| 410 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 411 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 412 | mockOpe.removeDevice(mockIDev); |
| 413 | mockOpe.commit(); |
| 414 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 415 | replay(mockOpe); |
| 416 | |
| 417 | deviceImpl.init(conf); |
| 418 | |
| 419 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 420 | assertNotNull(obj); |
| 421 | |
| 422 | deviceImpl.removeDevice(mockDev2); |
| 423 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 424 | assertNotNull(dev); |
| 425 | |
| 426 | deviceImpl.removeDevice(mockDev); |
| 427 | IDeviceObject dev2 = deviceImpl.getDeviceByMac(macAddr); |
| 428 | assertNull(dev2); |
| 429 | |
| 430 | verify(mockIDev); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 431 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 432 | } catch(Exception e) { |
| 433 | fail(e.getMessage()); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Desc: |
| 439 | * Test method for getDeviceByMac |
| 440 | * Condition: |
| 441 | * 1. Unregistered mac address argument is set |
| 442 | * Expect: |
| 443 | * 1.Nothing happen when you put unregistered mac address |
| 444 | * 2.Get the proper IDeviceObject. |
| 445 | * 3.Check the IDeviceObject properties set expectedly. |
| 446 | */ |
| 447 | //@Ignore |
| 448 | @Test |
| 449 | public void testGetDeviceByMac() { |
| 450 | try |
| 451 | { |
| 452 | IDevice mockDev = createMock(Device.class); |
| 453 | String macAddr = "99:99:99:99:99:99"; |
| 454 | String ip = "192.168.100.1"; |
| 455 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 456 | Integer[] ipaddrs = {ipInt}; |
| 457 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 458 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 459 | short portNum = 2; |
| 460 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 461 | SwitchPort[] sps = {sp1}; |
| 462 | |
| 463 | String dummyMac = "33:33:33:33:33:33"; |
| 464 | |
| 465 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 466 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 467 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 468 | replay(mockDev); |
| 469 | |
| 470 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 471 | IPortObject mockIPort = createMock(IPortObject.class); |
| 472 | mockIPort.setNumber(portNum); |
| 473 | mockIPort.setType("port"); |
| 474 | String iPortDesc = "port 1 at SEA Switch"; |
| 475 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 476 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 477 | replay(mockIPort); |
| 478 | |
| 479 | //Make Iterator for mockIport |
| 480 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 481 | portList.add(mockIPort); |
| 482 | |
| 483 | //Expectation for mockIDeviceObject |
| 484 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 485 | expect(mockIDev.getAttachedPorts()).andReturn(portList); |
| 486 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 487 | mockIDev.setMACAddress(macAddr); |
| 488 | mockIDev.setType("device"); |
| 489 | mockIDev.setState("ACTIVE"); |
| 490 | replay(mockIDev); |
| 491 | |
| 492 | //Expectation for mockOpe |
| 493 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 494 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 495 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 496 | mockOpe.commit(); |
| 497 | expect(mockOpe.searchDevice(dummyMac)).andReturn(null); |
| 498 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 499 | replay(mockOpe); |
| 500 | |
| 501 | deviceImpl.init(conf); |
| 502 | |
| 503 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 504 | assertNotNull(obj); |
| 505 | |
| 506 | IDeviceObject dummyDev = deviceImpl.getDeviceByMac(dummyMac); |
| 507 | assertNull(dummyDev); |
| 508 | |
| 509 | IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr); |
| 510 | assertNotNull(dev); |
| 511 | |
| 512 | verify(mockIDev); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 513 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 514 | } catch(Exception e) { |
| 515 | fail(e.getMessage()); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Desc: |
| 521 | * Test method for getDeviceByIP method. |
| 522 | * Condition: |
| 523 | * 1. Unregistered ip address argument is set |
| 524 | * Expect: |
| 525 | * 1. Nothing happen when you put unregistered mac address |
| 526 | * 2. Get the proper IDeviceObject. |
| 527 | * 3. Check the IDeviceObject properties set expectedly. |
| 528 | */ |
| 529 | //@Ignore |
| 530 | @Test |
| 531 | public void testGetDeviceByIP() { |
| 532 | try |
| 533 | { |
| 534 | IDevice mockDev = createMock(Device.class); |
| 535 | String macAddr = "99:99:99:99:99:99"; |
| 536 | String ip = "192.168.100.1"; |
| 537 | String ip2 = "192.168.100.2"; |
| 538 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 539 | Integer ipInt2 = IPv4.toIPv4Address(ip2); |
| 540 | Integer[] ipaddrs = {ipInt, ipInt2}; |
| 541 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 542 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 543 | short portNum = 2; |
| 544 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 545 | SwitchPort[] sps = {sp1}; |
| 546 | |
| 547 | String dummyIP = "222.222.222.222"; |
| 548 | |
| 549 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 550 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 551 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 552 | replay(mockDev); |
| 553 | |
| 554 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 555 | IPortObject mockIPort = createMock(IPortObject.class); |
| 556 | mockIPort.setNumber(portNum); |
| 557 | mockIPort.setType("port"); |
| 558 | String iPortDesc = "port 1 at SEA Switch"; |
| 559 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 560 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 561 | replay(mockIPort); |
| 562 | |
| 563 | //Make Iterator for mockIport |
| 564 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 565 | portList.add(mockIPort); |
| 566 | |
| 567 | //Expectation for mockIDeviceObject |
| 568 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 569 | expect(mockIDev.getAttachedPorts()).andReturn(portList); |
| 570 | expect(mockIDev.getIPAddress()).andReturn(makeIPStringFromArray(ipaddrs)).times(2); |
| 571 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 572 | mockIDev.setMACAddress(macAddr); |
| 573 | mockIDev.setType("device"); |
| 574 | mockIDev.setState("ACTIVE"); |
| 575 | replay(mockIDev); |
| 576 | |
| 577 | |
| 578 | //Make mock Iterator for IDeviceObject |
| 579 | List<IDeviceObject> deviceList = new ArrayList<IDeviceObject>(); |
| 580 | deviceList.add(mockIDev); |
| 581 | |
| 582 | //Expectation for mockOpe |
| 583 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 584 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 585 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 586 | mockOpe.commit(); |
| 587 | expect(mockOpe.getDevices()).andReturn(deviceList).times(2); |
| 588 | replay(mockOpe); |
| 589 | |
| 590 | deviceImpl.init(conf); |
| 591 | |
| 592 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 593 | assertNotNull(obj); |
| 594 | |
| 595 | IDeviceObject dummyDev = deviceImpl.getDeviceByIP(dummyIP); |
| 596 | assertNull(dummyDev); |
| 597 | |
| 598 | IDeviceObject dev = deviceImpl.getDeviceByIP(ip); |
| 599 | assertNotNull(dev); |
| 600 | |
| 601 | verify(mockIDev); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 602 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 603 | |
| 604 | } catch(Exception e) { |
| 605 | fail(e.getMessage()); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Desc: |
| 611 | * Test method for testChangeDeviceAttachmentsIDevice |
| 612 | * Condition: |
| 613 | * 1. Unexisting attachment point argument is set |
| 614 | * Expect: |
| 615 | * 1. Nothing happen when you put unexisting attachment point. |
| 616 | * 2. Set the attachment point expectedly; |
| 617 | */ |
| 618 | //@Ignore |
| 619 | @Test |
| 620 | public void testChangeDeviceAttachmentsIDevice() { |
| 621 | try |
| 622 | { |
| 623 | IDevice mockDev = createMock(Device.class); |
| 624 | String macAddr = "99:99:99:99:99:99"; |
| 625 | String ip = "192.168.100.1"; |
| 626 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 627 | Integer[] ipaddrs = {ipInt}; |
| 628 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 629 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 630 | short portNum = 2; |
| 631 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 632 | SwitchPort[] sps = {sp1}; |
| 633 | |
| 634 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 635 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 636 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 637 | replay(mockDev); |
| 638 | |
| 639 | //Dev2 |
| 640 | IDevice mockDev2 = createMock(Device.class); |
| 641 | String switchMacAddr2 = "00:00:00:00:00:00:0a:02"; |
| 642 | long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2); |
| 643 | short portNum2 = 2; |
| 644 | SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2); |
| 645 | SwitchPort sps2[] = {sp2}; |
| 646 | |
| 647 | expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 648 | expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs); |
| 649 | expect(mockDev2.getAttachmentPoints()).andReturn(sps2); |
| 650 | replay(mockDev2); |
| 651 | |
| 652 | //Dev3 |
| 653 | IDevice mockDev3 = createMock(Device.class); |
| 654 | String switchMacAddr3 = "00:00:00:00:00:00:00:00"; |
| 655 | long lSwitchMacAddr3 = HexString.toLong(switchMacAddr3); |
| 656 | short portNum3 = 1; |
| 657 | SwitchPort sp3 = new SwitchPort(lSwitchMacAddr3, portNum3); |
| 658 | SwitchPort sps3[] = {sp3}; |
| 659 | |
| 660 | expect(mockDev3.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 661 | expect(mockDev3.getIPv4Addresses()).andReturn(ipaddrs); |
| 662 | expect(mockDev3.getAttachmentPoints()).andReturn(sps3); |
| 663 | replay(mockDev3); |
| 664 | |
| 665 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 666 | |
| 667 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 668 | IPortObject mockIPort = createMock(IPortObject.class); |
| 669 | mockIPort.setNumber(portNum); |
| 670 | mockIPort.setType("port"); |
| 671 | String iPortDesc = "port 1 at SEA Switch"; |
| 672 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 673 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 674 | mockIPort.removeDevice(mockIDev); |
| 675 | mockIPort.removeDevice(mockIDev); |
| 676 | replay(mockIPort); |
| 677 | |
| 678 | //Make Iterator for mockIport |
| 679 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 680 | portList.add(mockIPort); |
| 681 | |
| 682 | //Expectation for mockIDeviceObject |
| 683 | expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes(); |
| 684 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 685 | mockIDev.setMACAddress(macAddr); |
| 686 | mockIDev.setType("device"); |
| 687 | mockIDev.setState("ACTIVE"); |
| 688 | replay(mockIDev); |
| 689 | |
| 690 | //Mock IPortObject 2 with dpid "00:00:00:00:00:00:0a:02" and port "2" |
| 691 | IPortObject mockIPort2 = createMock(IPortObject.class); |
| 692 | mockIPort2.setNumber(portNum2); |
| 693 | mockIPort2.setType("port"); |
| 694 | String iPortDesc2 = "port 2 at LAX Switch"; |
| 695 | expect(mockIPort2.getNumber()).andReturn(portNum2).anyTimes(); |
| 696 | expect(mockIPort2.getDesc()).andReturn(iPortDesc2).anyTimes(); |
| 697 | mockIPort2.setDevice(mockIDev); |
| 698 | replay(mockIPort2); |
| 699 | |
| 700 | //Make Iterator for mockIport |
| 701 | List<IPortObject> portList2 = new ArrayList<IPortObject>(); |
| 702 | portList2.add(mockIPort2); |
| 703 | |
| 704 | //Mock IPortObject 3 with dpid "00:00:00:00:00:00:00:00" and port "1" |
| 705 | IPortObject mockIPort3 = createMock(IPortObject.class); |
| 706 | mockIPort3.setNumber(portNum3); |
| 707 | mockIPort3.setType("port"); |
| 708 | String iPortDesc3 = "n/a"; |
| 709 | expect(mockIPort3.getNumber()).andReturn(portNum3).anyTimes(); |
| 710 | expect(mockIPort3.getDesc()).andReturn(iPortDesc3).anyTimes(); |
| 711 | mockIPort3.setDevice(mockIDev); |
| 712 | replay(mockIPort3); |
| 713 | |
| 714 | //Expectation for mockOpe |
| 715 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 716 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 717 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 718 | mockOpe.commit(); |
| 719 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 720 | expect(mockOpe.searchPort(switchMacAddr2, portNum2)).andReturn(mockIPort2); |
| 721 | mockOpe.commit(); |
| 722 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 723 | expect(mockOpe.searchPort(switchMacAddr3, portNum3)).andReturn(null); |
| 724 | mockOpe.commit(); |
| 725 | replay(mockOpe); |
| 726 | |
| 727 | deviceImpl.init(conf); |
| 728 | |
| 729 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 730 | assertNotNull(obj); |
| 731 | |
| 732 | deviceImpl.changeDeviceAttachments(mockDev2); |
| 733 | |
| 734 | deviceImpl.changeDeviceAttachments(mockDev3); |
| 735 | |
| 736 | verify(mockIDev); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 737 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 738 | |
| 739 | } catch(Exception e) { |
| 740 | fail(e.getMessage()); |
| 741 | } |
| 742 | } |
| 743 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 744 | @Ignore |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 745 | @Test |
| 746 | public void testChangeDeviceAttachmentsIDeviceIDeviceObject() { |
| 747 | //It is tested by the testChangeDeviceAttachmentsIDevice |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Desc: |
| 752 | * Test method for testChangeDeviceIPv4Address |
| 753 | * Condition: |
| 754 | * N/A |
| 755 | * Expect: |
| 756 | * 1. Set the ipadress expectedly. |
| 757 | */ |
| 758 | //@Ignore |
| 759 | @Test |
| 760 | public void testChangeDeviceIPv4Address() { |
| 761 | try |
| 762 | { |
| 763 | //Dev1 |
| 764 | IDevice mockDev = createMock(Device.class); |
| 765 | String macAddr = "99:99:99:99:99:99"; |
| 766 | String ip = "192.168.100.1"; |
| 767 | Integer ipInt = IPv4.toIPv4Address(ip); |
| 768 | Integer[] ipaddrs = {ipInt}; |
| 769 | String switchMacAddr = "00:00:00:00:00:00:0a:01"; |
| 770 | long switchMacAddrL = HexString.toLong(switchMacAddr); |
| 771 | short portNum = 2; |
| 772 | SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum); |
| 773 | SwitchPort[] sps = {sp1}; |
| 774 | |
| 775 | expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes(); |
| 776 | expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs); |
| 777 | expect(mockDev.getAttachmentPoints()).andReturn(sps); |
| 778 | replay(mockDev); |
| 779 | |
| 780 | //Dev2 |
| 781 | IDevice mockDev2 = createMock(Device.class); |
| 782 | String ip2 = "192.168.100.2"; |
| 783 | Integer ipInt2 = IPv4.toIPv4Address(ip2); |
| 784 | Integer[] ipaddrs2 = {ipInt2}; |
| 785 | expect(mockDev2.getMACAddressString()).andReturn(macAddr); |
| 786 | expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2); |
| 787 | replay(mockDev2); |
| 788 | |
| 789 | //Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1" |
| 790 | IPortObject mockIPort = createMock(IPortObject.class); |
| 791 | mockIPort.setNumber(portNum); |
| 792 | mockIPort.setType("port"); |
| 793 | String iPortDesc = "port 1 at SEA Switch"; |
| 794 | expect(mockIPort.getNumber()).andReturn(portNum).anyTimes(); |
| 795 | expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes(); |
| 796 | replay(mockIPort); |
| 797 | |
| 798 | //Make Iterator for mockIport |
| 799 | List<IPortObject> portList = new ArrayList<IPortObject>(); |
| 800 | portList.add(mockIPort); |
| 801 | |
| 802 | //Expectation for mockIDeviceObject |
| 803 | IDeviceObject mockIDev = createMock(IDeviceObject.class); |
| 804 | expect(mockIDev.getAttachedPorts()).andReturn(portList); |
| 805 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs)); |
| 806 | mockIDev.setMACAddress(macAddr); |
| 807 | mockIDev.setType("device"); |
| 808 | mockIDev.setState("ACTIVE"); |
| 809 | mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2)); |
| 810 | replay(mockIDev); |
| 811 | |
| 812 | //Expectation for mockOpe |
| 813 | expect(mockOpe.searchDevice(macAddr)).andReturn(null); |
| 814 | expect(mockOpe.newDevice()).andReturn(mockIDev); |
| 815 | expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort); |
| 816 | mockOpe.commit(); |
| 817 | expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev); |
| 818 | mockOpe.commit(); |
| 819 | replay(mockOpe); |
| 820 | |
| 821 | deviceImpl.init(conf); |
| 822 | |
| 823 | IDeviceObject obj = deviceImpl.addDevice(mockDev); |
| 824 | assertNotNull(obj); |
| 825 | |
| 826 | deviceImpl.changeDeviceIPv4Address(mockDev2); |
| 827 | |
| 828 | verify(mockIDev); |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 829 | |
Teru | ab4b01a | 2013-06-20 10:09:57 -0700 | [diff] [blame] | 830 | |
| 831 | } |
| 832 | catch(Exception e) { |
| 833 | fail(e.getMessage()); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | } |