blob: 1113b2bc86dd752c2adddd1163456b6cc8904b94 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -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;
23import com.google.common.util.concurrent.MoreExecutors;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070024import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070027import org.onosproject.cfg.ComponentConfigAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.core.ApplicationId;
Ray Milkeycc53abd2015-02-19 12:31:33 -080029import org.onosproject.core.CoreServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080031import org.onosproject.core.IdGenerator;
Thomas Vachuska36002e62015-05-19 16:12:29 -070032import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.DefaultDevice;
34import org.onosproject.net.Device;
35import org.onosproject.net.Device.Type;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.MastershipRole;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.device.DeviceListener;
41import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.flow.CompletedBatchOperation;
43import org.onosproject.net.flow.DefaultFlowEntry;
44import org.onosproject.net.flow.DefaultFlowRule;
45import org.onosproject.net.flow.FlowEntry;
46import org.onosproject.net.flow.FlowEntry.FlowEntryState;
47import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.flow.FlowRuleBatchOperation;
49import org.onosproject.net.flow.FlowRuleEvent;
50import org.onosproject.net.flow.FlowRuleListener;
51import org.onosproject.net.flow.FlowRuleProvider;
52import org.onosproject.net.flow.FlowRuleProviderRegistry;
53import org.onosproject.net.flow.FlowRuleProviderService;
54import org.onosproject.net.flow.FlowRuleService;
55import org.onosproject.net.flow.StoredFlowEntry;
56import org.onosproject.net.flow.TrafficSelector;
57import org.onosproject.net.flow.TrafficTreatment;
58import org.onosproject.net.flow.criteria.Criterion;
59import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080060import org.onosproject.net.flow.instructions.Instructions;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.provider.AbstractProvider;
62import org.onosproject.net.provider.ProviderId;
63import org.onosproject.store.trivial.impl.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070064
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070065import java.util.ArrayList;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070066import java.util.Collections;
67import java.util.HashMap;
68import java.util.List;
69import java.util.Map;
70import java.util.Set;
71import java.util.concurrent.ExecutionException;
72import java.util.concurrent.Executor;
73import java.util.concurrent.TimeUnit;
74import java.util.concurrent.TimeoutException;
75import java.util.concurrent.atomic.AtomicLong;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070076
Ray Milkeyda36c402015-02-18 10:06:06 -080077import static org.junit.Assert.assertEquals;
78import static org.junit.Assert.assertFalse;
79import static org.junit.Assert.assertNotNull;
80import static org.junit.Assert.assertTrue;
81import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADDED;
82import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
83import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
84import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
85import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070086
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070087/**
88 * Test codifying the flow rule service & flow rule provider service contracts.
89 */
tom202175a2014-09-19 19:00:11 -070090public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070091
alshabib92c65ad2014-10-08 21:56:05 -070092
tomf6ab2152014-09-18 12:08:29 -070093 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070094 private static final DeviceId DID = DeviceId.deviceId("of:001");
alshabibba5ac482014-10-02 17:15:20 -070095 private static final int TIMEOUT = 10;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070096 private static final Device DEV = new DefaultDevice(
alshabib7911a052014-10-16 17:49:37 -070097 PID, DID, Type.SWITCH, "", "", "", "", null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070098
tom202175a2014-09-19 19:00:11 -070099 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700100
101 protected FlowRuleService service;
102 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -0700103 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700104 protected TestProvider provider;
105 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -0700106 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700107
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800108
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700109 @Before
110 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700111 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700112 mgr.store = new SimpleFlowRuleStore();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700113 mgr.eventDispatcher = new TestEventDispatcher();
114 mgr.deviceService = new TestDeviceService();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800115 mgr.coreService = new TestCoreService();
116 mgr.operationsService = MoreExecutors.newDirectExecutorService();
117 mgr.deviceInstallers = MoreExecutors.newDirectExecutorService();
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700118 mgr.cfgService = new ComponentConfigAdapter();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700119 service = mgr;
120 registry = mgr;
121
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700122 mgr.activate(null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700123 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",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800222 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.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800226 validateState(ImmutableMap.of(
227 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.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800251 validateState(ImmutableMap.of(
252 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,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800280 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,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800320 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",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800377 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() {
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700414 return Collections.singletonList(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
alshabib346b5b32015-03-06 00:42:16 -0800561 public List<Instruction> deferred() {
562 return null;
563 }
564
565 @Override
566 public List<Instruction> immediate() {
567 return null;
568 }
569
570 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700571 public List<Instruction> allInstructions() {
572 return null;
573 }
574
575 @Override
alshabib346b5b32015-03-06 00:42:16 -0800576 public Instructions.TableTypeTransition tableTransition() {
577 return null;
578 }
579
580 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700581 public boolean clearedDeferred() {
582 return false;
alshabib346b5b32015-03-06 00:42:16 -0800583 }
584
585 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700586 public int hashCode() {
587 return testval;
588 }
589
590 @Override
591 public boolean equals(Object o) {
592 if (o instanceof TestTreatment) {
593 return this.testval == ((TestTreatment) o).testval;
594 }
595 return false;
596 }
597
598 }
599
alshabib92c65ad2014-10-08 21:56:05 -0700600 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800601 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700602 super(id, name);
603 }
604 }
605
Ray Milkeycc53abd2015-02-19 12:31:33 -0800606 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800607
608 @Override
609 public IdGenerator getIdGenerator(String topic) {
610 return new IdGenerator() {
611 private AtomicLong counter = new AtomicLong(0);
612 @Override
613 public long getNewId() {
614 return counter.getAndIncrement();
615 }
616 };
617 }
618 }
619
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700620}