Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -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 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.TestApplicationId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.CoreServiceAdapter; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 29 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualNetworkProvider; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualNetworkProviderRegistry; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualNetworkProviderService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.PortNumber; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 36 | import org.onosproject.net.intent.FakeIntentManager; |
| 37 | import org.onosproject.net.intent.FlowRuleIntent; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 38 | import org.onosproject.net.intent.Intent; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentCompiler; |
| 40 | import org.onosproject.net.intent.IntentEvent; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.IntentListener; |
| 43 | import org.onosproject.net.intent.IntentTestsMocks; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 44 | import org.onosproject.net.intent.MockIdGenerator; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 45 | import org.onosproject.net.intent.PointToPointIntent; |
| 46 | import org.onosproject.net.intent.TestableIntentService; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 47 | import org.onosproject.net.provider.AbstractProviderService; |
| 48 | import org.onosproject.net.provider.ProviderId; |
| 49 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 50 | import java.util.Collections; |
| 51 | import java.util.List; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 52 | import java.util.Set; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 53 | import java.util.concurrent.Semaphore; |
| 54 | import java.util.concurrent.TimeUnit; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 55 | import java.util.concurrent.atomic.AtomicLong; |
| 56 | |
| 57 | import static org.easymock.EasyMock.*; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 58 | import static org.junit.Assert.*; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Junit tests for PtToPtIntentVirtualNetworkProvider. |
| 62 | */ |
| 63 | public class PtToPtIntentVirtualNetworkProviderTest { |
| 64 | |
| 65 | private PtToPtIntentVirtualNetworkProvider provider; |
| 66 | private VirtualNetworkProviderRegistry providerRegistry; |
| 67 | |
| 68 | private final VirtualNetworkRegistryAdapter virtualNetworkRegistry = new VirtualNetworkRegistryAdapter(); |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 69 | private TestableIntentService intentService = new FakeIntentManager(); |
| 70 | private TestListener listener = new TestListener(); |
| 71 | protected TestIntentCompiler compiler = new TestIntentCompiler(); |
| 72 | private IntentExtensionService intentExtensionService; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 73 | |
| 74 | private static final ApplicationId APP_ID = |
| 75 | TestApplicationId.create(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID); |
| 76 | |
| 77 | private IdGenerator idGenerator = new MockIdGenerator(); |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 78 | private static final int MAX_WAIT_TIME = 5; |
| 79 | private static final int MAX_PERMITS = 2; |
| 80 | private static Semaphore created; |
| 81 | private static Semaphore removed; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 82 | |
| 83 | @Before |
| 84 | public void setUp() { |
| 85 | provider = new PtToPtIntentVirtualNetworkProvider(); |
| 86 | provider.providerRegistry = virtualNetworkRegistry; |
| 87 | final CoreService mockCoreService = createMock(CoreService.class); |
| 88 | provider.coreService = mockCoreService; |
| 89 | expect(mockCoreService.registerApplication(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID)) |
| 90 | .andReturn(APP_ID).anyTimes(); |
| 91 | replay(mockCoreService); |
| 92 | Intent.unbindIdGenerator(idGenerator); |
| 93 | Intent.bindIdGenerator(idGenerator); |
| 94 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 95 | intentService.addListener(listener); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 96 | provider.intentService = intentService; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 97 | |
| 98 | // Register a compiler and an installer both setup for success. |
| 99 | intentExtensionService = intentService; |
| 100 | intentExtensionService.registerCompiler(PointToPointIntent.class, compiler); |
| 101 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 102 | provider.activate(); |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 103 | created = new Semaphore(0, true); |
| 104 | removed = new Semaphore(0, true); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @After |
| 108 | public void tearDown() { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 109 | Intent.unbindIdGenerator(idGenerator); |
| 110 | intentService.removeListener(listener); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 111 | provider.deactivate(); |
| 112 | provider.providerRegistry = null; |
| 113 | provider.coreService = null; |
| 114 | provider.intentService = null; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 115 | created = null; |
| 116 | removed = null; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void basics() { |
| 121 | assertNotNull("registration expected", provider); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test a null network identifier. |
| 126 | */ |
| 127 | @Test(expected = NullPointerException.class) |
| 128 | public void testCreateTunnelNullNetworkId() { |
| 129 | provider.createTunnel(null, null, null); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Test a null source connect point. |
| 134 | */ |
| 135 | @Test(expected = NullPointerException.class) |
| 136 | public void testCreateTunnelNullSrc() { |
| 137 | ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2)); |
| 138 | |
| 139 | provider.createTunnel(NetworkId.networkId(0), null, dst); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Test a null destination connect point. |
| 144 | */ |
| 145 | @Test(expected = NullPointerException.class) |
| 146 | public void testCreateTunnelNullDst() { |
| 147 | ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1)); |
| 148 | |
| 149 | provider.createTunnel(NetworkId.networkId(0), src, null); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Test creating/destroying a valid tunnel. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testCreateRemoveTunnel() { |
| 157 | NetworkId networkId = NetworkId.networkId(0); |
| 158 | ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1)); |
| 159 | ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2)); |
| 160 | |
| 161 | TunnelId tunnelId = provider.createTunnel(networkId, src, dst); |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 162 | |
| 163 | // Wait for the tunnel to go into an INSTALLED state, and that the tunnelUp method was called. |
| 164 | try { |
| 165 | if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) { |
| 166 | fail("Failed to wait for tunnel to get installed."); |
| 167 | } |
| 168 | } catch (InterruptedException e) { |
| 169 | fail("Semaphore exception during tunnel installation." + e.getMessage()); |
| 170 | } |
| 171 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 172 | String key = String.format(PtToPtIntentVirtualNetworkProvider.KEY_FORMAT, |
| 173 | networkId.toString(), src.toString(), dst.toString()); |
| 174 | |
| 175 | assertEquals("TunnelId does not match as expected.", key, tunnelId.toString()); |
| 176 | provider.destroyTunnel(networkId, tunnelId); |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 177 | |
| 178 | // Wait for the tunnel to go into a WITHDRAWN state, and that the tunnelDown method was called. |
| 179 | try { |
| 180 | if (!removed.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) { |
| 181 | fail("Failed to wait for tunnel to get removed."); |
| 182 | } |
| 183 | } catch (InterruptedException e) { |
| 184 | fail("Semaphore exception during tunnel removal." + e.getMessage()); |
| 185 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Virtual network registry implementation for this test class. |
| 190 | */ |
| 191 | private class VirtualNetworkRegistryAdapter implements VirtualNetworkProviderRegistry { |
| 192 | private VirtualNetworkProvider provider; |
| 193 | |
| 194 | @Override |
| 195 | public VirtualNetworkProviderService register(VirtualNetworkProvider theProvider) { |
| 196 | this.provider = theProvider; |
| 197 | return new TestVirtualNetworkProviderService(theProvider); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void unregister(VirtualNetworkProvider theProvider) { |
| 202 | this.provider = null; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Set<ProviderId> getProviders() { |
| 207 | return null; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Virtual network provider service implementation for this test class. |
| 213 | */ |
| 214 | private class TestVirtualNetworkProviderService |
| 215 | extends AbstractProviderService<VirtualNetworkProvider> |
| 216 | implements VirtualNetworkProviderService { |
| 217 | |
| 218 | protected TestVirtualNetworkProviderService(VirtualNetworkProvider provider) { |
| 219 | super(provider); |
| 220 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 221 | |
| 222 | @Override |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame^] | 223 | public void topologyChanged(Set<Set<ConnectPoint>> clusters) { |
| 224 | |
| 225 | } |
| 226 | |
| 227 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 228 | public void tunnelUp(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId tunnelId) { |
| 229 | // Release one permit on the created semaphore since the tunnelUp method was called. |
| 230 | created.release(); |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 234 | public void tunnelDown(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId tunnelId) { |
| 235 | // Release one permit on the removed semaphore since the tunnelDown method was called. |
| 236 | removed.release(); |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 237 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 240 | private static class TestListener implements IntentListener { |
| 241 | |
| 242 | @Override |
| 243 | public void event(IntentEvent event) { |
| 244 | switch (event.type()) { |
| 245 | case INSTALLED: |
| 246 | // Release one permit on the created semaphore since the Intent event was received. |
| 247 | created.release(); |
| 248 | break; |
| 249 | case WITHDRAWN: |
| 250 | // Release one permit on the removed semaphore since the Intent event was received. |
| 251 | removed.release(); |
| 252 | break; |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Core service test class. |
| 261 | */ |
| 262 | private class TestCoreService extends CoreServiceAdapter { |
| 263 | |
| 264 | @Override |
| 265 | public IdGenerator getIdGenerator(String topic) { |
| 266 | return new IdGenerator() { |
| 267 | private AtomicLong counter = new AtomicLong(0); |
| 268 | |
| 269 | @Override |
| 270 | public long getNewId() { |
| 271 | return counter.getAndIncrement(); |
| 272 | } |
| 273 | }; |
| 274 | } |
| 275 | } |
| 276 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 277 | private static class TestIntentCompiler implements IntentCompiler<PointToPointIntent> { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 278 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 279 | public List<Intent> compile(PointToPointIntent intent, List<Intent> installable) { |
| 280 | return Lists.newArrayList(new MockInstallableIntent()); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 281 | } |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 282 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 283 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 284 | private static class MockInstallableIntent extends FlowRuleIntent { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 285 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 286 | public MockInstallableIntent() { |
| 287 | super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList()); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |