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