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