Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [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 | 45920dd | 2016-07-28 19:19:46 +0000 | [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 org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.junit.TestUtils; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 23 | import org.onlab.osgi.TestServiceDirectory; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 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.VirtualDevice; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 30 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 31 | import org.onosproject.net.ConnectPoint; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.DisjointPath; |
| 34 | import org.onosproject.net.ElementId; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.NetTestTools; |
| 37 | import org.onosproject.net.Path; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.TestDeviceParams; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 40 | import org.onosproject.net.topology.LinkWeigher; |
| 41 | import org.onosproject.net.topology.LinkWeigherAdapter; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 42 | import org.onosproject.net.topology.PathService; |
| 43 | import org.onosproject.store.service.TestStorageService; |
| 44 | |
| 45 | import java.util.Set; |
| 46 | |
| 47 | import static org.junit.Assert.assertEquals; |
| 48 | |
| 49 | /** |
| 50 | * Junit tests for VirtualNetworkPathService. |
| 51 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 52 | public class VirtualNetworkPathManagerTest extends TestDeviceParams { |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 53 | private final String tenantIdValue1 = "TENANT_ID1"; |
| 54 | |
| 55 | private VirtualNetworkManager manager; |
| 56 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 57 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 58 | private TestServiceDirectory testDirectory; |
| 59 | |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 60 | @Before |
| 61 | public void setUp() throws Exception { |
| 62 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 63 | |
| 64 | CoreService coreService = new TestCoreService(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 65 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 66 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 67 | virtualNetworkManagerStore.activate(); |
| 68 | |
| 69 | manager = new VirtualNetworkManager(); |
| 70 | manager.store = virtualNetworkManagerStore; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 71 | manager.coreService = coreService; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 72 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 73 | |
| 74 | testDirectory = new TestServiceDirectory(); |
| 75 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 76 | |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 77 | manager.activate(); |
| 78 | } |
| 79 | |
| 80 | @After |
| 81 | public void tearDown() { |
| 82 | virtualNetworkManagerStore.deactivate(); |
| 83 | manager.deactivate(); |
| 84 | NetTestTools.injectEventDispatcher(manager, null); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets up an empty virtual network (no devices, links). |
| 89 | * |
| 90 | * @return virtual network |
| 91 | */ |
| 92 | private VirtualNetwork setupEmptyVnet() { |
| 93 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 94 | return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Creates a virtual network for further testing. |
| 99 | * |
| 100 | * @return virtual network |
| 101 | */ |
| 102 | private VirtualNetwork setupVnet() { |
| 103 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 104 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 105 | VirtualDevice virtualDevice1 = |
| 106 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 107 | VirtualDevice virtualDevice2 = |
| 108 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
| 109 | VirtualDevice virtualDevice3 = |
| 110 | manager.createVirtualDevice(virtualNetwork.id(), DID3); |
| 111 | VirtualDevice virtualDevice4 = |
| 112 | manager.createVirtualDevice(virtualNetwork.id(), DID4); |
| 113 | |
| 114 | ConnectPoint cp11 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice1, 1); |
| 115 | ConnectPoint cp12 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice1, 2); |
| 116 | ConnectPoint cp23 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice2, 3); |
| 117 | ConnectPoint cp24 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice2, 4); |
| 118 | ConnectPoint cp35 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice3, 5); |
| 119 | ConnectPoint cp36 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice3, 6); |
| 120 | VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp11, cp23); |
| 121 | virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); |
| 122 | VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp23, cp11); |
| 123 | virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE); |
| 124 | VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp24, cp35); |
| 125 | virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE); |
| 126 | VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp35, cp24); |
| 127 | virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE); |
| 128 | VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp12, cp36); |
| 129 | virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE); |
| 130 | VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp36, cp12); |
| 131 | virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE); |
| 132 | |
| 133 | return virtualNetwork; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates a connect point and related virtual port. |
| 138 | * |
| 139 | * @param vnet virtual network |
| 140 | * @param vDev virtual device |
| 141 | * @param portNumber port number |
| 142 | * @return connect point |
| 143 | */ |
| 144 | private ConnectPoint createConnectPointAndVirtualPort( |
| 145 | VirtualNetwork vnet, VirtualDevice vDev, long portNumber) { |
| 146 | ConnectPoint cp = new ConnectPoint(vDev.id(), PortNumber.portNumber(portNumber)); |
| 147 | manager.createVirtualPort(vnet.id(), cp.deviceId(), cp.port(), |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 148 | new ConnectPoint(vDev.id(), cp.port())); |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 149 | return cp; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Tests getPaths(), getDisjointPaths() |
| 154 | * on a non-empty virtual network. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testGetPathsOnNonEmptyVnet() { |
| 158 | VirtualNetwork vnet = setupVnet(); |
| 159 | PathService pathService = manager.get(vnet.id(), PathService.class); |
| 160 | |
| 161 | // src and dest are in vnet and are connected by a virtual link |
| 162 | Set<Path> paths = pathService.getPaths(DID1, DID3); |
| 163 | validatePaths(paths, 1, 1, DID1, DID3, 1.0); |
| 164 | |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 165 | LinkWeigher linkWeight = new LinkWeigherAdapter(2.0); |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 166 | paths = pathService.getPaths(DID1, DID3, linkWeight); |
| 167 | validatePaths(paths, 1, 1, DID1, DID3, 2.0); |
| 168 | |
| 169 | Set<DisjointPath> disjointPaths = pathService.getDisjointPaths(DID1, DID3); |
| 170 | validatePaths(disjointPaths, 1, 1, DID1, DID3, 1.0); |
| 171 | |
| 172 | disjointPaths = pathService.getDisjointPaths(DID1, DID3, linkWeight); |
| 173 | validatePaths(disjointPaths, 1, 1, DID1, DID3, 2.0); |
| 174 | |
| 175 | // src and dest are in vnet but are not connected |
| 176 | paths = pathService.getPaths(DID4, DID3); |
| 177 | assertEquals("incorrect path count", 0, paths.size()); |
| 178 | |
| 179 | disjointPaths = pathService.getDisjointPaths(DID4, DID3); |
| 180 | assertEquals("incorrect path count", 0, disjointPaths.size()); |
| 181 | |
| 182 | // src is in vnet, but dest is not in vnet. |
| 183 | DeviceId nonExistentDeviceId = DeviceId.deviceId("nonExistentDevice"); |
| 184 | paths = pathService.getPaths(DID2, nonExistentDeviceId); |
| 185 | assertEquals("incorrect path count", 0, paths.size()); |
| 186 | |
| 187 | disjointPaths = pathService.getDisjointPaths(DID2, nonExistentDeviceId); |
| 188 | assertEquals("incorrect path count", 0, disjointPaths.size()); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Tests getPaths(), getDisjointPaths() |
| 193 | * on an empty virtual network. |
| 194 | */ |
| 195 | @Test |
| 196 | public void testGetPathsOnEmptyVnet() { |
| 197 | VirtualNetwork vnet = setupEmptyVnet(); |
| 198 | PathService pathService = manager.get(vnet.id(), PathService.class); |
| 199 | |
| 200 | Set<Path> paths = pathService.getPaths(DID1, DID3); |
| 201 | assertEquals("incorrect path count", 0, paths.size()); |
| 202 | |
| 203 | Set<DisjointPath> disjointPaths = pathService.getDisjointPaths(DID1, DID3); |
| 204 | assertEquals("incorrect path count", 0, disjointPaths.size()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Tests getPaths() using a null source device on an empty virtual network. |
| 209 | */ |
| 210 | @Test(expected = NullPointerException.class) |
| 211 | public void testGetPathsWithNullSrc() { |
| 212 | VirtualNetwork vnet = setupEmptyVnet(); |
| 213 | PathService pathService = manager.get(vnet.id(), PathService.class); |
| 214 | pathService.getPaths(null, DID3); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests getPaths() using a null destination device on a non-empty virtual network. |
| 219 | */ |
| 220 | @Test(expected = NullPointerException.class) |
| 221 | public void testGetPathsWithNullDest() { |
| 222 | VirtualNetwork vnet = setupVnet(); |
| 223 | PathService pathService = manager.get(vnet.id(), PathService.class); |
| 224 | pathService.getPaths(DID1, null); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // Makes sure the set of paths meets basic expectations. |
| 229 | private void validatePaths(Set<? extends Path> paths, int count, int length, |
| 230 | ElementId src, ElementId dst, double cost) { |
| 231 | assertEquals("incorrect path count", count, paths.size()); |
| 232 | for (Path path : paths) { |
| 233 | assertEquals("incorrect length", length, path.links().size()); |
| 234 | assertEquals("incorrect source", src, path.src().elementId()); |
| 235 | assertEquals("incorrect destination", dst, path.dst().elementId()); |
| 236 | assertEquals("incorrect cost", cost, path.cost(), 0); |
| 237 | } |
| 238 | } |
| 239 | } |