blob: 4bc96d53e138a0f3a5915a7a7d52f23e893df277 [file] [log] [blame]
Brian Stanke9a108972016-04-11 15:25:17 -04001/*
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.Sets;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.TestApplicationId;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.core.CoreServiceAdapter;
27import org.onosproject.core.IdGenerator;
28import org.onosproject.incubator.net.tunnel.TunnelId;
29import org.onosproject.incubator.net.virtual.NetworkId;
30import org.onosproject.incubator.net.virtual.VirtualNetworkProvider;
31import org.onosproject.incubator.net.virtual.VirtualNetworkProviderRegistry;
32import org.onosproject.incubator.net.virtual.VirtualNetworkProviderService;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentService;
38import org.onosproject.net.intent.IntentServiceAdapter;
39import org.onosproject.net.intent.IntentState;
40import org.onosproject.net.intent.Key;
41import org.onosproject.net.intent.MockIdGenerator;
42import org.onosproject.net.provider.AbstractProviderService;
43import org.onosproject.net.provider.ProviderId;
44
45import java.util.Set;
46import java.util.concurrent.atomic.AtomicLong;
47
48import static org.easymock.EasyMock.*;
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertNotNull;
51
52/**
53 * Junit tests for PtToPtIntentVirtualNetworkProvider.
54 */
55public 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 }
Brian Stanke4d579882016-04-22 13:28:46 -0400177
178 @Override
179 public void tunnelUp(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
180
181 }
182
183 @Override
184 public void tunnelDown(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
185
186 }
Brian Stanke9a108972016-04-11 15:25:17 -0400187 }
188
189
190 /**
191 * Core service test class.
192 */
193 private class TestCoreService extends CoreServiceAdapter {
194
195 @Override
196 public IdGenerator getIdGenerator(String topic) {
197 return new IdGenerator() {
198 private AtomicLong counter = new AtomicLong(0);
199
200 @Override
201 public long getNewId() {
202 return counter.getAndIncrement();
203 }
204 };
205 }
206 }
207
208 /**
209 * Represents a fake IntentService class that easily allows to store and
210 * retrieve intents without implementing the IntentService logic.
211 */
212 private class TestIntentService extends IntentServiceAdapter {
213
214 private Set<Intent> intents;
215
216 public TestIntentService() {
217 intents = Sets.newHashSet();
218 }
219
220 @Override
221 public void submit(Intent intent) {
222 intents.add(intent);
223 }
224
225 @Override
226 public void withdraw(Intent intent) {
227 }
228
229 @Override
230 public IntentState getIntentState(Key intentKey) {
231 return IntentState.WITHDRAWN;
232 }
233
234 @Override
235 public void purge(Intent intent) {
236 intents.remove(intent);
237 }
238
239 @Override
240 public long getIntentCount() {
241 return intents.size();
242 }
243
244 @Override
245 public Iterable<Intent> getIntents() {
246 return intents;
247 }
248
249 @Override
250 public Intent getIntent(Key intentKey) {
251 for (Intent intent : intents) {
252 if (intent.key().equals(intentKey)) {
253 return intent;
254 }
255 }
256 return null;
257 }
258 }
259}