blob: 92c6c931346f1d92ccfe3bc92f6e45d224809bb8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.common.event.impl.TestEventDispatcher;
27import org.onosproject.event.Event;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Host;
30import org.onosproject.net.HostId;
31import org.onosproject.net.HostLocation;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.config.NetworkConfigServiceAdapter;
34import org.onosproject.net.host.DefaultHostDescription;
35import org.onosproject.net.host.HostDescription;
36import org.onosproject.net.host.HostEvent;
37import org.onosproject.net.host.HostListener;
38import org.onosproject.net.host.HostProvider;
39import org.onosproject.net.host.HostProviderRegistry;
40import org.onosproject.net.host.HostProviderService;
41import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.trivial.SimpleHostStore;
44
45import java.util.List;
46import java.util.Set;
47
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070048import static org.junit.Assert.assertEquals;
49import static org.junit.Assert.assertFalse;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070050import static org.junit.Assert.assertNotNull;
Jonathan Hartc884f1b2014-09-24 11:53:33 -070051import static org.junit.Assert.assertNull;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070052import static org.junit.Assert.assertTrue;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070053import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
55import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
56import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
57import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070058
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070059/**
60 * Test codifying the host service & host provider service contracts.
61 */
tom202175a2014-09-19 19:00:11 -070062public class HostManagerTest {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070063
tom7e02cda2014-09-18 12:05:46 -070064 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070065
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070066 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 Ishiguro1eab7d52015-02-10 10:38:18 +090070 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 Koshibeac8e7292014-09-16 16:08:31 -070072 private static final HostId HID1 = HostId.hostId(MAC1, VLAN1);
73 private static final HostId HID2 = HostId.hostId(MAC2, VLAN1);
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090074 private static final HostId HID3 = HostId.hostId(MAC3, VLAN1);
75 private static final HostId HID4 = HostId.hostId(MAC4, VLAN1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070076
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070077 private static final IpAddress IP1 = IpAddress.valueOf("10.0.0.1");
78 private static final IpAddress IP2 = IpAddress.valueOf("10.0.0.2");
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +090079 private static final IpAddress IP3 = IpAddress.valueOf("2001::1");
80 private static final IpAddress IP4 = IpAddress.valueOf("2001::2");
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070081
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
tom202175a2014-09-19 19:00:11 -070089 private HostManager mgr;
Ayaka Koshibeac8e7292014-09-16 16:08:31 -070090
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() {
tom202175a2014-09-19 19:00:11 -070098 mgr = new HostManager();
tom5bcc9462014-09-19 10:11:31 -070099 mgr.store = new SimpleHostStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700100 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700101 registry = mgr;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700102 mgr.networkConfigService = new TestNetworkConfigService();
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700103 mgr.activate();
104
105 mgr.addListener(listener);
106
107 provider = new TestHostProvider();
108 providerService = registry.register(provider);
109 assertTrue("provider should be registered",
tom093340b2014-10-10 00:15:36 -0700110 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700111 }
112
113 @After
114 public void tearDown() {
115 registry.unregister(provider);
116 assertFalse("provider should not be registered",
tom093340b2014-10-10 00:15:36 -0700117 registry.getProviders().contains(provider.id()));
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700118
119 mgr.removeListener(listener);
120 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700121 injectEventDispatcher(mgr, null);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700122 }
123
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700124 private void detect(HostId hid, MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700125 HostLocation loc, IpAddress ip) {
tom093340b2014-10-10 00:15:36 -0700126 HostDescription descr = new DefaultHostDescription(mac, vlan, loc, ip);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700127 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
tom093340b2014-10-10 00:15:36 -0700146 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700147 assertEquals("exactly one should be found", 1, mgr.getHostCount());
tom093340b2014-10-10 00:15:36 -0700148 detect(HID2, MAC2, VLAN2, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700149 assertEquals("two hosts should be found", 2, mgr.getHostCount());
150 validateEvents(HOST_ADDED, HOST_ADDED);
151
152 // host motion
tom093340b2014-10-10 00:15:36 -0700153 detect(HID1, MAC1, VLAN1, LOC2, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700154 validateEvents(HOST_MOVED);
155 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
156
157 // host update
tom093340b2014-10-10 00:15:36 -0700158 detect(HID1, MAC1, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700159 validateEvents(HOST_UPDATED);
160 assertEquals("only two hosts should be found", 2, mgr.getHostCount());
161 }
162
163 @Test
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900164 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 Koshibeac8e7292014-09-16 16:08:31 -0700186 public void hostVanished() {
tom093340b2014-10-10 00:15:36 -0700187 detect(HID1, MAC1, VLAN1, LOC1, IP1);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700188 providerService.hostVanished(HID1);
189 validateEvents(HOST_ADDED, HOST_REMOVED);
190
191 assertNull("host should have been removed", mgr.getHost(HID1));
192 }
193
Kunihiro Ishiguro1eab7d52015-02-10 10:38:18 +0900194 @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 Koshibeac8e7292014-09-16 16:08:31 -0700203 private void validateHosts(
tom093340b2014-10-10 00:15:36 -0700204 String msg, Iterable<Host> hosts, HostId... ids) {
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700205 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() {
tom093340b2014-10-10 00:15:36 -0700214 detect(HID1, MAC1, VLAN1, LOC1, IP1);
215 detect(HID2, MAC2, VLAN1, LOC2, IP2);
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700216
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 Ishiguro1eab7d52015-02-10 10:38:18 +0900225 @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 Koshibeac8e7292014-09-16 16:08:31 -0700238 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 Hartc884f1b2014-09-24 11:53:33 -0700266
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700267 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
268 }
Ayaka Koshibeac8e7292014-09-16 16:08:31 -0700269}