blob: 991a10e98681150d719f7b5667504fd62382e24a [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Brian O'Connor427a1762014-11-19 18:40:32 -080017
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080018import java.util.Arrays;
Ray Milkey43a28222015-02-23 13:57:58 -080019import java.util.Collection;
20import java.util.Collections;
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.TimeUnit;
Ray Milkey77a455f2015-03-27 10:08:17 -070026import java.util.stream.Collectors;
27import java.util.stream.IntStream;
Ray Milkey43a28222015-02-23 13:57:58 -080028
Brian O'Connor427a1762014-11-19 18:40:32 -080029import org.hamcrest.Description;
Brian O'Connor427a1762014-11-19 18:40:32 -080030import org.hamcrest.TypeSafeMatcher;
31import org.junit.After;
32import org.junit.Before;
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080033import org.junit.Ignore;
Brian O'Connor427a1762014-11-19 18:40:32 -080034import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.TestApplicationId;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.impl.TestCoreManager;
38import org.onosproject.event.impl.TestEventDispatcher;
39import org.onosproject.net.NetworkResource;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040import org.onosproject.net.intent.FlowRuleIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.intent.Intent;
42import org.onosproject.net.intent.IntentCompiler;
43import org.onosproject.net.intent.IntentEvent;
44import org.onosproject.net.intent.IntentEvent.Type;
45import org.onosproject.net.intent.IntentExtensionService;
46import org.onosproject.net.intent.IntentId;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.intent.IntentListener;
48import org.onosproject.net.intent.IntentService;
49import org.onosproject.net.intent.IntentState;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080050import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.resource.LinkResourceAllocations;
Jonathan Hart6a8fd1d2015-02-25 15:44:37 -080052import org.onosproject.store.trivial.impl.SimpleIntentStore;
Brian O'Connor427a1762014-11-19 18:40:32 -080053
Ray Milkey43a28222015-02-23 13:57:58 -080054import com.google.common.collect.HashMultimap;
Ray Milkey43a28222015-02-23 13:57:58 -080055import com.google.common.collect.Lists;
56import com.google.common.collect.Maps;
57import com.google.common.collect.Multimap;
58import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080059
Ray Milkeye9a3e222014-12-03 16:46:06 -080060import static org.hamcrest.MatcherAssert.assertThat;
61import static org.hamcrest.Matchers.hasSize;
Ray Milkey77a455f2015-03-27 10:08:17 -070062import static org.hamcrest.Matchers.is;
Ray Milkey43a28222015-02-23 13:57:58 -080063import static org.junit.Assert.assertEquals;
64import static org.junit.Assert.assertNotNull;
65import static org.junit.Assert.assertTrue;
Ray Milkey9f74c082015-02-11 15:40:16 -080066import static org.onlab.junit.TestTools.assertAfter;
Brian O'Connor427a1762014-11-19 18:40:32 -080067import static org.onlab.util.Tools.delay;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070068import static org.onosproject.net.intent.IntentState.*;
Ray Milkey43a28222015-02-23 13:57:58 -080069import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule;
70import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
Brian O'Connor427a1762014-11-19 18:40:32 -080071
72/**
73 * Test intent manager and transitions.
74 *
75 * TODO implement the following tests:
76 * - {submit, withdraw, update, replace} intent
Sho SHIMIZU34660962015-01-22 17:58:44 -080077 * - {submit, update, recompiling} intent with failed compilation
Brian O'Connor427a1762014-11-19 18:40:32 -080078 * - failed reservation
79 * - push timeout recovery
80 * - failed items recovery
81 *
82 * in general, verify intents store, flow store, and work queue
83 */
Ray Milkey1c166dc2015-03-02 13:32:39 -080084
Brian O'Connor427a1762014-11-19 18:40:32 -080085public class IntentManagerTest {
86
Pavlin Radoslavov35b4ecb2015-03-03 15:06:04 -080087 private static final int SUBMIT_TIMEOUT_MS = 1000;
Brian O'Connor427a1762014-11-19 18:40:32 -080088 private static final ApplicationId APPID = new TestApplicationId("manager-test");
89
90 private IntentManager manager;
91 private MockFlowRuleService flowRuleService;
92
93 protected IntentService service;
94 protected IntentExtensionService extensionService;
95 protected TestListener listener = new TestListener();
96 protected TestIntentCompiler compiler = new TestIntentCompiler();
Brian O'Connor427a1762014-11-19 18:40:32 -080097
Ray Milkeye9a3e222014-12-03 16:46:06 -080098 private static class TestListener implements IntentListener {
99 final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create();
100 Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap();
101
102 @Override
103 public void event(IntentEvent event) {
104 events.put(event.type(), event);
105 if (latchMap.containsKey(event.type())) {
106 latchMap.get(event.type()).countDown();
107 }
108 }
109
110 public int getCounts(IntentEvent.Type type) {
111 return events.get(type).size();
112 }
113
114 public void setLatch(int count, IntentEvent.Type type) {
115 latchMap.put(type, new CountDownLatch(count));
116 }
117
118 public void await(IntentEvent.Type type) {
119 try {
120 assertTrue("Timed out waiting for: " + type,
121 latchMap.get(type).await(5, TimeUnit.SECONDS));
122 } catch (InterruptedException e) {
123 e.printStackTrace();
124 }
125 }
126 }
127
128 private static class TestIntentTracker implements ObjectiveTrackerService {
129 private TopologyChangeDelegate delegate;
130 @Override
131 public void setDelegate(TopologyChangeDelegate delegate) {
132 this.delegate = delegate;
133 }
134
135 @Override
136 public void unsetDelegate(TopologyChangeDelegate delegate) {
137 if (delegate.equals(this.delegate)) {
138 this.delegate = null;
139 }
140 }
141
142 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800143 public void addTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800144 //TODO
145 }
146
147 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800148 public void removeTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800149 //TODO
150 }
151 }
152
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800153 private static class MockInstallableIntent extends FlowRuleIntent {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800154
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800155 public MockInstallableIntent() {
156 super(APPID, Arrays.asList(new MockFlowRule(100)));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800157 }
158 }
159
160 private static class TestIntentCompiler implements IntentCompiler<MockIntent> {
161 @Override
162 public List<Intent> compile(MockIntent intent, List<Intent> installable,
163 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800164 return Lists.newArrayList(new MockInstallableIntent());
Ray Milkeye9a3e222014-12-03 16:46:06 -0800165 }
166 }
167
Ray Milkey77a455f2015-03-27 10:08:17 -0700168 private static class TestIntentCompilerMultipleFlows implements IntentCompiler<MockIntent> {
169 @Override
170 public List<Intent> compile(MockIntent intent, List<Intent> installable,
171 Set<LinkResourceAllocations> resources) {
172
173 return IntStream.rangeClosed(1, 5)
174 .mapToObj(mock -> (new MockInstallableIntent()))
175 .collect(Collectors.toList());
176 }
177 }
178
179
Ray Milkeye9a3e222014-12-03 16:46:06 -0800180 private static class TestIntentCompilerError implements IntentCompiler<MockIntent> {
181 @Override
182 public List<Intent> compile(MockIntent intent, List<Intent> installable,
183 Set<LinkResourceAllocations> resources) {
184 throw new IntentCompilationException("Compilation always fails");
185 }
186 }
187
Ray Milkeye9a3e222014-12-03 16:46:06 -0800188 /**
Ray Milkey77a455f2015-03-27 10:08:17 -0700189 * Hamcrest matcher to check that a collection of Intents contains an
Ray Milkeye9a3e222014-12-03 16:46:06 -0800190 * Intent with the specified Intent Id.
191 */
192 public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> {
193 private final IntentId id;
194
195 public EntryForIntentMatcher(IntentId idValue) {
196 id = idValue;
197 }
198
199 @Override
200 public boolean matchesSafely(Collection<Intent> intents) {
201 for (Intent intent : intents) {
202 if (intent.id().equals(id)) {
203 return true;
204 }
205 }
206 return false;
207 }
208
209 @Override
210 public void describeTo(Description description) {
211 description.appendText("an intent with id \" ").
212 appendText(id.toString()).
213 appendText("\"");
214 }
215 }
216
217 private static EntryForIntentMatcher hasIntentWithId(IntentId id) {
218 return new EntryForIntentMatcher(id);
219 }
220
Brian O'Connor427a1762014-11-19 18:40:32 -0800221 @Before
222 public void setUp() {
223 manager = new IntentManager();
224 flowRuleService = new MockFlowRuleService();
225 manager.store = new SimpleIntentStore();
Brian O'Connor427a1762014-11-19 18:40:32 -0800226 manager.eventDispatcher = new TestEventDispatcher();
227 manager.trackerService = new TestIntentTracker();
228 manager.flowRuleService = flowRuleService;
Brian O'Connor520c0522014-11-23 23:50:47 -0800229 manager.coreService = new TestCoreManager();
Brian O'Connor427a1762014-11-19 18:40:32 -0800230 service = manager;
231 extensionService = manager;
232
233 manager.activate();
234 service.addListener(listener);
235 extensionService.registerCompiler(MockIntent.class, compiler);
Brian O'Connor427a1762014-11-19 18:40:32 -0800236
237 assertTrue("store should be empty",
238 Sets.newHashSet(service.getIntents()).isEmpty());
239 assertEquals(0L, flowRuleService.getFlowRuleCount());
240 }
241
Ray Milkey9f74c082015-02-11 15:40:16 -0800242 public void verifyState() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800243 // verify that all intents are parked and the batch operation is unblocked
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700244 Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED, CORRUPT);
Brian O'Connor427a1762014-11-19 18:40:32 -0800245 for (Intent i : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800246 IntentState state = service.getIntentState(i.key());
Brian O'Connor427a1762014-11-19 18:40:32 -0800247 assertTrue("Intent " + i.id() + " is in invalid state " + state,
248 parked.contains(state));
249 }
250 //the batch has not yet been removed when we receive the last event
251 // FIXME: this doesn't guarantee to avoid the race
Brian O'Connorb499b352015-02-03 16:46:15 -0800252
253 //FIXME
254// for (int tries = 0; tries < 10; tries++) {
255// if (manager.batchService.getPendingOperations().isEmpty()) {
256// break;
257// }
258// delay(10);
259// }
260// assertTrue("There are still pending batch operations.",
261// manager.batchService.getPendingOperations().isEmpty());
Brian O'Connor427a1762014-11-19 18:40:32 -0800262
Ray Milkey9f74c082015-02-11 15:40:16 -0800263 }
264
265 @After
266 public void tearDown() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800267 extensionService.unregisterCompiler(MockIntent.class);
Brian O'Connor427a1762014-11-19 18:40:32 -0800268 service.removeListener(listener);
269 manager.deactivate();
270 // TODO null the other refs?
271 }
272
273 @Test
274 public void submitIntent() {
275 flowRuleService.setFuture(true);
276
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800277 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800278 listener.setLatch(1, Type.INSTALLED);
279 Intent intent = new MockIntent(MockIntent.nextId());
280 service.submit(intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800281 listener.await(Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800282 listener.await(Type.INSTALLED);
283 assertEquals(1L, service.getIntentCount());
284 assertEquals(1L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800285 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800286 }
287
288 @Test
289 public void withdrawIntent() {
290 flowRuleService.setFuture(true);
291
292 listener.setLatch(1, Type.INSTALLED);
293 Intent intent = new MockIntent(MockIntent.nextId());
294 service.submit(intent);
295 listener.await(Type.INSTALLED);
296 assertEquals(1L, service.getIntentCount());
297 assertEquals(1L, flowRuleService.getFlowRuleCount());
298
299 listener.setLatch(1, Type.WITHDRAWN);
300 service.withdraw(intent);
301 listener.await(Type.WITHDRAWN);
Brian O'Connor427a1762014-11-19 18:40:32 -0800302 assertEquals(0L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800303 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800304 }
305
306 @Test
Ray Milkey0811bdd2015-03-11 10:21:55 -0700307 @Ignore("This is disabled because we are seeing intermittent failures on Jenkins")
Ray Milkey9f74c082015-02-11 15:40:16 -0800308 public void stressSubmitWithdrawUnique() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800309 flowRuleService.setFuture(true);
310
311 int count = 500;
Ray Milkey9f74c082015-02-11 15:40:16 -0800312 Intent[] intents = new Intent[count];
Brian O'Connor427a1762014-11-19 18:40:32 -0800313
Brian O'Connor427a1762014-11-19 18:40:32 -0800314 listener.setLatch(count, Type.WITHDRAWN);
315
Ray Milkey9f74c082015-02-11 15:40:16 -0800316 for (int i = 0; i < count; i++) {
317 intents[i] = new MockIntent(MockIntent.nextId());
318 service.submit(intents[i]);
319 }
320
321 for (int i = 0; i < count; i++) {
322 service.withdraw(intents[i]);
323 }
324
325 listener.await(Type.WITHDRAWN);
326 assertEquals(0L, flowRuleService.getFlowRuleCount());
327 verifyState();
328 }
329
330 @Test
331 public void stressSubmitWithdrawSame() {
332 flowRuleService.setFuture(true);
333
334 int count = 50;
335
Brian O'Connor427a1762014-11-19 18:40:32 -0800336 Intent intent = new MockIntent(MockIntent.nextId());
337 for (int i = 0; i < count; i++) {
338 service.submit(intent);
339 service.withdraw(intent);
340 }
341
Pavlin Radoslavov35b4ecb2015-03-03 15:06:04 -0800342 assertAfter(SUBMIT_TIMEOUT_MS, () -> {
Ray Milkey9f74c082015-02-11 15:40:16 -0800343 assertEquals(1L, service.getIntentCount());
344 assertEquals(0L, flowRuleService.getFlowRuleCount());
345 });
346 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800347 }
348
Ray Milkey9f74c082015-02-11 15:40:16 -0800349
Ray Milkey93508c22014-12-02 11:35:56 -0800350 /**
351 * Tests for proper behavior of installation of an intent that triggers
352 * a compilation error.
353 */
354 @Test
355 public void errorIntentCompile() {
356 final TestIntentCompilerError errorCompiler = new TestIntentCompilerError();
357 extensionService.registerCompiler(MockIntent.class, errorCompiler);
358 MockIntent intent = new MockIntent(MockIntent.nextId());
359 listener.setLatch(1, Type.INSTALL_REQ);
360 listener.setLatch(1, Type.FAILED);
361 service.submit(intent);
362 listener.await(Type.INSTALL_REQ);
363 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800364 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800365 }
366
367 /**
368 * Tests handling a future that contains an error as a result of
369 * installing an intent.
370 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800371 @Ignore("skipping until we fix update ordering problem")
Ray Milkey93508c22014-12-02 11:35:56 -0800372 @Test
373 public void errorIntentInstallFromFlows() {
374 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800375 flowRuleService.setFuture(false);
Ray Milkey93508c22014-12-02 11:35:56 -0800376 MockIntent intent = new MockIntent(id);
377 listener.setLatch(1, Type.FAILED);
378 listener.setLatch(1, Type.INSTALL_REQ);
379 service.submit(intent);
380 listener.await(Type.INSTALL_REQ);
Ray Milkey93508c22014-12-02 11:35:56 -0800381 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800382 // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED
383 // ... the updates come out of order
384 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800385 }
386
387 /**
Ray Milkeye9a3e222014-12-03 16:46:06 -0800388 * Tests handling a future that contains an unresolvable error as a result of
389 * installing an intent.
Brian O'Connor427a1762014-11-19 18:40:32 -0800390 */
Ray Milkeye9a3e222014-12-03 16:46:06 -0800391 @Test
392 public void errorIntentInstallNeverTrue() {
393 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800394 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800395 MockIntent intent = new MockIntent(id);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700396 listener.setLatch(1, Type.CORRUPT);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800397 listener.setLatch(1, Type.INSTALL_REQ);
398 service.submit(intent);
399 listener.await(Type.INSTALL_REQ);
400 // The delay here forces the retry loop in the intent manager to time out
401 delay(100);
Brian O'Connor5811ac22015-02-09 19:17:07 -0800402 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800403 service.withdraw(intent);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700404 listener.await(Type.CORRUPT);
Ray Milkey9f74c082015-02-11 15:40:16 -0800405 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800406 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800407
Ray Milkeye9a3e222014-12-03 16:46:06 -0800408 /**
409 * Tests that a compiler for a subclass of an intent that already has a
410 * compiler is automatically added.
411 */
412 @Test
413 public void intentSubclassCompile() {
414 class MockIntentSubclass extends MockIntent {
415 public MockIntentSubclass(Long number) {
416 super(number);
417 }
418 }
419 flowRuleService.setFuture(true);
420
421 listener.setLatch(1, Type.INSTALL_REQ);
422 listener.setLatch(1, Type.INSTALLED);
423 Intent intent = new MockIntentSubclass(MockIntent.nextId());
424 service.submit(intent);
425 listener.await(Type.INSTALL_REQ);
426 listener.await(Type.INSTALLED);
427 assertEquals(1L, service.getIntentCount());
428 assertEquals(1L, flowRuleService.getFlowRuleCount());
429
430 final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers =
431 extensionService.getCompilers();
432 assertEquals(2, compilers.size());
433 assertNotNull(compilers.get(MockIntentSubclass.class));
434 assertNotNull(compilers.get(MockIntent.class));
Ray Milkey9f74c082015-02-11 15:40:16 -0800435 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800436 }
437
438 /**
439 * Tests an intent with no compiler.
440 */
441 @Test
442 public void intentWithoutCompiler() {
443 class IntentNoCompiler extends Intent {
444 IntentNoCompiler() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700445 super(APPID, null, Collections.emptyList(),
446 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800447 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800448 }
449
Ray Milkeye9a3e222014-12-03 16:46:06 -0800450 Intent intent = new IntentNoCompiler();
451 listener.setLatch(1, Type.INSTALL_REQ);
452 listener.setLatch(1, Type.FAILED);
453 service.submit(intent);
454 listener.await(Type.INSTALL_REQ);
455 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800456 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800457 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800458
Ray Milkeye9a3e222014-12-03 16:46:06 -0800459 /**
460 * Tests an intent with no installer.
461 */
462 @Test
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700463 @Ignore //FIXME corrupt or failed?
Ray Milkeye9a3e222014-12-03 16:46:06 -0800464 public void intentWithoutInstaller() {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800465 MockIntent intent = new MockIntent(MockIntent.nextId());
466 listener.setLatch(1, Type.INSTALL_REQ);
467 listener.setLatch(1, Type.FAILED);
468 service.submit(intent);
469 listener.await(Type.INSTALL_REQ);
470 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800471 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800472 }
473
474 /**
475 * Tests that the intent fetching methods are correct.
476 */
477 @Test
478 public void testIntentFetching() {
479 List<Intent> intents;
480
481 flowRuleService.setFuture(true);
482
483 intents = Lists.newArrayList(service.getIntents());
484 assertThat(intents, hasSize(0));
485
486 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
487 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
488
489 listener.setLatch(2, Type.INSTALL_REQ);
490 listener.setLatch(2, Type.INSTALLED);
491 service.submit(intent1);
492 service.submit(intent2);
493 listener.await(Type.INSTALL_REQ);
494 listener.await(Type.INSTALL_REQ);
495 listener.await(Type.INSTALLED);
496 listener.await(Type.INSTALLED);
497
498 intents = Lists.newArrayList(service.getIntents());
499 assertThat(intents, hasSize(2));
500
501 assertThat(intents, hasIntentWithId(intent1.id()));
502 assertThat(intents, hasIntentWithId(intent2.id()));
Ray Milkey9f74c082015-02-11 15:40:16 -0800503 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800504 }
Ray Milkey77a455f2015-03-27 10:08:17 -0700505
506 /**
507 * Tests that removing all intents results in no flows remaining.
508 */
509 @Test
510 public void testFlowRemoval() {
511 List<Intent> intents;
512
513 flowRuleService.setFuture(true);
514
515 intents = Lists.newArrayList(service.getIntents());
516 assertThat(intents, hasSize(0));
517
518 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
519 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
520
521 listener.setLatch(1, Type.INSTALL_REQ);
522 listener.setLatch(1, Type.INSTALLED);
523
524 service.submit(intent1);
525 listener.await(Type.INSTALL_REQ);
526 listener.await(Type.INSTALLED);
527
528
529 listener.setLatch(1, Type.INSTALL_REQ);
530 listener.setLatch(1, Type.INSTALLED);
531
532 service.submit(intent2);
533 listener.await(Type.INSTALL_REQ);
534 listener.await(Type.INSTALLED);
535
536 assertThat(listener.getCounts(Type.INSTALLED), is(2));
537 assertThat(flowRuleService.getFlowRuleCount(), is(2));
538
539 listener.setLatch(1, Type.WITHDRAWN);
540 service.withdraw(intent1);
541 listener.await(Type.WITHDRAWN);
542
543 listener.setLatch(1, Type.WITHDRAWN);
544 service.withdraw(intent2);
545 listener.await(Type.WITHDRAWN);
546
547 assertThat(listener.getCounts(Type.WITHDRAWN), is(2));
548 assertThat(flowRuleService.getFlowRuleCount(), is(0));
549 }
550
551 /**
552 * Tests that an intent that fails installation results in no flows remaining.
553 */
554 @Test
555 @Ignore("Cleanup state is not yet implemented in the intent manager")
556 public void testFlowRemovalInstallError() {
557 final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows();
558 extensionService.registerCompiler(MockIntent.class, errorCompiler);
559 List<Intent> intents;
560
561 flowRuleService.setFuture(true);
562 flowRuleService.setErrorFlow(3);
563
564 intents = Lists.newArrayList(service.getIntents());
565 assertThat(intents, hasSize(0));
566
567 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
568
569 listener.setLatch(1, Type.INSTALL_REQ);
570 listener.setLatch(1, Type.FAILED);
571
572 service.submit(intent1);
573 listener.await(Type.INSTALL_REQ);
574 listener.await(Type.FAILED);
575
576 assertThat(listener.getCounts(Type.FAILED), is(1));
577 assertThat(flowRuleService.getFlowRuleCount(), is(0));
578 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800579}