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