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 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onosproject.net.Host; |
| 24 | import org.onosproject.net.HostId; |
| 25 | import org.onosproject.net.HostLocation; |
| 26 | import org.onosproject.net.host.DefaultHostDescription; |
| 27 | import org.onosproject.net.host.HostDescription; |
| 28 | import org.onosproject.net.provider.ProviderId; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.TestStorageService; |
| 30 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 31 | import com.google.common.collect.Sets; |
| 32 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 33 | import java.util.HashSet; |
| 34 | import java.util.Set; |
| 35 | |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 36 | import static junit.framework.TestCase.assertTrue; |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 37 | import static org.junit.Assert.assertEquals; |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 38 | import static org.junit.Assert.assertFalse; |
| 39 | |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Tests for the ECHostStore. |
| 42 | */ |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 43 | public class DistributedHostStoreTest { |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 44 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 45 | private DistributedHostStore ecXHostStore; |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 46 | |
| 47 | 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] | 48 | 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] | 49 | |
| 50 | private static final IpAddress IP1 = IpAddress.valueOf("10.2.0.2"); |
| 51 | private static final IpAddress IP2 = IpAddress.valueOf("10.2.0.3"); |
| 52 | |
| 53 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 54 | private static final ProviderId PID2 = new ProviderId("of", "foo2"); |
| 55 | |
| 56 | private static final HostDescription HOST_LEARNT = |
| 57 | createHostDesc(HOSTID, Sets.newHashSet(IP1), false); |
| 58 | private static final HostDescription HOST_CONFIGURED = |
| 59 | createHostDesc(HOSTID, Sets.newHashSet(IP1), true); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 60 | |
| 61 | @Before |
| 62 | public void setUp() { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 63 | ecXHostStore = new DistributedHostStore(); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 64 | |
| 65 | ecXHostStore.storageService = new TestStorageService(); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 66 | ecXHostStore.activate(); |
| 67 | } |
| 68 | |
| 69 | @After |
| 70 | public void tearDown() { |
| 71 | ecXHostStore.deactivate(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests the removeIp method call. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testRemoveIp() { |
| 79 | Set<IpAddress> ips = new HashSet<>(); |
| 80 | ips.add(IP1); |
| 81 | ips.add(IP2); |
| 82 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 83 | HostDescription description = createHostDesc(HOSTID, ips); |
samanwita pal | e7c08de | 2015-09-24 21:59:49 -0700 | [diff] [blame] | 84 | ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false); |
| 85 | ecXHostStore.removeIp(HOSTID, IP1); |
| 86 | Host host = ecXHostStore.getHost(HOSTID); |
| 87 | |
| 88 | assertFalse(host.ipAddresses().contains(IP1)); |
| 89 | assertTrue(host.ipAddresses().contains(IP2)); |
| 90 | } |
| 91 | |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 92 | @Test |
| 93 | public void testAddHostByIp() { |
| 94 | Set<IpAddress> ips = new HashSet<>(); |
| 95 | ips.add(IP1); |
| 96 | ips.add(IP2); |
| 97 | |
| 98 | HostDescription description = createHostDesc(HOSTID, ips); |
| 99 | ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false); |
| 100 | |
| 101 | Set<Host> hosts = ecXHostStore.getHosts(IP1); |
| 102 | |
| 103 | assertFalse(hosts.size() > 1); |
| 104 | assertTrue(hosts.size() == 1); |
| 105 | |
| 106 | HostDescription description1 = createHostDesc(HOSTID1, Sets.newHashSet(IP2)); |
| 107 | ecXHostStore.createOrUpdateHost(PID, HOSTID1, description1, false); |
| 108 | |
| 109 | Set<Host> hosts1 = ecXHostStore.getHosts(IP2); |
| 110 | |
| 111 | assertFalse(hosts1.size() < 1); |
| 112 | assertTrue(hosts1.size() == 2); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testRemoveHostByIp() { |
| 117 | Set<IpAddress> ips = new HashSet<>(); |
| 118 | ips.add(IP1); |
| 119 | ips.add(IP2); |
| 120 | |
| 121 | HostDescription description = createHostDesc(HOSTID, ips); |
| 122 | ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false); |
| 123 | ecXHostStore.removeIp(HOSTID, IP1); |
| 124 | Set<Host> hosts = ecXHostStore.getHosts(IP1); |
| 125 | assertTrue(hosts.size() == 0); |
| 126 | } |
| 127 | |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 128 | @Test |
| 129 | public void testHostOverride() { |
| 130 | Host hostInStore; |
| 131 | ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false); |
| 132 | hostInStore = ecXHostStore.getHost(HOSTID); |
| 133 | assertFalse(hostInStore.configured()); |
| 134 | assertEquals(PID, hostInStore.providerId()); |
| 135 | |
| 136 | // Expect: configured host should override learnt host |
| 137 | ecXHostStore.createOrUpdateHost(PID2, HOSTID, HOST_CONFIGURED, true); |
| 138 | hostInStore = ecXHostStore.getHost(HOSTID); |
| 139 | assertTrue(hostInStore.configured()); |
| 140 | assertEquals(PID2, hostInStore.providerId()); |
| 141 | |
| 142 | // Expect: learnt host should not override configured host |
| 143 | ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false); |
| 144 | hostInStore = ecXHostStore.getHost(HOSTID); |
| 145 | assertTrue(hostInStore.configured()); |
| 146 | assertEquals(PID2, hostInStore.providerId()); |
| 147 | } |
| 148 | |
| 149 | private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) { |
| 150 | return createHostDesc(hostId, ips, false); |
| 151 | } |
| 152 | |
| 153 | private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips, |
| 154 | boolean configured) { |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 155 | return new DefaultHostDescription(hostId.mac(), |
| 156 | hostId.vlanId(), |
| 157 | HostLocation.NONE, |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 158 | ips, |
| 159 | configured); |
Deepa Vaddireddy | 0a71c8b | 2017-01-19 21:20:45 +0530 | [diff] [blame] | 160 | } |
Sho SHIMIZU | 53bcc24 | 2016-08-15 11:19:24 -0700 | [diff] [blame] | 161 | } |