blob: f7d7287b8d1de547564090b8f827a5155db0249c [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.impl;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070017
Ray Milkeyda36c402015-02-18 10:06:06 -080018import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.ExecutionException;
26import java.util.concurrent.Executor;
27import java.util.concurrent.TimeUnit;
28import java.util.concurrent.TimeoutException;
29import java.util.concurrent.atomic.AtomicLong;
30
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070031import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080035import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080037import org.onosproject.core.IdGenerator;
38import org.onosproject.core.Version;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.event.impl.TestEventDispatcher;
40import org.onosproject.net.DefaultDevice;
41import org.onosproject.net.Device;
42import org.onosproject.net.Device.Type;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.MastershipRole;
45import org.onosproject.net.Port;
46import org.onosproject.net.PortNumber;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.CompletedBatchOperation;
50import org.onosproject.net.flow.DefaultFlowEntry;
51import org.onosproject.net.flow.DefaultFlowRule;
52import org.onosproject.net.flow.FlowEntry;
53import org.onosproject.net.flow.FlowEntry.FlowEntryState;
54import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.flow.FlowRuleBatchOperation;
56import org.onosproject.net.flow.FlowRuleEvent;
57import org.onosproject.net.flow.FlowRuleListener;
58import org.onosproject.net.flow.FlowRuleProvider;
59import org.onosproject.net.flow.FlowRuleProviderRegistry;
60import org.onosproject.net.flow.FlowRuleProviderService;
61import org.onosproject.net.flow.FlowRuleService;
62import org.onosproject.net.flow.StoredFlowEntry;
63import org.onosproject.net.flow.TrafficSelector;
64import org.onosproject.net.flow.TrafficTreatment;
65import org.onosproject.net.flow.criteria.Criterion;
66import org.onosproject.net.flow.instructions.Instruction;
67import org.onosproject.net.provider.AbstractProvider;
68import org.onosproject.net.provider.ProviderId;
69import org.onosproject.store.trivial.impl.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070070
Ray Milkeyda36c402015-02-18 10:06:06 -080071import com.google.common.collect.ImmutableList;
72import com.google.common.collect.ImmutableMap;
73import com.google.common.collect.Lists;
74import com.google.common.collect.Sets;
75import com.google.common.util.concurrent.ListenableFuture;
76import com.google.common.util.concurrent.MoreExecutors;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070077
Ray Milkeyda36c402015-02-18 10:06:06 -080078import static org.junit.Assert.assertEquals;
79import static org.junit.Assert.assertFalse;
80import static org.junit.Assert.assertNotNull;
81import static org.junit.Assert.assertTrue;
82import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADDED;
83import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
84import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
85import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
86import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070087
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070088/**
89 * Test codifying the flow rule service & flow rule provider service contracts.
90 */
tom202175a2014-09-19 19:00:11 -070091public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070092
alshabib92c65ad2014-10-08 21:56:05 -070093
tomf6ab2152014-09-18 12:08:29 -070094 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070095 private static final DeviceId DID = DeviceId.deviceId("of:001");
alshabibba5ac482014-10-02 17:15:20 -070096 private static final int TIMEOUT = 10;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070097 private static final Device DEV = new DefaultDevice(
alshabib7911a052014-10-16 17:49:37 -070098 PID, DID, Type.SWITCH, "", "", "", "", null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070099
tom202175a2014-09-19 19:00:11 -0700100 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700101
102 protected FlowRuleService service;
103 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -0700104 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700105 protected TestProvider provider;
106 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -0700107 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700108
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800109
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700110 @Before
111 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700112 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700113 mgr.store = new SimpleFlowRuleStore();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700114 mgr.eventDispatcher = new TestEventDispatcher();
115 mgr.deviceService = new TestDeviceService();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800116 mgr.coreService = new TestCoreService();
117 mgr.operationsService = MoreExecutors.newDirectExecutorService();
118 mgr.deviceInstallers = MoreExecutors.newDirectExecutorService();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700119 service = mgr;
120 registry = mgr;
121
122 mgr.activate();
123 mgr.addListener(listener);
124 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700125 providerService = registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700127 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700128 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700129 }
130
131 @After
132 public void tearDown() {
133 registry.unregister(provider);
134 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700135 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700136 service.removeListener(listener);
137 mgr.deactivate();
138 mgr.eventDispatcher = null;
139 mgr.deviceService = null;
140 }
141
142 private FlowRule flowRule(int tsval, int trval) {
143 TestSelector ts = new TestSelector(tsval);
144 TestTreatment tr = new TestTreatment(trval);
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700145 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700146 }
147
alshabibbb8b1282014-09-22 17:00:18 -0700148
149 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700150 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700151 service.applyFlowRules(rule);
152
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700153 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700154 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700155 }
156
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700157 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700158 if (events == null) {
159 assertTrue("events generated", listener.events.isEmpty());
160 }
161
162 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700163 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700164 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800165 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700166 i++;
167 }
168
169 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700170 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700171
172 listener.events.clear();
173 }
174
175 private int flowCount() {
176 return Sets.newHashSet(service.getFlowEntries(DID)).size();
177 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700178
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700179 @Test
180 public void getFlowEntries() {
181 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700182 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700183 FlowRule f1 = addFlowRule(1);
184 FlowRule f2 = addFlowRule(2);
185
alshabib1c319ff2014-10-04 20:29:09 -0700186 FlowEntry fe1 = new DefaultFlowEntry(f1);
187 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700188 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700189
alshabib1c319ff2014-10-04 20:29:09 -0700190 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700191 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
192 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700193
194 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800195 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700196 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700197
alshabib1c319ff2014-10-04 20:29:09 -0700198 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700199 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700200 }
201
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700202 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
203 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700204 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700205 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700206 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
207 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
208 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700209 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700210 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700211 return true;
212 }
213
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700214 @Test
215 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700216
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700217 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700218 FlowRule r2 = flowRule(2, 2);
219 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700220
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700221 assertTrue("store should be empty",
alshabibcf369912014-10-13 14:16:42 -0700222 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700223 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700224 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700225 assertTrue("Entries should be pending add.",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700226 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700227 r1, FlowEntryState.PENDING_ADD,
228 r2, FlowEntryState.PENDING_ADD,
229 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700230 }
231
232 @Test
233 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700234 FlowRule f1 = addFlowRule(1);
235 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700236 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700237 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700238
alshabib1c319ff2014-10-04 20:29:09 -0700239 FlowEntry fe1 = new DefaultFlowEntry(f1);
240 FlowEntry fe2 = new DefaultFlowEntry(f2);
241 FlowEntry fe3 = new DefaultFlowEntry(f3);
242 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700243 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
244 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700245
alshabib1c319ff2014-10-04 20:29:09 -0700246 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700247 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700248 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700249 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700250 assertTrue("Entries should be pending remove.",
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700251 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700252 f1, FlowEntryState.PENDING_REMOVE,
253 f2, FlowEntryState.PENDING_REMOVE,
254 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700255
alshabib1c319ff2014-10-04 20:29:09 -0700256 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700257 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700258 }
259
260 @Test
261 public void flowRemoved() {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800262
alshabibbb8b1282014-09-22 17:00:18 -0700263 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700264 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700265 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700266 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800267
268
alshabib1c319ff2014-10-04 20:29:09 -0700269 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700270 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800271
alshabib1c319ff2014-10-04 20:29:09 -0700272 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800273
274
275
alshabib1c319ff2014-10-04 20:29:09 -0700276 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800277
278
alshabib3d643ec2014-10-22 18:33:00 -0700279 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
280 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700281
alshabib1c319ff2014-10-04 20:29:09 -0700282 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700283 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700284
alshabibba5ac482014-10-02 17:15:20 -0700285 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700286 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700287 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800288
alshabib1c319ff2014-10-04 20:29:09 -0700289 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700290 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700291
alshabib1c319ff2014-10-04 20:29:09 -0700292 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700293 validateEvents();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800294
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700295 }
296
alshabibbb8b1282014-09-22 17:00:18 -0700297 @Test
298 public void flowMetrics() {
299 FlowRule f1 = flowRule(1, 1);
300 FlowRule f2 = flowRule(2, 2);
301 FlowRule f3 = flowRule(3, 3);
302
alshabibba5ac482014-10-02 17:15:20 -0700303 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700304
alshabib1c319ff2014-10-04 20:29:09 -0700305 FlowEntry fe1 = new DefaultFlowEntry(f1);
306 FlowEntry fe2 = new DefaultFlowEntry(f2);
307
alshabib1c319ff2014-10-04 20:29:09 -0700308 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
309 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
310
311 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700312
313 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700314 validateState(ImmutableMap.of(
315 f1, FlowEntryState.ADDED,
316 f2, FlowEntryState.ADDED,
317 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700318
alshabib3d643ec2014-10-22 18:33:00 -0700319 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
320 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700321 }
322
323 @Test
324 public void extraneousFlow() {
325 FlowRule f1 = flowRule(1, 1);
326 FlowRule f2 = flowRule(2, 2);
327 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700328 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700329
alshabib1c319ff2014-10-04 20:29:09 -0700330// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
331// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
332// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
333 FlowEntry fe1 = new DefaultFlowEntry(f1);
334 FlowEntry fe2 = new DefaultFlowEntry(f2);
335 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700336
alshabib1c319ff2014-10-04 20:29:09 -0700337
338 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700339
alshabib3d643ec2014-10-22 18:33:00 -0700340 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700341
342 }
343
344 /*
345 * Tests whether a rule that was marked for removal but no flowRemoved was received
346 * is indeed removed at the next stats update.
347 */
348 @Test
349 public void flowMissingRemove() {
350 FlowRule f1 = flowRule(1, 1);
351 FlowRule f2 = flowRule(2, 2);
352 FlowRule f3 = flowRule(3, 3);
353
alshabib1c319ff2014-10-04 20:29:09 -0700354// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
355// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
356
357 FlowEntry fe1 = new DefaultFlowEntry(f1);
358 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700359 mgr.applyFlowRules(f1, f2, f3);
360
361 mgr.removeFlowRules(f3);
362
alshabib1c319ff2014-10-04 20:29:09 -0700363 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700364
alshabib3d643ec2014-10-22 18:33:00 -0700365 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
366 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700367
368 }
369
370 @Test
371 public void getByAppId() {
372 FlowRule f1 = flowRule(1, 1);
373 FlowRule f2 = flowRule(2, 2);
374 mgr.applyFlowRules(f1, f2);
375
376 assertTrue("should have two rules",
alshabibcf369912014-10-13 14:16:42 -0700377 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700378 }
379
380 @Test
381 public void removeByAppId() {
382 FlowRule f1 = flowRule(1, 1);
383 FlowRule f2 = flowRule(2, 2);
384 mgr.applyFlowRules(f1, f2);
385
386
387 mgr.removeFlowRulesById(appId);
388
389 //only check that we are in pending remove. Events and actual remove state will
390 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700391 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700392 f1, FlowEntryState.PENDING_REMOVE,
393 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700394 }
395
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700396 private static class TestListener implements FlowRuleListener {
397 final List<FlowRuleEvent> events = new ArrayList<>();
398
399 @Override
400 public void event(FlowRuleEvent event) {
401 events.add(event);
402 }
403 }
404
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800405 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700406
407 @Override
408 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700409 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700410 }
411
412 @Override
413 public Iterable<Device> getDevices() {
Madan Jampani6a456162014-10-24 11:36:17 -0700414 return Arrays.asList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700415 }
416
417 @Override
418 public Device getDevice(DeviceId deviceId) {
419 return DEV;
420 }
421
422 @Override
423 public MastershipRole getRole(DeviceId deviceId) {
424 return null;
425 }
426
427 @Override
428 public List<Port> getPorts(DeviceId deviceId) {
429 return null;
430 }
431
432 @Override
433 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
434 return null;
435 }
436
437 @Override
438 public boolean isAvailable(DeviceId deviceId) {
439 return false;
440 }
441
442 @Override
443 public void addListener(DeviceListener listener) {
444 }
445
446 @Override
447 public void removeListener(DeviceListener listener) {
448 }
449
450 }
451
452 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
453
454 protected TestProvider(ProviderId id) {
455 super(PID);
456 }
457
458 @Override
459 public void applyFlowRule(FlowRule... flowRules) {
460 }
461
462 @Override
463 public void removeFlowRule(FlowRule... flowRules) {
464 }
465
alshabiba68eb962014-09-24 20:34:13 -0700466 @Override
467 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
468 }
469
alshabib902d41b2014-10-07 16:52:05 -0700470 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800471 public void executeBatch(FlowRuleBatchOperation batch) {
472 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700473 }
474
alshabibcf369912014-10-13 14:16:42 -0700475 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700476 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700477
478 @Override
479 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800480 return false;
alshabibcf369912014-10-13 14:16:42 -0700481 }
482
483 @Override
484 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800485 return false;
alshabibcf369912014-10-13 14:16:42 -0700486 }
487
488 @Override
489 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800490 return true;
alshabibcf369912014-10-13 14:16:42 -0700491 }
492
493 @Override
494 public CompletedBatchOperation get()
495 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800496 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700497 }
498
499 @Override
500 public CompletedBatchOperation get(long timeout, TimeUnit unit)
501 throws InterruptedException,
502 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800503 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700504 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700505
506 @Override
507 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800508 if (isDone()) {
509 executor.execute(task);
510 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700511 }
alshabibcf369912014-10-13 14:16:42 -0700512 }
alshabiba68eb962014-09-24 20:34:13 -0700513
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700514 }
515
516 private class TestSelector implements TrafficSelector {
517
518 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700519 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700520
521 public TestSelector(int val) {
522 testval = val;
523 }
524
525 @Override
alshabibba5ac482014-10-02 17:15:20 -0700526 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700527 return null;
528 }
529
530 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700531 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800532 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700533 return null;
534 }
535
536 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700537 public int hashCode() {
538 return testval;
539 }
540
541 @Override
542 public boolean equals(Object o) {
543 if (o instanceof TestSelector) {
544 return this.testval == ((TestSelector) o).testval;
545 }
546 return false;
547 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700548
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700549 }
550
551 private class TestTreatment implements TrafficTreatment {
552
553 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700554 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700555
556 public TestTreatment(int val) {
557 testval = val;
558 }
559
560 @Override
561 public List<Instruction> instructions() {
562 return null;
563 }
564
565 @Override
566 public int hashCode() {
567 return testval;
568 }
569
570 @Override
571 public boolean equals(Object o) {
572 if (o instanceof TestTreatment) {
573 return this.testval == ((TestTreatment) o).testval;
574 }
575 return false;
576 }
577
578 }
579
alshabib92c65ad2014-10-08 21:56:05 -0700580 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800581 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700582 super(id, name);
583 }
584 }
585
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800586 private class TestCoreService implements CoreService {
587 @Override
588 public Version version() {
589 return null;
590 }
591
592 @Override
593 public Set<ApplicationId> getAppIds() {
594 return null;
595 }
596
597 @Override
598 public ApplicationId getAppId(Short id) {
599 return null;
600 }
601
602 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800603 public ApplicationId getAppId(String name) {
604 return null;
605 }
606
607 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800608 public ApplicationId registerApplication(String identifier) {
609 return null;
610 }
611
612 @Override
613 public IdGenerator getIdGenerator(String topic) {
614 return new IdGenerator() {
615 private AtomicLong counter = new AtomicLong(0);
616 @Override
617 public long getNewId() {
618 return counter.getAndIncrement();
619 }
620 };
621 }
622 }
623
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700624}