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