blob: 55232d10b4eea1a5bce98dbc20e42170fbe7a92e [file] [log] [blame]
Brian Stanke11f6d532016-07-05 16:17:59 -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.Iterators;
20import com.google.common.collect.Lists;
21import org.junit.After;
22import org.junit.Before;
Thomas Vachuskad832fc52017-01-11 13:13:06 -080023import org.junit.Ignore;
Brian Stanke11f6d532016-07-05 16:17:59 -040024import org.junit.Test;
25import org.onlab.junit.TestUtils;
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.osgi.TestServiceDirectory;
28import org.onlab.rest.BaseResource;
29import org.onosproject.TestApplicationId;
30import org.onosproject.common.event.impl.TestEventDispatcher;
31import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.core.CoreServiceAdapter;
34import org.onosproject.core.IdGenerator;
35import org.onosproject.incubator.net.virtual.TenantId;
36import org.onosproject.incubator.net.virtual.VirtualDevice;
37import org.onosproject.incubator.net.virtual.VirtualLink;
38import org.onosproject.incubator.net.virtual.VirtualNetwork;
39import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
40import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
41import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
42import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DefaultPort;
44import org.onosproject.net.EncapsulationType;
45import org.onosproject.net.Link;
46import org.onosproject.net.NetTestTools;
47import org.onosproject.net.Port;
48import org.onosproject.net.PortNumber;
49import org.onosproject.net.TestDeviceParams;
50import org.onosproject.net.intent.Constraint;
51import org.onosproject.net.intent.FakeIntentManager;
52import org.onosproject.net.intent.FlowRuleIntent;
53import org.onosproject.net.intent.Intent;
54import org.onosproject.net.intent.IntentCompiler;
55import org.onosproject.net.intent.IntentEvent;
56import org.onosproject.net.intent.IntentExtensionService;
57import org.onosproject.net.intent.IntentListener;
Madan Jampani3b8101a2016-09-15 13:22:01 -070058import org.onosproject.net.intent.WorkPartitionService;
59import org.onosproject.net.intent.WorkPartitionServiceAdapter;
Brian Stanke11f6d532016-07-05 16:17:59 -040060import org.onosproject.net.intent.IntentService;
61import org.onosproject.net.intent.IntentState;
62import org.onosproject.net.intent.IntentTestsMocks;
63import org.onosproject.net.intent.Key;
64import org.onosproject.net.intent.MockIdGenerator;
65import org.onosproject.net.intent.TestableIntentService;
66import org.onosproject.net.intent.constraint.EncapsulationConstraint;
67import org.onosproject.store.service.TestStorageService;
68
69import java.util.ArrayList;
70import java.util.Collections;
71import java.util.List;
72import java.util.concurrent.Semaphore;
73import java.util.concurrent.TimeUnit;
74import java.util.concurrent.atomic.AtomicLong;
75
76import static org.junit.Assert.*;
77
78/**
79 * Junit tests for VirtualNetworkIntentService.
80 */
Thomas Vachuskad832fc52017-01-11 13:13:06 -080081@Ignore("deprecated prototype implementation")
yoonseon214963b2016-11-21 15:41:07 -080082public class VirtualNetworkIntentManagerTest extends TestDeviceParams {
Brian Stanke11f6d532016-07-05 16:17:59 -040083
84 private final String tenantIdValue1 = "TENANT_ID1";
85 private static final ApplicationId APP_ID =
Brian Stanke682c19e2016-08-02 09:41:40 -040086 new TestApplicationId("MyAppId");
Brian Stanke11f6d532016-07-05 16:17:59 -040087
88 private ConnectPoint cp1;
89 private ConnectPoint cp2;
90 private ConnectPoint cp3;
91 private ConnectPoint cp4;
92 private ConnectPoint cp5;
93 private ConnectPoint cp6;
94 private VirtualLink link1;
95 private VirtualLink link2;
96 private VirtualLink link3;
97 private VirtualLink link4;
98 private VirtualLink link5;
99 private VirtualLink link6;
100
101 private VirtualNetworkManager manager;
102 private static DistributedVirtualNetworkStore virtualNetworkManagerStore;
103 private CoreService coreService;
104 private TestableIntentService intentService = new FakeIntentManager();
yoonseon214963b2016-11-21 15:41:07 -0800105 private VirtualNetworkIntentManager vnetIntentService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400106 private TestIntentCompiler compiler = new TestIntentCompiler();
107 private IntentExtensionService intentExtensionService;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700108 private WorkPartitionService workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400109 private ServiceDirectory testDirectory;
110 private TestListener listener = new TestListener();
111 private IdGenerator idGenerator = new MockIdGenerator();
112 private static final int MAX_WAIT_TIME = 5;
113 private static final int MAX_PERMITS = 1;
114 private static Semaphore created;
115 private static Semaphore withdrawn;
116 private static Semaphore purged;
117
118 @Before
119 public void setUp() throws Exception {
120 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
121
yoonseon214963b2016-11-21 15:41:07 -0800122 coreService = new VirtualNetworkIntentManagerTest.TestCoreService();
Brian Stanke11f6d532016-07-05 16:17:59 -0400123
124 Intent.unbindIdGenerator(idGenerator);
125 Intent.bindIdGenerator(idGenerator);
126
yoonseonc6a69272017-01-12 18:22:20 -0800127 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400128 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
129 virtualNetworkManagerStore.activate();
130
131 manager = new VirtualNetworkManager();
132 manager.store = virtualNetworkManagerStore;
133 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
134 manager.intentService = intentService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400135 intentService.addListener(listener);
136
137 // Register a compiler and an installer both setup for success.
138 intentExtensionService = intentService;
139 intentExtensionService.registerCompiler(VirtualNetworkIntent.class, compiler);
140
141 created = new Semaphore(0, true);
142 withdrawn = new Semaphore(0, true);
143 purged = new Semaphore(0, true);
144
Madan Jampani3b8101a2016-09-15 13:22:01 -0700145 workPartitionService = new WorkPartitionServiceAdapter();
Brian Stanke11f6d532016-07-05 16:17:59 -0400146 testDirectory = new TestServiceDirectory()
147 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
148 .add(IntentService.class, intentService)
Madan Jampani3b8101a2016-09-15 13:22:01 -0700149 .add(WorkPartitionService.class, workPartitionService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400150 BaseResource.setServiceDirectory(testDirectory);
yoonseonc6a69272017-01-12 18:22:20 -0800151 TestUtils.setField(manager, "serviceDirectory", testDirectory);
152
153 manager.activate();
Brian Stanke11f6d532016-07-05 16:17:59 -0400154 }
155
156 @After
157 public void tearDown() {
158 virtualNetworkManagerStore.deactivate();
159 manager.deactivate();
160 NetTestTools.injectEventDispatcher(manager, null);
161 Intent.unbindIdGenerator(idGenerator);
162 intentService.removeListener(listener);
163 created = null;
164 withdrawn = null;
165 purged = null;
166 }
167
168 /**
169 * Method to create the virtual network for further testing.
170 *
171 * @return virtual network
172 */
173 private VirtualNetwork setupVirtualNetworkTopology() {
174 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
175 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
176 VirtualDevice virtualDevice1 =
177 manager.createVirtualDevice(virtualNetwork.id(), DID1);
178 VirtualDevice virtualDevice2 =
179 manager.createVirtualDevice(virtualNetwork.id(), DID2);
180 VirtualDevice virtualDevice3 =
181 manager.createVirtualDevice(virtualNetwork.id(), DID3);
182 VirtualDevice virtualDevice4 =
183 manager.createVirtualDevice(virtualNetwork.id(), DID4);
184
185 Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400186 cp1 = new ConnectPoint(virtualDevice1.id(), port1.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700187 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1);
Brian Stanke11f6d532016-07-05 16:17:59 -0400188
189 Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400190 cp2 = new ConnectPoint(virtualDevice1.id(), port2.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700191 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2);
Brian Stanke11f6d532016-07-05 16:17:59 -0400192
193 Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400194 cp3 = new ConnectPoint(virtualDevice2.id(), port3.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700195 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3);
Brian Stanke11f6d532016-07-05 16:17:59 -0400196
197 Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400198 cp4 = new ConnectPoint(virtualDevice2.id(), port4.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700199 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4);
Brian Stanke11f6d532016-07-05 16:17:59 -0400200
201 Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400202 cp5 = new ConnectPoint(virtualDevice3.id(), port5.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700203 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5);
Brian Stanke11f6d532016-07-05 16:17:59 -0400204
205 Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400206 cp6 = new ConnectPoint(virtualDevice3.id(), port6.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700207 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6);
Brian Stanke11f6d532016-07-05 16:17:59 -0400208
209 link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
210 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
211 link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
212 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
213 link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
214 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
215 link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
216 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
217
yoonseonc6a69272017-01-12 18:22:20 -0800218 vnetIntentService = new VirtualNetworkIntentManager(manager, virtualNetwork.id());
Brian Stanke11f6d532016-07-05 16:17:59 -0400219 vnetIntentService.intentService = intentService;
220 vnetIntentService.store = virtualNetworkManagerStore;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700221 vnetIntentService.partitionService = workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400222 return virtualNetwork;
223 }
224
225 /**
226 * Tests the submit(), withdraw(), and purge() methods.
227 */
228 @Test
229 public void testCreateAndRemoveIntent() {
230 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
231
232 Key intentKey = Key.of("test", APP_ID);
233
234 List<Constraint> constraints = new ArrayList<>();
235 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
236
237 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
238 .networkId(virtualNetwork.id())
239 .key(intentKey)
240 .appId(APP_ID)
241 .ingressPoint(cp1)
242 .egressPoint(cp5)
243 .constraints(constraints)
244 .build();
245 // Test the submit() method.
246 vnetIntentService.submit(virtualIntent);
247
248 // Wait for the both intents to go into an INSTALLED state.
249 try {
250 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
251 fail("Failed to wait for intent to get installed.");
252 }
253 } catch (InterruptedException e) {
254 fail("Semaphore exception during intent installation." + e.getMessage());
255 }
256
257 // Test the getIntentState() method
258 assertEquals("The intent state did not match as expected.", IntentState.INSTALLED,
259 vnetIntentService.getIntentState(virtualIntent.key()));
260
261 // Test the withdraw() method.
262 vnetIntentService.withdraw(virtualIntent);
263 // Wait for the both intents to go into a WITHDRAWN state.
264 try {
265 if (!withdrawn.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
266 fail("Failed to wait for intent to get withdrawn.");
267 }
268 } catch (InterruptedException e) {
269 fail("Semaphore exception during intent withdrawal." + e.getMessage());
270 }
271
272 // Test the getIntentState() method
273 assertEquals("The intent state did not match as expected.", IntentState.WITHDRAWN,
274 vnetIntentService.getIntentState(virtualIntent.key()));
275
276 // Test the purge() method.
277 vnetIntentService.purge(virtualIntent);
278 // Wait for the both intents to be removed/purged.
279 try {
280 if (!purged.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
281 fail("Failed to wait for intent to get purged.");
282 }
283 } catch (InterruptedException e) {
284 fail("Semaphore exception during intent purging." + e.getMessage());
285 }
286
287 }
288
289 /**
290 * Tests the getIntents, getIntent(), getIntentData(), getIntentCount(),
291 * isLocal() methods.
292 */
293 @Test
294 public void testGetIntents() {
295 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
296
297 Key intentKey = Key.of("test", APP_ID);
298
299 List<Constraint> constraints = new ArrayList<>();
300 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
301
302 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
303 .networkId(virtualNetwork.id())
304 .key(intentKey)
305 .appId(APP_ID)
306 .ingressPoint(cp1)
307 .egressPoint(cp5)
308 .constraints(constraints)
309 .build();
310 // Test the submit() method.
311 vnetIntentService.submit(virtualIntent);
312
313 // Wait for the both intents to go into an INSTALLED state.
314 try {
315 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
316 fail("Failed to wait for intent to get installed.");
317 }
318 } catch (InterruptedException e) {
319 fail("Semaphore exception during intent installation." + e.getMessage());
320 }
321
322 // Test the getIntents() method
323 assertEquals("The intents size did not match as expected.", 1,
324 Iterators.size(vnetIntentService.getIntents().iterator()));
325
326 // Test the getIntent() method
327 assertNotNull("The intent should have been found.", vnetIntentService.getIntent(virtualIntent.key()));
328
329 // Test the getIntentData() method
330 assertEquals("The intent data size did not match as expected.", 1,
331 Iterators.size(vnetIntentService.getIntentData().iterator()));
332
333 // Test the getIntentCount() method
334 assertEquals("The intent count did not match as expected.", 1,
335 vnetIntentService.getIntentCount());
336
337 // Test the isLocal() method
338 assertTrue("The intent should be local.", vnetIntentService.isLocal(virtualIntent.key()));
339
340 }
341
342 /**
343 * Test listener to listen for intent events.
344 */
345 private static class TestListener implements IntentListener {
346
347 @Override
348 public void event(IntentEvent event) {
349 switch (event.type()) {
350 case INSTALLED:
351 // Release one permit on the created semaphore since the Intent event was received.
352 virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
353 created.release();
354 break;
355 case WITHDRAWN:
356 // Release one permit on the removed semaphore since the Intent event was received.
357 virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
358 withdrawn.release();
359 break;
360 case PURGED:
361 // Release one permit on the purged semaphore since the Intent event was received.
362 purged.release();
363 break;
364 default:
365 break;
366 }
367 }
368 }
369
370 /**
371 * Core service test class.
372 */
373 private class TestCoreService extends CoreServiceAdapter {
374
375 @Override
376 public IdGenerator getIdGenerator(String topic) {
377 return new IdGenerator() {
378 private AtomicLong counter = new AtomicLong(0);
379
380 @Override
381 public long getNewId() {
382 return counter.getAndIncrement();
383 }
384 };
385 }
386 }
387
388 private static class TestIntentCompiler implements IntentCompiler<VirtualNetworkIntent> {
389 @Override
390 public List<Intent> compile(VirtualNetworkIntent intent, List<Intent> installable) {
391 return Lists.newArrayList(new MockInstallableIntent());
392 }
393 }
394
395 private static class MockInstallableIntent extends FlowRuleIntent {
396
397 public MockInstallableIntent() {
398 super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
399 }
400 }
401}