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 | |
| 19 | import com.google.common.collect.Sets; |
| 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; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentService; |
| 38 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 39 | import org.onosproject.net.intent.IntentState; |
| 40 | import org.onosproject.net.intent.Key; |
| 41 | import org.onosproject.net.intent.MockIdGenerator; |
| 42 | import org.onosproject.net.provider.AbstractProviderService; |
| 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.atomic.AtomicLong; |
| 47 | |
| 48 | import static org.easymock.EasyMock.*; |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertNotNull; |
| 51 | |
| 52 | /** |
| 53 | * Junit tests for PtToPtIntentVirtualNetworkProvider. |
| 54 | */ |
| 55 | public class PtToPtIntentVirtualNetworkProviderTest { |
| 56 | |
| 57 | private PtToPtIntentVirtualNetworkProvider provider; |
| 58 | private VirtualNetworkProviderRegistry providerRegistry; |
| 59 | |
| 60 | private final VirtualNetworkRegistryAdapter virtualNetworkRegistry = new VirtualNetworkRegistryAdapter(); |
| 61 | private IntentService intentService; |
| 62 | |
| 63 | private static final ApplicationId APP_ID = |
| 64 | TestApplicationId.create(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID); |
| 65 | |
| 66 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 67 | |
| 68 | @Before |
| 69 | public void setUp() { |
| 70 | provider = new PtToPtIntentVirtualNetworkProvider(); |
| 71 | provider.providerRegistry = virtualNetworkRegistry; |
| 72 | final CoreService mockCoreService = createMock(CoreService.class); |
| 73 | provider.coreService = mockCoreService; |
| 74 | expect(mockCoreService.registerApplication(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID)) |
| 75 | .andReturn(APP_ID).anyTimes(); |
| 76 | replay(mockCoreService); |
| 77 | Intent.unbindIdGenerator(idGenerator); |
| 78 | Intent.bindIdGenerator(idGenerator); |
| 79 | |
| 80 | intentService = new TestIntentService(); |
| 81 | provider.intentService = intentService; |
| 82 | provider.activate(); |
| 83 | } |
| 84 | |
| 85 | @After |
| 86 | public void tearDown() { |
| 87 | provider.deactivate(); |
| 88 | provider.providerRegistry = null; |
| 89 | provider.coreService = null; |
| 90 | provider.intentService = null; |
| 91 | Intent.unbindIdGenerator(idGenerator); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void basics() { |
| 96 | assertNotNull("registration expected", provider); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test a null network identifier. |
| 101 | */ |
| 102 | @Test(expected = NullPointerException.class) |
| 103 | public void testCreateTunnelNullNetworkId() { |
| 104 | provider.createTunnel(null, null, null); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Test a null source connect point. |
| 109 | */ |
| 110 | @Test(expected = NullPointerException.class) |
| 111 | public void testCreateTunnelNullSrc() { |
| 112 | ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2)); |
| 113 | |
| 114 | provider.createTunnel(NetworkId.networkId(0), null, dst); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Test a null destination connect point. |
| 119 | */ |
| 120 | @Test(expected = NullPointerException.class) |
| 121 | public void testCreateTunnelNullDst() { |
| 122 | ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1)); |
| 123 | |
| 124 | provider.createTunnel(NetworkId.networkId(0), src, null); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Test creating/destroying a valid tunnel. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testCreateRemoveTunnel() { |
| 132 | NetworkId networkId = NetworkId.networkId(0); |
| 133 | ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1)); |
| 134 | ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2)); |
| 135 | |
| 136 | TunnelId tunnelId = provider.createTunnel(networkId, src, dst); |
| 137 | String key = String.format(PtToPtIntentVirtualNetworkProvider.KEY_FORMAT, |
| 138 | networkId.toString(), src.toString(), dst.toString()); |
| 139 | |
| 140 | assertEquals("TunnelId does not match as expected.", key, tunnelId.toString()); |
| 141 | provider.destroyTunnel(networkId, tunnelId); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Virtual network registry implementation for this test class. |
| 146 | */ |
| 147 | private class VirtualNetworkRegistryAdapter implements VirtualNetworkProviderRegistry { |
| 148 | private VirtualNetworkProvider provider; |
| 149 | |
| 150 | @Override |
| 151 | public VirtualNetworkProviderService register(VirtualNetworkProvider theProvider) { |
| 152 | this.provider = theProvider; |
| 153 | return new TestVirtualNetworkProviderService(theProvider); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void unregister(VirtualNetworkProvider theProvider) { |
| 158 | this.provider = null; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<ProviderId> getProviders() { |
| 163 | return null; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Virtual network provider service implementation for this test class. |
| 169 | */ |
| 170 | private class TestVirtualNetworkProviderService |
| 171 | extends AbstractProviderService<VirtualNetworkProvider> |
| 172 | implements VirtualNetworkProviderService { |
| 173 | |
| 174 | protected TestVirtualNetworkProviderService(VirtualNetworkProvider provider) { |
| 175 | super(provider); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Core service test class. |
| 182 | */ |
| 183 | private class TestCoreService extends CoreServiceAdapter { |
| 184 | |
| 185 | @Override |
| 186 | public IdGenerator getIdGenerator(String topic) { |
| 187 | return new IdGenerator() { |
| 188 | private AtomicLong counter = new AtomicLong(0); |
| 189 | |
| 190 | @Override |
| 191 | public long getNewId() { |
| 192 | return counter.getAndIncrement(); |
| 193 | } |
| 194 | }; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Represents a fake IntentService class that easily allows to store and |
| 200 | * retrieve intents without implementing the IntentService logic. |
| 201 | */ |
| 202 | private class TestIntentService extends IntentServiceAdapter { |
| 203 | |
| 204 | private Set<Intent> intents; |
| 205 | |
| 206 | public TestIntentService() { |
| 207 | intents = Sets.newHashSet(); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void submit(Intent intent) { |
| 212 | intents.add(intent); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void withdraw(Intent intent) { |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public IntentState getIntentState(Key intentKey) { |
| 221 | return IntentState.WITHDRAWN; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void purge(Intent intent) { |
| 226 | intents.remove(intent); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public long getIntentCount() { |
| 231 | return intents.size(); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Iterable<Intent> getIntents() { |
| 236 | return intents; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Intent getIntent(Key intentKey) { |
| 241 | for (Intent intent : intents) { |
| 242 | if (intent.key().equals(intentKey)) { |
| 243 | return intent; |
| 244 | } |
| 245 | } |
| 246 | return null; |
| 247 | } |
| 248 | } |
| 249 | } |