blob: 0695d4e1e9742ed20f4e7def9a33c2e20d73515a [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());
Brian Stanke11f6d532016-07-05 16:17:59 -0400220 vnetIntentService.intentService = intentService;
Yoonseon Han9e043792017-05-03 15:43:33 -0700221 vnetIntentService.intentStore = intentStore;
Madan Jampani3b8101a2016-09-15 13:22:01 -0700222 vnetIntentService.partitionService = workPartitionService;
Brian Stanke11f6d532016-07-05 16:17:59 -0400223 return virtualNetwork;
224 }
225
226 /**
227 * Tests the submit(), withdraw(), and purge() methods.
228 */
229 @Test
230 public void testCreateAndRemoveIntent() {
231 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
232
233 Key intentKey = Key.of("test", APP_ID);
234
235 List<Constraint> constraints = new ArrayList<>();
236 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
237
238 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
239 .networkId(virtualNetwork.id())
240 .key(intentKey)
241 .appId(APP_ID)
242 .ingressPoint(cp1)
243 .egressPoint(cp5)
244 .constraints(constraints)
245 .build();
246 // Test the submit() method.
247 vnetIntentService.submit(virtualIntent);
248
249 // Wait for the both intents to go into an INSTALLED state.
250 try {
251 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
252 fail("Failed to wait for intent to get installed.");
253 }
254 } catch (InterruptedException e) {
255 fail("Semaphore exception during intent installation." + e.getMessage());
256 }
257
258 // Test the getIntentState() method
259 assertEquals("The intent state did not match as expected.", IntentState.INSTALLED,
260 vnetIntentService.getIntentState(virtualIntent.key()));
261
262 // Test the withdraw() method.
263 vnetIntentService.withdraw(virtualIntent);
264 // Wait for the both intents to go into a WITHDRAWN state.
265 try {
266 if (!withdrawn.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
267 fail("Failed to wait for intent to get withdrawn.");
268 }
269 } catch (InterruptedException e) {
270 fail("Semaphore exception during intent withdrawal." + e.getMessage());
271 }
272
273 // Test the getIntentState() method
274 assertEquals("The intent state did not match as expected.", IntentState.WITHDRAWN,
275 vnetIntentService.getIntentState(virtualIntent.key()));
276
277 // Test the purge() method.
278 vnetIntentService.purge(virtualIntent);
279 // Wait for the both intents to be removed/purged.
280 try {
281 if (!purged.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
282 fail("Failed to wait for intent to get purged.");
283 }
284 } catch (InterruptedException e) {
285 fail("Semaphore exception during intent purging." + e.getMessage());
286 }
287
288 }
289
290 /**
291 * Tests the getIntents, getIntent(), getIntentData(), getIntentCount(),
292 * isLocal() methods.
293 */
294 @Test
295 public void testGetIntents() {
296 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
297
298 Key intentKey = Key.of("test", APP_ID);
299
300 List<Constraint> constraints = new ArrayList<>();
301 constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
302
303 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
304 .networkId(virtualNetwork.id())
305 .key(intentKey)
306 .appId(APP_ID)
307 .ingressPoint(cp1)
308 .egressPoint(cp5)
309 .constraints(constraints)
310 .build();
311 // Test the submit() method.
312 vnetIntentService.submit(virtualIntent);
313
314 // Wait for the both intents to go into an INSTALLED state.
315 try {
316 if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
317 fail("Failed to wait for intent to get installed.");
318 }
319 } catch (InterruptedException e) {
320 fail("Semaphore exception during intent installation." + e.getMessage());
321 }
322
323 // Test the getIntents() method
324 assertEquals("The intents size did not match as expected.", 1,
325 Iterators.size(vnetIntentService.getIntents().iterator()));
326
327 // Test the getIntent() method
328 assertNotNull("The intent should have been found.", vnetIntentService.getIntent(virtualIntent.key()));
329
330 // Test the getIntentData() method
331 assertEquals("The intent data size did not match as expected.", 1,
332 Iterators.size(vnetIntentService.getIntentData().iterator()));
333
334 // Test the getIntentCount() method
335 assertEquals("The intent count did not match as expected.", 1,
336 vnetIntentService.getIntentCount());
337
338 // Test the isLocal() method
339 assertTrue("The intent should be local.", vnetIntentService.isLocal(virtualIntent.key()));
340
341 }
342
343 /**
344 * Test listener to listen for intent events.
345 */
346 private static class TestListener implements IntentListener {
347
348 @Override
349 public void event(IntentEvent event) {
350 switch (event.type()) {
351 case INSTALLED:
352 // Release one permit on the created semaphore since the Intent event was received.
Yoonseon Han9e043792017-05-03 15:43:33 -0700353// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.INSTALLED);
Brian Stanke11f6d532016-07-05 16:17:59 -0400354 created.release();
355 break;
356 case WITHDRAWN:
357 // Release one permit on the removed semaphore since the Intent event was received.
Yoonseon Han9e043792017-05-03 15:43:33 -0700358// virtualNetworkManagerStore.addOrUpdateIntent(event.subject(), IntentState.WITHDRAWN);
Brian Stanke11f6d532016-07-05 16:17:59 -0400359 withdrawn.release();
360 break;
361 case PURGED:
362 // Release one permit on the purged semaphore since the Intent event was received.
363 purged.release();
364 break;
365 default:
366 break;
367 }
368 }
369 }
370
371 /**
372 * Core service test class.
373 */
374 private class TestCoreService extends CoreServiceAdapter {
375
376 @Override
377 public IdGenerator getIdGenerator(String topic) {
378 return new IdGenerator() {
379 private AtomicLong counter = new AtomicLong(0);
380
381 @Override
382 public long getNewId() {
383 return counter.getAndIncrement();
384 }
385 };
386 }
387 }
388
389 private static class TestIntentCompiler implements IntentCompiler<VirtualNetworkIntent> {
390 @Override
391 public List<Intent> compile(VirtualNetworkIntent intent, List<Intent> installable) {
392 return Lists.newArrayList(new MockInstallableIntent());
393 }
394 }
395
396 private static class MockInstallableIntent extends FlowRuleIntent {
397
398 public MockInstallableIntent() {
399 super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
400 }
401 }
Yoonseon Han9e043792017-05-03 15:43:33 -0700402
403// private void addOrUpdateIntent(Intent intent, IntentState state) {
404// checkNotNull(intent, "Intent cannot be null");
405// IntentData intentData = intentStore.(intent.key());
406// if (intentData == null) {
407// intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
408// } else {
409// intentData = new IntentData(intent, state, intentData.version());
410// }
411// intentKeyIntentDataMap.put(intent.key(), intentData);
412// }
Brian Stanke11f6d532016-07-05 16:17:59 -0400413}