Added host-related unit tests for model cache.
Change-Id: Iac27f62daed2c8e9114ce924c3014f35eb0ae5ce
Showing
3 changed files
with
153 additions
and
37 deletions
| ... | @@ -106,12 +106,30 @@ public class UiHost extends UiNode { | ... | @@ -106,12 +106,30 @@ public class UiHost extends UiNode { |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | /** | 108 | /** |
| 109 | - * Identifier for the edge link between this host and the device to which | 109 | + * Returns the identifier for the edge link between this host and |
| 110 | - * it is connected. | 110 | + * the device to which it is connected. |
| 111 | * | 111 | * |
| 112 | * @return edge link identifier | 112 | * @return edge link identifier |
| 113 | */ | 113 | */ |
| 114 | public UiLinkId edgeLinkId() { | 114 | public UiLinkId edgeLinkId() { |
| 115 | - return null; | 115 | + return edgeLinkId; |
| 116 | + } | ||
| 117 | + | ||
| 118 | + /** | ||
| 119 | + * Returns the identifier of the device to which the host is connected. | ||
| 120 | + * | ||
| 121 | + * @return device identifier | ||
| 122 | + */ | ||
| 123 | + public DeviceId locationDevice() { | ||
| 124 | + return locDevice; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + /** | ||
| 128 | + * Returns the port number of the device to which the host is connected. | ||
| 129 | + * | ||
| 130 | + * @return port number | ||
| 131 | + */ | ||
| 132 | + public PortNumber locationPort() { | ||
| 133 | + return locPort; | ||
| 116 | } | 134 | } |
| 117 | } | 135 | } | ... | ... |
| ... | @@ -525,47 +525,58 @@ abstract class AbstractTopoModelTest extends AbstractUiImplTest { | ... | @@ -525,47 +525,58 @@ abstract class AbstractTopoModelTest extends AbstractUiImplTest { |
| 525 | } | 525 | } |
| 526 | 526 | ||
| 527 | 527 | ||
| 528 | - private static class MockHostService extends HostServiceAdapter { | 528 | + /** |
| 529 | - private final Map<HostId, Host> hosts = new HashMap<>(); | 529 | + * Creates a default host connected at the given edge device and port. Note |
| 530 | - | 530 | + * that an identifying hex character ("a" - "f") should be supplied. This |
| 531 | - MockHostService() { | 531 | + * will be included in the MAC address of the host (and equivalent value |
| 532 | - for (Device d : ALL_DEVS) { | 532 | + * as last byte in IP address). |
| 533 | - // two hosts per device | 533 | + * |
| 534 | - createHosts(hosts, d); | 534 | + * @param device edge device |
| 535 | - } | 535 | + * @param port port number |
| 536 | - } | 536 | + * @param hexChar identifying hex character |
| 537 | - | 537 | + * @return host connected at that location |
| 538 | - private void createHosts(Map<HostId, Host> hosts, Device d) { | 538 | + */ |
| 539 | - DeviceId deviceId = d.id(); | 539 | + protected static Host createHost(Device device, int port, String hexChar) { |
| 540 | - String devNum = deviceId.toString().substring(1); | 540 | + DeviceId deviceId = device.id(); |
| 541 | - | 541 | + String devNum = deviceId.toString().substring(1); |
| 542 | - String ha = devNum + "a"; | ||
| 543 | - String hb = devNum + "b"; | ||
| 544 | 542 | ||
| 545 | - MacAddress macA = MacAddress.valueOf(HOST_MAC_PREFIX + ha); | 543 | + MacAddress mac = MacAddress.valueOf(HOST_MAC_PREFIX + devNum + hexChar); |
| 546 | - MacAddress macB = MacAddress.valueOf(HOST_MAC_PREFIX + hb); | 544 | + HostId hostId = hostId(String.format("%s/-1", mac)); |
| 547 | 545 | ||
| 548 | - HostId hostA = hostId(String.format("%s/-1", macA)); | 546 | + int ipByte = Integer.valueOf(hexChar, 16); |
| 549 | - HostId hostB = hostId(String.format("%s/-1", macB)); | 547 | + if (ipByte < 10 || ipByte > 15) { |
| 548 | + throw new IllegalArgumentException("hexChar must be a-f"); | ||
| 549 | + } | ||
| 550 | + HostLocation loc = new HostLocation(deviceId, portNumber(port), 0); | ||
| 550 | 551 | ||
| 551 | - PortNumber portA = portNumber(101); | 552 | + IpAddress ip = ip("10." + devNum + ".0." + ipByte); |
| 552 | - PortNumber portB = portNumber(102); | ||
| 553 | 553 | ||
| 554 | - HostLocation locA = new HostLocation(deviceId, portA, 0); | 554 | + return new DefaultHost(ProviderId.NONE, hostId, mac, VlanId.NONE, |
| 555 | - HostLocation locB = new HostLocation(deviceId, portB, 0); | 555 | + loc, ImmutableSet.of(ip)); |
| 556 | + } | ||
| 556 | 557 | ||
| 557 | - IpAddress ipA = ip("10." + devNum + ".0.1"); | 558 | + /** |
| 558 | - IpAddress ipB = ip("10." + devNum + ".0.2"); | 559 | + * Creates a pair of hosts connected to the specified device. |
| 560 | + * | ||
| 561 | + * @param d edge device | ||
| 562 | + * @return pair of hosts | ||
| 563 | + */ | ||
| 564 | + protected static List<Host> createHostPair(Device d) { | ||
| 565 | + List<Host> hosts = new ArrayList<>(); | ||
| 566 | + hosts.add(createHost(d, 101, "a")); | ||
| 567 | + hosts.add(createHost(d, 102, "b")); | ||
| 568 | + return hosts; | ||
| 569 | + } | ||
| 559 | 570 | ||
| 560 | - Host host = new DefaultHost(ProviderId.NONE, | 571 | + private static class MockHostService extends HostServiceAdapter { |
| 561 | - hostA, macA, VlanId.NONE, locA, | 572 | + private final Map<HostId, Host> hosts = new HashMap<>(); |
| 562 | - ImmutableSet.of(ipA)); | ||
| 563 | - hosts.put(hostA, host); | ||
| 564 | 573 | ||
| 565 | - host = new DefaultHost(ProviderId.NONE, | 574 | + MockHostService() { |
| 566 | - hostB, macB, VlanId.NONE, locB, | 575 | + for (Device d : ALL_DEVS) { |
| 567 | - ImmutableSet.of(ipB)); | 576 | + for (Host h : createHostPair(d)) { |
| 568 | - hosts.put(hostB, host); | 577 | + hosts.put(h.id(), h); |
| 578 | + } | ||
| 579 | + } | ||
| 569 | } | 580 | } |
| 570 | 581 | ||
| 571 | @Override | 582 | @Override | ... | ... |
| ... | @@ -22,12 +22,15 @@ import org.onosproject.event.Event; | ... | @@ -22,12 +22,15 @@ import org.onosproject.event.Event; |
| 22 | import org.onosproject.event.EventDispatcher; | 22 | import org.onosproject.event.EventDispatcher; |
| 23 | import org.onosproject.net.Device; | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; | 24 | import org.onosproject.net.DeviceId; |
| 25 | +import org.onosproject.net.Host; | ||
| 26 | +import org.onosproject.net.HostId; | ||
| 25 | import org.onosproject.net.Link; | 27 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.region.Region; | 28 | import org.onosproject.net.region.Region; |
| 27 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; | 29 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; |
| 28 | import org.onosproject.ui.model.topo.UiClusterMember; | 30 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 29 | import org.onosproject.ui.model.topo.UiDevice; | 31 | import org.onosproject.ui.model.topo.UiDevice; |
| 30 | import org.onosproject.ui.model.topo.UiElement; | 32 | import org.onosproject.ui.model.topo.UiElement; |
| 33 | +import org.onosproject.ui.model.topo.UiHost; | ||
| 31 | import org.onosproject.ui.model.topo.UiLink; | 34 | import org.onosproject.ui.model.topo.UiLink; |
| 32 | import org.onosproject.ui.model.topo.UiLinkId; | 35 | import org.onosproject.ui.model.topo.UiLinkId; |
| 33 | import org.onosproject.ui.model.topo.UiRegion; | 36 | import org.onosproject.ui.model.topo.UiRegion; |
| ... | @@ -38,8 +41,10 @@ import java.util.Iterator; | ... | @@ -38,8 +41,10 @@ import java.util.Iterator; |
| 38 | import static org.junit.Assert.assertEquals; | 41 | import static org.junit.Assert.assertEquals; |
| 39 | import static org.junit.Assert.assertFalse; | 42 | import static org.junit.Assert.assertFalse; |
| 40 | import static org.junit.Assert.assertNotNull; | 43 | import static org.junit.Assert.assertNotNull; |
| 44 | +import static org.junit.Assert.assertNull; | ||
| 41 | import static org.junit.Assert.assertTrue; | 45 | import static org.junit.Assert.assertTrue; |
| 42 | import static org.onosproject.cluster.NodeId.nodeId; | 46 | import static org.onosproject.cluster.NodeId.nodeId; |
| 47 | +import static org.onosproject.net.PortNumber.portNumber; | ||
| 43 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; | 48 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
| 44 | 49 | ||
| 45 | /** | 50 | /** |
| ... | @@ -305,6 +310,88 @@ public class ModelCacheTest extends AbstractTopoModelTest { | ... | @@ -305,6 +310,88 @@ public class ModelCacheTest extends AbstractTopoModelTest { |
| 305 | assertEquals("unex # links", 0, cache.linkCount()); | 310 | assertEquals("unex # links", 0, cache.linkCount()); |
| 306 | } | 311 | } |
| 307 | 312 | ||
| 313 | + private void assertHostLinkCounts(int nHosts, int nLinks) { | ||
| 314 | + assertEquals("unex # hosts", nHosts, cache.hostCount()); | ||
| 315 | + assertEquals("unex # links", nLinks, cache.linkCount()); | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + private void assertLocation(HostId hid, DeviceId expDev, int expPort) { | ||
| 319 | + UiHost h = cache.accessHost(hid); | ||
| 320 | + assertEquals("unex device", expDev, h.locationDevice()); | ||
| 321 | + assertEquals("unex port", portNumber(expPort), h.locationPort()); | ||
| 322 | + } | ||
| 323 | + | ||
| 324 | + @Test | ||
| 325 | + public void addHosts() { | ||
| 326 | + title("addHosts"); | ||
| 327 | + | ||
| 328 | + assertHostLinkCounts(0, 0); | ||
| 329 | + Host hostA = createHost(DEV_1, 101, "a"); | ||
| 330 | + Host hostB = createHost(DEV_1, 102, "b"); | ||
| 331 | + | ||
| 332 | + // add a host | ||
| 333 | + cache.addOrUpdateHost(hostA); | ||
| 334 | + dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostA.id().toString()); | ||
| 335 | + dispatcher.assertEventCount(1); | ||
| 336 | + assertHostLinkCounts(1, 1); | ||
| 337 | + assertLocation(hostA.id(), DEVID_1, 101); | ||
| 338 | + | ||
| 339 | + // add a second host | ||
| 340 | + cache.addOrUpdateHost(hostB); | ||
| 341 | + dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostB.id().toString()); | ||
| 342 | + dispatcher.assertEventCount(2); | ||
| 343 | + assertHostLinkCounts(2, 2); | ||
| 344 | + assertLocation(hostB.id(), DEVID_1, 102); | ||
| 345 | + | ||
| 346 | + // update the first host | ||
| 347 | + cache.addOrUpdateHost(hostA); | ||
| 348 | + dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostA.id().toString()); | ||
| 349 | + dispatcher.assertEventCount(3); | ||
| 350 | + assertHostLinkCounts(2, 2); | ||
| 351 | + assertLocation(hostA.id(), DEVID_1, 101); | ||
| 352 | + | ||
| 353 | + print(cache.dumpString()); | ||
| 354 | + | ||
| 355 | + // remove the second host | ||
| 356 | + cache.removeHost(hostB); | ||
| 357 | + dispatcher.assertLast(Type.HOST_REMOVED, hostB.id().toString()); | ||
| 358 | + dispatcher.assertEventCount(4); | ||
| 359 | + assertHostLinkCounts(1, 1); | ||
| 360 | + assertNull("still host B?", cache.accessHost(hostB.id())); | ||
| 361 | + | ||
| 362 | + print(cache.dumpString()); | ||
| 363 | + | ||
| 364 | + // first, verify where host A is currently residing | ||
| 365 | + assertLocation(hostA.id(), DEVID_1, 101); | ||
| 366 | + | ||
| 367 | + // now let's move hostA to a different port | ||
| 368 | + Host movedHost = createHost(DEV_1, 200, "a"); | ||
| 369 | + print(hostA); | ||
| 370 | + print(movedHost); | ||
| 371 | + | ||
| 372 | + cache.moveHost(movedHost, hostA); | ||
| 373 | + dispatcher.assertLast(Type.HOST_MOVED, hostA.id().toString()); | ||
| 374 | + dispatcher.assertEventCount(5); | ||
| 375 | + assertHostLinkCounts(1, 1); | ||
| 376 | + | ||
| 377 | + assertLocation(hostA.id(), DEVID_1, 200); | ||
| 378 | + | ||
| 379 | + print(cache.dumpString()); | ||
| 380 | + | ||
| 381 | + // finally, let's move the host to a different device and port | ||
| 382 | + Host movedAgain = createHost(DEV_8, 800, "a"); | ||
| 383 | + | ||
| 384 | + cache.moveHost(movedAgain, movedHost); | ||
| 385 | + dispatcher.assertLast(Type.HOST_MOVED, hostA.id().toString()); | ||
| 386 | + dispatcher.assertEventCount(6); | ||
| 387 | + assertHostLinkCounts(1, 1); | ||
| 388 | + | ||
| 389 | + assertLocation(hostA.id(), DEVID_8, 800); | ||
| 390 | + | ||
| 391 | + print(cache.dumpString()); | ||
| 392 | + } | ||
| 393 | + | ||
| 394 | + | ||
| 308 | @Test | 395 | @Test |
| 309 | public void load() { | 396 | public void load() { |
| 310 | title("load"); | 397 | title("load"); | ... | ... |
-
Please register or login to post a comment