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