samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.store.host.impl; |
| 17 | |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultHost; |
| 26 | import org.onosproject.net.DeviceId; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.HostLocation; |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 30 | import org.onosproject.net.PortNumber; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 31 | import org.onosproject.net.host.DefaultHostDescription; |
| 32 | import org.onosproject.net.host.HostDescription; |
| 33 | import org.onosproject.net.provider.ProviderId; |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 34 | import org.onosproject.store.service.MapEvent; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 35 | import org.onosproject.store.service.TestStorageService; |
| 36 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 37 | import com.google.common.collect.Sets; |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 38 | import org.onosproject.store.service.Versioned; |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 39 | |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 40 | import java.util.Collections; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 41 | import java.util.HashSet; |
| 42 | import java.util.Set; |
| 43 | |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 44 | import static junit.framework.TestCase.assertTrue; |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 45 | import static org.junit.Assert.*; |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 46 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Tests for the ECHostStore. |
| 49 | */ |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 50 | public class DistributedHostStoreTest { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 51 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 52 | private DistributedHostStore ecXHostStore; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 53 | |
| 54 | private static final HostId HOSTID = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1a")); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 55 | private static final HostId HOSTID1 = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1b")); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 56 | |
| 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 Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 61 | private static final ProviderId PID2 = new ProviderId("of", "foo2"); |
| 62 | |
| 63 | private static final HostDescription HOST_LEARNT = |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 64 | createHostDesc(HOSTID, Sets.newHashSet(IP1), false, Collections.emptySet()); |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 65 | private static final HostDescription HOST_CONFIGURED = |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 66 | 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 pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 100 | |
| 101 | @Before |
| 102 | public void setUp() { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 103 | ecXHostStore = new DistributedHostStore(); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 104 | |
| 105 | ecXHostStore.storageService = new TestStorageService(); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 106 | 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 Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 123 | HostDescription description = createHostDesc(HOSTID, ips); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 124 | 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 Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 132 | @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 Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 168 | @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 Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 189 | @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 Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 303 | private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) { |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 304 | return createHostDesc(hostId, ips, false, Collections.emptySet()); |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips, |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 308 | 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 Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 313 | } |
Harshada Chaundkar | 08bddcc | 2019-07-02 15:13:24 +0000 | [diff] [blame^] | 314 | |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 315 | } |