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