Brian Stanke | 8691428 | 2016-05-25 15:36:50 -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.core.CoreServiceAdapter; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.incubator.net.virtual.TenantId; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 32 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 33 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 34 | import org.onosproject.net.DefaultPort; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.NetTestTools; |
| 37 | import org.onosproject.net.PortNumber; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 38 | import org.onosproject.net.TestDeviceParams; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 39 | import org.onosproject.net.intent.FakeIntentManager; |
| 40 | import org.onosproject.net.intent.TestableIntentService; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 41 | import org.onosproject.net.link.LinkService; |
| 42 | import org.onosproject.store.service.TestStorageService; |
| 43 | |
| 44 | import java.util.Iterator; |
| 45 | import java.util.concurrent.atomic.AtomicLong; |
| 46 | |
| 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertNotEquals; |
| 49 | |
| 50 | /** |
| 51 | * Junit tests for VirtualNetworkLinkService. |
| 52 | */ |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 53 | public class VirtualNetworkLinkServiceTest extends TestDeviceParams { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 54 | |
| 55 | private final String tenantIdValue1 = "TENANT_ID1"; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 56 | |
| 57 | private VirtualNetworkManager manager; |
| 58 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 59 | private CoreService coreService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 60 | private TestableIntentService intentService = new FakeIntentManager(); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 61 | |
| 62 | @Before |
| 63 | public void setUp() throws Exception { |
| 64 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 65 | |
| 66 | coreService = new VirtualNetworkLinkServiceTest.TestCoreService(); |
| 67 | virtualNetworkManagerStore.setCoreService(coreService); |
| 68 | TestUtils.setField(coreService, "coreService", new VirtualNetworkLinkServiceTest.TestCoreService()); |
| 69 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 70 | virtualNetworkManagerStore.activate(); |
| 71 | |
| 72 | manager = new VirtualNetworkManager(); |
| 73 | manager.store = virtualNetworkManagerStore; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 74 | manager.intentService = intentService; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 75 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 76 | manager.activate(); |
| 77 | } |
| 78 | |
| 79 | @After |
| 80 | public void tearDown() { |
| 81 | virtualNetworkManagerStore.deactivate(); |
| 82 | manager.deactivate(); |
| 83 | NetTestTools.injectEventDispatcher(manager, null); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests the getLinks(), getActiveLinks(), getLinkCount(), getLink(), |
| 88 | * getLinks(ConnectPoint), getDeviceLinks(), getDeviceEgressLinks(), getDeviceIngressLinks(), |
| 89 | * getEgressLinks(), getIngressLinks() methods. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testGetLinks() { |
| 93 | |
| 94 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 95 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 96 | VirtualDevice srcVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 97 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 98 | VirtualDevice dstVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 99 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 100 | ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 101 | manager.createVirtualPort(virtualNetwork.id(), src.deviceId(), src.port(), |
| 102 | new DefaultPort(srcVirtualDevice, src.port(), true)); |
| 103 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 104 | ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 105 | manager.createVirtualPort(virtualNetwork.id(), dst.deviceId(), dst.port(), |
| 106 | new DefaultPort(dstVirtualDevice, dst.port(), true)); |
| 107 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 108 | VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst); |
| 109 | VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), dst, src); |
| 110 | |
| 111 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 112 | |
| 113 | // test the getLinks() method |
| 114 | Iterator<Link> it = linkService.getLinks().iterator(); |
| 115 | assertEquals("The link set size did not match.", 2, Iterators.size(it)); |
| 116 | |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 117 | // test the getActiveLinks() method where all links are INACTIVE |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 118 | Iterator<Link> it2 = linkService.getActiveLinks().iterator(); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 119 | assertEquals("The link set size did not match.", 0, Iterators.size(it2)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 120 | |
| 121 | // test the getActiveLinks() method where one link is ACTIVE |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 122 | virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 123 | Iterator<Link> it3 = linkService.getActiveLinks().iterator(); |
| 124 | assertEquals("The link set size did not match.", 1, Iterators.size(it3)); |
| 125 | |
| 126 | // test the getLinkCount() method |
| 127 | assertEquals("The link set size did not match.", 2, linkService.getLinkCount()); |
| 128 | |
| 129 | // test the getLink() method |
| 130 | assertEquals("The expect link did not match.", link1, |
| 131 | linkService.getLink(src, dst)); |
| 132 | assertEquals("The expect link did not match.", link2, |
| 133 | linkService.getLink(dst, src)); |
| 134 | assertNotEquals("The expect link should not have matched.", link1, |
| 135 | linkService.getLink(dst, src)); |
| 136 | |
| 137 | // test the getLinks(ConnectPoint) method |
| 138 | assertEquals("The link set size did not match.", 2, linkService.getLinks(src).size()); |
| 139 | assertEquals("The link set size did not match.", 2, linkService.getLinks(dst).size()); |
| 140 | ConnectPoint connectPoint = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(3)); |
| 141 | assertEquals("The link set size did not match.", 0, linkService.getLinks(connectPoint).size()); |
| 142 | |
| 143 | // test the getDeviceLinks() method |
| 144 | assertEquals("The link set size did not match.", 2, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 145 | linkService.getDeviceLinks(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 146 | assertEquals("The link set size did not match.", 2, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 147 | linkService.getDeviceLinks(DID2).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 148 | assertEquals("The link set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 149 | linkService.getDeviceLinks(DID3).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 150 | |
| 151 | // test the getDeviceEgressLinks() method |
| 152 | assertEquals("The link set size did not match.", 1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 153 | linkService.getDeviceEgressLinks(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 154 | assertEquals("The link set size did not match.", 1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 155 | linkService.getDeviceEgressLinks(DID2).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 156 | assertEquals("The link set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 157 | linkService.getDeviceEgressLinks(DID3).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 158 | |
| 159 | // test the getDeviceIngressLinks() method |
| 160 | assertEquals("The link set size did not match.", 1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 161 | linkService.getDeviceIngressLinks(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 162 | assertEquals("The link set size did not match.", 1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 163 | linkService.getDeviceIngressLinks(DID2).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 164 | assertEquals("The link set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 165 | linkService.getDeviceIngressLinks(DID3).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 166 | |
| 167 | // test the getEgressLinks() method |
| 168 | assertEquals("The link set size did not match.", 1, |
| 169 | linkService.getEgressLinks(src).size()); |
| 170 | assertEquals("The link set size did not match.", 1, |
| 171 | linkService.getEgressLinks(dst).size()); |
| 172 | assertEquals("The link set size did not match.", 0, |
| 173 | linkService.getEgressLinks(connectPoint).size()); |
| 174 | |
| 175 | // test the getIngressLinks() method |
| 176 | assertEquals("The link set size did not match.", 1, |
| 177 | linkService.getIngressLinks(src).size()); |
| 178 | assertEquals("The link set size did not match.", 1, |
| 179 | linkService.getIngressLinks(dst).size()); |
| 180 | assertEquals("The link set size did not match.", 0, |
| 181 | linkService.getIngressLinks(connectPoint).size()); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Tests the getLink() method using a null src connect point. |
| 186 | */ |
| 187 | @Test(expected = NullPointerException.class) |
| 188 | public void testGetLinkByNullSrc() { |
| 189 | |
| 190 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 191 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 192 | VirtualDevice srcVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 193 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 194 | VirtualDevice dstVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 195 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 196 | ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
| 197 | ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); |
| 198 | manager.createVirtualLink(virtualNetwork.id(), src, dst); |
| 199 | manager.createVirtualLink(virtualNetwork.id(), dst, src); |
| 200 | |
| 201 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 202 | |
| 203 | // test the getLink() method with a null src connect point. |
| 204 | linkService.getLink(null, dst); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Tests the getLink() method using a null dst connect point. |
| 209 | */ |
| 210 | @Test(expected = NullPointerException.class) |
| 211 | public void testGetLinkByNullDst() { |
| 212 | |
| 213 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 214 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 215 | VirtualDevice srcVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 216 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 217 | VirtualDevice dstVirtualDevice = |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 218 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 219 | ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
| 220 | ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); |
| 221 | manager.createVirtualLink(virtualNetwork.id(), src, dst); |
| 222 | manager.createVirtualLink(virtualNetwork.id(), dst, src); |
| 223 | |
| 224 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 225 | |
| 226 | // test the getLink() method with a null dst connect point. |
| 227 | linkService.getLink(src, null); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Tests querying for links using a null device identifier. |
| 232 | */ |
| 233 | @Test(expected = NullPointerException.class) |
| 234 | public void testGetDeviceLinksByNullId() { |
| 235 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 236 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 237 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 238 | |
| 239 | // test the getDeviceLinks() method with a null device identifier. |
| 240 | linkService.getDeviceLinks(null); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Tests querying for links using a null connect point. |
| 245 | */ |
| 246 | @Test(expected = NullPointerException.class) |
| 247 | public void testGetLinksByNullId() { |
| 248 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 249 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 250 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 251 | |
| 252 | // test the getLinks() method with a null connect point. |
| 253 | linkService.getLinks(null); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Tests querying for device egress links using a null device identifier. |
| 258 | */ |
| 259 | @Test(expected = NullPointerException.class) |
| 260 | public void testGetDeviceEgressLinksByNullId() { |
| 261 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 262 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 263 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 264 | |
| 265 | // test the getDeviceEgressLinks() method with a null device identifier. |
| 266 | linkService.getDeviceEgressLinks(null); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Tests querying for device ingress links using a null device identifier. |
| 271 | */ |
| 272 | @Test(expected = NullPointerException.class) |
| 273 | public void testGetDeviceIngressLinksByNullId() { |
| 274 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 275 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 276 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 277 | |
| 278 | // test the getDeviceIngressLinks() method with a null device identifier. |
| 279 | linkService.getDeviceIngressLinks(null); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests querying for egress links using a null connect point. |
| 284 | */ |
| 285 | @Test(expected = NullPointerException.class) |
| 286 | public void testGetEgressLinksByNullId() { |
| 287 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 288 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 289 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 290 | |
| 291 | // test the getEgressLinks() method with a null connect point. |
| 292 | linkService.getEgressLinks(null); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests querying for ingress links using a null connect point. |
| 297 | */ |
| 298 | @Test(expected = NullPointerException.class) |
| 299 | public void testGetIngressLinksByNullId() { |
| 300 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 301 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 302 | LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); |
| 303 | |
| 304 | // test the getIngressLinks() method with a null connect point. |
| 305 | linkService.getIngressLinks(null); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Core service test class. |
| 310 | */ |
| 311 | private class TestCoreService extends CoreServiceAdapter { |
| 312 | |
| 313 | @Override |
| 314 | public IdGenerator getIdGenerator(String topic) { |
| 315 | return new IdGenerator() { |
| 316 | private AtomicLong counter = new AtomicLong(0); |
| 317 | |
| 318 | @Override |
| 319 | public long getNewId() { |
| 320 | return counter.getAndIncrement(); |
| 321 | } |
| 322 | }; |
| 323 | } |
| 324 | } |
| 325 | } |