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