blob: 8634fb32b85d10b6db852a9924371ec7ff781e04 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.event.impl.TestEventDispatcher;
33import 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;
66import java.util.Arrays;
67import java.util.Collections;
68import java.util.HashMap;
69import java.util.List;
70import java.util.Map;
71import java.util.Set;
72import java.util.concurrent.ExecutionException;
73import java.util.concurrent.Executor;
74import java.util.concurrent.TimeUnit;
75import java.util.concurrent.TimeoutException;
76import java.util.concurrent.atomic.AtomicLong;
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();
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700119 mgr.cfgService = new ComponentConfigAdapter();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700120 service = mgr;
121 registry = mgr;
122
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700123 mgr.activate(null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700124 mgr.addListener(listener);
125 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700126 providerService = registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800127 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700128 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700129 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700130 }
131
132 @After
133 public void tearDown() {
134 registry.unregister(provider);
135 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700136 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700137 service.removeListener(listener);
138 mgr.deactivate();
139 mgr.eventDispatcher = null;
140 mgr.deviceService = null;
141 }
142
143 private FlowRule flowRule(int tsval, int trval) {
144 TestSelector ts = new TestSelector(tsval);
145 TestTreatment tr = new TestTreatment(trval);
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700146 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700147 }
148
alshabibbb8b1282014-09-22 17:00:18 -0700149
150 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700151 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700152 service.applyFlowRules(rule);
153
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700154 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700155 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700156 }
157
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700158 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 if (events == null) {
160 assertTrue("events generated", listener.events.isEmpty());
161 }
162
163 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700164 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700165 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800166 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700167 i++;
168 }
169
170 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700171 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700172
173 listener.events.clear();
174 }
175
176 private int flowCount() {
177 return Sets.newHashSet(service.getFlowEntries(DID)).size();
178 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700179
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700180 @Test
181 public void getFlowEntries() {
182 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700183 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700184 FlowRule f1 = addFlowRule(1);
185 FlowRule f2 = addFlowRule(2);
186
alshabib1c319ff2014-10-04 20:29:09 -0700187 FlowEntry fe1 = new DefaultFlowEntry(f1);
188 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700189 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700190
alshabib1c319ff2014-10-04 20:29:09 -0700191 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700192 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
193 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700194
195 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800196 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700197 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700198
alshabib1c319ff2014-10-04 20:29:09 -0700199 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700200 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700201 }
202
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700203 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
204 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700205 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700206 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700207 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
208 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
209 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700210 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700211 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700212 return true;
213 }
214
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700215 @Test
216 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700217
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700218 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700219 FlowRule r2 = flowRule(2, 2);
220 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700221
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700222 assertTrue("store should be empty",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800223 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700224 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700225 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700226 assertTrue("Entries should be pending add.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800227 validateState(ImmutableMap.of(
228 r1, FlowEntryState.PENDING_ADD,
229 r2, FlowEntryState.PENDING_ADD,
230 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700231 }
232
233 @Test
234 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700235 FlowRule f1 = addFlowRule(1);
236 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700237 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700238 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700239
alshabib1c319ff2014-10-04 20:29:09 -0700240 FlowEntry fe1 = new DefaultFlowEntry(f1);
241 FlowEntry fe2 = new DefaultFlowEntry(f2);
242 FlowEntry fe3 = new DefaultFlowEntry(f3);
243 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700244 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
245 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700246
alshabib1c319ff2014-10-04 20:29:09 -0700247 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700248 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700249 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700250 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700251 assertTrue("Entries should be pending remove.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800252 validateState(ImmutableMap.of(
253 f1, FlowEntryState.PENDING_REMOVE,
254 f2, FlowEntryState.PENDING_REMOVE,
255 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700256
alshabib1c319ff2014-10-04 20:29:09 -0700257 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700258 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700259 }
260
261 @Test
262 public void flowRemoved() {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800263
alshabibbb8b1282014-09-22 17:00:18 -0700264 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700265 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700266 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700267 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800268
269
alshabib1c319ff2014-10-04 20:29:09 -0700270 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700271 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800272
alshabib1c319ff2014-10-04 20:29:09 -0700273 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800274
275
276
alshabib1c319ff2014-10-04 20:29:09 -0700277 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800278
279
alshabib3d643ec2014-10-22 18:33:00 -0700280 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800281 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700282
alshabib1c319ff2014-10-04 20:29:09 -0700283 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700284 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700285
alshabibba5ac482014-10-02 17:15:20 -0700286 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700287 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700288 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800289
alshabib1c319ff2014-10-04 20:29:09 -0700290 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700291 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700292
alshabib1c319ff2014-10-04 20:29:09 -0700293 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700294 validateEvents();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800295
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700296 }
297
alshabibbb8b1282014-09-22 17:00:18 -0700298 @Test
299 public void flowMetrics() {
300 FlowRule f1 = flowRule(1, 1);
301 FlowRule f2 = flowRule(2, 2);
302 FlowRule f3 = flowRule(3, 3);
303
alshabibba5ac482014-10-02 17:15:20 -0700304 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700305
alshabib1c319ff2014-10-04 20:29:09 -0700306 FlowEntry fe1 = new DefaultFlowEntry(f1);
307 FlowEntry fe2 = new DefaultFlowEntry(f2);
308
alshabib1c319ff2014-10-04 20:29:09 -0700309 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
310 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
311
312 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700313
314 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700315 validateState(ImmutableMap.of(
316 f1, FlowEntryState.ADDED,
317 f2, FlowEntryState.ADDED,
318 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700319
alshabib3d643ec2014-10-22 18:33:00 -0700320 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800321 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700322 }
323
324 @Test
325 public void extraneousFlow() {
326 FlowRule f1 = flowRule(1, 1);
327 FlowRule f2 = flowRule(2, 2);
328 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700329 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700330
alshabib1c319ff2014-10-04 20:29:09 -0700331// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
332// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
333// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
334 FlowEntry fe1 = new DefaultFlowEntry(f1);
335 FlowEntry fe2 = new DefaultFlowEntry(f2);
336 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700337
alshabib1c319ff2014-10-04 20:29:09 -0700338
339 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700340
alshabib3d643ec2014-10-22 18:33:00 -0700341 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700342
343 }
344
345 /*
346 * Tests whether a rule that was marked for removal but no flowRemoved was received
347 * is indeed removed at the next stats update.
348 */
349 @Test
350 public void flowMissingRemove() {
351 FlowRule f1 = flowRule(1, 1);
352 FlowRule f2 = flowRule(2, 2);
353 FlowRule f3 = flowRule(3, 3);
354
alshabib1c319ff2014-10-04 20:29:09 -0700355// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
356// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
357
358 FlowEntry fe1 = new DefaultFlowEntry(f1);
359 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700360 mgr.applyFlowRules(f1, f2, f3);
361
362 mgr.removeFlowRules(f3);
363
alshabib1c319ff2014-10-04 20:29:09 -0700364 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700365
alshabib3d643ec2014-10-22 18:33:00 -0700366 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
367 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700368
369 }
370
371 @Test
372 public void getByAppId() {
373 FlowRule f1 = flowRule(1, 1);
374 FlowRule f2 = flowRule(2, 2);
375 mgr.applyFlowRules(f1, f2);
376
377 assertTrue("should have two rules",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800378 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700379 }
380
381 @Test
382 public void removeByAppId() {
383 FlowRule f1 = flowRule(1, 1);
384 FlowRule f2 = flowRule(2, 2);
385 mgr.applyFlowRules(f1, f2);
386
387
388 mgr.removeFlowRulesById(appId);
389
390 //only check that we are in pending remove. Events and actual remove state will
391 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700392 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700393 f1, FlowEntryState.PENDING_REMOVE,
394 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700395 }
396
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700397 private static class TestListener implements FlowRuleListener {
398 final List<FlowRuleEvent> events = new ArrayList<>();
399
400 @Override
401 public void event(FlowRuleEvent event) {
402 events.add(event);
403 }
404 }
405
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800406 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700407
408 @Override
409 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700410 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700411 }
412
413 @Override
414 public Iterable<Device> getDevices() {
Madan Jampani6a456162014-10-24 11:36:17 -0700415 return Arrays.asList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700416 }
417
418 @Override
419 public Device getDevice(DeviceId deviceId) {
420 return DEV;
421 }
422
423 @Override
424 public MastershipRole getRole(DeviceId deviceId) {
425 return null;
426 }
427
428 @Override
429 public List<Port> getPorts(DeviceId deviceId) {
430 return null;
431 }
432
433 @Override
434 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
435 return null;
436 }
437
438 @Override
439 public boolean isAvailable(DeviceId deviceId) {
440 return false;
441 }
442
443 @Override
444 public void addListener(DeviceListener listener) {
445 }
446
447 @Override
448 public void removeListener(DeviceListener listener) {
449 }
450
451 }
452
453 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
454
455 protected TestProvider(ProviderId id) {
456 super(PID);
457 }
458
459 @Override
460 public void applyFlowRule(FlowRule... flowRules) {
461 }
462
463 @Override
464 public void removeFlowRule(FlowRule... flowRules) {
465 }
466
alshabiba68eb962014-09-24 20:34:13 -0700467 @Override
468 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
469 }
470
alshabib902d41b2014-10-07 16:52:05 -0700471 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800472 public void executeBatch(FlowRuleBatchOperation batch) {
473 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700474 }
475
alshabibcf369912014-10-13 14:16:42 -0700476 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700477 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700478
479 @Override
480 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800481 return false;
alshabibcf369912014-10-13 14:16:42 -0700482 }
483
484 @Override
485 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800486 return false;
alshabibcf369912014-10-13 14:16:42 -0700487 }
488
489 @Override
490 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800491 return true;
alshabibcf369912014-10-13 14:16:42 -0700492 }
493
494 @Override
495 public CompletedBatchOperation get()
496 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800497 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700498 }
499
500 @Override
501 public CompletedBatchOperation get(long timeout, TimeUnit unit)
502 throws InterruptedException,
503 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800504 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700505 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700506
507 @Override
508 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800509 if (isDone()) {
510 executor.execute(task);
511 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700512 }
alshabibcf369912014-10-13 14:16:42 -0700513 }
alshabiba68eb962014-09-24 20:34:13 -0700514
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700515 }
516
517 private class TestSelector implements TrafficSelector {
518
519 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700520 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700521
522 public TestSelector(int val) {
523 testval = val;
524 }
525
526 @Override
alshabibba5ac482014-10-02 17:15:20 -0700527 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700528 return null;
529 }
530
531 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700532 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800533 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700534 return null;
535 }
536
537 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700538 public int hashCode() {
539 return testval;
540 }
541
542 @Override
543 public boolean equals(Object o) {
544 if (o instanceof TestSelector) {
545 return this.testval == ((TestSelector) o).testval;
546 }
547 return false;
548 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700549
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700550 }
551
552 private class TestTreatment implements TrafficTreatment {
553
554 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700555 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700556
557 public TestTreatment(int val) {
558 testval = val;
559 }
560
561 @Override
alshabib346b5b32015-03-06 00:42:16 -0800562 public List<Instruction> deferred() {
563 return null;
564 }
565
566 @Override
567 public List<Instruction> immediate() {
568 return null;
569 }
570
571 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700572 public List<Instruction> allInstructions() {
573 return null;
574 }
575
576 @Override
alshabib346b5b32015-03-06 00:42:16 -0800577 public Instructions.TableTypeTransition tableTransition() {
578 return null;
579 }
580
581 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700582 public boolean clearedDeferred() {
583 return false;
alshabib346b5b32015-03-06 00:42:16 -0800584 }
585
586 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700587 public int hashCode() {
588 return testval;
589 }
590
591 @Override
592 public boolean equals(Object o) {
593 if (o instanceof TestTreatment) {
594 return this.testval == ((TestTreatment) o).testval;
595 }
596 return false;
597 }
598
599 }
600
alshabib92c65ad2014-10-08 21:56:05 -0700601 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800602 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700603 super(id, name);
604 }
605 }
606
Ray Milkeycc53abd2015-02-19 12:31:33 -0800607 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800608
609 @Override
610 public IdGenerator getIdGenerator(String topic) {
611 return new IdGenerator() {
612 private AtomicLong counter = new AtomicLong(0);
613 @Override
614 public long getNewId() {
615 return counter.getAndIncrement();
616 }
617 };
618 }
619 }
620
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700621}