blob: dd97d21c2de6f43ad9e1971a18f57628651fdaa6 [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
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530101 private static final ComponentContextAdapter REMOVE_DUPS_MONITOR =
soumya3e6f05e2016-08-05 15:11:11 -0700102 new ComponentContextAdapter() {
103 @Override
104 public Dictionary getProperties() {
105 Hashtable<String, String> props = new Hashtable<>();
106 props.put("allowDuplicateIps", "true");
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530107 props.put("monitorHosts", "true");
108 props.put("probeRate", "40000");
soumya3e6f05e2016-08-05 15:11:11 -0700109 return props;
110 }
111 };
112
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700113 @Before
114 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700115 mgr = new HostManager();
tom5bcc9462014-09-19 10:11:31 -0700116 mgr.store = new SimpleHostStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700117 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700118 registry = mgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700119 mgr.networkConfigService = new TestNetworkConfigService();
soumya3e6f05e2016-08-05 15:11:11 -0700120 mgr.cfgService = new ComponentConfigAdapter();
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530121
122 mgr.activate(REMOVE_DUPS_MONITOR);
123
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700124 mgr.addListener(listener);
125
126 provider = new TestHostProvider();
127 providerService = registry.register(provider);
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530128 assertTrue("provider should be registered", registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700129 }
130
131 @After
132 public void tearDown() {
133 registry.unregister(provider);
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530134 assertFalse("provider should not be registered", registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700135
136 mgr.removeListener(listener);
137 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700138 injectEventDispatcher(mgr, null);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700139 }
140
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530141 private void detect(HostId hid, MacAddress mac, VlanId vlan, HostLocation loc, IpAddress ip) {
tom093340b2014-10-10 00:15:36 -0700142 HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
Ray Milkeydc083442016-02-22 11:27:57 -0800143 providerService.hostDetected(hid, descr, false);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700144 assertNotNull("host should be found", mgr.getHost(hid));
145 }
146
147 private void validateEvents(Enum... types) {
Charles Chan009c3082015-11-10 14:18:04 -0800148 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 Koshibeac8e7292014-09-16 16:08:31 -0700157 }
158
159 @Test
160 public void hostDetected() {
161 assertNull("host shouldn't be found", mgr.getHost(HID1));
162
163 // host addition
tom093340b2014-10-10 00:15:36 -0700164 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700165 assertEquals("exactly one should be found", 1, mgr.getHostCount());
tom093340b2014-10-10 00:15:36 -0700166 detect(HID2, MAC2, VLAN2, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700167 assertEquals("two hosts should be found", 2, mgr.getHostCount());
168 validateEvents(HOST_ADDED, HOST_ADDED);
169
170 // host motion
tom093340b2014-10-10 00:15:36 -0700171 detect(HID1, MAC1, VLAN1, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700172 validateEvents(HOST_MOVED);
173 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
174
175 // host update
tom093340b2014-10-10 00:15:36 -0700176 detect(HID1, MAC1, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700177 validateEvents(HOST_UPDATED);
178 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
179 }
180
181 @Test
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900182 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 Koshibeac8e7292014-09-16 16:08:31 -0700204 public void hostVanished() {
tom093340b2014-10-10 00:15:36 -0700205 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700206 providerService.hostVanished(HID1);
207 validateEvents(HOST_ADDED, HOST_REMOVED);
208
209 assertNull("host should have been removed", mgr.getHost(HID1));
210 }
211
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900212 @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 Vaddireddy50ad0802016-08-08 19:27:36 +0530221 private void validateHosts(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
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530255 private static class TestHostProvider extends AbstractProvider implements HostProvider {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700256
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 Hartc884f1b2014-09-24 11:53:33 -0700282
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700283 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
284 }
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700285}
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530286