blob: a8caf2d82baab6b5cd5e7f86cb2dad6e68523f29 [file] [log] [blame]
samanwita pale7c08de2015-09-24 21:59:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
samanwita pale7c08de2015-09-24 21:59:49 -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 */
16package org.onosproject.store.host.impl;
17
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000018import com.google.common.collect.ImmutableSet;
samanwita pale7c08de2015-09-24 21:59:49 -070019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultHost;
26import org.onosproject.net.DeviceId;
samanwita pale7c08de2015-09-24 21:59:49 -070027import org.onosproject.net.Host;
28import org.onosproject.net.HostId;
29import org.onosproject.net.HostLocation;
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000030import org.onosproject.net.PortNumber;
samanwita pale7c08de2015-09-24 21:59:49 -070031import org.onosproject.net.host.DefaultHostDescription;
32import org.onosproject.net.host.HostDescription;
33import org.onosproject.net.provider.ProviderId;
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000034import org.onosproject.store.service.MapEvent;
samanwita pale7c08de2015-09-24 21:59:49 -070035import org.onosproject.store.service.TestStorageService;
36
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053037import com.google.common.collect.Sets;
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000038import org.onosproject.store.service.Versioned;
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053039
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000040import java.util.Collections;
samanwita pale7c08de2015-09-24 21:59:49 -070041import java.util.HashSet;
42import java.util.Set;
43
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070044import static junit.framework.TestCase.assertTrue;
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000045import static org.junit.Assert.*;
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070046
samanwita pale7c08de2015-09-24 21:59:49 -070047/**
48 * Tests for the ECHostStore.
49 */
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070050public class DistributedHostStoreTest {
samanwita pale7c08de2015-09-24 21:59:49 -070051
alshabib8a4a6002015-11-25 14:31:16 -080052 private DistributedHostStore ecXHostStore;
samanwita pale7c08de2015-09-24 21:59:49 -070053
54 private static final HostId HOSTID = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1a"));
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053055 private static final HostId HOSTID1 = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1b"));
samanwita pale7c08de2015-09-24 21:59:49 -070056
57 private static final IpAddress IP1 = IpAddress.valueOf("10.2.0.2");
58 private static final IpAddress IP2 = IpAddress.valueOf("10.2.0.3");
59
60 private static final ProviderId PID = new ProviderId("of", "foo");
Charles Chanb1e99242017-07-07 14:11:09 -070061 private static final ProviderId PID2 = new ProviderId("of", "foo2");
62
63 private static final HostDescription HOST_LEARNT =
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000064 createHostDesc(HOSTID, Sets.newHashSet(IP1), false, Collections.emptySet());
Charles Chanb1e99242017-07-07 14:11:09 -070065 private static final HostDescription HOST_CONFIGURED =
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +000066 createHostDesc(HOSTID, Sets.newHashSet(IP1), true, Collections.emptySet());
67 // Host with locations
68 private static final DeviceId DEV1 = DeviceId.deviceId("of:0000000000000001");
69 private static final PortNumber P1 = PortNumber.portNumber(1);
70 private static final PortNumber P2 = PortNumber.portNumber(2);
71 private static final ConnectPoint CP11 = new ConnectPoint(DEV1, P1);
72 private static final HostLocation HOST_LOC11 = new HostLocation(CP11, 0);
73 private static final ConnectPoint CP12 = new ConnectPoint(DEV1, P2);
74 private static final HostLocation HOST_LOC12 = new HostLocation(CP12, 0);
75 private static final Set<HostLocation> HOST_LOCATIONS = ImmutableSet.of(HOST_LOC11, HOST_LOC12);
76 private static final Set<HostLocation> HOST_LOCATION = ImmutableSet.of(HOST_LOC11);
77 private static final Set<HostLocation> NONE_LOCATION = ImmutableSet.of(HostLocation.NONE);
78 private static final Set<IpAddress> HOST_ADDRESS = ImmutableSet.of(IP1);
79 private static final Set<IpAddress> HOST_ADDRESSES = ImmutableSet.of(IP1, IP2);
80 private static final HostDescription HOST_LEARNT_WITH_LOCATIONS =
81 createHostDesc(HOSTID, HOST_ADDRESS, false, HOST_LOCATIONS);
82 private static final HostDescription HOST_LEARNT_WITH_ADDRESSES =
83 createHostDesc(HOSTID, Sets.newHashSet(IP1, IP2), false, Collections.emptySet());
84 private static final DefaultHost OLD_HOST = new DefaultHost(PID, HOSTID,
85 HOST_LEARNT_WITH_ADDRESSES.hwAddress(),
86 HOST_LEARNT_WITH_ADDRESSES.vlan(),
87 HOST_LEARNT_WITH_ADDRESSES.locations(),
88 HOST_LEARNT_WITH_ADDRESSES.ipAddress(),
89 HOST_LEARNT_WITH_ADDRESSES.configured(),
90 HOST_LEARNT_WITH_ADDRESSES.annotations());
91 private static final DefaultHost NEW_HOST = new DefaultHost(PID, HOSTID,
92 HOST_LEARNT_WITH_ADDRESSES.hwAddress(),
93 HOST_LEARNT_WITH_ADDRESSES.vlan(),
94 HOST_LEARNT_WITH_ADDRESSES.locations(),
95 HOST_ADDRESS,
96 HOST_LEARNT_WITH_ADDRESSES.configured(),
97 HOST_LEARNT_WITH_ADDRESSES.annotations());
98 private static final MapEvent<HostId, DefaultHost> HOST_EVENT =
99 new MapEvent<>("foobar", HOSTID, new Versioned<>(NEW_HOST, 0), new Versioned<>(OLD_HOST, 0));
samanwita pale7c08de2015-09-24 21:59:49 -0700100
101 @Before
102 public void setUp() {
alshabib8a4a6002015-11-25 14:31:16 -0800103 ecXHostStore = new DistributedHostStore();
samanwita pale7c08de2015-09-24 21:59:49 -0700104
105 ecXHostStore.storageService = new TestStorageService();
samanwita pale7c08de2015-09-24 21:59:49 -0700106 ecXHostStore.activate();
107 }
108
109 @After
110 public void tearDown() {
111 ecXHostStore.deactivate();
112 }
113
114 /**
115 * Tests the removeIp method call.
116 */
117 @Test
118 public void testRemoveIp() {
119 Set<IpAddress> ips = new HashSet<>();
120 ips.add(IP1);
121 ips.add(IP2);
122
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530123 HostDescription description = createHostDesc(HOSTID, ips);
samanwita pale7c08de2015-09-24 21:59:49 -0700124 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
125 ecXHostStore.removeIp(HOSTID, IP1);
126 Host host = ecXHostStore.getHost(HOSTID);
127
128 assertFalse(host.ipAddresses().contains(IP1));
129 assertTrue(host.ipAddresses().contains(IP2));
130 }
131
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530132 @Test
133 public void testAddHostByIp() {
134 Set<IpAddress> ips = new HashSet<>();
135 ips.add(IP1);
136 ips.add(IP2);
137
138 HostDescription description = createHostDesc(HOSTID, ips);
139 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
140
141 Set<Host> hosts = ecXHostStore.getHosts(IP1);
142
143 assertFalse(hosts.size() > 1);
144 assertTrue(hosts.size() == 1);
145
146 HostDescription description1 = createHostDesc(HOSTID1, Sets.newHashSet(IP2));
147 ecXHostStore.createOrUpdateHost(PID, HOSTID1, description1, false);
148
149 Set<Host> hosts1 = ecXHostStore.getHosts(IP2);
150
151 assertFalse(hosts1.size() < 1);
152 assertTrue(hosts1.size() == 2);
153 }
154
155 @Test
156 public void testRemoveHostByIp() {
157 Set<IpAddress> ips = new HashSet<>();
158 ips.add(IP1);
159 ips.add(IP2);
160
161 HostDescription description = createHostDesc(HOSTID, ips);
162 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
163 ecXHostStore.removeIp(HOSTID, IP1);
164 Set<Host> hosts = ecXHostStore.getHosts(IP1);
165 assertTrue(hosts.size() == 0);
166 }
167
Charles Chanb1e99242017-07-07 14:11:09 -0700168 @Test
169 public void testHostOverride() {
170 Host hostInStore;
171 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
172 hostInStore = ecXHostStore.getHost(HOSTID);
173 assertFalse(hostInStore.configured());
174 assertEquals(PID, hostInStore.providerId());
175
176 // Expect: configured host should override learnt host
177 ecXHostStore.createOrUpdateHost(PID2, HOSTID, HOST_CONFIGURED, true);
178 hostInStore = ecXHostStore.getHost(HOSTID);
179 assertTrue(hostInStore.configured());
180 assertEquals(PID2, hostInStore.providerId());
181
182 // Expect: learnt host should not override configured host
183 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
184 hostInStore = ecXHostStore.getHost(HOSTID);
185 assertTrue(hostInStore.configured());
186 assertEquals(PID2, hostInStore.providerId());
187 }
188
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +0000189 @Test
190 public void testRemoteUpdateHostsByIp() {
191 // Add host in the store
192 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT_WITH_ADDRESSES, false);
193
194 // Expected a learnt host with an IP
195 Host hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
196 .findFirst().orElse(null);
197 assertNotNull(hostInHostsByIp);
198 assertFalse(hostInHostsByIp.configured());
199 assertEquals(HOSTID, hostInHostsByIp.id());
200 assertEquals(PID, hostInHostsByIp.providerId());
201 assertEquals(NONE_LOCATION, hostInHostsByIp.locations());
202 assertEquals(HOST_ADDRESSES, hostInHostsByIp.ipAddresses());
203
204 // Remove one ip - simulating the update in other instances
205 ecXHostStore.hostLocationTracker.event(HOST_EVENT);
206
207 // Expected null
208 hostInHostsByIp = ecXHostStore.getHosts(IP2).stream()
209 .findFirst().orElse(null);
210 assertNull(hostInHostsByIp);
211
212 // Expected an host with an ip address
213 hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
214 .findFirst().orElse(null);
215 assertNotNull(hostInHostsByIp);
216 assertFalse(hostInHostsByIp.configured());
217 assertEquals(HOSTID, hostInHostsByIp.id());
218 assertEquals(PID, hostInHostsByIp.providerId());
219 assertEquals(NONE_LOCATION, hostInHostsByIp.locations());
220 assertEquals(HOST_ADDRESS, hostInHostsByIp.ipAddresses());
221
222 }
223
224 @Test
225 public void testLocalUpdateHostsByIp() {
226 // Add host in the store
227 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT_WITH_ADDRESSES, false);
228
229 // Expected a learnt host with an IP
230 Host hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
231 .findFirst().orElse(null);
232 assertNotNull(hostInHostsByIp);
233 assertFalse(hostInHostsByIp.configured());
234 assertEquals(HOSTID, hostInHostsByIp.id());
235 assertEquals(PID, hostInHostsByIp.providerId());
236 assertEquals(NONE_LOCATION, hostInHostsByIp.locations());
237 assertEquals(HOST_ADDRESSES, hostInHostsByIp.ipAddresses());
238
239 // Remove one ip
240 ecXHostStore.removeIp(HOSTID, IP2);
241
242 // Expected null
243 hostInHostsByIp = ecXHostStore.getHosts(IP2).stream()
244 .findFirst().orElse(null);
245 assertNull(hostInHostsByIp);
246
247 // Expected an host with an ip address
248 hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
249 .findFirst().orElse(null);
250 assertNotNull(hostInHostsByIp);
251 assertFalse(hostInHostsByIp.configured());
252 assertEquals(HOSTID, hostInHostsByIp.id());
253 assertEquals(PID, hostInHostsByIp.providerId());
254 assertEquals(NONE_LOCATION, hostInHostsByIp.locations());
255 assertEquals(HOST_ADDRESS, hostInHostsByIp.ipAddresses());
256
257 }
258
259 @Test
260 public void testUpdateLocationInHostsByIp() {
261 // Add host in the store
262 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT_WITH_LOCATIONS, false);
263 Host hostInHosts = ecXHostStore.getHost(HOSTID);
264
265 // Expected a learnt host with an IP
266 assertFalse(hostInHosts.configured());
267 assertEquals(HOSTID, hostInHosts.id());
268 assertEquals(PID, hostInHosts.providerId());
269 assertEquals(HOST_LOCATIONS, hostInHosts.locations());
270 assertEquals(HOST_ADDRESS, hostInHosts.ipAddresses());
271 Host hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
272 .findFirst().orElse(null);
273 assertNotNull(hostInHostsByIp);
274 assertFalse(hostInHostsByIp.configured());
275 assertEquals(HOSTID, hostInHostsByIp.id());
276 assertEquals(PID, hostInHostsByIp.providerId());
277 assertEquals(HOST_LOCATIONS, hostInHostsByIp.locations());
278 assertEquals(HOST_ADDRESS, hostInHostsByIp.ipAddresses());
279
280 // Remove one location
281 ecXHostStore.removeLocation(HOSTID, HOST_LOC12);
282
283 // Verify hosts is updated
284 hostInHosts = ecXHostStore.getHost(HOSTID);
285 assertFalse(hostInHosts.configured());
286 assertEquals(HOSTID, hostInHosts.id());
287 assertEquals(PID, hostInHosts.providerId());
288 assertEquals(HOST_LOCATION, hostInHosts.locations());
289 assertEquals(HOST_ADDRESS, hostInHosts.ipAddresses());
290
291 // Verify hostsByIp is updated
292 hostInHostsByIp = ecXHostStore.getHosts(IP1).stream()
293 .findFirst().orElse(null);
294 assertNotNull(hostInHostsByIp);
295 assertFalse(hostInHostsByIp.configured());
296 assertEquals(HOSTID, hostInHostsByIp.id());
297 assertEquals(PID, hostInHostsByIp.providerId());
298 assertEquals(HOST_LOCATION, hostInHostsByIp.locations());
299 assertEquals(HOST_ADDRESS, hostInHostsByIp.ipAddresses());
300 }
301
302
Charles Chanb1e99242017-07-07 14:11:09 -0700303 private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) {
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +0000304 return createHostDesc(hostId, ips, false, Collections.emptySet());
Charles Chanb1e99242017-07-07 14:11:09 -0700305 }
306
307 private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips,
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +0000308 boolean configured, Set<HostLocation> locations) {
309 return locations.isEmpty() ?
310 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), HostLocation.NONE, ips, configured) :
311 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), locations, ips, configured);
312
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530313 }
Harshada Chaundkar08bddcc2019-07-02 15:13:24 +0000314
Sho SHIMIZU53bcc242016-08-15 11:19:24 -0700315}