blob: bdb3e8db6a7d4887b7c49c763b4a23c7610e269b [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
Brian O'Connor3c58e962015-04-28 23:21:51 -070018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
22import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080023import org.hamcrest.Description;
Brian O'Connor427a1762014-11-19 18:40:32 -080024import org.hamcrest.TypeSafeMatcher;
25import org.junit.After;
26import org.junit.Before;
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080027import org.junit.Ignore;
Brian O'Connor427a1762014-11-19 18:40:32 -080028import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.TestApplicationId;
Brian O'Connor3c58e962015-04-28 23:21:51 -070030import org.onosproject.cfg.ComponentConfigAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.impl.TestCoreManager;
33import org.onosproject.event.impl.TestEventDispatcher;
34import org.onosproject.net.NetworkResource;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080035import org.onosproject.net.intent.FlowRuleIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentCompiler;
38import org.onosproject.net.intent.IntentEvent;
39import org.onosproject.net.intent.IntentEvent.Type;
40import org.onosproject.net.intent.IntentExtensionService;
41import org.onosproject.net.intent.IntentId;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.intent.IntentListener;
43import org.onosproject.net.intent.IntentService;
44import org.onosproject.net.intent.IntentState;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080045import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.resource.LinkResourceAllocations;
Jonathan Hart6a8fd1d2015-02-25 15:44:37 -080047import org.onosproject.store.trivial.impl.SimpleIntentStore;
Brian O'Connor427a1762014-11-19 18:40:32 -080048
Brian O'Connor3c58e962015-04-28 23:21:51 -070049import java.util.Arrays;
50import java.util.Collection;
51import java.util.Collections;
52import java.util.List;
53import java.util.Map;
54import java.util.Set;
55import java.util.concurrent.CountDownLatch;
56import java.util.concurrent.TimeUnit;
57import java.util.stream.Collectors;
58import java.util.stream.IntStream;
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;
Brian O'Connor3c58e962015-04-28 23:21:51 -070063import static org.junit.Assert.*;
Ray Milkey9f74c082015-02-11 15:40:16 -080064import static org.onlab.junit.TestTools.assertAfter;
Brian O'Connor427a1762014-11-19 18:40:32 -080065import static org.onlab.util.Tools.delay;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070066import static org.onosproject.net.intent.IntentState.*;
Ray Milkey43a28222015-02-23 13:57:58 -080067import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule;
68import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
Brian O'Connor427a1762014-11-19 18:40:32 -080069
70/**
71 * Test intent manager and transitions.
72 *
73 * TODO implement the following tests:
74 * - {submit, withdraw, update, replace} intent
Sho SHIMIZU34660962015-01-22 17:58:44 -080075 * - {submit, update, recompiling} intent with failed compilation
Brian O'Connor427a1762014-11-19 18:40:32 -080076 * - failed reservation
77 * - push timeout recovery
78 * - failed items recovery
79 *
80 * in general, verify intents store, flow store, and work queue
81 */
Ray Milkey1c166dc2015-03-02 13:32:39 -080082
Brian O'Connor427a1762014-11-19 18:40:32 -080083public class IntentManagerTest {
84
Pavlin Radoslavov35b4ecb2015-03-03 15:06:04 -080085 private static final int SUBMIT_TIMEOUT_MS = 1000;
Brian O'Connor427a1762014-11-19 18:40:32 -080086 private static final ApplicationId APPID = new TestApplicationId("manager-test");
87
88 private IntentManager manager;
89 private MockFlowRuleService flowRuleService;
90
91 protected IntentService service;
92 protected IntentExtensionService extensionService;
93 protected TestListener listener = new TestListener();
94 protected TestIntentCompiler compiler = new TestIntentCompiler();
Brian O'Connor427a1762014-11-19 18:40:32 -080095
Ray Milkeye9a3e222014-12-03 16:46:06 -080096 private static class TestListener implements IntentListener {
97 final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create();
98 Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap();
99
100 @Override
101 public void event(IntentEvent event) {
102 events.put(event.type(), event);
103 if (latchMap.containsKey(event.type())) {
104 latchMap.get(event.type()).countDown();
105 }
106 }
107
108 public int getCounts(IntentEvent.Type type) {
109 return events.get(type).size();
110 }
111
112 public void setLatch(int count, IntentEvent.Type type) {
113 latchMap.put(type, new CountDownLatch(count));
114 }
115
116 public void await(IntentEvent.Type type) {
117 try {
118 assertTrue("Timed out waiting for: " + type,
119 latchMap.get(type).await(5, TimeUnit.SECONDS));
120 } catch (InterruptedException e) {
121 e.printStackTrace();
122 }
123 }
124 }
125
126 private static class TestIntentTracker implements ObjectiveTrackerService {
127 private TopologyChangeDelegate delegate;
128 @Override
129 public void setDelegate(TopologyChangeDelegate delegate) {
130 this.delegate = delegate;
131 }
132
133 @Override
134 public void unsetDelegate(TopologyChangeDelegate delegate) {
135 if (delegate.equals(this.delegate)) {
136 this.delegate = null;
137 }
138 }
139
140 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800141 public void addTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800142 //TODO
143 }
144
145 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800146 public void removeTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800147 //TODO
148 }
149 }
150
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800151 private static class MockInstallableIntent extends FlowRuleIntent {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800152
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800153 public MockInstallableIntent() {
154 super(APPID, Arrays.asList(new MockFlowRule(100)));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800155 }
156 }
157
158 private static class TestIntentCompiler implements IntentCompiler<MockIntent> {
159 @Override
160 public List<Intent> compile(MockIntent intent, List<Intent> installable,
161 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800162 return Lists.newArrayList(new MockInstallableIntent());
Ray Milkeye9a3e222014-12-03 16:46:06 -0800163 }
164 }
165
Ray Milkey77a455f2015-03-27 10:08:17 -0700166 private static class TestIntentCompilerMultipleFlows implements IntentCompiler<MockIntent> {
167 @Override
168 public List<Intent> compile(MockIntent intent, List<Intent> installable,
169 Set<LinkResourceAllocations> resources) {
170
171 return IntStream.rangeClosed(1, 5)
172 .mapToObj(mock -> (new MockInstallableIntent()))
173 .collect(Collectors.toList());
174 }
175 }
176
177
Ray Milkeye9a3e222014-12-03 16:46:06 -0800178 private static class TestIntentCompilerError implements IntentCompiler<MockIntent> {
179 @Override
180 public List<Intent> compile(MockIntent intent, List<Intent> installable,
181 Set<LinkResourceAllocations> resources) {
182 throw new IntentCompilationException("Compilation always fails");
183 }
184 }
185
Ray Milkeye9a3e222014-12-03 16:46:06 -0800186 /**
Ray Milkey77a455f2015-03-27 10:08:17 -0700187 * Hamcrest matcher to check that a collection of Intents contains an
Ray Milkeye9a3e222014-12-03 16:46:06 -0800188 * Intent with the specified Intent Id.
189 */
190 public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> {
191 private final IntentId id;
192
193 public EntryForIntentMatcher(IntentId idValue) {
194 id = idValue;
195 }
196
197 @Override
198 public boolean matchesSafely(Collection<Intent> intents) {
199 for (Intent intent : intents) {
200 if (intent.id().equals(id)) {
201 return true;
202 }
203 }
204 return false;
205 }
206
207 @Override
208 public void describeTo(Description description) {
209 description.appendText("an intent with id \" ").
210 appendText(id.toString()).
211 appendText("\"");
212 }
213 }
214
215 private static EntryForIntentMatcher hasIntentWithId(IntentId id) {
216 return new EntryForIntentMatcher(id);
217 }
218
Brian O'Connor427a1762014-11-19 18:40:32 -0800219 @Before
220 public void setUp() {
221 manager = new IntentManager();
222 flowRuleService = new MockFlowRuleService();
223 manager.store = new SimpleIntentStore();
Brian O'Connor427a1762014-11-19 18:40:32 -0800224 manager.eventDispatcher = new TestEventDispatcher();
225 manager.trackerService = new TestIntentTracker();
226 manager.flowRuleService = flowRuleService;
Brian O'Connor520c0522014-11-23 23:50:47 -0800227 manager.coreService = new TestCoreManager();
Brian O'Connor427a1762014-11-19 18:40:32 -0800228 service = manager;
229 extensionService = manager;
230
231 manager.activate();
232 service.addListener(listener);
233 extensionService.registerCompiler(MockIntent.class, compiler);
Brian O'Connor427a1762014-11-19 18:40:32 -0800234
235 assertTrue("store should be empty",
236 Sets.newHashSet(service.getIntents()).isEmpty());
237 assertEquals(0L, flowRuleService.getFlowRuleCount());
238 }
239
Ray Milkey9f74c082015-02-11 15:40:16 -0800240 public void verifyState() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800241 // verify that all intents are parked and the batch operation is unblocked
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700242 Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED, CORRUPT);
Brian O'Connor427a1762014-11-19 18:40:32 -0800243 for (Intent i : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800244 IntentState state = service.getIntentState(i.key());
Brian O'Connor427a1762014-11-19 18:40:32 -0800245 assertTrue("Intent " + i.id() + " is in invalid state " + state,
246 parked.contains(state));
247 }
248 //the batch has not yet been removed when we receive the last event
249 // FIXME: this doesn't guarantee to avoid the race
Brian O'Connorb499b352015-02-03 16:46:15 -0800250
251 //FIXME
252// for (int tries = 0; tries < 10; tries++) {
253// if (manager.batchService.getPendingOperations().isEmpty()) {
254// break;
255// }
256// delay(10);
257// }
258// assertTrue("There are still pending batch operations.",
259// manager.batchService.getPendingOperations().isEmpty());
Brian O'Connor427a1762014-11-19 18:40:32 -0800260
Ray Milkey9f74c082015-02-11 15:40:16 -0800261 }
262
263 @After
264 public void tearDown() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800265 extensionService.unregisterCompiler(MockIntent.class);
Brian O'Connor427a1762014-11-19 18:40:32 -0800266 service.removeListener(listener);
267 manager.deactivate();
268 // TODO null the other refs?
269 }
270
271 @Test
272 public void submitIntent() {
273 flowRuleService.setFuture(true);
274
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800275 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800276 listener.setLatch(1, Type.INSTALLED);
277 Intent intent = new MockIntent(MockIntent.nextId());
278 service.submit(intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800279 listener.await(Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800280 listener.await(Type.INSTALLED);
281 assertEquals(1L, service.getIntentCount());
282 assertEquals(1L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800283 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800284 }
285
286 @Test
287 public void withdrawIntent() {
288 flowRuleService.setFuture(true);
289
290 listener.setLatch(1, Type.INSTALLED);
291 Intent intent = new MockIntent(MockIntent.nextId());
292 service.submit(intent);
293 listener.await(Type.INSTALLED);
294 assertEquals(1L, service.getIntentCount());
295 assertEquals(1L, flowRuleService.getFlowRuleCount());
296
297 listener.setLatch(1, Type.WITHDRAWN);
298 service.withdraw(intent);
299 listener.await(Type.WITHDRAWN);
Brian O'Connor427a1762014-11-19 18:40:32 -0800300 assertEquals(0L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800301 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800302 }
303
304 @Test
Ray Milkey0811bdd2015-03-11 10:21:55 -0700305 @Ignore("This is disabled because we are seeing intermittent failures on Jenkins")
Ray Milkey9f74c082015-02-11 15:40:16 -0800306 public void stressSubmitWithdrawUnique() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800307 flowRuleService.setFuture(true);
308
309 int count = 500;
Ray Milkey9f74c082015-02-11 15:40:16 -0800310 Intent[] intents = new Intent[count];
Brian O'Connor427a1762014-11-19 18:40:32 -0800311
Brian O'Connor427a1762014-11-19 18:40:32 -0800312 listener.setLatch(count, Type.WITHDRAWN);
313
Ray Milkey9f74c082015-02-11 15:40:16 -0800314 for (int i = 0; i < count; i++) {
315 intents[i] = new MockIntent(MockIntent.nextId());
316 service.submit(intents[i]);
317 }
318
319 for (int i = 0; i < count; i++) {
320 service.withdraw(intents[i]);
321 }
322
323 listener.await(Type.WITHDRAWN);
324 assertEquals(0L, flowRuleService.getFlowRuleCount());
325 verifyState();
326 }
327
328 @Test
329 public void stressSubmitWithdrawSame() {
330 flowRuleService.setFuture(true);
331
332 int count = 50;
333
Brian O'Connor427a1762014-11-19 18:40:32 -0800334 Intent intent = new MockIntent(MockIntent.nextId());
335 for (int i = 0; i < count; i++) {
336 service.submit(intent);
337 service.withdraw(intent);
338 }
339
Pavlin Radoslavov35b4ecb2015-03-03 15:06:04 -0800340 assertAfter(SUBMIT_TIMEOUT_MS, () -> {
Ray Milkey9f74c082015-02-11 15:40:16 -0800341 assertEquals(1L, service.getIntentCount());
342 assertEquals(0L, flowRuleService.getFlowRuleCount());
343 });
344 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800345 }
346
Ray Milkey9f74c082015-02-11 15:40:16 -0800347
Ray Milkey93508c22014-12-02 11:35:56 -0800348 /**
349 * Tests for proper behavior of installation of an intent that triggers
350 * a compilation error.
351 */
352 @Test
353 public void errorIntentCompile() {
354 final TestIntentCompilerError errorCompiler = new TestIntentCompilerError();
355 extensionService.registerCompiler(MockIntent.class, errorCompiler);
356 MockIntent intent = new MockIntent(MockIntent.nextId());
357 listener.setLatch(1, Type.INSTALL_REQ);
358 listener.setLatch(1, Type.FAILED);
359 service.submit(intent);
360 listener.await(Type.INSTALL_REQ);
361 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800362 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800363 }
364
365 /**
366 * Tests handling a future that contains an error as a result of
367 * installing an intent.
368 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800369 @Ignore("skipping until we fix update ordering problem")
Ray Milkey93508c22014-12-02 11:35:56 -0800370 @Test
371 public void errorIntentInstallFromFlows() {
372 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800373 flowRuleService.setFuture(false);
Ray Milkey93508c22014-12-02 11:35:56 -0800374 MockIntent intent = new MockIntent(id);
375 listener.setLatch(1, Type.FAILED);
376 listener.setLatch(1, Type.INSTALL_REQ);
377 service.submit(intent);
378 listener.await(Type.INSTALL_REQ);
Ray Milkey93508c22014-12-02 11:35:56 -0800379 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800380 // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED
381 // ... the updates come out of order
382 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800383 }
384
385 /**
Ray Milkeye9a3e222014-12-03 16:46:06 -0800386 * Tests handling a future that contains an unresolvable error as a result of
387 * installing an intent.
Brian O'Connor427a1762014-11-19 18:40:32 -0800388 */
Ray Milkeye9a3e222014-12-03 16:46:06 -0800389 @Test
390 public void errorIntentInstallNeverTrue() {
391 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800392 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800393 MockIntent intent = new MockIntent(id);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700394 listener.setLatch(1, Type.CORRUPT);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800395 listener.setLatch(1, Type.INSTALL_REQ);
396 service.submit(intent);
397 listener.await(Type.INSTALL_REQ);
398 // The delay here forces the retry loop in the intent manager to time out
399 delay(100);
Brian O'Connor5811ac22015-02-09 19:17:07 -0800400 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800401 service.withdraw(intent);
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700402 listener.await(Type.CORRUPT);
Ray Milkey9f74c082015-02-11 15:40:16 -0800403 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800404 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800405
Ray Milkeye9a3e222014-12-03 16:46:06 -0800406 /**
407 * Tests that a compiler for a subclass of an intent that already has a
408 * compiler is automatically added.
409 */
410 @Test
411 public void intentSubclassCompile() {
412 class MockIntentSubclass extends MockIntent {
413 public MockIntentSubclass(Long number) {
414 super(number);
415 }
416 }
417 flowRuleService.setFuture(true);
418
419 listener.setLatch(1, Type.INSTALL_REQ);
420 listener.setLatch(1, Type.INSTALLED);
421 Intent intent = new MockIntentSubclass(MockIntent.nextId());
422 service.submit(intent);
423 listener.await(Type.INSTALL_REQ);
424 listener.await(Type.INSTALLED);
425 assertEquals(1L, service.getIntentCount());
426 assertEquals(1L, flowRuleService.getFlowRuleCount());
427
428 final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers =
429 extensionService.getCompilers();
430 assertEquals(2, compilers.size());
431 assertNotNull(compilers.get(MockIntentSubclass.class));
432 assertNotNull(compilers.get(MockIntent.class));
Ray Milkey9f74c082015-02-11 15:40:16 -0800433 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800434 }
435
436 /**
437 * Tests an intent with no compiler.
438 */
439 @Test
440 public void intentWithoutCompiler() {
441 class IntentNoCompiler extends Intent {
442 IntentNoCompiler() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700443 super(APPID, null, Collections.emptyList(),
444 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800445 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800446 }
447
Ray Milkeye9a3e222014-12-03 16:46:06 -0800448 Intent intent = new IntentNoCompiler();
449 listener.setLatch(1, Type.INSTALL_REQ);
450 listener.setLatch(1, Type.FAILED);
451 service.submit(intent);
452 listener.await(Type.INSTALL_REQ);
453 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800454 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800455 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800456
Ray Milkeye9a3e222014-12-03 16:46:06 -0800457 /**
458 * Tests an intent with no installer.
459 */
460 @Test
461 public void intentWithoutInstaller() {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800462 MockIntent intent = new MockIntent(MockIntent.nextId());
463 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700464 listener.setLatch(1, Type.CORRUPT);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800465 service.submit(intent);
466 listener.await(Type.INSTALL_REQ);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700467 listener.await(Type.CORRUPT);
Ray Milkey9f74c082015-02-11 15:40:16 -0800468 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800469 }
470
471 /**
472 * Tests that the intent fetching methods are correct.
473 */
474 @Test
475 public void testIntentFetching() {
476 List<Intent> intents;
477
478 flowRuleService.setFuture(true);
479
480 intents = Lists.newArrayList(service.getIntents());
481 assertThat(intents, hasSize(0));
482
483 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
484 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
485
486 listener.setLatch(2, Type.INSTALL_REQ);
487 listener.setLatch(2, Type.INSTALLED);
488 service.submit(intent1);
489 service.submit(intent2);
490 listener.await(Type.INSTALL_REQ);
491 listener.await(Type.INSTALL_REQ);
492 listener.await(Type.INSTALLED);
493 listener.await(Type.INSTALLED);
494
495 intents = Lists.newArrayList(service.getIntents());
496 assertThat(intents, hasSize(2));
497
498 assertThat(intents, hasIntentWithId(intent1.id()));
499 assertThat(intents, hasIntentWithId(intent2.id()));
Ray Milkey9f74c082015-02-11 15:40:16 -0800500 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800501 }
Ray Milkey77a455f2015-03-27 10:08:17 -0700502
503 /**
504 * Tests that removing all intents results in no flows remaining.
505 */
506 @Test
507 public void testFlowRemoval() {
508 List<Intent> intents;
509
510 flowRuleService.setFuture(true);
511
512 intents = Lists.newArrayList(service.getIntents());
513 assertThat(intents, hasSize(0));
514
515 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
516 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
517
518 listener.setLatch(1, Type.INSTALL_REQ);
519 listener.setLatch(1, Type.INSTALLED);
520
521 service.submit(intent1);
522 listener.await(Type.INSTALL_REQ);
523 listener.await(Type.INSTALLED);
524
525
526 listener.setLatch(1, Type.INSTALL_REQ);
527 listener.setLatch(1, Type.INSTALLED);
528
529 service.submit(intent2);
530 listener.await(Type.INSTALL_REQ);
531 listener.await(Type.INSTALLED);
532
533 assertThat(listener.getCounts(Type.INSTALLED), is(2));
534 assertThat(flowRuleService.getFlowRuleCount(), is(2));
535
536 listener.setLatch(1, Type.WITHDRAWN);
537 service.withdraw(intent1);
538 listener.await(Type.WITHDRAWN);
539
540 listener.setLatch(1, Type.WITHDRAWN);
541 service.withdraw(intent2);
542 listener.await(Type.WITHDRAWN);
543
544 assertThat(listener.getCounts(Type.WITHDRAWN), is(2));
545 assertThat(flowRuleService.getFlowRuleCount(), is(0));
546 }
547
548 /**
Brian O'Connor3c58e962015-04-28 23:21:51 -0700549 * Test failure to install an intent, then succeed on retry via IntentCleanup.
550 */
551 @Test
552 public void testCorruptCleanup() {
553 IntentCleanup cleanup = new IntentCleanup();
554 cleanup.service = manager;
555 cleanup.store = manager.store;
556 cleanup.cfgService = new ComponentConfigAdapter();
557
558 try {
559 cleanup.activate();
560
561 final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows();
562 extensionService.registerCompiler(MockIntent.class, errorCompiler);
563 List<Intent> intents;
564
565 flowRuleService.setFuture(false);
566
567 intents = Lists.newArrayList(service.getIntents());
568 assertThat(intents, hasSize(0));
569
570 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
571
572 listener.setLatch(1, Type.INSTALL_REQ);
573 listener.setLatch(1, Type.CORRUPT);
574 listener.setLatch(1, Type.INSTALLED);
575
576 service.submit(intent1);
577
578 listener.await(Type.INSTALL_REQ);
579 listener.await(Type.CORRUPT);
580
581 flowRuleService.setFuture(true);
582
583 listener.await(Type.INSTALLED);
584
585 assertThat(listener.getCounts(Type.CORRUPT), is(1));
586 assertThat(listener.getCounts(Type.INSTALLED), is(1));
587 assertEquals(INSTALLED, manager.getIntentState(intent1.key()));
588 assertThat(flowRuleService.getFlowRuleCount(), is(5));
589 } finally {
590 cleanup.deactivate();
591 }
592 }
593
594 /**
Ray Milkey77a455f2015-03-27 10:08:17 -0700595 * Tests that an intent that fails installation results in no flows remaining.
596 */
597 @Test
Brian O'Connor3c58e962015-04-28 23:21:51 -0700598 @Ignore("MockFlowRule numbering issue") //test works if run independently
Ray Milkey77a455f2015-03-27 10:08:17 -0700599 public void testFlowRemovalInstallError() {
600 final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows();
601 extensionService.registerCompiler(MockIntent.class, errorCompiler);
602 List<Intent> intents;
603
604 flowRuleService.setFuture(true);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700605 //FIXME relying on "3" is brittle
Ray Milkey77a455f2015-03-27 10:08:17 -0700606 flowRuleService.setErrorFlow(3);
607
608 intents = Lists.newArrayList(service.getIntents());
609 assertThat(intents, hasSize(0));
610
611 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
612
613 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700614 listener.setLatch(1, Type.CORRUPT);
Ray Milkey77a455f2015-03-27 10:08:17 -0700615
616 service.submit(intent1);
617 listener.await(Type.INSTALL_REQ);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700618 listener.await(Type.CORRUPT);
Ray Milkey77a455f2015-03-27 10:08:17 -0700619
Brian O'Connor3c58e962015-04-28 23:21:51 -0700620 assertThat(listener.getCounts(Type.CORRUPT), is(1));
621 // in this test, there will still be flows abandoned on the data plane
622 //assertThat(flowRuleService.getFlowRuleCount(), is(0));
Ray Milkey77a455f2015-03-27 10:08:17 -0700623 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800624}