blob: 7ead51603f5eb77cedeb0e1a696491727c3e9a8f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.impl;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.core.ApplicationId;
Ray Milkeycc53abd2015-02-19 12:31:33 -080028import org.onosproject.core.CoreServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080030import org.onosproject.core.IdGenerator;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.event.impl.TestEventDispatcher;
32import org.onosproject.net.DefaultDevice;
33import org.onosproject.net.Device;
34import org.onosproject.net.Device.Type;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.MastershipRole;
37import org.onosproject.net.Port;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.device.DeviceListener;
40import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.flow.CompletedBatchOperation;
42import org.onosproject.net.flow.DefaultFlowEntry;
43import org.onosproject.net.flow.DefaultFlowRule;
44import org.onosproject.net.flow.FlowEntry;
45import org.onosproject.net.flow.FlowEntry.FlowEntryState;
46import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.flow.FlowRuleBatchOperation;
48import org.onosproject.net.flow.FlowRuleEvent;
49import org.onosproject.net.flow.FlowRuleListener;
50import org.onosproject.net.flow.FlowRuleProvider;
51import org.onosproject.net.flow.FlowRuleProviderRegistry;
52import org.onosproject.net.flow.FlowRuleProviderService;
53import org.onosproject.net.flow.FlowRuleService;
54import org.onosproject.net.flow.StoredFlowEntry;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.flow.criteria.Criterion;
58import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080059import org.onosproject.net.flow.instructions.Instructions;
Brian O'Connorabafb502014-12-02 22:26:20 -080060import org.onosproject.net.provider.AbstractProvider;
61import org.onosproject.net.provider.ProviderId;
62import org.onosproject.store.trivial.impl.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070063
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070064import java.util.ArrayList;
65import java.util.Arrays;
66import 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();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700118 service = mgr;
119 registry = mgr;
120
121 mgr.activate();
122 mgr.addListener(listener);
123 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700124 providerService = registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700126 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700127 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700128 }
129
130 @After
131 public void tearDown() {
132 registry.unregister(provider);
133 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700134 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700135 service.removeListener(listener);
136 mgr.deactivate();
137 mgr.eventDispatcher = null;
138 mgr.deviceService = null;
139 }
140
141 private FlowRule flowRule(int tsval, int trval) {
142 TestSelector ts = new TestSelector(tsval);
143 TestTreatment tr = new TestTreatment(trval);
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700144 return new DefaultFlowRule(DID, ts, tr, 10, appId, TIMEOUT, false);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700145 }
146
alshabibbb8b1282014-09-22 17:00:18 -0700147
148 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700149 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700150 service.applyFlowRules(rule);
151
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700152 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700153 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700154 }
155
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700156 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700157 if (events == null) {
158 assertTrue("events generated", listener.events.isEmpty());
159 }
160
161 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700162 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700163 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800164 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700165 i++;
166 }
167
168 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700169 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700170
171 listener.events.clear();
172 }
173
174 private int flowCount() {
175 return Sets.newHashSet(service.getFlowEntries(DID)).size();
176 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700177
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700178 @Test
179 public void getFlowEntries() {
180 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700181 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700182 FlowRule f1 = addFlowRule(1);
183 FlowRule f2 = addFlowRule(2);
184
alshabib1c319ff2014-10-04 20:29:09 -0700185 FlowEntry fe1 = new DefaultFlowEntry(f1);
186 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700187 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700188
alshabib1c319ff2014-10-04 20:29:09 -0700189 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700190 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
191 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700192
193 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800194 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700195 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700196
alshabib1c319ff2014-10-04 20:29:09 -0700197 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
alshabib219ebaa2014-09-22 15:41:24 -0700198 validateEvents(RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700199 }
200
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700201 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
202 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700203 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700204 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700205 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
206 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
207 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700208 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700209 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700210 return true;
211 }
212
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700213 @Test
214 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700215
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700216 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700217 FlowRule r2 = flowRule(2, 2);
218 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700219
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700220 assertTrue("store should be empty",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800221 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700222 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700223 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700224 assertTrue("Entries should be pending add.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800225 validateState(ImmutableMap.of(
226 r1, FlowEntryState.PENDING_ADD,
227 r2, FlowEntryState.PENDING_ADD,
228 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700229 }
230
231 @Test
232 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700233 FlowRule f1 = addFlowRule(1);
234 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700235 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700236 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700237
alshabib1c319ff2014-10-04 20:29:09 -0700238 FlowEntry fe1 = new DefaultFlowEntry(f1);
239 FlowEntry fe2 = new DefaultFlowEntry(f2);
240 FlowEntry fe3 = new DefaultFlowEntry(f3);
241 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700242 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
243 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700244
alshabib1c319ff2014-10-04 20:29:09 -0700245 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700246 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700247 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700248 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700249 assertTrue("Entries should be pending remove.",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800250 validateState(ImmutableMap.of(
251 f1, FlowEntryState.PENDING_REMOVE,
252 f2, FlowEntryState.PENDING_REMOVE,
253 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700254
alshabib1c319ff2014-10-04 20:29:09 -0700255 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700256 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700257 }
258
259 @Test
260 public void flowRemoved() {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800261
alshabibbb8b1282014-09-22 17:00:18 -0700262 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700263 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700264 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700265 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800266
267
alshabib1c319ff2014-10-04 20:29:09 -0700268 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700269 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800270
alshabib1c319ff2014-10-04 20:29:09 -0700271 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800272
273
274
alshabib1c319ff2014-10-04 20:29:09 -0700275 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800276
277
alshabib3d643ec2014-10-22 18:33:00 -0700278 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800279 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700280
alshabib1c319ff2014-10-04 20:29:09 -0700281 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700282 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700283
alshabibba5ac482014-10-02 17:15:20 -0700284 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700285 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700286 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800287
alshabib1c319ff2014-10-04 20:29:09 -0700288 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700289 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700290
alshabib1c319ff2014-10-04 20:29:09 -0700291 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700292 validateEvents();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800293
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700294 }
295
alshabibbb8b1282014-09-22 17:00:18 -0700296 @Test
297 public void flowMetrics() {
298 FlowRule f1 = flowRule(1, 1);
299 FlowRule f2 = flowRule(2, 2);
300 FlowRule f3 = flowRule(3, 3);
301
alshabibba5ac482014-10-02 17:15:20 -0700302 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700303
alshabib1c319ff2014-10-04 20:29:09 -0700304 FlowEntry fe1 = new DefaultFlowEntry(f1);
305 FlowEntry fe2 = new DefaultFlowEntry(f2);
306
alshabib1c319ff2014-10-04 20:29:09 -0700307 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
308 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
309
310 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700311
312 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700313 validateState(ImmutableMap.of(
314 f1, FlowEntryState.ADDED,
315 f2, FlowEntryState.ADDED,
316 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700317
alshabib3d643ec2014-10-22 18:33:00 -0700318 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Ray Milkeycc53abd2015-02-19 12:31:33 -0800319 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700320 }
321
322 @Test
323 public void extraneousFlow() {
324 FlowRule f1 = flowRule(1, 1);
325 FlowRule f2 = flowRule(2, 2);
326 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700327 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700328
alshabib1c319ff2014-10-04 20:29:09 -0700329// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
330// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
331// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
332 FlowEntry fe1 = new DefaultFlowEntry(f1);
333 FlowEntry fe2 = new DefaultFlowEntry(f2);
334 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700335
alshabib1c319ff2014-10-04 20:29:09 -0700336
337 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700338
alshabib3d643ec2014-10-22 18:33:00 -0700339 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700340
341 }
342
343 /*
344 * Tests whether a rule that was marked for removal but no flowRemoved was received
345 * is indeed removed at the next stats update.
346 */
347 @Test
348 public void flowMissingRemove() {
349 FlowRule f1 = flowRule(1, 1);
350 FlowRule f2 = flowRule(2, 2);
351 FlowRule f3 = flowRule(3, 3);
352
alshabib1c319ff2014-10-04 20:29:09 -0700353// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
354// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
355
356 FlowEntry fe1 = new DefaultFlowEntry(f1);
357 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700358 mgr.applyFlowRules(f1, f2, f3);
359
360 mgr.removeFlowRules(f3);
361
alshabib1c319ff2014-10-04 20:29:09 -0700362 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700363
alshabib3d643ec2014-10-22 18:33:00 -0700364 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
365 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700366
367 }
368
369 @Test
370 public void getByAppId() {
371 FlowRule f1 = flowRule(1, 1);
372 FlowRule f2 = flowRule(2, 2);
373 mgr.applyFlowRules(f1, f2);
374
375 assertTrue("should have two rules",
Ray Milkeycc53abd2015-02-19 12:31:33 -0800376 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700377 }
378
379 @Test
380 public void removeByAppId() {
381 FlowRule f1 = flowRule(1, 1);
382 FlowRule f2 = flowRule(2, 2);
383 mgr.applyFlowRules(f1, f2);
384
385
386 mgr.removeFlowRulesById(appId);
387
388 //only check that we are in pending remove. Events and actual remove state will
389 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700390 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700391 f1, FlowEntryState.PENDING_REMOVE,
392 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700393 }
394
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700395 private static class TestListener implements FlowRuleListener {
396 final List<FlowRuleEvent> events = new ArrayList<>();
397
398 @Override
399 public void event(FlowRuleEvent event) {
400 events.add(event);
401 }
402 }
403
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800404 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700405
406 @Override
407 public int getDeviceCount() {
Madan Jampani6a456162014-10-24 11:36:17 -0700408 return 1;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700409 }
410
411 @Override
412 public Iterable<Device> getDevices() {
Madan Jampani6a456162014-10-24 11:36:17 -0700413 return Arrays.asList(DEV);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700414 }
415
416 @Override
417 public Device getDevice(DeviceId deviceId) {
418 return DEV;
419 }
420
421 @Override
422 public MastershipRole getRole(DeviceId deviceId) {
423 return null;
424 }
425
426 @Override
427 public List<Port> getPorts(DeviceId deviceId) {
428 return null;
429 }
430
431 @Override
432 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
433 return null;
434 }
435
436 @Override
437 public boolean isAvailable(DeviceId deviceId) {
438 return false;
439 }
440
441 @Override
442 public void addListener(DeviceListener listener) {
443 }
444
445 @Override
446 public void removeListener(DeviceListener listener) {
447 }
448
449 }
450
451 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
452
453 protected TestProvider(ProviderId id) {
454 super(PID);
455 }
456
457 @Override
458 public void applyFlowRule(FlowRule... flowRules) {
459 }
460
461 @Override
462 public void removeFlowRule(FlowRule... flowRules) {
463 }
464
alshabiba68eb962014-09-24 20:34:13 -0700465 @Override
466 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
467 }
468
alshabib902d41b2014-10-07 16:52:05 -0700469 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800470 public void executeBatch(FlowRuleBatchOperation batch) {
471 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700472 }
473
alshabibcf369912014-10-13 14:16:42 -0700474 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700475 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700476
477 @Override
478 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800479 return false;
alshabibcf369912014-10-13 14:16:42 -0700480 }
481
482 @Override
483 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800484 return false;
alshabibcf369912014-10-13 14:16:42 -0700485 }
486
487 @Override
488 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800489 return true;
alshabibcf369912014-10-13 14:16:42 -0700490 }
491
492 @Override
493 public CompletedBatchOperation get()
494 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800495 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700496 }
497
498 @Override
499 public CompletedBatchOperation get(long timeout, TimeUnit unit)
500 throws InterruptedException,
501 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800502 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700503 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700504
505 @Override
506 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800507 if (isDone()) {
508 executor.execute(task);
509 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700510 }
alshabibcf369912014-10-13 14:16:42 -0700511 }
alshabiba68eb962014-09-24 20:34:13 -0700512
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700513 }
514
515 private class TestSelector implements TrafficSelector {
516
517 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700518 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700519
520 public TestSelector(int val) {
521 testval = val;
522 }
523
524 @Override
alshabibba5ac482014-10-02 17:15:20 -0700525 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700526 return null;
527 }
528
529 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700530 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800531 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700532 return null;
533 }
534
535 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700536 public int hashCode() {
537 return testval;
538 }
539
540 @Override
541 public boolean equals(Object o) {
542 if (o instanceof TestSelector) {
543 return this.testval == ((TestSelector) o).testval;
544 }
545 return false;
546 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700547
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700548 }
549
550 private class TestTreatment implements TrafficTreatment {
551
552 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700553 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700554
555 public TestTreatment(int val) {
556 testval = val;
557 }
558
559 @Override
alshabib346b5b32015-03-06 00:42:16 -0800560 public List<Instruction> deferred() {
561 return null;
562 }
563
564 @Override
565 public List<Instruction> immediate() {
566 return null;
567 }
568
569 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700570 public List<Instruction> allInstructions() {
571 return null;
572 }
573
574 @Override
alshabib346b5b32015-03-06 00:42:16 -0800575 public Instructions.TableTypeTransition tableTransition() {
576 return null;
577 }
578
579 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700580 public boolean clearedDeferred() {
581 return false;
alshabib346b5b32015-03-06 00:42:16 -0800582 }
583
584 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700585 public int hashCode() {
586 return testval;
587 }
588
589 @Override
590 public boolean equals(Object o) {
591 if (o instanceof TestTreatment) {
592 return this.testval == ((TestTreatment) o).testval;
593 }
594 return false;
595 }
596
597 }
598
alshabib92c65ad2014-10-08 21:56:05 -0700599 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800600 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700601 super(id, name);
602 }
603 }
604
Ray Milkeycc53abd2015-02-19 12:31:33 -0800605 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800606
607 @Override
608 public IdGenerator getIdGenerator(String topic) {
609 return new IdGenerator() {
610 private AtomicLong counter = new AtomicLong(0);
611 @Override
612 public long getNewId() {
613 return counter.getAndIncrement();
614 }
615 };
616 }
617 }
618
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700619}