blob: 1eb014fab0f9e2a64928c35570e097f8f72b4d23 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host.impl;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070017
Jonathan Hart46080b62015-08-31 11:10:21 +020018import com.google.common.collect.Lists;
19import com.google.common.collect.Sets;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Charles Chan009c3082015-11-10 14:18:04 -080023import org.onlab.junit.TestTools;
soumya3e6f05e2016-08-05 15:11:11 -070024import org.onlab.osgi.ComponentContextAdapter;
Jonathan Hart46080b62015-08-31 11:10:21 +020025import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
soumya3e6f05e2016-08-05 15:11:11 -070028import org.onosproject.cfg.ComponentConfigAdapter;
Jonathan Hart46080b62015-08-31 11:10:21 +020029import org.onosproject.common.event.impl.TestEventDispatcher;
30import org.onosproject.event.Event;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Host;
33import org.onosproject.net.HostId;
34import org.onosproject.net.HostLocation;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.config.NetworkConfigServiceAdapter;
37import org.onosproject.net.host.DefaultHostDescription;
38import org.onosproject.net.host.HostDescription;
39import org.onosproject.net.host.HostEvent;
40import org.onosproject.net.host.HostListener;
41import org.onosproject.net.host.HostProvider;
42import org.onosproject.net.host.HostProviderRegistry;
43import org.onosproject.net.host.HostProviderService;
44import org.onosproject.net.provider.AbstractProvider;
45import org.onosproject.net.provider.ProviderId;
46import org.onosproject.store.trivial.SimpleHostStore;
47
soumya3e6f05e2016-08-05 15:11:11 -070048import java.util.Dictionary;
49import java.util.Hashtable;
Jonathan Hart46080b62015-08-31 11:10:21 +020050import java.util.List;
51import java.util.Set;
52
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070053import static org.junit.Assert.assertEquals;
54import static org.junit.Assert.assertFalse;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070055import static org.junit.Assert.assertNotNull;
Jonathan Hartc884f1b2014-09-24 11:53:33 -070056import static org.junit.Assert.assertNull;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070057import static org.junit.Assert.assertTrue;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070058import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
60import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
61import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
62import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070063
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070064/**
65 * Test codifying the host service & host provider service contracts.
66 */
tom202175a2014-09-19 19:00:11 -070067public class HostManagerTest {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070068
tom7e02cda2014-09-18 12:05:46 -070069 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070070
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070071 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 Ishiguro1eab7d52015-02-10 10:38:18 +090075 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 Koshibeac8e7292014-09-16 16:08:31 -070077 private static final HostId HID1 = HostId.hostId(MAC1, VLAN1);
78 private static final HostId HID2 = HostId.hostId(MAC2, VLAN1);
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090079 private static final HostId HID3 = HostId.hostId(MAC3, VLAN1);
80 private static final HostId HID4 = HostId.hostId(MAC4, VLAN1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070081
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070082 private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1");
83 private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2");
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090084 private static final IpAddress IP3 = IpAddress.valueOf("2001::1");
85 private static final IpAddress IP4 = IpAddress.valueOf("2001::2");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070086
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
tom202175a2014-09-19 19:00:11 -070094 private HostManager mgr;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070095
96 protected TestListener listener = new TestListener();
97 protected HostProviderRegistry registry;
98 protected TestHostProvider provider;
99 protected HostProviderService providerService;
100
soumya3e6f05e2016-08-05 15:11:11 -0700101 private static final ComponentContextAdapter REMOVE_DUPS =
102 new ComponentContextAdapter() {
103 @Override
104 public Dictionary getProperties() {
105 Hashtable<String, String> props = new Hashtable<>();
106 props.put("allowDuplicateIps", "true");
107 return props;
108 }
109 };
110
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700111 @Before
112 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700113 mgr = new HostManager();
tom5bcc9462014-09-19 10:11:31 -0700114 mgr.store = new SimpleHostStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700115 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700116 registry = mgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700117 mgr.networkConfigService = new TestNetworkConfigService();
soumya3e6f05e2016-08-05 15:11:11 -0700118 mgr.cfgService = new ComponentConfigAdapter();
119 mgr.activate(REMOVE_DUPS);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700120 mgr.addListener(listener);
121
122 provider = new TestHostProvider();
123 providerService = registry.register(provider);
124 assertTrue("provider should be registered",
tom093340b2014-10-10 00:15:36 -0700125 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700126 }
127
128 @After
129 public void tearDown() {
130 registry.unregister(provider);
131 assertFalse("provider should not be registered",
tom093340b2014-10-10 00:15:36 -0700132 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700133
134 mgr.removeListener(listener);
135 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700136 injectEventDispatcher(mgr, null);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700137 }
138
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700139 private void detect(HostId hid, MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700140 HostLocation loc, IpAddress ip) {
tom093340b2014-10-10 00:15:36 -0700141 HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
Ray Milkeydc083442016-02-22 11:27:57 -0800142 providerService.hostDetected(hid, descr, false);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700143 assertNotNull("host should be found", mgr.getHost(hid));
144 }
145
146 private void validateEvents(Enum... types) {
Charles Chan009c3082015-11-10 14:18:04 -0800147 TestTools.assertAfter(100, () -> {
148 int i = 0;
149 assertEquals("wrong events received", types.length, listener.events.size());
150 for (Event event : listener.events) {
151 assertEquals("incorrect event type", types[i], event.type());
152 i++;
153 }
154 listener.events.clear();
155 });
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700156 }
157
158 @Test
159 public void hostDetected() {
160 assertNull("host shouldn't be found", mgr.getHost(HID1));
161
162 // host addition
tom093340b2014-10-10 00:15:36 -0700163 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700164 assertEquals("exactly one should be found", 1, mgr.getHostCount());
tom093340b2014-10-10 00:15:36 -0700165 detect(HID2, MAC2, VLAN2, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700166 assertEquals("two hosts should be found", 2, mgr.getHostCount());
167 validateEvents(HOST_ADDED, HOST_ADDED);
168
169 // host motion
tom093340b2014-10-10 00:15:36 -0700170 detect(HID1, MAC1, VLAN1, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700171 validateEvents(HOST_MOVED);
172 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
173
174 // host update
tom093340b2014-10-10 00:15:36 -0700175 detect(HID1, MAC1, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700176 validateEvents(HOST_UPDATED);
177 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
178 }
179
180 @Test
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900181 public void hostDetectedIPv6() {
182 assertNull("host shouldn't be found", mgr.getHost(HID3));
183
184 // host addition
185 detect(HID3, MAC3, VLAN1, LOC1, IP3);
186 assertEquals("exactly one should be found", 1, mgr.getHostCount());
187 detect(HID4, MAC4, VLAN2, LOC2, IP3);
188 assertEquals("two hosts should be found", 2, mgr.getHostCount());
189 validateEvents(HOST_ADDED, HOST_ADDED);
190
191 // host motion
192 detect(HID3, MAC3, VLAN1, LOC2, IP3);
193 validateEvents(HOST_MOVED);
194 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
195
196 // host update
197 detect(HID3, MAC3, VLAN1, LOC2, IP4);
198 validateEvents(HOST_UPDATED);
199 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
200 }
201
202 @Test
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700203 public void hostVanished() {
tom093340b2014-10-10 00:15:36 -0700204 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700205 providerService.hostVanished(HID1);
206 validateEvents(HOST_ADDED, HOST_REMOVED);
207
208 assertNull("host should have been removed", mgr.getHost(HID1));
209 }
210
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900211 @Test
212 public void hostVanishedIPv6() {
213 detect(HID3, MAC3, VLAN1, LOC1, IP3);
214 providerService.hostVanished(HID3);
215 validateEvents(HOST_ADDED, HOST_REMOVED);
216
217 assertNull("host should have been removed", mgr.getHost(HID3));
218 }
219
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700220 private void validateHosts(
tom093340b2014-10-10 00:15:36 -0700221 String msg, Iterable<Host> hosts, HostId... ids) {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700222 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() {
tom093340b2014-10-10 00:15:36 -0700231 detect(HID1, MAC1, VLAN1, LOC1, IP1);
232 detect(HID2, MAC2, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700233
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 Ishiguro1eab7d52015-02-10 10:38:18 +0900242 @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
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700255 private static class TestHostProvider extends AbstractProvider
256 implements HostProvider {
257
258 protected TestHostProvider() {
259 super(PID);
260 }
261
262 @Override
263 public ProviderId id() {
264 return PID;
265 }
266
267 @Override
268 public void triggerProbe(Host host) {
269 }
270
271 }
272
273 private static class TestListener implements HostListener {
274
275 protected List<HostEvent> events = Lists.newArrayList();
276
277 @Override
278 public void event(HostEvent event) {
279 events.add(event);
280 }
281
282 }
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700283
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700284 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
285 }
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700286}