blob: f7434d4d6baab8c1529a1363bb80187d8e769587 [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
127 virtualNetworkManagerStore.setCoreService(coreService);
yoonseon214963b2016-11-21 15:41:07 -0800128 TestUtils.setField(coreService, "coreService", new VirtualNetworkIntentManagerTest.TestCoreService());
Brian Stanke11f6d532016-07-05 16:17:59 -0400129 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
130 virtualNetworkManagerStore.activate();
131
132 manager = new VirtualNetworkManager();
133 manager.store = virtualNetworkManagerStore;
134 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
135 manager.intentService = intentService;
136 manager.activate();
137 intentService.addListener(listener);
138
139 // Register a compiler and an installer both setup for success.
140 intentExtensionService = intentService;
141 intentExtensionService.registerCompiler(VirtualNetworkIntent.class, compiler);
142
143 created = new Semaphore(0, true);
144 withdrawn = new Semaphore(0, true);
145 purged = new Semaphore(0, true);
146
Madan Jampani3b8101a2016-09-15 13:22:01 -0700147 workPartitionService = new WorkPartitionServiceAdapter();
Brian Stanke11f6d532016-07-05 16:17:59 -0400148 testDirectory = new TestServiceDirectory()
149 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
150 .add(IntentService.class, intentService)
Madan Jampani3b8101a2016-09-15 13:22:01 -0700151 .add(WorkPartitionService.class, workPartitionService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400152 BaseResource.setServiceDirectory(testDirectory);
153 }
154
155 @After
156 public void tearDown() {
157 virtualNetworkManagerStore.deactivate();
158 manager.deactivate();
159 NetTestTools.injectEventDispatcher(manager, null);
160 Intent.unbindIdGenerator(idGenerator);
161 intentService.removeListener(listener);
162 created = null;
163 withdrawn = null;
164 purged = null;
165 }
166
167 /**
168 * Method to create the virtual network for further testing.
169 *
170 * @return virtual network
171 */
172 private VirtualNetwork setupVirtualNetworkTopology() {
173 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
174 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
175 VirtualDevice virtualDevice1 =
176 manager.createVirtualDevice(virtualNetwork.id(), DID1);
177 VirtualDevice virtualDevice2 =
178 manager.createVirtualDevice(virtualNetwork.id(), DID2);
179 VirtualDevice virtualDevice3 =
180 manager.createVirtualDevice(virtualNetwork.id(), DID3);
181 VirtualDevice virtualDevice4 =
182 manager.createVirtualDevice(virtualNetwork.id(), DID4);
183
184 Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400185 cp1 = new ConnectPoint(virtualDevice1.id(), port1.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700186 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1);
Brian Stanke11f6d532016-07-05 16:17:59 -0400187
188 Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400189 cp2 = new ConnectPoint(virtualDevice1.id(), port2.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700190 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2);
Brian Stanke11f6d532016-07-05 16:17:59 -0400191
192 Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400193 cp3 = new ConnectPoint(virtualDevice2.id(), port3.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700194 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3);
Brian Stanke11f6d532016-07-05 16:17:59 -0400195
196 Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400197 cp4 = new ConnectPoint(virtualDevice2.id(), port4.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700198 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4);
Brian Stanke11f6d532016-07-05 16:17:59 -0400199
200 Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400201 cp5 = new ConnectPoint(virtualDevice3.id(), port5.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700202 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5);
Brian Stanke11f6d532016-07-05 16:17:59 -0400203
204 Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400205 cp6 = new ConnectPoint(virtualDevice3.id(), port6.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700206 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6);
Brian Stanke11f6d532016-07-05 16:17:59 -0400207
208 link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
209 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
210 link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
211 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
212 link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
213 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
214 link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
215 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
216
yoonseon214963b2016-11-21 15:41:07 -0800217 vnetIntentService = new VirtualNetworkIntentManager(manager, virtualNetwork, testDirectory);
Brian Stanke11f6d532016-07-05 16:17:59 -0400218 vnetIntentService.intentService = intentService;
219 vnetIntentService.store = virtualNetworkManagerStore;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700220 vnetIntentService.partitionService = workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400221 return virtualNetwork;
222 }
223
224 /**
225 * Tests the submit(), withdraw(), and purge() methods.
226 */
227 @Test
228 public void testCreateAndRemoveIntent() {
229 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
230
231 Key intentKey = Key.of("test", APP_ID);
232
233 List<Constraint> constraints = new ArrayList<>();
234 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
235
236 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
237 .networkId(virtualNetwork.id())
238 .key(intentKey)
239 .appId(APP_ID)
240 .ingressPoint(cp1)
241 .egressPoint(cp5)
242 .constraints(constraints)
243 .build();
244 // Test the submit() method.
245 vnetIntentService.submit(virtualIntent);
246
247 // Wait for the both intents to go into an INSTALLED state.
248 try {
249 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
250 fail("Failed to wait for intent to get installed.");
251 }
252 } catch (InterruptedException e) {
253 fail("Semaphore exception during intent installation." + e.getMessage());
254 }
255
256 // Test the getIntentState() method
257 assertEquals("The intent state did not match as expected.", IntentState.INSTALLED,
258 vnetIntentService.getIntentState(virtualIntent.key()));
259
260 // Test the withdraw() method.
261 vnetIntentService.withdraw(virtualIntent);
262 // Wait for the both intents to go into a WITHDRAWN state.
263 try {
264 if (!withdrawn.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
265 fail("Failed to wait for intent to get withdrawn.");
266 }
267 } catch (InterruptedException e) {
268 fail("Semaphore exception during intent withdrawal." + e.getMessage());
269 }
270
271 // Test the getIntentState() method
272 assertEquals("The intent state did not match as expected.", IntentState.WITHDRAWN,
273 vnetIntentService.getIntentState(virtualIntent.key()));
274
275 // Test the purge() method.
276 vnetIntentService.purge(virtualIntent);
277 // Wait for the both intents to be removed/purged.
278 try {
279 if (!purged.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
280 fail("Failed to wait for intent to get purged.");
281 }
282 } catch (InterruptedException e) {
283 fail("Semaphore exception during intent purging." + e.getMessage());
284 }
285
286 }
287
288 /**
289 * Tests the getIntents, getIntent(), getIntentData(), getIntentCount(),
290 * isLocal() methods.
291 */
292 @Test
293 public void testGetIntents() {
294 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
295
296 Key intentKey = Key.of("test", APP_ID);
297
298 List<Constraint> constraints = new ArrayList<>();
299 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
300
301 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
302 .networkId(virtualNetwork.id())
303 .key(intentKey)
304 .appId(APP_ID)
305 .ingressPoint(cp1)
306 .egressPoint(cp5)
307 .constraints(constraints)
308 .build();
309 // Test the submit() method.
310 vnetIntentService.submit(virtualIntent);
311
312 // Wait for the both intents to go into an INSTALLED state.
313 try {
314 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
315 fail("Failed to wait for intent to get installed.");
316 }
317 } catch (InterruptedException e) {
318 fail("Semaphore exception during intent installation." + e.getMessage());
319 }
320
321 // Test the getIntents() method
322 assertEquals("The intents size did not match as expected.", 1,
323 Iterators.size(vnetIntentService.getIntents().iterator()));
324
325 // Test the getIntent() method
326 assertNotNull("The intent should have been found.", vnetIntentService.getIntent(virtualIntent.key()));
327
328 // Test the getIntentData() method
329 assertEquals("The intent data size did not match as expected.", 1,
330 Iterators.size(vnetIntentService.getIntentData().iterator()));
331
332 // Test the getIntentCount() method
333 assertEquals("The intent count did not match as expected.", 1,
334 vnetIntentService.getIntentCount());
335
336 // Test the isLocal() method
337 assertTrue("The intent should be local.", vnetIntentService.isLocal(virtualIntent.key()));
338
339 }
340
341 /**
342 * Test listener to listen for intent events.
343 */
344 private static class TestListener implements IntentListener {
345
346 @Override
347 public void event(IntentEvent event) {
348 switch (event.type()) {
349 case INSTALLED:
350 // Release one permit on the created semaphore since the Intent event was received.
351 virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
352 created.release();
353 break;
354 case WITHDRAWN:
355 // Release one permit on the removed semaphore since the Intent event was received.
356 virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
357 withdrawn.release();
358 break;
359 case PURGED:
360 // Release one permit on the purged semaphore since the Intent event was received.
361 purged.release();
362 break;
363 default:
364 break;
365 }
366 }
367 }
368
369 /**
370 * Core service test class.
371 */
372 private class TestCoreService extends CoreServiceAdapter {
373
374 @Override
375 public IdGenerator getIdGenerator(String topic) {
376 return new IdGenerator() {
377 private AtomicLong counter = new AtomicLong(0);
378
379 @Override
380 public long getNewId() {
381 return counter.getAndIncrement();
382 }
383 };
384 }
385 }
386
387 private static class TestIntentCompiler implements IntentCompiler<VirtualNetworkIntent> {
388 @Override
389 public List<Intent> compile(VirtualNetworkIntent intent, List<Intent> installable) {
390 return Lists.newArrayList(new MockInstallableIntent());
391 }
392 }
393
394 private static class MockInstallableIntent extends FlowRuleIntent {
395
396 public MockInstallableIntent() {
397 super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
398 }
399 }
400}