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