blob: 5097bb8dfbcfa92c9dc5bbc147abe86fdd2e8714 [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;
65import org.onosproject.store.trivial.impl.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);
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700147 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700148 }
149
alshabibbb8b1282014-09-22 17:00:18 -0700150
151 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700152 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700153 service.applyFlowRules(rule);
154
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700155 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700156 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700157 }
158
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700159 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700160 if (events == null) {
161 assertTrue("events generated", listener.events.isEmpty());
162 }
163
164 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700165 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700166 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800167 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700168 i++;
169 }
170
171 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700172 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700173
174 listener.events.clear();
175 }
176
177 private int flowCount() {
178 return Sets.newHashSet(service.getFlowEntries(DID)).size();
179 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700180
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700181 @Test
182 public void getFlowEntries() {
183 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700184 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700185 FlowRule f1 = addFlowRule(1);
186 FlowRule f2 = addFlowRule(2);
187
alshabib1c319ff2014-10-04 20:29:09 -0700188 FlowEntry fe1 = new DefaultFlowEntry(f1);
189 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700190 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700191
alshabib1c319ff2014-10-04 20:29:09 -0700192 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700193 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
194 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700195
196 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800197 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700198 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700199
alshabib1c319ff2014-10-04 20:29:09 -0700200 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700201 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700202 }
203
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700204 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
205 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700206 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700207 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700208 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
209 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
210 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700211 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700212 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700213 return true;
214 }
215
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700216 @Test
217 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700218
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700219 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700220 FlowRule r2 = flowRule(2, 2);
221 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700222
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700223 assertTrue("store should be empty",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800224 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700225 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700226 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700227 assertTrue("Entries should be pending add.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800228 validateState(ImmutableMap.of(
229 r1, FlowEntryState.PENDING_ADD,
230 r2, FlowEntryState.PENDING_ADD,
231 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700232 }
233
234 @Test
235 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700236 FlowRule f1 = addFlowRule(1);
237 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700238 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700239 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700240
alshabib1c319ff2014-10-04 20:29:09 -0700241 FlowEntry fe1 = new DefaultFlowEntry(f1);
242 FlowEntry fe2 = new DefaultFlowEntry(f2);
243 FlowEntry fe3 = new DefaultFlowEntry(f3);
244 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700245 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
246 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700247
alshabib1c319ff2014-10-04 20:29:09 -0700248 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700249 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700250 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700251 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700252 assertTrue("Entries should be pending remove.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800253 validateState(ImmutableMap.of(
254 f1, FlowEntryState.PENDING_REMOVE,
255 f2, FlowEntryState.PENDING_REMOVE,
256 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700257
alshabib1c319ff2014-10-04 20:29:09 -0700258 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700259 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700260 }
261
262 @Test
263 public void flowRemoved() {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800264
alshabibbb8b1282014-09-22 17:00:18 -0700265 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700266 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700267 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700268 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800269
270
alshabib1c319ff2014-10-04 20:29:09 -0700271 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700272 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800273
alshabib1c319ff2014-10-04 20:29:09 -0700274 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800275
276
277
alshabib1c319ff2014-10-04 20:29:09 -0700278 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800279
280
alshabib3d643ec2014-10-22 18:33:00 -0700281 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800282 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700283
alshabib1c319ff2014-10-04 20:29:09 -0700284 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700285 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700286
alshabibba5ac482014-10-02 17:15:20 -0700287 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700288 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700289 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800290
alshabib1c319ff2014-10-04 20:29:09 -0700291 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700292 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700293
alshabib1c319ff2014-10-04 20:29:09 -0700294 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700295 validateEvents();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800296
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700297 }
298
alshabibbb8b1282014-09-22 17:00:18 -0700299 @Test
300 public void flowMetrics() {
301 FlowRule f1 = flowRule(1, 1);
302 FlowRule f2 = flowRule(2, 2);
303 FlowRule f3 = flowRule(3, 3);
304
alshabibba5ac482014-10-02 17:15:20 -0700305 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700306
alshabib1c319ff2014-10-04 20:29:09 -0700307 FlowEntry fe1 = new DefaultFlowEntry(f1);
308 FlowEntry fe2 = new DefaultFlowEntry(f2);
309
alshabib1c319ff2014-10-04 20:29:09 -0700310 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
311 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
312
313 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700314
315 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700316 validateState(ImmutableMap.of(
317 f1, FlowEntryState.ADDED,
318 f2, FlowEntryState.ADDED,
319 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700320
alshabib3d643ec2014-10-22 18:33:00 -0700321 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800322 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700323 }
324
325 @Test
326 public void extraneousFlow() {
327 FlowRule f1 = flowRule(1, 1);
328 FlowRule f2 = flowRule(2, 2);
329 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700330 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700331
alshabib1c319ff2014-10-04 20:29:09 -0700332// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
333// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
334// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
335 FlowEntry fe1 = new DefaultFlowEntry(f1);
336 FlowEntry fe2 = new DefaultFlowEntry(f2);
337 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700338
alshabib1c319ff2014-10-04 20:29:09 -0700339
340 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700341
alshabib3d643ec2014-10-22 18:33:00 -0700342 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700343
344 }
345
346 /*
347 * Tests whether a rule that was marked for removal but no flowRemoved was received
348 * is indeed removed at the next stats update.
349 */
350 @Test
351 public void flowMissingRemove() {
352 FlowRule f1 = flowRule(1, 1);
353 FlowRule f2 = flowRule(2, 2);
354 FlowRule f3 = flowRule(3, 3);
355
alshabib1c319ff2014-10-04 20:29:09 -0700356// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
357// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
358
359 FlowEntry fe1 = new DefaultFlowEntry(f1);
360 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700361 mgr.applyFlowRules(f1, f2, f3);
362
363 mgr.removeFlowRules(f3);
364
alshabib1c319ff2014-10-04 20:29:09 -0700365 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700366
alshabib3d643ec2014-10-22 18:33:00 -0700367 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
368 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700369
370 }
371
372 @Test
373 public void getByAppId() {
374 FlowRule f1 = flowRule(1, 1);
375 FlowRule f2 = flowRule(2, 2);
376 mgr.applyFlowRules(f1, f2);
377
378 assertTrue("should have two rules",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800379 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700380 }
381
382 @Test
383 public void removeByAppId() {
384 FlowRule f1 = flowRule(1, 1);
385 FlowRule f2 = flowRule(2, 2);
386 mgr.applyFlowRules(f1, f2);
387
388
389 mgr.removeFlowRulesById(appId);
390
391 //only check that we are in pending remove. Events and actual remove state will
392 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700393 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700394 f1, FlowEntryState.PENDING_REMOVE,
395 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700396 }
397
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700398 private static class TestListener implements FlowRuleListener {
399 final List<FlowRuleEvent> events = new ArrayList<>();
400
401 @Override
402 public void event(FlowRuleEvent event) {
403 events.add(event);
404 }
405 }
406
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800407 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700408
409 @Override
410 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700411 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700412 }
413
414 @Override
415 public Iterable<Device> getDevices() {
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700416 return Collections.singletonList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700417 }
418
419 @Override
420 public Device getDevice(DeviceId deviceId) {
421 return DEV;
422 }
423
424 @Override
425 public MastershipRole getRole(DeviceId deviceId) {
426 return null;
427 }
428
429 @Override
430 public List<Port> getPorts(DeviceId deviceId) {
431 return null;
432 }
433
434 @Override
435 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
436 return null;
437 }
438
439 @Override
440 public boolean isAvailable(DeviceId deviceId) {
441 return false;
442 }
443
444 @Override
445 public void addListener(DeviceListener listener) {
446 }
447
448 @Override
449 public void removeListener(DeviceListener listener) {
450 }
451
452 }
453
454 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
455
456 protected TestProvider(ProviderId id) {
457 super(PID);
458 }
459
460 @Override
461 public void applyFlowRule(FlowRule... flowRules) {
462 }
463
464 @Override
465 public void removeFlowRule(FlowRule... flowRules) {
466 }
467
alshabiba68eb962014-09-24 20:34:13 -0700468 @Override
469 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
470 }
471
alshabib902d41b2014-10-07 16:52:05 -0700472 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800473 public void executeBatch(FlowRuleBatchOperation batch) {
474 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700475 }
476
alshabibcf369912014-10-13 14:16:42 -0700477 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700478 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700479
480 @Override
481 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800482 return false;
alshabibcf369912014-10-13 14:16:42 -0700483 }
484
485 @Override
486 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800487 return false;
alshabibcf369912014-10-13 14:16:42 -0700488 }
489
490 @Override
491 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800492 return true;
alshabibcf369912014-10-13 14:16:42 -0700493 }
494
495 @Override
496 public CompletedBatchOperation get()
497 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800498 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700499 }
500
501 @Override
502 public CompletedBatchOperation get(long timeout, TimeUnit unit)
503 throws InterruptedException,
504 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800505 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700506 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700507
508 @Override
509 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800510 if (isDone()) {
511 executor.execute(task);
512 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700513 }
alshabibcf369912014-10-13 14:16:42 -0700514 }
alshabiba68eb962014-09-24 20:34:13 -0700515
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700516 }
517
518 private class TestSelector implements TrafficSelector {
519
520 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700521 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700522
523 public TestSelector(int val) {
524 testval = val;
525 }
526
527 @Override
alshabibba5ac482014-10-02 17:15:20 -0700528 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700529 return null;
530 }
531
532 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700533 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800534 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700535 return null;
536 }
537
538 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700539 public int hashCode() {
540 return testval;
541 }
542
543 @Override
544 public boolean equals(Object o) {
545 if (o instanceof TestSelector) {
546 return this.testval == ((TestSelector) o).testval;
547 }
548 return false;
549 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700550
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700551 }
552
553 private class TestTreatment implements TrafficTreatment {
554
555 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700556 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700557
558 public TestTreatment(int val) {
559 testval = val;
560 }
561
562 @Override
alshabib346b5b32015-03-06 00:42:16 -0800563 public List<Instruction> deferred() {
564 return null;
565 }
566
567 @Override
568 public List<Instruction> immediate() {
569 return null;
570 }
571
572 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700573 public List<Instruction> allInstructions() {
574 return null;
575 }
576
577 @Override
alshabib346b5b32015-03-06 00:42:16 -0800578 public Instructions.TableTypeTransition tableTransition() {
579 return null;
580 }
581
582 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700583 public boolean clearedDeferred() {
584 return false;
alshabib346b5b32015-03-06 00:42:16 -0800585 }
586
587 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700588 public int hashCode() {
589 return testval;
590 }
591
592 @Override
593 public boolean equals(Object o) {
594 if (o instanceof TestTreatment) {
595 return this.testval == ((TestTreatment) o).testval;
596 }
597 return false;
598 }
599
Saurav Das86af8f12015-05-25 23:55:33 -0700600 @Override
601 public MetadataInstruction writeMetadata() {
602 return null;
603 }
604
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700605 }
606
alshabib92c65ad2014-10-08 21:56:05 -0700607 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800608 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700609 super(id, name);
610 }
611 }
612
Ray Milkeycc53abd2015-02-19 12:31:33 -0800613 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800614
615 @Override
616 public IdGenerator getIdGenerator(String topic) {
617 return new IdGenerator() {
618 private AtomicLong counter = new AtomicLong(0);
619 @Override
620 public long getNewId() {
621 return counter.getAndIncrement();
622 }
623 };
624 }
625 }
626
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700627}