blob: 3b59e9a758e28b3db4b3f1516672a7fe58814502 [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;
Yoonseon Han9e043792017-05-03 15:43:33 -070040import org.onosproject.incubator.net.virtual.VirtualNetworkIntentStore;
Brian Stanke11f6d532016-07-05 16:17:59 -040041import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
42import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
Yoonseon Han9e043792017-05-03 15:43:33 -070043import org.onosproject.incubator.store.virtual.impl.SimpleVirtualIntentStore;
Brian Stanke11f6d532016-07-05 16:17:59 -040044import org.onosproject.net.ConnectPoint;
45import org.onosproject.net.DefaultPort;
46import org.onosproject.net.EncapsulationType;
47import org.onosproject.net.Link;
48import org.onosproject.net.NetTestTools;
49import org.onosproject.net.Port;
50import org.onosproject.net.PortNumber;
51import org.onosproject.net.TestDeviceParams;
52import org.onosproject.net.intent.Constraint;
53import org.onosproject.net.intent.FakeIntentManager;
54import org.onosproject.net.intent.FlowRuleIntent;
55import org.onosproject.net.intent.Intent;
56import org.onosproject.net.intent.IntentCompiler;
57import org.onosproject.net.intent.IntentEvent;
58import org.onosproject.net.intent.IntentExtensionService;
59import org.onosproject.net.intent.IntentListener;
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;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070066import org.onosproject.net.intent.WorkPartitionService;
67import org.onosproject.net.intent.WorkPartitionServiceAdapter;
Brian Stanke11f6d532016-07-05 16:17:59 -040068import org.onosproject.net.intent.constraint.EncapsulationConstraint;
69import org.onosproject.store.service.TestStorageService;
70
71import java.util.ArrayList;
72import java.util.Collections;
73import java.util.List;
74import java.util.concurrent.Semaphore;
75import java.util.concurrent.TimeUnit;
76import java.util.concurrent.atomic.AtomicLong;
77
78import static org.junit.Assert.*;
79
80/**
81 * Junit tests for VirtualNetworkIntentService.
82 */
Thomas Vachuskad832fc52017-01-11 13:13:06 -080083@Ignore("deprecated prototype implementation")
yoonseon214963b2016-11-21 15:41:07 -080084public class VirtualNetworkIntentManagerTest extends TestDeviceParams {
Brian Stanke11f6d532016-07-05 16:17:59 -040085
86 private final String tenantIdValue1 = "TENANT_ID1";
87 private static final ApplicationId APP_ID =
Brian Stanke682c19e2016-08-02 09:41:40 -040088 new TestApplicationId("MyAppId");
Brian Stanke11f6d532016-07-05 16:17:59 -040089
90 private ConnectPoint cp1;
91 private ConnectPoint cp2;
92 private ConnectPoint cp3;
93 private ConnectPoint cp4;
94 private ConnectPoint cp5;
95 private ConnectPoint cp6;
96 private VirtualLink link1;
97 private VirtualLink link2;
98 private VirtualLink link3;
99 private VirtualLink link4;
100 private VirtualLink link5;
101 private VirtualLink link6;
102
103 private VirtualNetworkManager manager;
104 private static DistributedVirtualNetworkStore virtualNetworkManagerStore;
Yoonseon Han9e043792017-05-03 15:43:33 -0700105 private VirtualNetworkIntentStore intentStore;
Brian Stanke11f6d532016-07-05 16:17:59 -0400106 private CoreService coreService;
107 private TestableIntentService intentService = new FakeIntentManager();
yoonseon214963b2016-11-21 15:41:07 -0800108 private VirtualNetworkIntentManager vnetIntentService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400109 private TestIntentCompiler compiler = new TestIntentCompiler();
110 private IntentExtensionService intentExtensionService;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700111 private WorkPartitionService workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400112 private ServiceDirectory testDirectory;
113 private TestListener listener = new TestListener();
Brian Stanke11f6d532016-07-05 16:17:59 -0400114 private static final int MAX_WAIT_TIME = 5;
115 private static final int MAX_PERMITS = 1;
116 private static Semaphore created;
117 private static Semaphore withdrawn;
118 private static Semaphore purged;
119
120 @Before
121 public void setUp() throws Exception {
122 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
Yoonseon Han9e043792017-05-03 15:43:33 -0700123 intentStore = new SimpleVirtualIntentStore();
Brian Stanke11f6d532016-07-05 16:17:59 -0400124
yoonseon214963b2016-11-21 15:41:07 -0800125 coreService = new VirtualNetworkIntentManagerTest.TestCoreService();
Brian Stanke11f6d532016-07-05 16:17:59 -0400126
Thomas Vachuska2048c1f2017-05-10 19:32:22 -0700127 MockIdGenerator.cleanBind();
Brian Stanke11f6d532016-07-05 16:17:59 -0400128
yoonseonc6a69272017-01-12 18:22:20 -0800129 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400130 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
131 virtualNetworkManagerStore.activate();
132
133 manager = new VirtualNetworkManager();
134 manager.store = virtualNetworkManagerStore;
135 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
136 manager.intentService = intentService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400137 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);
yoonseonc6a69272017-01-12 18:22:20 -0800153 TestUtils.setField(manager, "serviceDirectory", testDirectory);
154
155 manager.activate();
Brian Stanke11f6d532016-07-05 16:17:59 -0400156 }
157
158 @After
159 public void tearDown() {
160 virtualNetworkManagerStore.deactivate();
161 manager.deactivate();
162 NetTestTools.injectEventDispatcher(manager, null);
Thomas Vachuska2048c1f2017-05-10 19:32:22 -0700163 MockIdGenerator.unbind();
Brian Stanke11f6d532016-07-05 16:17:59 -0400164 intentService.removeListener(listener);
165 created = null;
166 withdrawn = null;
167 purged = null;
168 }
169
170 /**
171 * Method to create the virtual network for further testing.
172 *
173 * @return virtual network
174 */
175 private VirtualNetwork setupVirtualNetworkTopology() {
176 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
177 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
178 VirtualDevice virtualDevice1 =
179 manager.createVirtualDevice(virtualNetwork.id(), DID1);
180 VirtualDevice virtualDevice2 =
181 manager.createVirtualDevice(virtualNetwork.id(), DID2);
182 VirtualDevice virtualDevice3 =
183 manager.createVirtualDevice(virtualNetwork.id(), DID3);
184 VirtualDevice virtualDevice4 =
185 manager.createVirtualDevice(virtualNetwork.id(), DID4);
186
187 Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400188 cp1 = new ConnectPoint(virtualDevice1.id(), port1.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700189 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1);
Brian Stanke11f6d532016-07-05 16:17:59 -0400190
191 Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400192 cp2 = new ConnectPoint(virtualDevice1.id(), port2.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700193 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2);
Brian Stanke11f6d532016-07-05 16:17:59 -0400194
195 Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400196 cp3 = new ConnectPoint(virtualDevice2.id(), port3.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700197 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3);
Brian Stanke11f6d532016-07-05 16:17:59 -0400198
199 Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400200 cp4 = new ConnectPoint(virtualDevice2.id(), port4.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700201 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4);
Brian Stanke11f6d532016-07-05 16:17:59 -0400202
203 Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400204 cp5 = new ConnectPoint(virtualDevice3.id(), port5.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700205 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5);
Brian Stanke11f6d532016-07-05 16:17:59 -0400206
207 Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400208 cp6 = new ConnectPoint(virtualDevice3.id(), port6.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700209 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6);
Brian Stanke11f6d532016-07-05 16:17:59 -0400210
211 link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
212 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
213 link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
214 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
215 link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
216 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
217 link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
218 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
219
yoonseonc6a69272017-01-12 18:22:20 -0800220 vnetIntentService = new VirtualNetworkIntentManager(manager, virtualNetwork.id());
Brian Stanke11f6d532016-07-05 16:17:59 -0400221 vnetIntentService.intentService = intentService;
Yoonseon Han9e043792017-05-03 15:43:33 -0700222 vnetIntentService.intentStore = intentStore;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700223 vnetIntentService.partitionService = workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400224 return virtualNetwork;
225 }
226
227 /**
228 * Tests the submit(), withdraw(), and purge() methods.
229 */
230 @Test
231 public void testCreateAndRemoveIntent() {
232 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
233
234 Key intentKey = Key.of("test", APP_ID);
235
236 List<Constraint> constraints = new ArrayList<>();
237 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
238
239 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
240 .networkId(virtualNetwork.id())
241 .key(intentKey)
242 .appId(APP_ID)
243 .ingressPoint(cp1)
244 .egressPoint(cp5)
245 .constraints(constraints)
246 .build();
247 // Test the submit() method.
248 vnetIntentService.submit(virtualIntent);
249
250 // Wait for the both intents to go into an INSTALLED state.
251 try {
252 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
253 fail("Failed to wait for intent to get installed.");
254 }
255 } catch (InterruptedException e) {
256 fail("Semaphore exception during intent installation." + e.getMessage());
257 }
258
259 // Test the getIntentState() method
260 assertEquals("The intent state did not match as expected.", IntentState.INSTALLED,
261 vnetIntentService.getIntentState(virtualIntent.key()));
262
263 // Test the withdraw() method.
264 vnetIntentService.withdraw(virtualIntent);
265 // Wait for the both intents to go into a WITHDRAWN state.
266 try {
267 if (!withdrawn.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
268 fail("Failed to wait for intent to get withdrawn.");
269 }
270 } catch (InterruptedException e) {
271 fail("Semaphore exception during intent withdrawal." + e.getMessage());
272 }
273
274 // Test the getIntentState() method
275 assertEquals("The intent state did not match as expected.", IntentState.WITHDRAWN,
276 vnetIntentService.getIntentState(virtualIntent.key()));
277
278 // Test the purge() method.
279 vnetIntentService.purge(virtualIntent);
280 // Wait for the both intents to be removed/purged.
281 try {
282 if (!purged.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
283 fail("Failed to wait for intent to get purged.");
284 }
285 } catch (InterruptedException e) {
286 fail("Semaphore exception during intent purging." + e.getMessage());
287 }
288
289 }
290
291 /**
292 * Tests the getIntents, getIntent(), getIntentData(), getIntentCount(),
293 * isLocal() methods.
294 */
295 @Test
296 public void testGetIntents() {
297 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
298
299 Key intentKey = Key.of("test", APP_ID);
300
301 List<Constraint> constraints = new ArrayList<>();
302 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
303
304 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
305 .networkId(virtualNetwork.id())
306 .key(intentKey)
307 .appId(APP_ID)
308 .ingressPoint(cp1)
309 .egressPoint(cp5)
310 .constraints(constraints)
311 .build();
312 // Test the submit() method.
313 vnetIntentService.submit(virtualIntent);
314
315 // Wait for the both intents to go into an INSTALLED state.
316 try {
317 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
318 fail("Failed to wait for intent to get installed.");
319 }
320 } catch (InterruptedException e) {
321 fail("Semaphore exception during intent installation." + e.getMessage());
322 }
323
324 // Test the getIntents() method
325 assertEquals("The intents size did not match as expected.", 1,
326 Iterators.size(vnetIntentService.getIntents().iterator()));
327
328 // Test the getIntent() method
329 assertNotNull("The intent should have been found.", vnetIntentService.getIntent(virtualIntent.key()));
330
331 // Test the getIntentData() method
332 assertEquals("The intent data size did not match as expected.", 1,
333 Iterators.size(vnetIntentService.getIntentData().iterator()));
334
335 // Test the getIntentCount() method
336 assertEquals("The intent count did not match as expected.", 1,
337 vnetIntentService.getIntentCount());
338
339 // Test the isLocal() method
340 assertTrue("The intent should be local.", vnetIntentService.isLocal(virtualIntent.key()));
341
342 }
343
344 /**
345 * Test listener to listen for intent events.
346 */
347 private static class TestListener implements IntentListener {
348
349 @Override
350 public void event(IntentEvent event) {
351 switch (event.type()) {
352 case INSTALLED:
353 // Release one permit on the created semaphore since the Intent event was received.
Yoonseon Han9e043792017-05-03 15:43:33 -0700354// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
Brian Stanke11f6d532016-07-05 16:17:59 -0400355 created.release();
356 break;
357 case WITHDRAWN:
358 // Release one permit on the removed semaphore since the Intent event was received.
Yoonseon Han9e043792017-05-03 15:43:33 -0700359// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
Brian Stanke11f6d532016-07-05 16:17:59 -0400360 withdrawn.release();
361 break;
362 case PURGED:
363 // Release one permit on the purged semaphore since the Intent event was received.
364 purged.release();
365 break;
366 default:
367 break;
368 }
369 }
370 }
371
372 /**
373 * Core service test class.
374 */
375 private class TestCoreService extends CoreServiceAdapter {
376
377 @Override
378 public IdGenerator getIdGenerator(String topic) {
379 return new IdGenerator() {
380 private AtomicLong counter = new AtomicLong(0);
381
382 @Override
383 public long getNewId() {
384 return counter.getAndIncrement();
385 }
386 };
387 }
388 }
389
390 private static class TestIntentCompiler implements IntentCompiler<VirtualNetworkIntent> {
391 @Override
392 public List<Intent> compile(VirtualNetworkIntent intent, List<Intent> installable) {
393 return Lists.newArrayList(new MockInstallableIntent());
394 }
395 }
396
397 private static class MockInstallableIntent extends FlowRuleIntent {
398
399 public MockInstallableIntent() {
400 super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
401 }
402 }
Yoonseon Han9e043792017-05-03 15:43:33 -0700403
404// private void addOrUpdateIntent(Intent intent, IntentState state) {
405// checkNotNull(intent, "Intent cannot be null");
406// IntentData intentData = intentStore.(intent.key());
407// if (intentData == null) {
408// intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
409// } else {
410// intentData = new IntentData(intent, state, intentData.version());
411// }
412// intentKeyIntentDataMap.put(intent.key(), intentData);
413// }
Brian Stanke11f6d532016-07-05 16:17:59 -0400414}