| package org.onlab.onos.net.flow.impl; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertTrue; |
| import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Set; |
| import java.util.concurrent.Future; |
| |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.onlab.onos.ApplicationId; |
| import org.onlab.onos.event.impl.TestEventDispatcher; |
| import org.onlab.onos.net.DefaultDevice; |
| import org.onlab.onos.net.Device; |
| import org.onlab.onos.net.Device.Type; |
| import org.onlab.onos.net.DeviceId; |
| import org.onlab.onos.net.MastershipRole; |
| import org.onlab.onos.net.Port; |
| import org.onlab.onos.net.PortNumber; |
| import org.onlab.onos.net.device.DeviceListener; |
| import org.onlab.onos.net.device.DeviceService; |
| import org.onlab.onos.net.flow.CompletedBatchOperation; |
| import org.onlab.onos.net.flow.DefaultFlowEntry; |
| import org.onlab.onos.net.flow.DefaultFlowRule; |
| import org.onlab.onos.net.flow.FlowEntry; |
| import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
| import org.onlab.onos.net.flow.FlowRule; |
| import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| import org.onlab.onos.net.flow.FlowRuleEvent; |
| import org.onlab.onos.net.flow.FlowRuleListener; |
| import org.onlab.onos.net.flow.FlowRuleProvider; |
| import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| import org.onlab.onos.net.flow.FlowRuleProviderService; |
| import org.onlab.onos.net.flow.FlowRuleService; |
| import org.onlab.onos.net.flow.TrafficSelector; |
| import org.onlab.onos.net.flow.TrafficTreatment; |
| import org.onlab.onos.net.flow.criteria.Criterion; |
| import org.onlab.onos.net.flow.instructions.Instruction; |
| import org.onlab.onos.net.intent.BatchOperation; |
| import org.onlab.onos.net.provider.AbstractProvider; |
| import org.onlab.onos.net.provider.ProviderId; |
| import org.onlab.onos.store.trivial.impl.SimpleFlowRuleStore; |
| |
| import com.google.common.collect.ImmutableList; |
| import com.google.common.collect.Lists; |
| import com.google.common.collect.Sets; |
| |
| /** |
| * Test codifying the flow rule service & flow rule provider service contracts. |
| */ |
| public class FlowRuleManagerTest { |
| |
| private static final ProviderId PID = new ProviderId("of", "foo"); |
| private static final DeviceId DID = DeviceId.deviceId("of:001"); |
| private static final int TIMEOUT = 10; |
| private static final Device DEV = new DefaultDevice( |
| PID, DID, Type.SWITCH, "", "", "", ""); |
| |
| private FlowRuleManager mgr; |
| |
| protected FlowRuleService service; |
| protected FlowRuleProviderRegistry registry; |
| protected FlowRuleProviderService providerService; |
| protected TestProvider provider; |
| protected TestListener listener = new TestListener(); |
| private ApplicationId appId; |
| |
| @Before |
| public void setUp() { |
| mgr = new FlowRuleManager(); |
| mgr.store = new SimpleFlowRuleStore(); |
| mgr.eventDispatcher = new TestEventDispatcher(); |
| mgr.deviceService = new TestDeviceService(); |
| service = mgr; |
| registry = mgr; |
| |
| mgr.activate(); |
| mgr.addListener(listener); |
| provider = new TestProvider(PID); |
| providerService = registry.register(provider); |
| appId = ApplicationId.getAppId(); |
| assertTrue("provider should be registered", |
| registry.getProviders().contains(provider.id())); |
| } |
| |
| @After |
| public void tearDown() { |
| registry.unregister(provider); |
| assertFalse("provider should not be registered", |
| registry.getProviders().contains(provider.id())); |
| service.removeListener(listener); |
| mgr.deactivate(); |
| mgr.eventDispatcher = null; |
| mgr.deviceService = null; |
| } |
| |
| private FlowRule flowRule(int tsval, int trval) { |
| TestSelector ts = new TestSelector(tsval); |
| TestTreatment tr = new TestTreatment(trval); |
| return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT); |
| } |
| |
| |
| private FlowRule addFlowRule(int hval) { |
| FlowRule rule = flowRule(hval, hval); |
| service.applyFlowRules(rule); |
| |
| assertNotNull("rule should be found", service.getFlowEntries(DID)); |
| return rule; |
| } |
| |
| private void validateEvents(FlowRuleEvent.Type ... events) { |
| if (events == null) { |
| assertTrue("events generated", listener.events.isEmpty()); |
| } |
| |
| int i = 0; |
| System.err.println("events :" + listener.events); |
| for (FlowRuleEvent e : listener.events) { |
| assertTrue("unexpected event", e.type().equals(events[i])); |
| i++; |
| } |
| |
| assertEquals("mispredicted number of events", |
| events.length, listener.events.size()); |
| |
| listener.events.clear(); |
| } |
| |
| private int flowCount() { |
| return Sets.newHashSet(service.getFlowEntries(DID)).size(); |
| } |
| @Test |
| public void getFlowEntries() { |
| assertTrue("store should be empty", |
| Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
| FlowRule f1 = addFlowRule(1); |
| FlowRule f2 = addFlowRule(2); |
| |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| assertEquals("2 rules should exist", 2, flowCount()); |
| |
| providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
| validateEvents(RULE_ADDED, RULE_ADDED); |
| |
| addFlowRule(1); |
| assertEquals("should still be 2 rules", 2, flowCount()); |
| |
| providerService.pushFlowMetrics(DID, ImmutableList.of(fe1)); |
| validateEvents(RULE_UPDATED); |
| } |
| |
| |
| //backing store is sensitive to the order of additions/removals |
| private boolean validateState(FlowEntryState... state) { |
| Iterable<FlowEntry> rules = service.getFlowEntries(DID); |
| int i = 0; |
| for (FlowEntry f : rules) { |
| if (f.state() != state[i]) { |
| return false; |
| } |
| i++; |
| } |
| return true; |
| } |
| |
| @Test |
| public void applyFlowRules() { |
| |
| FlowRule r1 = flowRule(1, 1); |
| FlowRule r2 = flowRule(2, 2); |
| FlowRule r3 = flowRule(3, 3); |
| |
| assertTrue("store should be empty", |
| Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
| mgr.applyFlowRules(r1, r2, r3); |
| assertEquals("3 rules should exist", 3, flowCount()); |
| assertTrue("Entries should be pending add.", |
| validateState(FlowEntryState.PENDING_ADD, FlowEntryState.PENDING_ADD, |
| FlowEntryState.PENDING_ADD)); |
| } |
| |
| @Test |
| public void removeFlowRules() { |
| FlowRule f1 = addFlowRule(1); |
| FlowRule f2 = addFlowRule(2); |
| FlowRule f3 = addFlowRule(3); |
| assertEquals("3 rules should exist", 3, flowCount()); |
| |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| FlowEntry fe3 = new DefaultFlowEntry(f3); |
| providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3)); |
| validateEvents(RULE_ADDED, RULE_ADDED, RULE_ADDED); |
| |
| mgr.removeFlowRules(f1, f2); |
| //removing from north, so no events generated |
| validateEvents(); |
| assertEquals("3 rule should exist", 3, flowCount()); |
| assertTrue("Entries should be pending remove.", |
| validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE, |
| FlowEntryState.ADDED)); |
| |
| mgr.removeFlowRules(f1); |
| assertEquals("3 rule should still exist", 3, flowCount()); |
| } |
| |
| @Test |
| public void flowRemoved() { |
| FlowRule f1 = addFlowRule(1); |
| FlowRule f2 = addFlowRule(2); |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
| service.removeFlowRules(f1); |
| fe1.setState(FlowEntryState.REMOVED); |
| providerService.flowRemoved(fe1); |
| validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
| |
| providerService.flowRemoved(fe1); |
| validateEvents(); |
| |
| FlowRule f3 = flowRule(3, 3); |
| FlowEntry fe3 = new DefaultFlowEntry(f3); |
| service.applyFlowRules(f3); |
| providerService.pushFlowMetrics(DID, Collections.singletonList(fe3)); |
| validateEvents(RULE_ADDED); |
| |
| providerService.flowRemoved(fe3); |
| validateEvents(); |
| } |
| |
| @Test |
| public void flowMetrics() { |
| FlowRule f1 = flowRule(1, 1); |
| FlowRule f2 = flowRule(2, 2); |
| FlowRule f3 = flowRule(3, 3); |
| |
| mgr.applyFlowRules(f1, f2, f3); |
| |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| |
| |
| //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| |
| providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
| |
| assertTrue("Entries should be added.", |
| validateState(FlowEntryState.ADDED, FlowEntryState.ADDED, |
| FlowEntryState.PENDING_ADD)); |
| |
| validateEvents(RULE_ADDED, RULE_ADDED); |
| } |
| |
| @Test |
| public void extraneousFlow() { |
| FlowRule f1 = flowRule(1, 1); |
| FlowRule f2 = flowRule(2, 2); |
| FlowRule f3 = flowRule(3, 3); |
| mgr.applyFlowRules(f1, f2); |
| |
| // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| // FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED); |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| FlowEntry fe3 = new DefaultFlowEntry(f3); |
| |
| |
| providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3)); |
| |
| validateEvents(RULE_ADDED, RULE_ADDED); |
| |
| } |
| |
| /* |
| * Tests whether a rule that was marked for removal but no flowRemoved was received |
| * is indeed removed at the next stats update. |
| */ |
| @Test |
| public void flowMissingRemove() { |
| FlowRule f1 = flowRule(1, 1); |
| FlowRule f2 = flowRule(2, 2); |
| FlowRule f3 = flowRule(3, 3); |
| |
| // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| |
| FlowEntry fe1 = new DefaultFlowEntry(f1); |
| FlowEntry fe2 = new DefaultFlowEntry(f2); |
| mgr.applyFlowRules(f1, f2, f3); |
| |
| mgr.removeFlowRules(f3); |
| |
| providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
| |
| validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
| |
| } |
| |
| @Test |
| public void getByAppId() { |
| FlowRule f1 = flowRule(1, 1); |
| FlowRule f2 = flowRule(2, 2); |
| mgr.applyFlowRules(f1, f2); |
| |
| assertTrue("should have two rules", |
| Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2); |
| } |
| |
| @Test |
| public void removeByAppId() { |
| FlowRule f1 = flowRule(1, 1); |
| FlowRule f2 = flowRule(2, 2); |
| mgr.applyFlowRules(f1, f2); |
| |
| |
| mgr.removeFlowRulesById(appId); |
| |
| //only check that we are in pending remove. Events and actual remove state will |
| // be set by flowRemoved call. |
| validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE); |
| } |
| |
| private static class TestListener implements FlowRuleListener { |
| final List<FlowRuleEvent> events = new ArrayList<>(); |
| |
| @Override |
| public void event(FlowRuleEvent event) { |
| events.add(event); |
| } |
| } |
| |
| private static class TestDeviceService implements DeviceService { |
| |
| @Override |
| public int getDeviceCount() { |
| return 0; |
| } |
| |
| @Override |
| public Iterable<Device> getDevices() { |
| return null; |
| } |
| |
| @Override |
| public Device getDevice(DeviceId deviceId) { |
| return DEV; |
| } |
| |
| @Override |
| public MastershipRole getRole(DeviceId deviceId) { |
| return null; |
| } |
| |
| @Override |
| public List<Port> getPorts(DeviceId deviceId) { |
| return null; |
| } |
| |
| @Override |
| public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| return null; |
| } |
| |
| @Override |
| public boolean isAvailable(DeviceId deviceId) { |
| return false; |
| } |
| |
| @Override |
| public void addListener(DeviceListener listener) { |
| } |
| |
| @Override |
| public void removeListener(DeviceListener listener) { |
| } |
| |
| } |
| |
| private class TestProvider extends AbstractProvider implements FlowRuleProvider { |
| |
| protected TestProvider(ProviderId id) { |
| super(PID); |
| } |
| |
| @Override |
| public void applyFlowRule(FlowRule... flowRules) { |
| } |
| |
| @Override |
| public void removeFlowRule(FlowRule... flowRules) { |
| } |
| |
| @Override |
| public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| } |
| |
| @Override |
| public Future<CompletedBatchOperation> executeBatch( |
| BatchOperation<FlowRuleBatchEntry> batch) { |
| // TODO Auto-generated method stub |
| return null; |
| } |
| |
| |
| } |
| |
| private class TestSelector implements TrafficSelector { |
| |
| //for controlling hashcode uniqueness; |
| private final int testval; |
| |
| public TestSelector(int val) { |
| testval = val; |
| } |
| |
| @Override |
| public Set<Criterion> criteria() { |
| return null; |
| } |
| |
| @Override |
| public int hashCode() { |
| return testval; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (o instanceof TestSelector) { |
| return this.testval == ((TestSelector) o).testval; |
| } |
| return false; |
| } |
| } |
| |
| private class TestTreatment implements TrafficTreatment { |
| |
| //for controlling hashcode uniqueness; |
| private final int testval; |
| |
| public TestTreatment(int val) { |
| testval = val; |
| } |
| |
| @Override |
| public List<Instruction> instructions() { |
| return null; |
| } |
| |
| @Override |
| public int hashCode() { |
| return testval; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (o instanceof TestTreatment) { |
| return this.testval == ((TestTreatment) o).testval; |
| } |
| return false; |
| } |
| |
| } |
| |
| } |