tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.impl; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 2 | |
alshabib | 44521ec | 2014-10-22 18:36:02 -0700 | [diff] [blame] | 3 | |
| 4 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 5 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.*; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 6 | |
alshabib | 44521ec | 2014-10-22 18:36:02 -0700 | [diff] [blame] | 7 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 8 | import java.util.ArrayList; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 9 | import java.util.Collections; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 10 | import java.util.HashMap; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 11 | import java.util.List; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 12 | import java.util.Map; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 13 | import java.util.Set; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ExecutionException; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 15 | import java.util.concurrent.Future; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 16 | import java.util.concurrent.TimeUnit; |
| 17 | import java.util.concurrent.TimeoutException; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 22 | import org.onlab.onos.ApplicationId; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 23 | import org.onlab.onos.event.impl.TestEventDispatcher; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 24 | import org.onlab.onos.impl.DefaultApplicationId; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.DefaultDevice; |
| 26 | import org.onlab.onos.net.Device; |
| 27 | import org.onlab.onos.net.Device.Type; |
| 28 | import org.onlab.onos.net.DeviceId; |
| 29 | import org.onlab.onos.net.MastershipRole; |
| 30 | import org.onlab.onos.net.Port; |
| 31 | import org.onlab.onos.net.PortNumber; |
| 32 | import org.onlab.onos.net.device.DeviceListener; |
| 33 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.DefaultFlowRule; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.FlowEntry; |
| 38 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 43 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 44 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 45 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 46 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 47 | import org.onlab.onos.net.flow.FlowRuleService; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.flow.StoredFlowEntry; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 49 | import org.onlab.onos.net.flow.TrafficSelector; |
| 50 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 51 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 52 | import org.onlab.onos.net.flow.instructions.Instruction; |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 53 | import org.onlab.onos.net.flow.BatchOperation; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.provider.AbstractProvider; |
| 55 | import org.onlab.onos.net.provider.ProviderId; |
tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 56 | import org.onlab.onos.store.trivial.impl.SimpleFlowRuleStore; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 57 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 58 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 59 | import com.google.common.collect.ImmutableMap; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 60 | import com.google.common.collect.Lists; |
| 61 | import com.google.common.collect.Sets; |
| 62 | |
Ray Milkey | 5154ec3 | 2014-10-22 10:51:18 -0700 | [diff] [blame] | 63 | import static java.util.Collections.EMPTY_LIST; |
| 64 | import static org.junit.Assert.assertEquals; |
| 65 | import static org.junit.Assert.assertFalse; |
| 66 | import static org.junit.Assert.assertNotNull; |
| 67 | import static org.junit.Assert.assertTrue; |
| 68 | import static org.junit.Assert.fail; |
| 69 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| 70 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 71 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
| 72 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Test codifying the flow rule service & flow rule provider service contracts. |
| 75 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 76 | public class FlowRuleManagerTest { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 77 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 78 | |
| 79 | |
tom | f6ab215 | 2014-09-18 12:08:29 -0700 | [diff] [blame] | 80 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 81 | private static final DeviceId DID = DeviceId.deviceId("of:001"); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 82 | private static final int TIMEOUT = 10; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 83 | private static final Device DEV = new DefaultDevice( |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 84 | PID, DID, Type.SWITCH, "", "", "", "", null); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 85 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 86 | private FlowRuleManager mgr; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 87 | |
| 88 | protected FlowRuleService service; |
| 89 | protected FlowRuleProviderRegistry registry; |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 90 | protected FlowRuleProviderService providerService; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 91 | protected TestProvider provider; |
| 92 | protected TestListener listener = new TestListener(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 93 | private ApplicationId appId; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 94 | |
| 95 | @Before |
| 96 | public void setUp() { |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 97 | mgr = new FlowRuleManager(); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 98 | mgr.store = new SimpleFlowRuleStore(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 99 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 100 | mgr.deviceService = new TestDeviceService(); |
| 101 | service = mgr; |
| 102 | registry = mgr; |
| 103 | |
| 104 | mgr.activate(); |
| 105 | mgr.addListener(listener); |
| 106 | provider = new TestProvider(PID); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 107 | providerService = registry.register(provider); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 108 | appId = new TestApplicationId((short) 0, "FlowRuleManagerTest"); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 109 | assertTrue("provider should be registered", |
| 110 | registry.getProviders().contains(provider.id())); |
| 111 | } |
| 112 | |
| 113 | @After |
| 114 | public void tearDown() { |
| 115 | registry.unregister(provider); |
| 116 | assertFalse("provider should not be registered", |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 117 | registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 118 | service.removeListener(listener); |
| 119 | mgr.deactivate(); |
| 120 | mgr.eventDispatcher = null; |
| 121 | mgr.deviceService = null; |
| 122 | } |
| 123 | |
| 124 | private FlowRule flowRule(int tsval, int trval) { |
| 125 | TestSelector ts = new TestSelector(tsval); |
| 126 | TestTreatment tr = new TestTreatment(trval); |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 127 | return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 130 | |
| 131 | private FlowRule addFlowRule(int hval) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 132 | FlowRule rule = flowRule(hval, hval); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 133 | service.applyFlowRules(rule); |
| 134 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 135 | assertNotNull("rule should be found", service.getFlowEntries(DID)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 136 | return rule; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private void validateEvents(FlowRuleEvent.Type ... events) { |
| 140 | if (events == null) { |
| 141 | assertTrue("events generated", listener.events.isEmpty()); |
| 142 | } |
| 143 | |
| 144 | int i = 0; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 145 | System.err.println("events :" + listener.events); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 146 | for (FlowRuleEvent e : listener.events) { |
| 147 | assertTrue("unexpected event", e.type().equals(events[i])); |
| 148 | i++; |
| 149 | } |
| 150 | |
| 151 | assertEquals("mispredicted number of events", |
| 152 | events.length, listener.events.size()); |
| 153 | |
| 154 | listener.events.clear(); |
| 155 | } |
| 156 | |
| 157 | private int flowCount() { |
| 158 | return Sets.newHashSet(service.getFlowEntries(DID)).size(); |
| 159 | } |
| 160 | @Test |
| 161 | public void getFlowEntries() { |
| 162 | assertTrue("store should be empty", |
| 163 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 164 | FlowRule f1 = addFlowRule(1); |
| 165 | FlowRule f2 = addFlowRule(2); |
| 166 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 167 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 168 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 169 | assertEquals("2 rules should exist", 2, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 170 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 171 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 172 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 173 | RULE_ADDED, RULE_ADDED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 174 | |
| 175 | addFlowRule(1); |
| 176 | assertEquals("should still be 2 rules", 2, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 177 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 178 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1)); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 179 | validateEvents(RULE_UPDATED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 180 | } |
| 181 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 182 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 183 | // TODO: If preserving iteration order is a requirement, redo FlowRuleStore. |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 184 | //backing store is sensitive to the order of additions/removals |
Ray Milkey | 5154ec3 | 2014-10-22 10:51:18 -0700 | [diff] [blame] | 185 | @SuppressWarnings("unchecked") |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 186 | private boolean validateState(Map<FlowRule, FlowEntryState> expected) { |
| 187 | Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 188 | Iterable<FlowEntry> rules = service.getFlowEntries(DID); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 189 | for (FlowEntry f : rules) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 190 | assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f)); |
| 191 | assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state()); |
| 192 | expectedToCheck.remove(f); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 193 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 194 | assertEquals(Collections.emptySet(), expectedToCheck.entrySet()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 198 | @Test |
| 199 | public void applyFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 200 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 201 | FlowRule r1 = flowRule(1, 1); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 202 | FlowRule r2 = flowRule(2, 2); |
| 203 | FlowRule r3 = flowRule(3, 3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 204 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 205 | assertTrue("store should be empty", |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 206 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 207 | mgr.applyFlowRules(r1, r2, r3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 208 | assertEquals("3 rules should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 209 | assertTrue("Entries should be pending add.", |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 210 | validateState(ImmutableMap.of( |
| 211 | r1, FlowEntryState.PENDING_ADD, |
| 212 | r2, FlowEntryState.PENDING_ADD, |
| 213 | r3, FlowEntryState.PENDING_ADD))); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void removeFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 218 | FlowRule f1 = addFlowRule(1); |
| 219 | FlowRule f2 = addFlowRule(2); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 220 | FlowRule f3 = addFlowRule(3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 221 | assertEquals("3 rules should exist", 3, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 222 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 223 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 224 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 225 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 226 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3)); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 227 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 228 | RULE_ADDED, RULE_ADDED, RULE_ADDED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 229 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 230 | mgr.removeFlowRules(f1, f2); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 231 | //removing from north, so no events generated |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 232 | validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 233 | assertEquals("3 rule should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 234 | assertTrue("Entries should be pending remove.", |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 235 | validateState(ImmutableMap.of( |
| 236 | f1, FlowEntryState.PENDING_REMOVE, |
| 237 | f2, FlowEntryState.PENDING_REMOVE, |
| 238 | f3, FlowEntryState.ADDED))); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 239 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 240 | mgr.removeFlowRules(f1); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 241 | assertEquals("3 rule should still exist", 3, flowCount()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void flowRemoved() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 246 | FlowRule f1 = addFlowRule(1); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 247 | FlowRule f2 = addFlowRule(2); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 248 | StoredFlowEntry fe1 = new DefaultFlowEntry(f1); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 249 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 250 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 251 | service.removeFlowRules(f1); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 252 | fe1.setState(FlowEntryState.REMOVED); |
| 253 | providerService.flowRemoved(fe1); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 254 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, |
| 255 | RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 256 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 257 | providerService.flowRemoved(fe1); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 258 | validateEvents(); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 259 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 260 | FlowRule f3 = flowRule(3, 3); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 261 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 262 | service.applyFlowRules(f3); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 263 | providerService.pushFlowMetrics(DID, Collections.singletonList(fe3)); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 264 | validateEvents(RULE_ADD_REQUESTED, RULE_ADDED); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 265 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 266 | providerService.flowRemoved(fe3); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 267 | validateEvents(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 268 | } |
| 269 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 270 | @Test |
| 271 | public void flowMetrics() { |
| 272 | FlowRule f1 = flowRule(1, 1); |
| 273 | FlowRule f2 = flowRule(2, 2); |
| 274 | FlowRule f3 = flowRule(3, 3); |
| 275 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 276 | mgr.applyFlowRules(f1, f2, f3); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 277 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 278 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 279 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 280 | |
| 281 | |
| 282 | //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 283 | //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 284 | |
| 285 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 286 | |
| 287 | assertTrue("Entries should be added.", |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 288 | validateState(ImmutableMap.of( |
| 289 | f1, FlowEntryState.ADDED, |
| 290 | f2, FlowEntryState.ADDED, |
| 291 | f3, FlowEntryState.PENDING_ADD))); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 292 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 293 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 294 | RULE_ADDED, RULE_ADDED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void extraneousFlow() { |
| 299 | FlowRule f1 = flowRule(1, 1); |
| 300 | FlowRule f2 = flowRule(2, 2); |
| 301 | FlowRule f3 = flowRule(3, 3); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 302 | mgr.applyFlowRules(f1, f2); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 303 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 304 | // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 305 | // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 306 | // FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED); |
| 307 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 308 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 309 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 310 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 311 | |
| 312 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3)); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 313 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 314 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 315 | |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Tests whether a rule that was marked for removal but no flowRemoved was received |
| 320 | * is indeed removed at the next stats update. |
| 321 | */ |
| 322 | @Test |
| 323 | public void flowMissingRemove() { |
| 324 | FlowRule f1 = flowRule(1, 1); |
| 325 | FlowRule f2 = flowRule(2, 2); |
| 326 | FlowRule f3 = flowRule(3, 3); |
| 327 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 328 | // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 329 | // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 330 | |
| 331 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 332 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 333 | mgr.applyFlowRules(f1, f2, f3); |
| 334 | |
| 335 | mgr.removeFlowRules(f3); |
| 336 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 337 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 338 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 339 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 340 | RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 341 | |
| 342 | } |
| 343 | |
| 344 | @Test |
| 345 | public void getByAppId() { |
| 346 | FlowRule f1 = flowRule(1, 1); |
| 347 | FlowRule f2 = flowRule(2, 2); |
| 348 | mgr.applyFlowRules(f1, f2); |
| 349 | |
| 350 | assertTrue("should have two rules", |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 351 | Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Test |
| 355 | public void removeByAppId() { |
| 356 | FlowRule f1 = flowRule(1, 1); |
| 357 | FlowRule f2 = flowRule(2, 2); |
| 358 | mgr.applyFlowRules(f1, f2); |
| 359 | |
| 360 | |
| 361 | mgr.removeFlowRulesById(appId); |
| 362 | |
| 363 | //only check that we are in pending remove. Events and actual remove state will |
| 364 | // be set by flowRemoved call. |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 365 | validateState(ImmutableMap.of( |
| 366 | f1, FlowEntryState.PENDING_REMOVE, |
| 367 | f2, FlowEntryState.PENDING_REMOVE)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 368 | } |
| 369 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 370 | @Test |
| 371 | public void applyBatch() { |
| 372 | FlowRule f1 = flowRule(1, 1); |
| 373 | FlowRule f2 = flowRule(2, 2); |
| 374 | |
| 375 | |
| 376 | mgr.applyFlowRules(f1); |
| 377 | |
| 378 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 379 | providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1)); |
| 380 | |
| 381 | FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry( |
| 382 | FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1); |
| 383 | |
| 384 | FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry( |
| 385 | FlowRuleBatchEntry.FlowRuleOperation.ADD, f2); |
| 386 | |
| 387 | FlowRuleBatchOperation fbo = new FlowRuleBatchOperation( |
| 388 | Lists.newArrayList(fbe1, fbe2)); |
| 389 | Future<CompletedBatchOperation> future = mgr.applyBatch(fbo); |
| 390 | assertTrue("Entries in wrong state", |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 391 | validateState(ImmutableMap.of( |
| 392 | f1, FlowEntryState.PENDING_REMOVE, |
| 393 | f2, FlowEntryState.PENDING_ADD))); |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 394 | CompletedBatchOperation completed = null; |
| 395 | try { |
| 396 | completed = future.get(); |
| 397 | } catch (InterruptedException | ExecutionException e) { |
| 398 | fail("Unexpected exception: " + e); |
| 399 | } |
| 400 | if (!completed.isSuccess()) { |
| 401 | fail("Installation should be a success"); |
| 402 | } |
| 403 | |
| 404 | } |
| 405 | |
| 406 | @Test |
| 407 | public void cancelBatch() { |
| 408 | FlowRule f1 = flowRule(1, 1); |
| 409 | FlowRule f2 = flowRule(2, 2); |
| 410 | |
| 411 | |
| 412 | mgr.applyFlowRules(f1); |
| 413 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 414 | assertTrue("Entries in wrong state", |
| 415 | validateState(ImmutableMap.of( |
| 416 | f1, FlowEntryState.PENDING_ADD))); |
| 417 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 418 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 419 | providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1)); |
| 420 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 421 | assertTrue("Entries in wrong state", |
| 422 | validateState(ImmutableMap.of( |
| 423 | f1, FlowEntryState.ADDED))); |
| 424 | |
| 425 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 426 | FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry( |
| 427 | FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1); |
| 428 | |
| 429 | FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry( |
| 430 | FlowRuleBatchEntry.FlowRuleOperation.ADD, f2); |
| 431 | |
| 432 | FlowRuleBatchOperation fbo = new FlowRuleBatchOperation( |
| 433 | Lists.newArrayList(fbe1, fbe2)); |
| 434 | Future<CompletedBatchOperation> future = mgr.applyBatch(fbo); |
| 435 | |
| 436 | future.cancel(true); |
| 437 | |
| 438 | assertTrue(flowCount() == 2); |
| 439 | |
| 440 | /* |
| 441 | * Rule f1 should be re-added to the list and therefore be in a pending add |
| 442 | * state. |
| 443 | */ |
| 444 | assertTrue("Entries in wrong state", |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 445 | validateState(ImmutableMap.of( |
| 446 | f2, FlowEntryState.PENDING_REMOVE, |
| 447 | f1, FlowEntryState.PENDING_ADD))); |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 448 | |
| 449 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
| 453 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 454 | private static class TestListener implements FlowRuleListener { |
| 455 | final List<FlowRuleEvent> events = new ArrayList<>(); |
| 456 | |
| 457 | @Override |
| 458 | public void event(FlowRuleEvent event) { |
| 459 | events.add(event); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private static class TestDeviceService implements DeviceService { |
| 464 | |
| 465 | @Override |
| 466 | public int getDeviceCount() { |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public Iterable<Device> getDevices() { |
| 472 | return null; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public Device getDevice(DeviceId deviceId) { |
| 477 | return DEV; |
| 478 | } |
| 479 | |
| 480 | @Override |
| 481 | public MastershipRole getRole(DeviceId deviceId) { |
| 482 | return null; |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public List<Port> getPorts(DeviceId deviceId) { |
| 487 | return null; |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 492 | return null; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public boolean isAvailable(DeviceId deviceId) { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void addListener(DeviceListener listener) { |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void removeListener(DeviceListener listener) { |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | |
| 510 | private class TestProvider extends AbstractProvider implements FlowRuleProvider { |
| 511 | |
| 512 | protected TestProvider(ProviderId id) { |
| 513 | super(PID); |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void applyFlowRule(FlowRule... flowRules) { |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void removeFlowRule(FlowRule... flowRules) { |
| 522 | } |
| 523 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 524 | @Override |
| 525 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 526 | } |
| 527 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 528 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 529 | public Future<CompletedBatchOperation> executeBatch( |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 530 | BatchOperation<FlowRuleBatchEntry> batch) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 531 | return new TestInstallationFuture(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 532 | } |
| 533 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 534 | private class TestInstallationFuture |
| 535 | implements Future<CompletedBatchOperation> { |
| 536 | |
| 537 | @Override |
| 538 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public boolean isCancelled() { |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public boolean isDone() { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | @Override |
Ray Milkey | 5154ec3 | 2014-10-22 10:51:18 -0700 | [diff] [blame] | 553 | @SuppressWarnings("unchecked") |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 554 | public CompletedBatchOperation get() |
| 555 | throws InterruptedException, ExecutionException { |
| 556 | return new CompletedBatchOperation(true, EMPTY_LIST); |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 561 | throws InterruptedException, |
| 562 | ExecutionException, TimeoutException { |
| 563 | return null; |
| 564 | } |
| 565 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 566 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | private class TestSelector implements TrafficSelector { |
| 570 | |
| 571 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 572 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 573 | |
| 574 | public TestSelector(int val) { |
| 575 | testval = val; |
| 576 | } |
| 577 | |
| 578 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 579 | public Set<Criterion> criteria() { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 580 | return null; |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public int hashCode() { |
| 585 | return testval; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public boolean equals(Object o) { |
| 590 | if (o instanceof TestSelector) { |
| 591 | return this.testval == ((TestSelector) o).testval; |
| 592 | } |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | private class TestTreatment implements TrafficTreatment { |
| 598 | |
| 599 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 600 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 601 | |
| 602 | public TestTreatment(int val) { |
| 603 | testval = val; |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | public List<Instruction> instructions() { |
| 608 | return null; |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public int hashCode() { |
| 613 | return testval; |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | public boolean equals(Object o) { |
| 618 | if (o instanceof TestTreatment) { |
| 619 | return this.testval == ((TestTreatment) o).testval; |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | } |
| 625 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 626 | public class TestApplicationId extends DefaultApplicationId { |
| 627 | |
| 628 | public TestApplicationId(short id, String name) { |
| 629 | super(id, name); |
| 630 | } |
| 631 | } |
| 632 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 633 | } |