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