blob: bd5d68f17b8582404639a904021a72231b25f120 [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;
Jonathan Hart46080b62015-08-31 11:10:21 +020024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.common.event.impl.TestEventDispatcher;
28import org.onosproject.event.Event;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
32import org.onosproject.net.HostLocation;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.config.NetworkConfigServiceAdapter;
35import org.onosproject.net.host.DefaultHostDescription;
36import org.onosproject.net.host.HostDescription;
37import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostListener;
39import org.onosproject.net.host.HostProvider;
40import org.onosproject.net.host.HostProviderRegistry;
41import org.onosproject.net.host.HostProviderService;
42import org.onosproject.net.provider.AbstractProvider;
43import org.onosproject.net.provider.ProviderId;
44import org.onosproject.store.trivial.SimpleHostStore;
45
46import java.util.List;
47import java.util.Set;
48
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070049import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertFalse;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070051import static org.junit.Assert.assertNotNull;
Jonathan Hartc884f1b2014-09-24 11:53:33 -070052import static org.junit.Assert.assertNull;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070053import static org.junit.Assert.assertTrue;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070054import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
56import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
57import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
58import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070059
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070060/**
61 * Test codifying the host service & host provider service contracts.
62 */
tom202175a2014-09-19 19:00:11 -070063public class HostManagerTest {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070064
tom7e02cda2014-09-18 12:05:46 -070065 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070066
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 private static final VlanId VLAN1 = VlanId.vlanId((short) 1);
68 private static final VlanId VLAN2 = VlanId.vlanId((short) 2);
69 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:11:00:00:01");
70 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02");
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090071 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:33:00:00:03");
72 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:44:00:00:04");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070073 private static final HostId HID1 = HostId.hostId(MAC1, VLAN1);
74 private static final HostId HID2 = HostId.hostId(MAC2, VLAN1);
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090075 private static final HostId HID3 = HostId.hostId(MAC3, VLAN1);
76 private static final HostId HID4 = HostId.hostId(MAC4, VLAN1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070077
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070078 private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1");
79 private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2");
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090080 private static final IpAddress IP3 = IpAddress.valueOf("2001::1");
81 private static final IpAddress IP4 = IpAddress.valueOf("2001::2");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070082
83 private static final DeviceId DID1 = DeviceId.deviceId("of:001");
84 private static final DeviceId DID2 = DeviceId.deviceId("of:002");
85 private static final PortNumber P1 = PortNumber.portNumber(100);
86 private static final PortNumber P2 = PortNumber.portNumber(200);
87 private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L);
88 private static final HostLocation LOC2 = new HostLocation(DID1, P2, 123L);
89
tom202175a2014-09-19 19:00:11 -070090 private HostManager mgr;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070091
92 protected TestListener listener = new TestListener();
93 protected HostProviderRegistry registry;
94 protected TestHostProvider provider;
95 protected HostProviderService providerService;
96
97 @Before
98 public void setUp() {
tom202175a2014-09-19 19:00:11 -070099 mgr = new HostManager();
tom5bcc9462014-09-19 10:11:31 -0700100 mgr.store = new SimpleHostStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700101 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700102 registry = mgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700103 mgr.networkConfigService = new TestNetworkConfigService();
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700104 mgr.activate();
105
106 mgr.addListener(listener);
107
108 provider = new TestHostProvider();
109 providerService = registry.register(provider);
110 assertTrue("provider should be registered",
tom093340b2014-10-10 00:15:36 -0700111 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700112 }
113
114 @After
115 public void tearDown() {
116 registry.unregister(provider);
117 assertFalse("provider should not be registered",
tom093340b2014-10-10 00:15:36 -0700118 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700119
120 mgr.removeListener(listener);
121 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700122 injectEventDispatcher(mgr, null);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700123 }
124
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700125 private void detect(HostId hid, MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700126 HostLocation loc, IpAddress ip) {
tom093340b2014-10-10 00:15:36 -0700127 HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
Ray Milkeydc083442016-02-22 11:27:57 -0800128 providerService.hostDetected(hid, descr, false);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700129 assertNotNull("host should be found", mgr.getHost(hid));
130 }
131
132 private void validateEvents(Enum... types) {
Charles Chan009c3082015-11-10 14:18:04 -0800133 TestTools.assertAfter(100, () -> {
134 int i = 0;
135 assertEquals("wrong events received", types.length, listener.events.size());
136 for (Event event : listener.events) {
137 assertEquals("incorrect event type", types[i], event.type());
138 i++;
139 }
140 listener.events.clear();
141 });
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700142 }
143
144 @Test
145 public void hostDetected() {
146 assertNull("host shouldn't be found", mgr.getHost(HID1));
147
148 // host addition
tom093340b2014-10-10 00:15:36 -0700149 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700150 assertEquals("exactly one should be found", 1, mgr.getHostCount());
tom093340b2014-10-10 00:15:36 -0700151 detect(HID2, MAC2, VLAN2, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700152 assertEquals("two hosts should be found", 2, mgr.getHostCount());
153 validateEvents(HOST_ADDED, HOST_ADDED);
154
155 // host motion
tom093340b2014-10-10 00:15:36 -0700156 detect(HID1, MAC1, VLAN1, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700157 validateEvents(HOST_MOVED);
158 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
159
160 // host update
tom093340b2014-10-10 00:15:36 -0700161 detect(HID1, MAC1, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700162 validateEvents(HOST_UPDATED);
163 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
164 }
165
166 @Test
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900167 public void hostDetectedIPv6() {
168 assertNull("host shouldn't be found", mgr.getHost(HID3));
169
170 // host addition
171 detect(HID3, MAC3, VLAN1, LOC1, IP3);
172 assertEquals("exactly one should be found", 1, mgr.getHostCount());
173 detect(HID4, MAC4, VLAN2, LOC2, IP3);
174 assertEquals("two hosts should be found", 2, mgr.getHostCount());
175 validateEvents(HOST_ADDED, HOST_ADDED);
176
177 // host motion
178 detect(HID3, MAC3, VLAN1, LOC2, IP3);
179 validateEvents(HOST_MOVED);
180 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
181
182 // host update
183 detect(HID3, MAC3, VLAN1, LOC2, IP4);
184 validateEvents(HOST_UPDATED);
185 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
186 }
187
188 @Test
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700189 public void hostVanished() {
tom093340b2014-10-10 00:15:36 -0700190 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700191 providerService.hostVanished(HID1);
192 validateEvents(HOST_ADDED, HOST_REMOVED);
193
194 assertNull("host should have been removed", mgr.getHost(HID1));
195 }
196
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900197 @Test
198 public void hostVanishedIPv6() {
199 detect(HID3, MAC3, VLAN1, LOC1, IP3);
200 providerService.hostVanished(HID3);
201 validateEvents(HOST_ADDED, HOST_REMOVED);
202
203 assertNull("host should have been removed", mgr.getHost(HID3));
204 }
205
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700206 private void validateHosts(
tom093340b2014-10-10 00:15:36 -0700207 String msg, Iterable<Host> hosts, HostId... ids) {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700208 Set<HostId> hids = Sets.newHashSet(ids);
209 for (Host h : hosts) {
210 assertTrue(msg, hids.remove(h.id()));
211 }
212 assertTrue("expected hosts not fetched from store", hids.isEmpty());
213 }
214
215 @Test
216 public void getHosts() {
tom093340b2014-10-10 00:15:36 -0700217 detect(HID1, MAC1, VLAN1, LOC1, IP1);
218 detect(HID2, MAC2, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700219
220 validateHosts("host not properly stored", mgr.getHosts(), HID1, HID2);
221 validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID1, HID2);
222 validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC1), HID1);
223 validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP1), HID1);
224 validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID1);
225 assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
226 }
227
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900228 @Test
229 public void getHostsIPv6() {
230 detect(HID3, MAC3, VLAN1, LOC1, IP3);
231 detect(HID4, MAC4, VLAN1, LOC2, IP4);
232
233 validateHosts("host not properly stored", mgr.getHosts(), HID3, HID4);
234 validateHosts("can't get hosts by VLAN", mgr.getHostsByVlan(VLAN1), HID3, HID4);
235 validateHosts("can't get hosts by MAC", mgr.getHostsByMac(MAC3), HID3);
236 validateHosts("can't get hosts by IP", mgr.getHostsByIp(IP3), HID3);
237 validateHosts("can't get hosts by location", mgr.getConnectedHosts(LOC1), HID3);
238 assertTrue("incorrect host location", mgr.getConnectedHosts(DID2).isEmpty());
239 }
240
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700241 private static class TestHostProvider extends AbstractProvider
242 implements HostProvider {
243
244 protected TestHostProvider() {
245 super(PID);
246 }
247
248 @Override
249 public ProviderId id() {
250 return PID;
251 }
252
253 @Override
254 public void triggerProbe(Host host) {
255 }
256
257 }
258
259 private static class TestListener implements HostListener {
260
261 protected List<HostEvent> events = Lists.newArrayList();
262
263 @Override
264 public void event(HostEvent event) {
265 events.add(event);
266 }
267
268 }
Jonathan Hartc884f1b2014-09-24 11:53:33 -0700269
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700270 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
271 }
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700272}