Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.host.impl; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNull; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | import static org.junit.Assert.assertTrue; |
| 8 | |
| 9 | import java.util.List; |
| 10 | import java.util.Set; |
| 11 | |
| 12 | import org.junit.After; |
| 13 | import org.junit.Before; |
| 14 | import org.junit.Test; |
| 15 | import org.onlab.onos.event.Event; |
| 16 | import org.onlab.onos.event.impl.TestEventDispatcher; |
| 17 | import org.onlab.onos.net.DeviceId; |
| 18 | import org.onlab.onos.net.Host; |
| 19 | import org.onlab.onos.net.HostId; |
| 20 | import org.onlab.onos.net.HostLocation; |
| 21 | import org.onlab.onos.net.PortNumber; |
| 22 | import org.onlab.onos.net.host.DefaultHostDescription; |
| 23 | import org.onlab.onos.net.host.HostDescription; |
| 24 | import org.onlab.onos.net.host.HostEvent; |
| 25 | import org.onlab.onos.net.host.HostListener; |
| 26 | import org.onlab.onos.net.host.HostProvider; |
| 27 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 28 | import org.onlab.onos.net.host.HostProviderService; |
| 29 | import org.onlab.onos.net.provider.AbstractProvider; |
| 30 | import org.onlab.onos.net.provider.ProviderId; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 34 | |
| 35 | import com.google.common.collect.Lists; |
| 36 | import com.google.common.collect.Sets; |
| 37 | |
| 38 | import static org.onlab.onos.net.host.HostEvent.Type.*; |
| 39 | |
| 40 | /** |
| 41 | * Test codifying the host service & host provider service contracts. |
| 42 | */ |
| 43 | public class SimpleHostManagerTest { |
| 44 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame^] | 45 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 46 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 47 | private static final VlanId VLAN1 = VlanId.vlanId((short) 1); |
| 48 | private static final VlanId VLAN2 = VlanId.vlanId((short) 2); |
| 49 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:11:00:00:01"); |
| 50 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02"); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 51 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN1); |
| 52 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN1); |
| 53 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 54 | private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1"); |
| 55 | private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2"); |
| 56 | private static final Set<IpAddress> IPSET1 = Sets.newHashSet(IP1); |
| 57 | private static final Set<IpAddress> IPSET2 = Sets.newHashSet(IP2); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 58 | |
| 59 | private static final DeviceId DID1 = DeviceId.deviceId("of:001"); |
| 60 | private static final DeviceId DID2 = DeviceId.deviceId("of:002"); |
| 61 | private static final PortNumber P1 = PortNumber.portNumber(100); |
| 62 | private static final PortNumber P2 = PortNumber.portNumber(200); |
| 63 | private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L); |
| 64 | private static final HostLocation LOC2 = new HostLocation(DID1, P2, 123L); |
| 65 | |
| 66 | private SimpleHostManager mgr; |
| 67 | |
| 68 | protected TestListener listener = new TestListener(); |
| 69 | protected HostProviderRegistry registry; |
| 70 | protected TestHostProvider provider; |
| 71 | protected HostProviderService providerService; |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() { |
| 75 | mgr = new SimpleHostManager(); |
| 76 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 77 | registry = mgr; |
| 78 | mgr.activate(); |
| 79 | |
| 80 | mgr.addListener(listener); |
| 81 | |
| 82 | provider = new TestHostProvider(); |
| 83 | providerService = registry.register(provider); |
| 84 | assertTrue("provider should be registered", |
| 85 | registry.getProviders().contains(provider.id())); |
| 86 | } |
| 87 | |
| 88 | @After |
| 89 | public void tearDown() { |
| 90 | registry.unregister(provider); |
| 91 | assertFalse("provider should not be registered", |
| 92 | registry.getProviders().contains(provider.id())); |
| 93 | |
| 94 | mgr.removeListener(listener); |
| 95 | mgr.deactivate(); |
| 96 | mgr.eventDispatcher = null; |
| 97 | } |
| 98 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 99 | private void detect(HostId hid, MacAddress mac, VlanId vlan, |
| 100 | HostLocation loc, Set<IpAddress> ips) { |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 101 | HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ips); |
| 102 | providerService.hostDetected(hid, descr); |
| 103 | assertNotNull("host should be found", mgr.getHost(hid)); |
| 104 | } |
| 105 | |
| 106 | private void validateEvents(Enum... types) { |
| 107 | int i = 0; |
| 108 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 109 | for (Event event : listener.events) { |
| 110 | assertEquals("incorrect event type", types[i], event.type()); |
| 111 | i++; |
| 112 | } |
| 113 | listener.events.clear(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void hostDetected() { |
| 118 | assertNull("host shouldn't be found", mgr.getHost(HID1)); |
| 119 | |
| 120 | // host addition |
| 121 | detect(HID1, MAC1, VLAN1, LOC1, IPSET1); |
| 122 | assertEquals("exactly one should be found", 1, mgr.getHostCount()); |
| 123 | detect(HID2, MAC2, VLAN2, LOC2, IPSET1); |
| 124 | assertEquals("two hosts should be found", 2, mgr.getHostCount()); |
| 125 | validateEvents(HOST_ADDED, HOST_ADDED); |
| 126 | |
| 127 | // host motion |
| 128 | detect(HID1, MAC1, VLAN1, LOC2, IPSET1); |
| 129 | validateEvents(HOST_MOVED); |
| 130 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 131 | |
| 132 | // host update |
| 133 | detect(HID1, MAC1, VLAN1, LOC2, IPSET2); |
| 134 | validateEvents(HOST_UPDATED); |
| 135 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void hostVanished() { |
| 140 | detect(HID1, MAC1, VLAN1, LOC1, IPSET1); |
| 141 | providerService.hostVanished(HID1); |
| 142 | validateEvents(HOST_ADDED, HOST_REMOVED); |
| 143 | |
| 144 | assertNull("host should have been removed", mgr.getHost(HID1)); |
| 145 | } |
| 146 | |
| 147 | private void validateHosts( |
| 148 | String msg, Iterable<Host> hosts, HostId ... ids) { |
| 149 | Set<HostId> hids = Sets.newHashSet(ids); |
| 150 | for (Host h : hosts) { |
| 151 | assertTrue(msg, hids.remove(h.id())); |
| 152 | } |
| 153 | assertTrue("expected hosts not fetched from store", hids.isEmpty()); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void getHosts() { |
| 158 | detect(HID1, MAC1, VLAN1, LOC1, IPSET1); |
| 159 | detect(HID2, MAC2, VLAN1, LOC2, IPSET2); |
| 160 | |
| 161 | validateHosts("host not properly stored", mgr.getHosts(), HID1, HID2); |
| 162 | validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID1, HID2); |
| 163 | validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC1), HID1); |
| 164 | validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP1), HID1); |
| 165 | validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID1); |
| 166 | assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty()); |
| 167 | } |
| 168 | |
| 169 | private static class TestHostProvider extends AbstractProvider |
| 170 | implements HostProvider { |
| 171 | |
| 172 | protected TestHostProvider() { |
| 173 | super(PID); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public ProviderId id() { |
| 178 | return PID; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void triggerProbe(Host host) { |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | private static class TestListener implements HostListener { |
| 188 | |
| 189 | protected List<HostEvent> events = Lists.newArrayList(); |
| 190 | |
| 191 | @Override |
| 192 | public void event(HostEvent event) { |
| 193 | events.add(event); |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | } |