blob: 6a71f4344f377e5f1c419723aa7d21e80c851c71 [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
sdn94b00152016-08-30 02:12:32 -070094 public static final ComponentContextAdapter REMOVE_DUPS_MONITOR = new ComponentContextAdapter() {
95 @Override
96 public Dictionary getProperties() {
97 Hashtable<String, String> props = new Hashtable<String, String>();
98 props.put("allowDuplicateIps", "true");
99 props.put("monitorHosts", "true");
100 props.put("probeRate", "40000");
101 return props;
102 }
103 };
104
tom202175a2014-09-19 19:00:11 -0700105 private HostManager mgr;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700106
107 protected TestListener listener = new TestListener();
108 protected HostProviderRegistry registry;
109 protected TestHostProvider provider;
110 protected HostProviderService providerService;
111
112 @Before
113 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700114 mgr = new HostManager();
tom5bcc9462014-09-19 10:11:31 -0700115 mgr.store = new SimpleHostStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700116 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700117 registry = mgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700118 mgr.networkConfigService = new TestNetworkConfigService();
soumya3e6f05e2016-08-05 15:11:11 -0700119 mgr.cfgService = new ComponentConfigAdapter();
sdn94b00152016-08-30 02:12:32 -0700120
121 mgr.activate(REMOVE_DUPS_MONITOR);
122
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700123 mgr.addListener(listener);
124
125 provider = new TestHostProvider();
126 providerService = registry.register(provider);
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000127 assertTrue("provider should be registered",
128 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700129 }
130
131 @After
132 public void tearDown() {
133 registry.unregister(provider);
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000134 assertFalse("provider should not be registered",
135 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700136
137 mgr.removeListener(listener);
138 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700139 injectEventDispatcher(mgr, null);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700140 }
141
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000142 private void detect(HostId hid, MacAddress mac, VlanId vlan,
143 HostLocation loc, IpAddress ip) {
tom093340b2014-10-10 00:15:36 -0700144 HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
Ray Milkeydc083442016-02-22 11:27:57 -0800145 providerService.hostDetected(hid, descr, false);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700146 assertNotNull("host should be found", mgr.getHost(hid));
147 }
148
149 private void validateEvents(Enum... types) {
Charles Chan009c3082015-11-10 14:18:04 -0800150 TestTools.assertAfter(100, () -> {
151 int i = 0;
152 assertEquals("wrong events received", types.length, listener.events.size());
153 for (Event event : listener.events) {
154 assertEquals("incorrect event type", types[i], event.type());
155 i++;
156 }
157 listener.events.clear();
158 });
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700159 }
160
161 @Test
162 public void hostDetected() {
163 assertNull("host shouldn't be found", mgr.getHost(HID1));
164
165 // host addition
tom093340b2014-10-10 00:15:36 -0700166 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700167 assertEquals("exactly one should be found", 1, mgr.getHostCount());
tom093340b2014-10-10 00:15:36 -0700168 detect(HID2, MAC2, VLAN2, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700169 assertEquals("two hosts should be found", 2, mgr.getHostCount());
170 validateEvents(HOST_ADDED, HOST_ADDED);
171
172 // host motion
tom093340b2014-10-10 00:15:36 -0700173 detect(HID1, MAC1, VLAN1, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700174 validateEvents(HOST_MOVED);
175 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
176
177 // host update
tom093340b2014-10-10 00:15:36 -0700178 detect(HID1, MAC1, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700179 validateEvents(HOST_UPDATED);
180 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
181 }
182
183 @Test
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900184 public void hostDetectedIPv6() {
185 assertNull("host shouldn't be found", mgr.getHost(HID3));
186
187 // host addition
188 detect(HID3, MAC3, VLAN1, LOC1, IP3);
189 assertEquals("exactly one should be found", 1, mgr.getHostCount());
190 detect(HID4, MAC4, VLAN2, LOC2, IP3);
191 assertEquals("two hosts should be found", 2, mgr.getHostCount());
192 validateEvents(HOST_ADDED, HOST_ADDED);
193
194 // host motion
195 detect(HID3, MAC3, VLAN1, LOC2, IP3);
196 validateEvents(HOST_MOVED);
197 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
198
199 // host update
200 detect(HID3, MAC3, VLAN1, LOC2, IP4);
201 validateEvents(HOST_UPDATED);
202 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
203 }
204
205 @Test
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700206 public void hostVanished() {
tom093340b2014-10-10 00:15:36 -0700207 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700208 providerService.hostVanished(HID1);
209 validateEvents(HOST_ADDED, HOST_REMOVED);
210
211 assertNull("host should have been removed", mgr.getHost(HID1));
212 }
213
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900214 @Test
215 public void hostVanishedIPv6() {
216 detect(HID3, MAC3, VLAN1, LOC1, IP3);
217 providerService.hostVanished(HID3);
218 validateEvents(HOST_ADDED, HOST_REMOVED);
219
220 assertNull("host should have been removed", mgr.getHost(HID3));
221 }
222
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000223 private void validateHosts(
224 String msg, Iterable<Host> hosts, HostId... ids) {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700225 Set<HostId> hids = Sets.newHashSet(ids);
226 for (Host h : hosts) {
227 assertTrue(msg, hids.remove(h.id()));
228 }
229 assertTrue("expected hosts not fetched from store", hids.isEmpty());
230 }
231
232 @Test
233 public void getHosts() {
tom093340b2014-10-10 00:15:36 -0700234 detect(HID1, MAC1, VLAN1, LOC1, IP1);
235 detect(HID2, MAC2, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700236
237 validateHosts("host not properly stored", mgr.getHosts(), HID1, HID2);
238 validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID1, HID2);
239 validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC1), HID1);
240 validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP1), HID1);
241 validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID1);
242 assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
243 }
244
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900245 @Test
246 public void getHostsIPv6() {
247 detect(HID3, MAC3, VLAN1, LOC1, IP3);
248 detect(HID4, MAC4, VLAN1, LOC2, IP4);
249
250 validateHosts("host not properly stored", mgr.getHosts(), HID3, HID4);
251 validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID3, HID4);
252 validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC3), HID3);
253 validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP3), HID3);
254 validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID3);
255 assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
256 }
257
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000258 private static class TestHostProvider extends AbstractProvider
259 implements HostProvider {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700260
261 protected TestHostProvider() {
262 super(PID);
263 }
264
265 @Override
266 public ProviderId id() {
267 return PID;
268 }
269
270 @Override
271 public void triggerProbe(Host host) {
272 }
273
274 }
275
276 private static class TestListener implements HostListener {
277
278 protected List<HostEvent> events = Lists.newArrayList();
279
280 @Override
281 public void event(HostEvent event) {
282 events.add(event);
283 }
284
285 }
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700286
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700287 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
288 }
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700289}