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