blob: db434c7676be17ff45e119624a4bcc8f27c46e9c [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;
Saurav Das86af8f12015-05-25 23:55:33 -070024
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070025import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070028import org.onosproject.cfg.ComponentConfigAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
Ray Milkeycc53abd2015-02-19 12:31:33 -080030import org.onosproject.core.CoreServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080032import org.onosproject.core.IdGenerator;
Thomas Vachuska36002e62015-05-19 16:12:29 -070033import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.DefaultDevice;
35import org.onosproject.net.Device;
36import org.onosproject.net.Device.Type;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.MastershipRole;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.flow.CompletedBatchOperation;
44import org.onosproject.net.flow.DefaultFlowEntry;
45import org.onosproject.net.flow.DefaultFlowRule;
46import org.onosproject.net.flow.FlowEntry;
47import org.onosproject.net.flow.FlowEntry.FlowEntryState;
48import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.FlowRuleBatchOperation;
50import org.onosproject.net.flow.FlowRuleEvent;
51import org.onosproject.net.flow.FlowRuleListener;
52import org.onosproject.net.flow.FlowRuleProvider;
53import org.onosproject.net.flow.FlowRuleProviderRegistry;
54import org.onosproject.net.flow.FlowRuleProviderService;
55import org.onosproject.net.flow.FlowRuleService;
56import org.onosproject.net.flow.StoredFlowEntry;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criterion;
60import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080061import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070062import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.provider.AbstractProvider;
64import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070065import org.onosproject.store.trivial.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070066
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070067import java.util.ArrayList;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070068import java.util.Collections;
69import java.util.HashMap;
70import java.util.List;
71import java.util.Map;
72import java.util.Set;
73import java.util.concurrent.ExecutionException;
74import java.util.concurrent.Executor;
75import java.util.concurrent.TimeUnit;
76import java.util.concurrent.TimeoutException;
77import java.util.concurrent.atomic.AtomicLong;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070078
Ray Milkeyda36c402015-02-18 10:06:06 -080079import static org.junit.Assert.assertEquals;
80import static org.junit.Assert.assertFalse;
81import static org.junit.Assert.assertNotNull;
82import static org.junit.Assert.assertTrue;
83import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADDED;
84import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
85import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
86import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
87import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070088
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070089/**
90 * Test codifying the flow rule service & flow rule provider service contracts.
91 */
tom202175a2014-09-19 19:00:11 -070092public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070093
alshabib92c65ad2014-10-08 21:56:05 -070094
tomf6ab2152014-09-18 12:08:29 -070095 private static final ProviderId PID = new ProviderId("of", "foo");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070096 private static final DeviceId DID = DeviceId.deviceId("of:001");
alshabibba5ac482014-10-02 17:15:20 -070097 private static final int TIMEOUT = 10;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070098 private static final Device DEV = new DefaultDevice(
alshabib7911a052014-10-16 17:49:37 -070099 PID, DID, Type.SWITCH, "", "", "", "", null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700100
tom202175a2014-09-19 19:00:11 -0700101 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700102
103 protected FlowRuleService service;
104 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -0700105 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700106 protected TestProvider provider;
107 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -0700108 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700109
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700111 @Before
112 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700113 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700114 mgr.store = new SimpleFlowRuleStore();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700115 mgr.eventDispatcher = new TestEventDispatcher();
116 mgr.deviceService = new TestDeviceService();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800117 mgr.coreService = new TestCoreService();
118 mgr.operationsService = MoreExecutors.newDirectExecutorService();
119 mgr.deviceInstallers = MoreExecutors.newDirectExecutorService();
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700120 mgr.cfgService = new ComponentConfigAdapter();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700121 service = mgr;
122 registry = mgr;
123
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700124 mgr.activate(null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700125 mgr.addListener(listener);
126 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700127 providerService = registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800128 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700129 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700130 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700131 }
132
133 @After
134 public void tearDown() {
135 registry.unregister(provider);
136 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700137 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700138 service.removeListener(listener);
139 mgr.deactivate();
140 mgr.eventDispatcher = null;
141 mgr.deviceService = null;
142 }
143
144 private FlowRule flowRule(int tsval, int trval) {
145 TestSelector ts = new TestSelector(tsval);
146 TestTreatment tr = new TestTreatment(trval);
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700147 return DefaultFlowRule.builder()
148 .forDevice(DID)
149 .withSelector(ts)
150 .withTreatment(tr)
151 .withPriority(10)
152 .fromApp(appId)
153 .makeTemporary(TIMEOUT)
154 .build();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700155 }
156
alshabibbb8b1282014-09-22 17:00:18 -0700157
158 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700160 service.applyFlowRules(rule);
161
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700162 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700163 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700164 }
165
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700166 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700167 if (events == null) {
168 assertTrue("events generated", listener.events.isEmpty());
169 }
170
171 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700172 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700173 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800174 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700175 i++;
176 }
177
178 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700179 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700180
181 listener.events.clear();
182 }
183
184 private int flowCount() {
185 return Sets.newHashSet(service.getFlowEntries(DID)).size();
186 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700187
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700188 @Test
189 public void getFlowEntries() {
190 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700191 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700192 FlowRule f1 = addFlowRule(1);
193 FlowRule f2 = addFlowRule(2);
194
alshabib1c319ff2014-10-04 20:29:09 -0700195 FlowEntry fe1 = new DefaultFlowEntry(f1);
196 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700197 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700198
alshabib1c319ff2014-10-04 20:29:09 -0700199 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700200 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
201 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700202
203 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800204 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700205 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700206
alshabib1c319ff2014-10-04 20:29:09 -0700207 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700208 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700209 }
210
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700211 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
212 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700213 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700214 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700215 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
216 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
217 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700218 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700219 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700220 return true;
221 }
222
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700223 @Test
224 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700225
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700226 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700227 FlowRule r2 = flowRule(2, 2);
228 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700229
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700230 assertTrue("store should be empty",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800231 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700232 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700233 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700234 assertTrue("Entries should be pending add.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800235 validateState(ImmutableMap.of(
236 r1, FlowEntryState.PENDING_ADD,
237 r2, FlowEntryState.PENDING_ADD,
238 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700239 }
240
241 @Test
242 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700243 FlowRule f1 = addFlowRule(1);
244 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700245 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700246 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700247
alshabib1c319ff2014-10-04 20:29:09 -0700248 FlowEntry fe1 = new DefaultFlowEntry(f1);
249 FlowEntry fe2 = new DefaultFlowEntry(f2);
250 FlowEntry fe3 = new DefaultFlowEntry(f3);
251 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700252 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
253 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700254
alshabib1c319ff2014-10-04 20:29:09 -0700255 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700256 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700257 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700258 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700259 assertTrue("Entries should be pending remove.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800260 validateState(ImmutableMap.of(
261 f1, FlowEntryState.PENDING_REMOVE,
262 f2, FlowEntryState.PENDING_REMOVE,
263 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700264
alshabib1c319ff2014-10-04 20:29:09 -0700265 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700266 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700267 }
268
269 @Test
270 public void flowRemoved() {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800271
alshabibbb8b1282014-09-22 17:00:18 -0700272 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700273 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700274 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700275 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800276
277
alshabib1c319ff2014-10-04 20:29:09 -0700278 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700279 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800280
alshabib1c319ff2014-10-04 20:29:09 -0700281 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800282
283
284
alshabib1c319ff2014-10-04 20:29:09 -0700285 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800286
287
alshabib3d643ec2014-10-22 18:33:00 -0700288 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800289 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700290
alshabib1c319ff2014-10-04 20:29:09 -0700291 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700292 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700293
alshabibba5ac482014-10-02 17:15:20 -0700294 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700295 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700296 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800297
alshabib1c319ff2014-10-04 20:29:09 -0700298 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700299 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700300
alshabib1c319ff2014-10-04 20:29:09 -0700301 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700302 validateEvents();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800303
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700304 }
305
alshabibbb8b1282014-09-22 17:00:18 -0700306 @Test
307 public void flowMetrics() {
308 FlowRule f1 = flowRule(1, 1);
309 FlowRule f2 = flowRule(2, 2);
310 FlowRule f3 = flowRule(3, 3);
311
alshabibba5ac482014-10-02 17:15:20 -0700312 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700313
alshabib1c319ff2014-10-04 20:29:09 -0700314 FlowEntry fe1 = new DefaultFlowEntry(f1);
315 FlowEntry fe2 = new DefaultFlowEntry(f2);
316
alshabib1c319ff2014-10-04 20:29:09 -0700317 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
318 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
319
320 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700321
322 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700323 validateState(ImmutableMap.of(
324 f1, FlowEntryState.ADDED,
325 f2, FlowEntryState.ADDED,
326 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700327
alshabib3d643ec2014-10-22 18:33:00 -0700328 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800329 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700330 }
331
332 @Test
333 public void extraneousFlow() {
334 FlowRule f1 = flowRule(1, 1);
335 FlowRule f2 = flowRule(2, 2);
336 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700337 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700338
alshabib1c319ff2014-10-04 20:29:09 -0700339// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
340// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
341// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
342 FlowEntry fe1 = new DefaultFlowEntry(f1);
343 FlowEntry fe2 = new DefaultFlowEntry(f2);
344 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700345
alshabib1c319ff2014-10-04 20:29:09 -0700346
347 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700348
alshabib3d643ec2014-10-22 18:33:00 -0700349 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700350
351 }
352
353 /*
354 * Tests whether a rule that was marked for removal but no flowRemoved was received
355 * is indeed removed at the next stats update.
356 */
357 @Test
358 public void flowMissingRemove() {
359 FlowRule f1 = flowRule(1, 1);
360 FlowRule f2 = flowRule(2, 2);
361 FlowRule f3 = flowRule(3, 3);
362
alshabib1c319ff2014-10-04 20:29:09 -0700363// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
364// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
365
366 FlowEntry fe1 = new DefaultFlowEntry(f1);
367 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700368 mgr.applyFlowRules(f1, f2, f3);
369
370 mgr.removeFlowRules(f3);
371
alshabib1c319ff2014-10-04 20:29:09 -0700372 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700373
alshabib3d643ec2014-10-22 18:33:00 -0700374 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
375 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700376
377 }
378
379 @Test
380 public void getByAppId() {
381 FlowRule f1 = flowRule(1, 1);
382 FlowRule f2 = flowRule(2, 2);
383 mgr.applyFlowRules(f1, f2);
384
385 assertTrue("should have two rules",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800386 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700387 }
388
389 @Test
390 public void removeByAppId() {
391 FlowRule f1 = flowRule(1, 1);
392 FlowRule f2 = flowRule(2, 2);
393 mgr.applyFlowRules(f1, f2);
394
395
396 mgr.removeFlowRulesById(appId);
397
398 //only check that we are in pending remove. Events and actual remove state will
399 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700400 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700401 f1, FlowEntryState.PENDING_REMOVE,
402 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700403 }
404
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700405 private static class TestListener implements FlowRuleListener {
406 final List<FlowRuleEvent> events = new ArrayList<>();
407
408 @Override
409 public void event(FlowRuleEvent event) {
410 events.add(event);
411 }
412 }
413
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800414 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700415
416 @Override
417 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700418 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700419 }
420
421 @Override
422 public Iterable<Device> getDevices() {
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700423 return Collections.singletonList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700424 }
425
426 @Override
427 public Device getDevice(DeviceId deviceId) {
428 return DEV;
429 }
430
431 @Override
432 public MastershipRole getRole(DeviceId deviceId) {
433 return null;
434 }
435
436 @Override
437 public List<Port> getPorts(DeviceId deviceId) {
438 return null;
439 }
440
441 @Override
442 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
443 return null;
444 }
445
446 @Override
447 public boolean isAvailable(DeviceId deviceId) {
448 return false;
449 }
450
451 @Override
452 public void addListener(DeviceListener listener) {
453 }
454
455 @Override
456 public void removeListener(DeviceListener listener) {
457 }
458
459 }
460
461 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
462
463 protected TestProvider(ProviderId id) {
464 super(PID);
465 }
466
467 @Override
468 public void applyFlowRule(FlowRule... flowRules) {
469 }
470
471 @Override
472 public void removeFlowRule(FlowRule... flowRules) {
473 }
474
alshabiba68eb962014-09-24 20:34:13 -0700475 @Override
476 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
477 }
478
alshabib902d41b2014-10-07 16:52:05 -0700479 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 public void executeBatch(FlowRuleBatchOperation batch) {
481 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700482 }
483
alshabibcf369912014-10-13 14:16:42 -0700484 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700485 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700486
487 @Override
488 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800489 return false;
alshabibcf369912014-10-13 14:16:42 -0700490 }
491
492 @Override
493 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800494 return false;
alshabibcf369912014-10-13 14:16:42 -0700495 }
496
497 @Override
498 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800499 return true;
alshabibcf369912014-10-13 14:16:42 -0700500 }
501
502 @Override
503 public CompletedBatchOperation get()
504 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800505 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700506 }
507
508 @Override
509 public CompletedBatchOperation get(long timeout, TimeUnit unit)
510 throws InterruptedException,
511 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800512 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700513 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700514
515 @Override
516 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800517 if (isDone()) {
518 executor.execute(task);
519 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700520 }
alshabibcf369912014-10-13 14:16:42 -0700521 }
alshabiba68eb962014-09-24 20:34:13 -0700522
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700523 }
524
525 private class TestSelector implements TrafficSelector {
526
527 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700528 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700529
530 public TestSelector(int val) {
531 testval = val;
532 }
533
534 @Override
alshabibba5ac482014-10-02 17:15:20 -0700535 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700536 return null;
537 }
538
539 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700540 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800541 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700542 return null;
543 }
544
545 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700546 public int hashCode() {
547 return testval;
548 }
549
550 @Override
551 public boolean equals(Object o) {
552 if (o instanceof TestSelector) {
553 return this.testval == ((TestSelector) o).testval;
554 }
555 return false;
556 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700557
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700558 }
559
560 private class TestTreatment implements TrafficTreatment {
561
562 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700563 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700564
565 public TestTreatment(int val) {
566 testval = val;
567 }
568
569 @Override
alshabib346b5b32015-03-06 00:42:16 -0800570 public List<Instruction> deferred() {
571 return null;
572 }
573
574 @Override
575 public List<Instruction> immediate() {
576 return null;
577 }
578
579 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700580 public List<Instruction> allInstructions() {
581 return null;
582 }
583
584 @Override
alshabib346b5b32015-03-06 00:42:16 -0800585 public Instructions.TableTypeTransition tableTransition() {
586 return null;
587 }
588
589 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700590 public boolean clearedDeferred() {
591 return false;
alshabib346b5b32015-03-06 00:42:16 -0800592 }
593
594 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700595 public int hashCode() {
596 return testval;
597 }
598
599 @Override
600 public boolean equals(Object o) {
601 if (o instanceof TestTreatment) {
602 return this.testval == ((TestTreatment) o).testval;
603 }
604 return false;
605 }
606
Saurav Das86af8f12015-05-25 23:55:33 -0700607 @Override
608 public MetadataInstruction writeMetadata() {
609 return null;
610 }
611
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700612 }
613
alshabib92c65ad2014-10-08 21:56:05 -0700614 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800615 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700616 super(id, name);
617 }
618 }
619
Ray Milkeycc53abd2015-02-19 12:31:33 -0800620 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800621
622 @Override
623 public IdGenerator getIdGenerator(String topic) {
624 return new IdGenerator() {
625 private AtomicLong counter = new AtomicLong(0);
626 @Override
627 public long getNewId() {
628 return counter.getAndIncrement();
629 }
630 };
631 }
632 }
633
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700634}