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