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