alshabib

flowrule service : apply batch tests

Change-Id: I8e8660e979a2d250496a5ae3100e66e48a67290a
...@@ -25,6 +25,7 @@ import org.onlab.onos.net.Device; ...@@ -25,6 +25,7 @@ import org.onlab.onos.net.Device;
25 import org.onlab.onos.net.DeviceId; 25 import org.onlab.onos.net.DeviceId;
26 import org.onlab.onos.net.device.DeviceService; 26 import org.onlab.onos.net.device.DeviceService;
27 import org.onlab.onos.net.flow.CompletedBatchOperation; 27 import org.onlab.onos.net.flow.CompletedBatchOperation;
28 +import org.onlab.onos.net.flow.DefaultFlowEntry;
28 import org.onlab.onos.net.flow.FlowEntry; 29 import org.onlab.onos.net.flow.FlowEntry;
29 import org.onlab.onos.net.flow.FlowRule; 30 import org.onlab.onos.net.flow.FlowRule;
30 import org.onlab.onos.net.flow.FlowRuleBatchEntry; 31 import org.onlab.onos.net.flow.FlowRuleBatchEntry;
...@@ -472,6 +473,7 @@ public class FlowRuleManager ...@@ -472,6 +473,7 @@ public class FlowRuleManager
472 fbe.getOperator() == FlowRuleOperation.MODIFY) { 473 fbe.getOperator() == FlowRuleOperation.MODIFY) {
473 store.deleteFlowRule(fbe.getTarget()); 474 store.deleteFlowRule(fbe.getTarget());
474 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { 475 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
476 + store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget()));
475 store.storeFlowRule(fbe.getTarget()); 477 store.storeFlowRule(fbe.getTarget());
476 } 478 }
477 } 479 }
......
1 package org.onlab.onos.net.flow.impl; 1 package org.onlab.onos.net.flow.impl;
2 2
3 -import static org.junit.Assert.assertEquals; 3 +import static java.util.Collections.EMPTY_LIST;
4 -import static org.junit.Assert.assertFalse; 4 +import static org.junit.Assert.*;
5 -import static org.junit.Assert.assertNotNull;
6 -import static org.junit.Assert.assertTrue;
7 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; 5 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED;
8 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; 6 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
9 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED; 7 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
...@@ -12,7 +10,10 @@ import java.util.ArrayList; ...@@ -12,7 +10,10 @@ import java.util.ArrayList;
12 import java.util.Collections; 10 import java.util.Collections;
13 import java.util.List; 11 import java.util.List;
14 import java.util.Set; 12 import java.util.Set;
13 +import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future; 14 import java.util.concurrent.Future;
15 +import java.util.concurrent.TimeUnit;
16 +import java.util.concurrent.TimeoutException;
16 17
17 import org.junit.After; 18 import org.junit.After;
18 import org.junit.Before; 19 import org.junit.Before;
...@@ -36,6 +37,7 @@ import org.onlab.onos.net.flow.FlowEntry; ...@@ -36,6 +37,7 @@ import org.onlab.onos.net.flow.FlowEntry;
36 import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; 37 import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
37 import org.onlab.onos.net.flow.FlowRule; 38 import org.onlab.onos.net.flow.FlowRule;
38 import org.onlab.onos.net.flow.FlowRuleBatchEntry; 39 import org.onlab.onos.net.flow.FlowRuleBatchEntry;
40 +import org.onlab.onos.net.flow.FlowRuleBatchOperation;
39 import org.onlab.onos.net.flow.FlowRuleEvent; 41 import org.onlab.onos.net.flow.FlowRuleEvent;
40 import org.onlab.onos.net.flow.FlowRuleListener; 42 import org.onlab.onos.net.flow.FlowRuleListener;
41 import org.onlab.onos.net.flow.FlowRuleProvider; 43 import org.onlab.onos.net.flow.FlowRuleProvider;
...@@ -167,6 +169,7 @@ public class FlowRuleManagerTest { ...@@ -167,6 +169,7 @@ public class FlowRuleManagerTest {
167 //backing store is sensitive to the order of additions/removals 169 //backing store is sensitive to the order of additions/removals
168 private boolean validateState(FlowEntryState... state) { 170 private boolean validateState(FlowEntryState... state) {
169 Iterable<FlowEntry> rules = service.getFlowEntries(DID); 171 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
172 + System.out.println(rules);
170 int i = 0; 173 int i = 0;
171 for (FlowEntry f : rules) { 174 for (FlowEntry f : rules) {
172 if (f.state() != state[i]) { 175 if (f.state() != state[i]) {
...@@ -337,6 +340,79 @@ public class FlowRuleManagerTest { ...@@ -337,6 +340,79 @@ public class FlowRuleManagerTest {
337 validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE); 340 validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE);
338 } 341 }
339 342
343 + @Test
344 + public void applyBatch() {
345 + FlowRule f1 = flowRule(1, 1);
346 + FlowRule f2 = flowRule(2, 2);
347 +
348 +
349 + mgr.applyFlowRules(f1);
350 +
351 + FlowEntry fe1 = new DefaultFlowEntry(f1);
352 + providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1));
353 +
354 + FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry(
355 + FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1);
356 +
357 + FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry(
358 + FlowRuleBatchEntry.FlowRuleOperation.ADD, f2);
359 +
360 + FlowRuleBatchOperation fbo = new FlowRuleBatchOperation(
361 + Lists.newArrayList(fbe1, fbe2));
362 + Future<CompletedBatchOperation> future = mgr.applyBatch(fbo);
363 + assertTrue("Entries in wrong state",
364 + validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_ADD));
365 + CompletedBatchOperation completed = null;
366 + try {
367 + completed = future.get();
368 + } catch (InterruptedException | ExecutionException e) {
369 + fail("Unexpected exception: " + e);
370 + }
371 + if (!completed.isSuccess()) {
372 + fail("Installation should be a success");
373 + }
374 +
375 + }
376 +
377 + @Test
378 + public void cancelBatch() {
379 + FlowRule f1 = flowRule(1, 1);
380 + FlowRule f2 = flowRule(2, 2);
381 +
382 +
383 + mgr.applyFlowRules(f1);
384 +
385 + FlowEntry fe1 = new DefaultFlowEntry(f1);
386 + providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1));
387 +
388 + FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry(
389 + FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1);
390 +
391 + FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry(
392 + FlowRuleBatchEntry.FlowRuleOperation.ADD, f2);
393 +
394 + FlowRuleBatchOperation fbo = new FlowRuleBatchOperation(
395 + Lists.newArrayList(fbe1, fbe2));
396 + Future<CompletedBatchOperation> future = mgr.applyBatch(fbo);
397 +
398 + future.cancel(true);
399 +
400 + assertTrue(flowCount() == 2);
401 +
402 + /*
403 + * Rule f1 should be re-added to the list and therefore be in a pending add
404 + * state.
405 + */
406 + assertTrue("Entries in wrong state",
407 + validateState(FlowEntryState.PENDING_REMOVE,
408 + FlowEntryState.PENDING_ADD));
409 +
410 +
411 +
412 + }
413 +
414 +
415 +
340 private static class TestListener implements FlowRuleListener { 416 private static class TestListener implements FlowRuleListener {
341 final List<FlowRuleEvent> events = new ArrayList<>(); 417 final List<FlowRuleEvent> events = new ArrayList<>();
342 418
...@@ -414,10 +490,40 @@ public class FlowRuleManagerTest { ...@@ -414,10 +490,40 @@ public class FlowRuleManagerTest {
414 @Override 490 @Override
415 public Future<CompletedBatchOperation> executeBatch( 491 public Future<CompletedBatchOperation> executeBatch(
416 BatchOperation<FlowRuleBatchEntry> batch) { 492 BatchOperation<FlowRuleBatchEntry> batch) {
417 - // TODO Auto-generated method stub 493 + return new TestInstallationFuture();
418 - return null;
419 } 494 }
420 495
496 + private class TestInstallationFuture
497 + implements Future<CompletedBatchOperation> {
498 +
499 + @Override
500 + public boolean cancel(boolean mayInterruptIfRunning) {
501 + return true;
502 + }
503 +
504 + @Override
505 + public boolean isCancelled() {
506 + return true;
507 + }
508 +
509 + @Override
510 + public boolean isDone() {
511 + return false;
512 + }
513 +
514 + @Override
515 + public CompletedBatchOperation get()
516 + throws InterruptedException, ExecutionException {
517 + return new CompletedBatchOperation(true, EMPTY_LIST);
518 + }
519 +
520 + @Override
521 + public CompletedBatchOperation get(long timeout, TimeUnit unit)
522 + throws InterruptedException,
523 + ExecutionException, TimeoutException {
524 + return null;
525 + }
526 + }
421 527
422 } 528 }
423 529
......