Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Iterators; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 24 | import org.onlab.osgi.TestServiceDirectory; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.incubator.net.virtual.TenantId; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 30 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.NetTestTools; |
| 35 | import org.onosproject.net.TestDeviceParams; |
| 36 | import org.onosproject.net.host.HostService; |
| 37 | import org.onosproject.net.intent.FakeIntentManager; |
| 38 | import org.onosproject.net.intent.TestableIntentService; |
| 39 | import org.onosproject.store.service.TestStorageService; |
| 40 | |
| 41 | import java.util.Collection; |
| 42 | import java.util.Iterator; |
| 43 | |
| 44 | import static org.junit.Assert.*; |
| 45 | |
| 46 | /** |
| 47 | * Junit tests for VirtualNetworkHostService. |
| 48 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 49 | public class VirtualNetworkHostManagerTest extends TestDeviceParams { |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 50 | private final String tenantIdValue1 = "TENANT_ID1"; |
| 51 | |
| 52 | private VirtualNetworkManager manager; |
| 53 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 54 | private TestableIntentService intentService = new FakeIntentManager(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 55 | private TestServiceDirectory testDirectory; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 56 | |
| 57 | @Before |
| 58 | public void setUp() throws Exception { |
| 59 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 60 | |
| 61 | CoreService coreService = new TestCoreService(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 62 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 63 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 64 | virtualNetworkManagerStore.activate(); |
| 65 | |
| 66 | manager = new VirtualNetworkManager(); |
| 67 | manager.store = virtualNetworkManagerStore; |
| 68 | manager.intentService = intentService; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 69 | manager.coreService = coreService; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 70 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 71 | |
| 72 | testDirectory = new TestServiceDirectory(); |
| 73 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 74 | |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 75 | manager.activate(); |
| 76 | } |
| 77 | |
| 78 | @After |
| 79 | public void tearDown() { |
| 80 | virtualNetworkManagerStore.deactivate(); |
| 81 | manager.deactivate(); |
| 82 | NetTestTools.injectEventDispatcher(manager, null); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Sets up a virtual network with hosts. |
| 87 | * |
| 88 | * @return virtual network |
| 89 | */ |
| 90 | private VirtualNetwork setupVnet() { |
| 91 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 92 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 93 | manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1); |
| 94 | manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2); |
| 95 | return virtualNetwork; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets up a virtual network with no hosts. |
| 100 | * |
| 101 | * @return virtual network |
| 102 | */ |
| 103 | private VirtualNetwork setupEmptyVnet() { |
| 104 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 105 | return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods |
| 110 | * on a non-empty virtual network. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testGetHostsOnNonEmptyVnet() { |
| 114 | VirtualNetwork virtualNetwork = setupEmptyVnet(); |
| 115 | VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1); |
| 116 | VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2); |
| 117 | HostService hostService = manager.get(virtualNetwork.id(), HostService.class); |
| 118 | |
| 119 | // test the getHosts() and getHostCount() methods |
| 120 | Iterator<Host> itHosts = hostService.getHosts().iterator(); |
| 121 | assertEquals("The host set size did not match.", 2, Iterators.size(itHosts)); |
| 122 | assertEquals("The host count did not match.", 2, hostService.getHostCount()); |
| 123 | |
| 124 | // test the getHost() method |
| 125 | Host testHost = hostService.getHost(HID2); |
| 126 | assertEquals("The expected host did not match.", vhost2, testHost); |
| 127 | |
| 128 | // test the getHostsByVlan(...) method |
| 129 | Collection<Host> collHost = hostService.getHostsByVlan(VLAN1); |
| 130 | assertEquals("The host set size did not match.", 1, collHost.size()); |
| 131 | assertTrue("The host did not match.", collHost.contains(vhost1)); |
| 132 | |
| 133 | // test the getHostsByMac(...) method |
| 134 | collHost = hostService.getHostsByMac(MAC2); |
| 135 | assertEquals("The host set size did not match.", 1, collHost.size()); |
| 136 | assertTrue("The host did not match.", collHost.contains(vhost2)); |
| 137 | |
| 138 | // test the getHostsByIp(...) method |
| 139 | collHost = hostService.getHostsByIp(IP1); |
| 140 | assertEquals("The host set size did not match.", 2, collHost.size()); |
| 141 | collHost = hostService.getHostsByIp(IP2); |
| 142 | assertEquals("The host set size did not match.", 1, collHost.size()); |
| 143 | assertTrue("The host did not match.", collHost.contains(vhost1)); |
| 144 | |
| 145 | // test the getConnectedHosts(ConnectPoint) method |
| 146 | collHost = hostService.getConnectedHosts(LOC1); |
| 147 | assertEquals("The host set size did not match.", 1, collHost.size()); |
| 148 | assertTrue("The host did not match.", collHost.contains(vhost1)); |
| 149 | |
| 150 | // test the getConnectedHosts(DeviceId) method |
| 151 | collHost = hostService.getConnectedHosts(DID2); |
| 152 | assertEquals("The host set size did not match.", 1, collHost.size()); |
| 153 | assertTrue("The host did not match.", collHost.contains(vhost2)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods |
| 158 | * on an empty virtual network. |
| 159 | */ |
| 160 | @Test |
| 161 | public void testGetHostsOnEmptyVnet() { |
| 162 | VirtualNetwork virtualNetwork = setupEmptyVnet(); |
| 163 | HostService hostService = manager.get(virtualNetwork.id(), HostService.class); |
| 164 | |
| 165 | // test the getHosts() and getHostCount() methods |
| 166 | Iterator<Host> itHosts = hostService.getHosts().iterator(); |
| 167 | assertEquals("The host set size did not match.", 0, Iterators.size(itHosts)); |
| 168 | assertEquals("The host count did not match.", 0, hostService.getHostCount()); |
| 169 | |
| 170 | // test the getHost() method |
| 171 | Host testHost = hostService.getHost(HID2); |
| 172 | assertNull("The host should be null.", testHost); |
| 173 | |
| 174 | // test the getHostsByVlan(...) method |
| 175 | Collection<Host> collHost = hostService.getHostsByVlan(VLAN1); |
| 176 | assertEquals("The host set size did not match.", 0, collHost.size()); |
| 177 | |
| 178 | // test the getHostsByMac(...) method |
| 179 | collHost = hostService.getHostsByMac(MAC2); |
| 180 | assertEquals("The host set size did not match.", 0, collHost.size()); |
| 181 | |
| 182 | // test the getHostsByIp(...) method |
| 183 | collHost = hostService.getHostsByIp(IP1); |
| 184 | assertEquals("The host set size did not match.", 0, collHost.size()); |
| 185 | |
| 186 | // test the getConnectedHosts(ConnectPoint) method |
| 187 | collHost = hostService.getConnectedHosts(LOC1); |
| 188 | assertEquals("The host set size did not match.", 0, collHost.size()); |
| 189 | |
| 190 | // test the getConnectedHosts(DeviceId) method |
| 191 | collHost = hostService.getConnectedHosts(DID2); |
| 192 | assertEquals("The host set size did not match.", 0, collHost.size()); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Tests querying for a host using a null host identifier. |
| 197 | */ |
| 198 | @Test(expected = NullPointerException.class) |
| 199 | public void testGetHostByNullId() { |
| 200 | VirtualNetwork vnet = setupEmptyVnet(); |
| 201 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 202 | |
| 203 | hostService.getHost(null); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Tests querying for hosts with null mac. |
| 208 | */ |
| 209 | @Test(expected = NullPointerException.class) |
| 210 | public void testGetHostsByNullMac() { |
| 211 | VirtualNetwork vnet = setupEmptyVnet(); |
| 212 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 213 | |
| 214 | hostService.getHostsByMac(null); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests querying for hosts with null vlan. |
| 219 | */ |
| 220 | @Test(expected = NullPointerException.class) |
| 221 | public void testGetHostsByNullVlan() { |
| 222 | VirtualNetwork vnet = setupEmptyVnet(); |
| 223 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 224 | |
| 225 | hostService.getHostsByVlan(null); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests querying for hosts with null ip. |
| 230 | */ |
| 231 | @Test(expected = NullPointerException.class) |
| 232 | public void testGetHostsByNullIp() { |
| 233 | VirtualNetwork vnet = setupVnet(); |
| 234 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 235 | |
| 236 | hostService.getHostsByIp(null); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Tests querying for connected hosts with null host location (connect point). |
| 241 | */ |
| 242 | @Test(expected = NullPointerException.class) |
| 243 | public void testGetConnectedHostsByNullLoc() { |
| 244 | VirtualNetwork vnet = setupEmptyVnet(); |
| 245 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 246 | |
| 247 | hostService.getConnectedHosts((ConnectPoint) null); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests querying for connected hosts with null device id. |
| 252 | */ |
| 253 | @Test(expected = NullPointerException.class) |
| 254 | public void testGetConnectedHostsByNullDeviceId() { |
| 255 | VirtualNetwork vnet = setupVnet(); |
| 256 | HostService hostService = manager.get(vnet.id(), HostService.class); |
| 257 | |
| 258 | hostService.getConnectedHosts((DeviceId) null); |
| 259 | } |
| 260 | |
| 261 | } |