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