blob: 60521b43d0b8416a15b09a6d7ae5afe805260872 [file] [log] [blame]
Brian Stanke11f6d532016-07-05 16:17:59 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke11f6d532016-07-05 16:17:59 -04003 *
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());
Brian Stanke11f6d532016-07-05 16:17:59 -0400136 intentService.addListener(listener);
137
138 // Register a compiler and an installer both setup for success.
139 intentExtensionService = intentService;
140 intentExtensionService.registerCompiler(VirtualNetworkIntent.class, compiler);
141
142 created = new Semaphore(0, true);
143 withdrawn = new Semaphore(0, true);
144 purged = new Semaphore(0, true);
145
Madan Jampani3b8101a2016-09-15 13:22:01 -0700146 workPartitionService = new WorkPartitionServiceAdapter();
Brian Stanke11f6d532016-07-05 16:17:59 -0400147 testDirectory = new TestServiceDirectory()
148 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
149 .add(IntentService.class, intentService)
Madan Jampani3b8101a2016-09-15 13:22:01 -0700150 .add(WorkPartitionService.class, workPartitionService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400151 BaseResource.setServiceDirectory(testDirectory);
yoonseonc6a69272017-01-12 18:22:20 -0800152 TestUtils.setField(manager, "serviceDirectory", testDirectory);
153
154 manager.activate();
Brian Stanke11f6d532016-07-05 16:17:59 -0400155 }
156
157 @After
158 public void tearDown() {
159 virtualNetworkManagerStore.deactivate();
160 manager.deactivate();
161 NetTestTools.injectEventDispatcher(manager, null);
Thomas Vachuska2048c1f2017-05-10 19:32:22 -0700162 MockIdGenerator.unbind();
Brian Stanke11f6d532016-07-05 16:17:59 -0400163 intentService.removeListener(listener);
164 created = null;
165 withdrawn = null;
166 purged = null;
167 }
168
169 /**
170 * Method to create the virtual network for further testing.
171 *
172 * @return virtual network
173 */
174 private VirtualNetwork setupVirtualNetworkTopology() {
175 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
176 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
177 VirtualDevice virtualDevice1 =
178 manager.createVirtualDevice(virtualNetwork.id(), DID1);
179 VirtualDevice virtualDevice2 =
180 manager.createVirtualDevice(virtualNetwork.id(), DID2);
181 VirtualDevice virtualDevice3 =
182 manager.createVirtualDevice(virtualNetwork.id(), DID3);
183 VirtualDevice virtualDevice4 =
184 manager.createVirtualDevice(virtualNetwork.id(), DID4);
185
186 Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400187 cp1 = new ConnectPoint(virtualDevice1.id(), port1.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700188 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1);
Brian Stanke11f6d532016-07-05 16:17:59 -0400189
190 Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400191 cp2 = new ConnectPoint(virtualDevice1.id(), port2.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700192 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2);
Brian Stanke11f6d532016-07-05 16:17:59 -0400193
194 Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400195 cp3 = new ConnectPoint(virtualDevice2.id(), port3.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700196 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3);
Brian Stanke11f6d532016-07-05 16:17:59 -0400197
198 Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400199 cp4 = new ConnectPoint(virtualDevice2.id(), port4.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700200 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4);
Brian Stanke11f6d532016-07-05 16:17:59 -0400201
202 Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400203 cp5 = new ConnectPoint(virtualDevice3.id(), port5.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700204 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5);
Brian Stanke11f6d532016-07-05 16:17:59 -0400205
206 Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400207 cp6 = new ConnectPoint(virtualDevice3.id(), port6.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700208 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6);
Brian Stanke11f6d532016-07-05 16:17:59 -0400209
210 link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
211 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
212 link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
213 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
214 link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
215 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
216 link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
217 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
218
yoonseonc6a69272017-01-12 18:22:20 -0800219 vnetIntentService = new VirtualNetworkIntentManager(manager, virtualNetwork.id());
Yoonseon Han9e043792017-05-03 15:43:33 -0700220 vnetIntentService.intentStore = intentStore;
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.
Yoonseon Han9e043792017-05-03 15:43:33 -0700351// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
Brian Stanke11f6d532016-07-05 16:17:59 -0400352 created.release();
353 break;
354 case WITHDRAWN:
355 // Release one permit on the removed semaphore since the Intent event was received.
Yoonseon Han9e043792017-05-03 15:43:33 -0700356// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
Brian Stanke11f6d532016-07-05 16:17:59 -0400357 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 }
Yoonseon Han9e043792017-05-03 15:43:33 -0700400
401// private void addOrUpdateIntent(Intent intent, IntentState state) {
402// checkNotNull(intent, "Intent cannot be null");
403// IntentData intentData = intentStore.(intent.key());
404// if (intentData == null) {
405// intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
406// } else {
407// intentData = new IntentData(intent, state, intentData.version());
408// }
409// intentKeyIntentDataMap.put(intent.key(), intentData);
410// }
Brian Stanke11f6d532016-07-05 16:17:59 -0400411}