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