blob: 2d7b3742c4f185fb94dd1888bf3c172f239d89c9 [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.flow.impl;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -07002
alshabibcf369912014-10-13 14:16:42 -07003import static java.util.Collections.EMPTY_LIST;
4import static org.junit.Assert.*;
alshabib97044902014-09-18 14:52:16 -07005import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED;
6import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
alshabib219ebaa2014-09-22 15:41:24 -07007import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -07008
9import java.util.ArrayList;
alshabibba5ac482014-10-02 17:15:20 -070010import java.util.Collections;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070011import java.util.List;
alshabibba5ac482014-10-02 17:15:20 -070012import java.util.Set;
alshabibcf369912014-10-13 14:16:42 -070013import java.util.concurrent.ExecutionException;
alshabib902d41b2014-10-07 16:52:05 -070014import java.util.concurrent.Future;
alshabibcf369912014-10-13 14:16:42 -070015import java.util.concurrent.TimeUnit;
16import java.util.concurrent.TimeoutException;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
alshabiba68eb962014-09-24 20:34:13 -070021import org.onlab.onos.ApplicationId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070022import org.onlab.onos.event.impl.TestEventDispatcher;
alshabib92c65ad2014-10-08 21:56:05 -070023import org.onlab.onos.impl.DefaultApplicationId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070024import org.onlab.onos.net.DefaultDevice;
25import org.onlab.onos.net.Device;
26import org.onlab.onos.net.Device.Type;
27import org.onlab.onos.net.DeviceId;
28import org.onlab.onos.net.MastershipRole;
29import org.onlab.onos.net.Port;
30import org.onlab.onos.net.PortNumber;
31import org.onlab.onos.net.device.DeviceListener;
32import org.onlab.onos.net.device.DeviceService;
alshabib193525b2014-10-08 18:58:03 -070033import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070034import org.onlab.onos.net.flow.DefaultFlowEntry;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070035import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib1c319ff2014-10-04 20:29:09 -070036import org.onlab.onos.net.flow.FlowEntry;
37import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070038import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070039import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabibcf369912014-10-13 14:16:42 -070040import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070041import org.onlab.onos.net.flow.FlowRuleEvent;
42import org.onlab.onos.net.flow.FlowRuleListener;
43import org.onlab.onos.net.flow.FlowRuleProvider;
44import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
45import org.onlab.onos.net.flow.FlowRuleProviderService;
46import org.onlab.onos.net.flow.FlowRuleService;
47import org.onlab.onos.net.flow.TrafficSelector;
48import org.onlab.onos.net.flow.TrafficTreatment;
49import org.onlab.onos.net.flow.criteria.Criterion;
50import org.onlab.onos.net.flow.instructions.Instruction;
alshabib902d41b2014-10-07 16:52:05 -070051import org.onlab.onos.net.intent.BatchOperation;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070052import org.onlab.onos.net.provider.AbstractProvider;
53import org.onlab.onos.net.provider.ProviderId;
tomea961ff2014-10-01 12:45:15 -070054import org.onlab.onos.store.trivial.impl.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070055
alshabibba5ac482014-10-02 17:15:20 -070056import com.google.common.collect.ImmutableList;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070057import com.google.common.collect.Lists;
58import com.google.common.collect.Sets;
59
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070060/**
61 * Test codifying the flow rule service & flow rule provider service contracts.
62 */
tom202175a2014-09-19 19:00:11 -070063public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070064
alshabib92c65ad2014-10-08 21:56:05 -070065
66
tomf6ab2152014-09-18 12:08:29 -070067 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070068 private static final DeviceId DID = DeviceId.deviceId("of:001");
alshabibba5ac482014-10-02 17:15:20 -070069 private static final int TIMEOUT = 10;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070070 private static final Device DEV = new DefaultDevice(
71 PID, DID, Type.SWITCH, "", "", "", "");
72
tom202175a2014-09-19 19:00:11 -070073 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070074
75 protected FlowRuleService service;
76 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -070077 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070078 protected TestProvider provider;
79 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -070080 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070081
82 @Before
83 public void setUp() {
tom202175a2014-09-19 19:00:11 -070084 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -070085 mgr.store = new SimpleFlowRuleStore();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070086 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);
alshabibbb8b1282014-09-22 17:00:18 -070094 providerService = registry.register(provider);
alshabib92c65ad2014-10-08 21:56:05 -070095 appId = new TestApplicationId((short) 0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070096 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",
alshabib97044902014-09-18 14:52:16 -0700104 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700105 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);
alshabiba0e04982014-10-03 13:03:19 -0700114 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700115 }
116
alshabibbb8b1282014-09-22 17:00:18 -0700117
118 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700119 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700120 service.applyFlowRules(rule);
121
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700122 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700123 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700124 }
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;
alshabibbb42cad2014-09-25 11:43:05 -0700132 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700133 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());
alshabibba5ac482014-10-02 17:15:20 -0700151 FlowRule f1 = addFlowRule(1);
152 FlowRule f2 = addFlowRule(2);
153
alshabib1c319ff2014-10-04 20:29:09 -0700154 FlowEntry fe1 = new DefaultFlowEntry(f1);
155 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700156 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700157
alshabib1c319ff2014-10-04 20:29:09 -0700158 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 validateEvents(RULE_ADDED, RULE_ADDED);
160
161 addFlowRule(1);
162 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700163
alshabib1c319ff2014-10-04 20:29:09 -0700164 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700165 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700166 }
167
alshabibbb8b1282014-09-22 17:00:18 -0700168
169 //backing store is sensitive to the order of additions/removals
alshabib1c319ff2014-10-04 20:29:09 -0700170 private boolean validateState(FlowEntryState... state) {
171 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabibcf369912014-10-13 14:16:42 -0700172 System.out.println(rules);
alshabibbb8b1282014-09-22 17:00:18 -0700173 int i = 0;
alshabib1c319ff2014-10-04 20:29:09 -0700174 for (FlowEntry f : rules) {
alshabibbb8b1282014-09-22 17:00:18 -0700175 if (f.state() != state[i]) {
176 return false;
177 }
178 i++;
179 }
180 return true;
181 }
182
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700183 @Test
184 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700185
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700186 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700187 FlowRule r2 = flowRule(2, 2);
188 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700189
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700190 assertTrue("store should be empty",
alshabibcf369912014-10-13 14:16:42 -0700191 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700192 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700193 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700194 assertTrue("Entries should be pending add.",
alshabibcf369912014-10-13 14:16:42 -0700195 validateState(FlowEntryState.PENDING_ADD, FlowEntryState.PENDING_ADD,
196 FlowEntryState.PENDING_ADD));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700197 }
198
199 @Test
200 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700201 FlowRule f1 = addFlowRule(1);
202 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700203 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700204 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700205
alshabib1c319ff2014-10-04 20:29:09 -0700206 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 Koshibeb55524f2014-09-18 09:59:24 -0700210 validateEvents(RULE_ADDED, RULE_ADDED, RULE_ADDED);
211
alshabib1c319ff2014-10-04 20:29:09 -0700212 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700213 //removing from north, so no events generated
214 validateEvents();
alshabib219ebaa2014-09-22 15:41:24 -0700215 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700216 assertTrue("Entries should be pending remove.",
alshabibcf369912014-10-13 14:16:42 -0700217 validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE,
218 FlowEntryState.ADDED));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700219
alshabib1c319ff2014-10-04 20:29:09 -0700220 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700221 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700222 }
223
224 @Test
225 public void flowRemoved() {
alshabibbb8b1282014-09-22 17:00:18 -0700226 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700227 FlowRule f2 = addFlowRule(2);
alshabib1c319ff2014-10-04 20:29:09 -0700228 FlowEntry fe1 = new DefaultFlowEntry(f1);
229 FlowEntry fe2 = new DefaultFlowEntry(f2);
230 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700231 service.removeFlowRules(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700232 fe1.setState(FlowEntryState.REMOVED);
233 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700234 validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED);
235
alshabib1c319ff2014-10-04 20:29:09 -0700236 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700237 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700238
alshabibba5ac482014-10-02 17:15:20 -0700239 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700240 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700241 service.applyFlowRules(f3);
alshabib1c319ff2014-10-04 20:29:09 -0700242 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700243 validateEvents(RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700244
alshabib1c319ff2014-10-04 20:29:09 -0700245 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700246 validateEvents();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700247 }
248
alshabibbb8b1282014-09-22 17:00:18 -0700249 @Test
250 public void flowMetrics() {
251 FlowRule f1 = flowRule(1, 1);
252 FlowRule f2 = flowRule(2, 2);
253 FlowRule f3 = flowRule(3, 3);
254
alshabibba5ac482014-10-02 17:15:20 -0700255 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700256
alshabib1c319ff2014-10-04 20:29:09 -0700257 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));
alshabibbb8b1282014-09-22 17:00:18 -0700265
266 assertTrue("Entries should be added.",
alshabib1c319ff2014-10-04 20:29:09 -0700267 validateState(FlowEntryState.ADDED, FlowEntryState.ADDED,
268 FlowEntryState.PENDING_ADD));
alshabibbb42cad2014-09-25 11:43:05 -0700269
alshabibba5ac482014-10-02 17:15:20 -0700270 validateEvents(RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700271 }
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);
alshabibba5ac482014-10-02 17:15:20 -0700278 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700279
alshabib1c319ff2014-10-04 20:29:09 -0700280// 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);
alshabibbb42cad2014-09-25 11:43:05 -0700286
alshabib1c319ff2014-10-04 20:29:09 -0700287
288 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700289
alshabibba5ac482014-10-02 17:15:20 -0700290 validateEvents(RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700291
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
alshabib1c319ff2014-10-04 20:29:09 -0700304// 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);
alshabibbb42cad2014-09-25 11:43:05 -0700309 mgr.applyFlowRules(f1, f2, f3);
310
311 mgr.removeFlowRules(f3);
312
alshabib1c319ff2014-10-04 20:29:09 -0700313 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700314
alshabibba5ac482014-10-02 17:15:20 -0700315 validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700316
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",
alshabibcf369912014-10-13 14:16:42 -0700326 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700327 }
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.
alshabib1c319ff2014-10-04 20:29:09 -0700340 validateState(FlowEntryState.PENDING_REMOVE, FlowEntryState.PENDING_REMOVE);
alshabibbb8b1282014-09-22 17:00:18 -0700341 }
342
alshabibcf369912014-10-13 14:16:42 -0700343 @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 Koshibeb55524f2014-09-18 09:59:24 -0700416 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
alshabiba68eb962014-09-24 20:34:13 -0700486 @Override
487 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
488 }
489
alshabib902d41b2014-10-07 16:52:05 -0700490 @Override
alshabib193525b2014-10-08 18:58:03 -0700491 public Future<CompletedBatchOperation> executeBatch(
alshabib902d41b2014-10-07 16:52:05 -0700492 BatchOperation<FlowRuleBatchEntry> batch) {
alshabibcf369912014-10-13 14:16:42 -0700493 return new TestInstallationFuture();
alshabib902d41b2014-10-07 16:52:05 -0700494 }
495
alshabibcf369912014-10-13 14:16:42 -0700496 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 }
alshabiba68eb962014-09-24 20:34:13 -0700527
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700528 }
529
530 private class TestSelector implements TrafficSelector {
531
532 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700533 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700534
535 public TestSelector(int val) {
536 testval = val;
537 }
538
539 @Override
alshabibba5ac482014-10-02 17:15:20 -0700540 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700541 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;
alshabib97044902014-09-18 14:52:16 -0700561 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700562
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
alshabib92c65ad2014-10-08 21:56:05 -0700587 public class TestApplicationId extends DefaultApplicationId {
588
589 public TestApplicationId(short id, String name) {
590 super(id, name);
591 }
592 }
593
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700594}