Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host.impl; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 46080b6 | 2015-08-31 11:10:21 +0200 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 23 | import org.onlab.junit.TestTools; |
Jonathan Hart | 46080b6 | 2015-08-31 11:10:21 +0200 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 28 | import org.onosproject.event.Event; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.HostLocation; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 35 | import org.onosproject.net.host.DefaultHostDescription; |
| 36 | import org.onosproject.net.host.HostDescription; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostListener; |
| 39 | import org.onosproject.net.host.HostProvider; |
| 40 | import org.onosproject.net.host.HostProviderRegistry; |
| 41 | import org.onosproject.net.host.HostProviderService; |
| 42 | import org.onosproject.net.provider.AbstractProvider; |
| 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | import org.onosproject.store.trivial.SimpleHostStore; |
| 45 | |
| 46 | import java.util.List; |
| 47 | import java.util.Set; |
| 48 | |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertFalse; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 51 | import static org.junit.Assert.assertNotNull; |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 52 | import static org.junit.Assert.assertNull; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 53 | import static org.junit.Assert.assertTrue; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 54 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 56 | import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED; |
| 57 | import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
| 58 | import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 59 | |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Test codifying the host service & host provider service contracts. |
| 62 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 63 | public class HostManagerTest { |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 64 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 65 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 66 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 67 | private static final VlanId VLAN1 = VlanId.vlanId((short) 1); |
| 68 | private static final VlanId VLAN2 = VlanId.vlanId((short) 2); |
| 69 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:11:00:00:01"); |
| 70 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02"); |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 71 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:33:00:00:03"); |
| 72 | private static final MacAddress MAC4 = MacAddress.valueOf("00:00:44:00:00:04"); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 73 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN1); |
| 74 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN1); |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 75 | private static final HostId HID3 = HostId.hostId(MAC3, VLAN1); |
| 76 | private static final HostId HID4 = HostId.hostId(MAC4, VLAN1); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 77 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 78 | private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1"); |
| 79 | private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2"); |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 80 | private static final IpAddress IP3 = IpAddress.valueOf("2001::1"); |
| 81 | private static final IpAddress IP4 = IpAddress.valueOf("2001::2"); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 82 | |
| 83 | private static final DeviceId DID1 = DeviceId.deviceId("of:001"); |
| 84 | private static final DeviceId DID2 = DeviceId.deviceId("of:002"); |
| 85 | private static final PortNumber P1 = PortNumber.portNumber(100); |
| 86 | private static final PortNumber P2 = PortNumber.portNumber(200); |
| 87 | private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L); |
| 88 | private static final HostLocation LOC2 = new HostLocation(DID1, P2, 123L); |
| 89 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 90 | private HostManager mgr; |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 91 | |
| 92 | protected TestListener listener = new TestListener(); |
| 93 | protected HostProviderRegistry registry; |
| 94 | protected TestHostProvider provider; |
| 95 | protected HostProviderService providerService; |
| 96 | |
| 97 | @Before |
| 98 | public void setUp() { |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 99 | mgr = new HostManager(); |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 100 | mgr.store = new SimpleHostStore(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 101 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 102 | registry = mgr; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 103 | mgr.networkConfigService = new TestNetworkConfigService(); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 104 | mgr.activate(); |
| 105 | |
| 106 | mgr.addListener(listener); |
| 107 | |
| 108 | provider = new TestHostProvider(); |
| 109 | providerService = registry.register(provider); |
| 110 | assertTrue("provider should be registered", |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 111 | registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @After |
| 115 | public void tearDown() { |
| 116 | registry.unregister(provider); |
| 117 | assertFalse("provider should not be registered", |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 118 | registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 119 | |
| 120 | mgr.removeListener(listener); |
| 121 | mgr.deactivate(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 122 | injectEventDispatcher(mgr, null); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 125 | private void detect(HostId hid, MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 126 | HostLocation loc, IpAddress ip) { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 127 | HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame^] | 128 | providerService.hostDetected(hid, descr, false); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 129 | assertNotNull("host should be found", mgr.getHost(hid)); |
| 130 | } |
| 131 | |
| 132 | private void validateEvents(Enum... types) { |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 133 | TestTools.assertAfter(100, () -> { |
| 134 | int i = 0; |
| 135 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 136 | for (Event event : listener.events) { |
| 137 | assertEquals("incorrect event type", types[i], event.type()); |
| 138 | i++; |
| 139 | } |
| 140 | listener.events.clear(); |
| 141 | }); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void hostDetected() { |
| 146 | assertNull("host shouldn't be found", mgr.getHost(HID1)); |
| 147 | |
| 148 | // host addition |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 149 | detect(HID1, MAC1, VLAN1, LOC1, IP1); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 150 | assertEquals("exactly one should be found", 1, mgr.getHostCount()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 151 | detect(HID2, MAC2, VLAN2, LOC2, IP1); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 152 | assertEquals("two hosts should be found", 2, mgr.getHostCount()); |
| 153 | validateEvents(HOST_ADDED, HOST_ADDED); |
| 154 | |
| 155 | // host motion |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 156 | detect(HID1, MAC1, VLAN1, LOC2, IP1); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 157 | validateEvents(HOST_MOVED); |
| 158 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 159 | |
| 160 | // host update |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 161 | detect(HID1, MAC1, VLAN1, LOC2, IP2); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 162 | validateEvents(HOST_UPDATED); |
| 163 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 164 | } |
| 165 | |
| 166 | @Test |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 167 | public void hostDetectedIPv6() { |
| 168 | assertNull("host shouldn't be found", mgr.getHost(HID3)); |
| 169 | |
| 170 | // host addition |
| 171 | detect(HID3, MAC3, VLAN1, LOC1, IP3); |
| 172 | assertEquals("exactly one should be found", 1, mgr.getHostCount()); |
| 173 | detect(HID4, MAC4, VLAN2, LOC2, IP3); |
| 174 | assertEquals("two hosts should be found", 2, mgr.getHostCount()); |
| 175 | validateEvents(HOST_ADDED, HOST_ADDED); |
| 176 | |
| 177 | // host motion |
| 178 | detect(HID3, MAC3, VLAN1, LOC2, IP3); |
| 179 | validateEvents(HOST_MOVED); |
| 180 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 181 | |
| 182 | // host update |
| 183 | detect(HID3, MAC3, VLAN1, LOC2, IP4); |
| 184 | validateEvents(HOST_UPDATED); |
| 185 | assertEquals("only two hosts should be found", 2, mgr.getHostCount()); |
| 186 | } |
| 187 | |
| 188 | @Test |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 189 | public void hostVanished() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 190 | detect(HID1, MAC1, VLAN1, LOC1, IP1); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 191 | providerService.hostVanished(HID1); |
| 192 | validateEvents(HOST_ADDED, HOST_REMOVED); |
| 193 | |
| 194 | assertNull("host should have been removed", mgr.getHost(HID1)); |
| 195 | } |
| 196 | |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 197 | @Test |
| 198 | public void hostVanishedIPv6() { |
| 199 | detect(HID3, MAC3, VLAN1, LOC1, IP3); |
| 200 | providerService.hostVanished(HID3); |
| 201 | validateEvents(HOST_ADDED, HOST_REMOVED); |
| 202 | |
| 203 | assertNull("host should have been removed", mgr.getHost(HID3)); |
| 204 | } |
| 205 | |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 206 | private void validateHosts( |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 207 | String msg, Iterable<Host> hosts, HostId... ids) { |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 208 | Set<HostId> hids = Sets.newHashSet(ids); |
| 209 | for (Host h : hosts) { |
| 210 | assertTrue(msg, hids.remove(h.id())); |
| 211 | } |
| 212 | assertTrue("expected hosts not fetched from store", hids.isEmpty()); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void getHosts() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 217 | detect(HID1, MAC1, VLAN1, LOC1, IP1); |
| 218 | detect(HID2, MAC2, VLAN1, LOC2, IP2); |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 219 | |
| 220 | validateHosts("host not properly stored", mgr.getHosts(), HID1, HID2); |
| 221 | validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID1, HID2); |
| 222 | validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC1), HID1); |
| 223 | validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP1), HID1); |
| 224 | validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID1); |
| 225 | assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty()); |
| 226 | } |
| 227 | |
Kunihiro Ishiguro | 1eab7d5 | 2015-02-10 10:38:18 +0900 | [diff] [blame] | 228 | @Test |
| 229 | public void getHostsIPv6() { |
| 230 | detect(HID3, MAC3, VLAN1, LOC1, IP3); |
| 231 | detect(HID4, MAC4, VLAN1, LOC2, IP4); |
| 232 | |
| 233 | validateHosts("host not properly stored", mgr.getHosts(), HID3, HID4); |
| 234 | validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID3, HID4); |
| 235 | validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC3), HID3); |
| 236 | validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP3), HID3); |
| 237 | validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID3); |
| 238 | assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty()); |
| 239 | } |
| 240 | |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 241 | private static class TestHostProvider extends AbstractProvider |
| 242 | implements HostProvider { |
| 243 | |
| 244 | protected TestHostProvider() { |
| 245 | super(PID); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public ProviderId id() { |
| 250 | return PID; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void triggerProbe(Host host) { |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | private static class TestListener implements HostListener { |
| 260 | |
| 261 | protected List<HostEvent> events = Lists.newArrayList(); |
| 262 | |
| 263 | @Override |
| 264 | public void event(HostEvent event) { |
| 265 | events.add(event); |
| 266 | } |
| 267 | |
| 268 | } |
Jonathan Hart | c884f1b | 2014-09-24 11:53:33 -0700 | [diff] [blame] | 269 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 270 | private class TestNetworkConfigService extends NetworkConfigServiceAdapter { |
| 271 | } |
Ayaka Koshibe | ac8e729 | 2014-09-16 16:08:31 -0700 | [diff] [blame] | 272 | } |