blob: 2bb0965328f3bc47ef794b670f578f664c1a119d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.flow.impl;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070017
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Sets;
22import com.google.common.util.concurrent.ListenableFuture;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070023import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070026import org.onlab.onos.core.ApplicationId;
27import org.onlab.onos.core.DefaultApplicationId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070028import org.onlab.onos.event.impl.TestEventDispatcher;
29import org.onlab.onos.net.DefaultDevice;
30import org.onlab.onos.net.Device;
31import org.onlab.onos.net.Device.Type;
32import org.onlab.onos.net.DeviceId;
33import org.onlab.onos.net.MastershipRole;
34import org.onlab.onos.net.Port;
35import org.onlab.onos.net.PortNumber;
36import org.onlab.onos.net.device.DeviceListener;
37import org.onlab.onos.net.device.DeviceService;
Jonathan Hart936c49d2014-10-23 16:38:59 -070038import org.onlab.onos.net.flow.BatchOperation;
alshabib193525b2014-10-08 18:58:03 -070039import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070040import org.onlab.onos.net.flow.DefaultFlowEntry;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070041import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib1c319ff2014-10-04 20:29:09 -070042import org.onlab.onos.net.flow.FlowEntry;
43import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070044import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070045import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabibcf369912014-10-13 14:16:42 -070046import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070047import org.onlab.onos.net.flow.FlowRuleEvent;
48import org.onlab.onos.net.flow.FlowRuleListener;
49import org.onlab.onos.net.flow.FlowRuleProvider;
50import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
51import org.onlab.onos.net.flow.FlowRuleProviderService;
52import org.onlab.onos.net.flow.FlowRuleService;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070053import org.onlab.onos.net.flow.StoredFlowEntry;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070054import org.onlab.onos.net.flow.TrafficSelector;
55import org.onlab.onos.net.flow.TrafficTreatment;
56import org.onlab.onos.net.flow.criteria.Criterion;
57import org.onlab.onos.net.flow.instructions.Instruction;
58import org.onlab.onos.net.provider.AbstractProvider;
59import org.onlab.onos.net.provider.ProviderId;
tomea961ff2014-10-01 12:45:15 -070060import org.onlab.onos.store.trivial.impl.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070061
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070062import java.util.ArrayList;
63import java.util.Arrays;
64import java.util.Collections;
65import java.util.HashMap;
66import java.util.List;
67import java.util.Map;
68import java.util.Set;
69import java.util.concurrent.ExecutionException;
70import java.util.concurrent.Executor;
71import java.util.concurrent.Future;
72import java.util.concurrent.TimeUnit;
73import java.util.concurrent.TimeoutException;
74
75import static org.junit.Assert.*;
76import static org.onlab.onos.net.flow.FlowRuleEvent.Type.*;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070077
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070078/**
79 * Test codifying the flow rule service & flow rule provider service contracts.
80 */
tom202175a2014-09-19 19:00:11 -070081public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070082
alshabib92c65ad2014-10-08 21:56:05 -070083
tomf6ab2152014-09-18 12:08:29 -070084 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070085 private static final DeviceId DID = DeviceId.deviceId("of:001");
alshabibba5ac482014-10-02 17:15:20 -070086 private static final int TIMEOUT = 10;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070087 private static final Device DEV = new DefaultDevice(
alshabib7911a052014-10-16 17:49:37 -070088 PID, DID, Type.SWITCH, "", "", "", "", null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070089
tom202175a2014-09-19 19:00:11 -070090 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070091
92 protected FlowRuleService service;
93 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -070094 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070095 protected TestProvider provider;
96 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -070097 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070098
99 @Before
100 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700101 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700102 mgr.store = new SimpleFlowRuleStore();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700103 mgr.eventDispatcher = new TestEventDispatcher();
104 mgr.deviceService = new TestDeviceService();
105 service = mgr;
106 registry = mgr;
107
108 mgr.activate();
109 mgr.addListener(listener);
110 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700111 providerService = registry.register(provider);
alshabib92c65ad2014-10-08 21:56:05 -0700112 appId = new TestApplicationId((short) 0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700113 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700114 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700115 }
116
117 @After
118 public void tearDown() {
119 registry.unregister(provider);
120 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700121 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700122 service.removeListener(listener);
123 mgr.deactivate();
124 mgr.eventDispatcher = null;
125 mgr.deviceService = null;
126 }
127
128 private FlowRule flowRule(int tsval, int trval) {
129 TestSelector ts = new TestSelector(tsval);
130 TestTreatment tr = new TestTreatment(trval);
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700131 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700132 }
133
alshabibbb8b1282014-09-22 17:00:18 -0700134
135 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700136 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700137 service.applyFlowRules(rule);
138
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700139 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700140 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700141 }
142
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700143 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700144 if (events == null) {
145 assertTrue("events generated", listener.events.isEmpty());
146 }
147
148 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700149 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700150 for (FlowRuleEvent e : listener.events) {
151 assertTrue("unexpected event", e.type().equals(events[i]));
152 i++;
153 }
154
155 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700156 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700157
158 listener.events.clear();
159 }
160
161 private int flowCount() {
162 return Sets.newHashSet(service.getFlowEntries(DID)).size();
163 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700164
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700165 @Test
166 public void getFlowEntries() {
167 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700168 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700169 FlowRule f1 = addFlowRule(1);
170 FlowRule f2 = addFlowRule(2);
171
alshabib1c319ff2014-10-04 20:29:09 -0700172 FlowEntry fe1 = new DefaultFlowEntry(f1);
173 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700174 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700175
alshabib1c319ff2014-10-04 20:29:09 -0700176 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700177 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
178 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700179
180 addFlowRule(1);
181 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700182
alshabib1c319ff2014-10-04 20:29:09 -0700183 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700184 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700185 }
186
alshabibbb8b1282014-09-22 17:00:18 -0700187
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700188 // TODO: If preserving iteration order is a requirement, redo FlowRuleStore.
alshabibbb8b1282014-09-22 17:00:18 -0700189 //backing store is sensitive to the order of additions/removals
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700190 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
191 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700192 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700193 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700194 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
195 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
196 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700197 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700198 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700199 return true;
200 }
201
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700202 @Test
203 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700204
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700205 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700206 FlowRule r2 = flowRule(2, 2);
207 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700208
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700209 assertTrue("store should be empty",
alshabibcf369912014-10-13 14:16:42 -0700210 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700211 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700212 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700213 assertTrue("Entries should be pending add.",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700214 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700215 r1, FlowEntryState.PENDING_ADD,
216 r2, FlowEntryState.PENDING_ADD,
217 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700218 }
219
220 @Test
221 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700222 FlowRule f1 = addFlowRule(1);
223 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700224 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700225 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700226
alshabib1c319ff2014-10-04 20:29:09 -0700227 FlowEntry fe1 = new DefaultFlowEntry(f1);
228 FlowEntry fe2 = new DefaultFlowEntry(f2);
229 FlowEntry fe3 = new DefaultFlowEntry(f3);
230 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700231 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
232 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700233
alshabib1c319ff2014-10-04 20:29:09 -0700234 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700235 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700236 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700237 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700238 assertTrue("Entries should be pending remove.",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700239 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700240 f1, FlowEntryState.PENDING_REMOVE,
241 f2, FlowEntryState.PENDING_REMOVE,
242 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700243
alshabib1c319ff2014-10-04 20:29:09 -0700244 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700245 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700246 }
247
248 @Test
249 public void flowRemoved() {
alshabibbb8b1282014-09-22 17:00:18 -0700250 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700251 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700252 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700253 FlowEntry fe2 = new DefaultFlowEntry(f2);
254 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700255 service.removeFlowRules(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700256 fe1.setState(FlowEntryState.REMOVED);
257 providerService.flowRemoved(fe1);
alshabib3d643ec2014-10-22 18:33:00 -0700258 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
259 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700260
alshabib1c319ff2014-10-04 20:29:09 -0700261 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700262 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700263
alshabibba5ac482014-10-02 17:15:20 -0700264 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700265 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700266 service.applyFlowRules(f3);
alshabib1c319ff2014-10-04 20:29:09 -0700267 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700268 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700269
alshabib1c319ff2014-10-04 20:29:09 -0700270 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700271 validateEvents();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700272 }
273
alshabibbb8b1282014-09-22 17:00:18 -0700274 @Test
275 public void flowMetrics() {
276 FlowRule f1 = flowRule(1, 1);
277 FlowRule f2 = flowRule(2, 2);
278 FlowRule f3 = flowRule(3, 3);
279
alshabibba5ac482014-10-02 17:15:20 -0700280 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700281
alshabib1c319ff2014-10-04 20:29:09 -0700282 FlowEntry fe1 = new DefaultFlowEntry(f1);
283 FlowEntry fe2 = new DefaultFlowEntry(f2);
284
285
286 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
287 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
288
289 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700290
291 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700292 validateState(ImmutableMap.of(
293 f1, FlowEntryState.ADDED,
294 f2, FlowEntryState.ADDED,
295 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700296
alshabib3d643ec2014-10-22 18:33:00 -0700297 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
298 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700299 }
300
301 @Test
302 public void extraneousFlow() {
303 FlowRule f1 = flowRule(1, 1);
304 FlowRule f2 = flowRule(2, 2);
305 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700306 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700307
alshabib1c319ff2014-10-04 20:29:09 -0700308// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
309// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
310// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
311 FlowEntry fe1 = new DefaultFlowEntry(f1);
312 FlowEntry fe2 = new DefaultFlowEntry(f2);
313 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700314
alshabib1c319ff2014-10-04 20:29:09 -0700315
316 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700317
alshabib3d643ec2014-10-22 18:33:00 -0700318 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700319
320 }
321
322 /*
323 * Tests whether a rule that was marked for removal but no flowRemoved was received
324 * is indeed removed at the next stats update.
325 */
326 @Test
327 public void flowMissingRemove() {
328 FlowRule f1 = flowRule(1, 1);
329 FlowRule f2 = flowRule(2, 2);
330 FlowRule f3 = flowRule(3, 3);
331
alshabib1c319ff2014-10-04 20:29:09 -0700332// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
333// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
334
335 FlowEntry fe1 = new DefaultFlowEntry(f1);
336 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700337 mgr.applyFlowRules(f1, f2, f3);
338
339 mgr.removeFlowRules(f3);
340
alshabib1c319ff2014-10-04 20:29:09 -0700341 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700342
alshabib3d643ec2014-10-22 18:33:00 -0700343 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
344 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700345
346 }
347
348 @Test
349 public void getByAppId() {
350 FlowRule f1 = flowRule(1, 1);
351 FlowRule f2 = flowRule(2, 2);
352 mgr.applyFlowRules(f1, f2);
353
354 assertTrue("should have two rules",
alshabibcf369912014-10-13 14:16:42 -0700355 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700356 }
357
358 @Test
359 public void removeByAppId() {
360 FlowRule f1 = flowRule(1, 1);
361 FlowRule f2 = flowRule(2, 2);
362 mgr.applyFlowRules(f1, f2);
363
364
365 mgr.removeFlowRulesById(appId);
366
367 //only check that we are in pending remove. Events and actual remove state will
368 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700369 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700370 f1, FlowEntryState.PENDING_REMOVE,
371 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700372 }
373
alshabibcf369912014-10-13 14:16:42 -0700374 @Test
375 public void applyBatch() {
376 FlowRule f1 = flowRule(1, 1);
377 FlowRule f2 = flowRule(2, 2);
378
379
380 mgr.applyFlowRules(f1);
381
382 FlowEntry fe1 = new DefaultFlowEntry(f1);
383 providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1));
384
385 FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry(
386 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1);
387
388 FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry(
389 FlowRuleBatchEntry.FlowRuleOperation.ADD, f2);
390
391 FlowRuleBatchOperation fbo = new FlowRuleBatchOperation(
392 Lists.newArrayList(fbe1, fbe2));
393 Future<CompletedBatchOperation> future = mgr.applyBatch(fbo);
394 assertTrue("Entries in wrong state",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700395 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700396 f1, FlowEntryState.PENDING_REMOVE,
397 f2, FlowEntryState.PENDING_ADD)));
alshabibcf369912014-10-13 14:16:42 -0700398 CompletedBatchOperation completed = null;
399 try {
400 completed = future.get();
401 } catch (InterruptedException | ExecutionException e) {
402 fail("Unexpected exception: " + e);
403 }
404 if (!completed.isSuccess()) {
405 fail("Installation should be a success");
406 }
407
408 }
409
410 @Test
411 public void cancelBatch() {
412 FlowRule f1 = flowRule(1, 1);
413 FlowRule f2 = flowRule(2, 2);
414
415
416 mgr.applyFlowRules(f1);
417
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700418 assertTrue("Entries in wrong state",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700419 validateState(ImmutableMap.of(
420 f1, FlowEntryState.PENDING_ADD)));
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700421
alshabibcf369912014-10-13 14:16:42 -0700422 FlowEntry fe1 = new DefaultFlowEntry(f1);
423 providerService.pushFlowMetrics(DID, Collections.<FlowEntry>singletonList(fe1));
424
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700425 assertTrue("Entries in wrong state",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700426 validateState(ImmutableMap.of(
427 f1, FlowEntryState.ADDED)));
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700428
429
alshabibcf369912014-10-13 14:16:42 -0700430 FlowRuleBatchEntry fbe1 = new FlowRuleBatchEntry(
431 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, f1);
432
433 FlowRuleBatchEntry fbe2 = new FlowRuleBatchEntry(
434 FlowRuleBatchEntry.FlowRuleOperation.ADD, f2);
435
436 FlowRuleBatchOperation fbo = new FlowRuleBatchOperation(
437 Lists.newArrayList(fbe1, fbe2));
438 Future<CompletedBatchOperation> future = mgr.applyBatch(fbo);
439
440 future.cancel(true);
441
442 assertTrue(flowCount() == 2);
443
444 /*
445 * Rule f1 should be re-added to the list and therefore be in a pending add
446 * state.
447 */
448 assertTrue("Entries in wrong state",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700449 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700450 f2, FlowEntryState.PENDING_REMOVE,
451 f1, FlowEntryState.PENDING_ADD)));
alshabibcf369912014-10-13 14:16:42 -0700452
453
alshabibcf369912014-10-13 14:16:42 -0700454 }
455
456
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700457 private static class TestListener implements FlowRuleListener {
458 final List<FlowRuleEvent> events = new ArrayList<>();
459
460 @Override
461 public void event(FlowRuleEvent event) {
462 events.add(event);
463 }
464 }
465
466 private static class TestDeviceService implements DeviceService {
467
468 @Override
469 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700470 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700471 }
472
473 @Override
474 public Iterable<Device> getDevices() {
Madan Jampani6a456162014-10-24 11:36:17 -0700475 return Arrays.asList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700476 }
477
478 @Override
479 public Device getDevice(DeviceId deviceId) {
480 return DEV;
481 }
482
483 @Override
484 public MastershipRole getRole(DeviceId deviceId) {
485 return null;
486 }
487
488 @Override
489 public List<Port> getPorts(DeviceId deviceId) {
490 return null;
491 }
492
493 @Override
494 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
495 return null;
496 }
497
498 @Override
499 public boolean isAvailable(DeviceId deviceId) {
500 return false;
501 }
502
503 @Override
504 public void addListener(DeviceListener listener) {
505 }
506
507 @Override
508 public void removeListener(DeviceListener listener) {
509 }
510
511 }
512
513 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
514
515 protected TestProvider(ProviderId id) {
516 super(PID);
517 }
518
519 @Override
520 public void applyFlowRule(FlowRule... flowRules) {
521 }
522
523 @Override
524 public void removeFlowRule(FlowRule... flowRules) {
525 }
526
alshabiba68eb962014-09-24 20:34:13 -0700527 @Override
528 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
529 }
530
alshabib902d41b2014-10-07 16:52:05 -0700531 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700532 public ListenableFuture<CompletedBatchOperation> executeBatch(
alshabib902d41b2014-10-07 16:52:05 -0700533 BatchOperation<FlowRuleBatchEntry> batch) {
alshabibcf369912014-10-13 14:16:42 -0700534 return new TestInstallationFuture();
alshabib902d41b2014-10-07 16:52:05 -0700535 }
536
alshabibcf369912014-10-13 14:16:42 -0700537 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700538 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700539
540 @Override
541 public boolean cancel(boolean mayInterruptIfRunning) {
542 return true;
543 }
544
545 @Override
546 public boolean isCancelled() {
547 return true;
548 }
549
550 @Override
551 public boolean isDone() {
552 return false;
553 }
554
555 @Override
556 public CompletedBatchOperation get()
557 throws InterruptedException, ExecutionException {
Madan Jampani117aaae2014-10-23 10:04:05 -0700558 return new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet());
alshabibcf369912014-10-13 14:16:42 -0700559 }
560
561 @Override
562 public CompletedBatchOperation get(long timeout, TimeUnit unit)
563 throws InterruptedException,
564 ExecutionException, TimeoutException {
565 return null;
566 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700567
568 @Override
569 public void addListener(Runnable task, Executor executor) {
570 // TODO: add stuff.
571 }
alshabibcf369912014-10-13 14:16:42 -0700572 }
alshabiba68eb962014-09-24 20:34:13 -0700573
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700574 }
575
576 private class TestSelector implements TrafficSelector {
577
578 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700579 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700580
581 public TestSelector(int val) {
582 testval = val;
583 }
584
585 @Override
alshabibba5ac482014-10-02 17:15:20 -0700586 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700587 return null;
588 }
589
590 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700591 public Criterion getCriterion(
592 org.onlab.onos.net.flow.criteria.Criterion.Type type) {
593 return null;
594 }
595
596 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700597 public int hashCode() {
598 return testval;
599 }
600
601 @Override
602 public boolean equals(Object o) {
603 if (o instanceof TestSelector) {
604 return this.testval == ((TestSelector) o).testval;
605 }
606 return false;
607 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700608
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700609 }
610
611 private class TestTreatment implements TrafficTreatment {
612
613 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700614 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700615
616 public TestTreatment(int val) {
617 testval = val;
618 }
619
620 @Override
621 public List<Instruction> instructions() {
622 return null;
623 }
624
625 @Override
626 public int hashCode() {
627 return testval;
628 }
629
630 @Override
631 public boolean equals(Object o) {
632 if (o instanceof TestTreatment) {
633 return this.testval == ((TestTreatment) o).testval;
634 }
635 return false;
636 }
637
638 }
639
alshabib92c65ad2014-10-08 21:56:05 -0700640 public class TestApplicationId extends DefaultApplicationId {
641
642 public TestApplicationId(short id, String name) {
643 super(id, name);
644 }
645 }
646
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700647}