blob: 1e621f5185d07367771884cc587a40cf992fa244 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080027import org.onlab.junit.TestTools;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070028import org.onosproject.cfg.ComponentConfigAdapter;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080029import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
Ray Milkeycc53abd2015-02-19 12:31:33 -080031import org.onosproject.core.CoreServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080033import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080034import org.onosproject.mastership.MastershipServiceAdapter;
35import org.onosproject.net.AnnotationKeys;
36import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.DefaultDevice;
38import org.onosproject.net.Device;
39import org.onosproject.net.Device.Type;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.MastershipRole;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.device.DeviceServiceAdapter;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080043import org.onosproject.net.driver.AbstractHandlerBehaviour;
44import org.onosproject.net.driver.DefaultDriver;
45import org.onosproject.net.driver.impl.DriverManager;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.CompletedBatchOperation;
47import org.onosproject.net.flow.DefaultFlowEntry;
48import org.onosproject.net.flow.DefaultFlowRule;
49import org.onosproject.net.flow.FlowEntry;
50import org.onosproject.net.flow.FlowEntry.FlowEntryState;
51import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.flow.FlowRuleBatchOperation;
53import org.onosproject.net.flow.FlowRuleEvent;
54import org.onosproject.net.flow.FlowRuleListener;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080055import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.flow.FlowRuleProvider;
57import org.onosproject.net.flow.FlowRuleProviderRegistry;
58import org.onosproject.net.flow.FlowRuleProviderService;
59import org.onosproject.net.flow.FlowRuleService;
60import org.onosproject.net.flow.StoredFlowEntry;
61import org.onosproject.net.flow.TrafficSelector;
62import org.onosproject.net.flow.TrafficTreatment;
63import org.onosproject.net.flow.criteria.Criterion;
64import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080065import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070066import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.provider.AbstractProvider;
68import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070069import org.onosproject.store.trivial.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070070
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070071import java.util.ArrayList;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080072import java.util.Collection;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070073import java.util.Collections;
74import java.util.HashMap;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080075import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070076import java.util.List;
77import java.util.Map;
78import java.util.Set;
79import java.util.concurrent.ExecutionException;
80import java.util.concurrent.Executor;
81import java.util.concurrent.TimeUnit;
82import java.util.concurrent.TimeoutException;
83import java.util.concurrent.atomic.AtomicLong;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070084
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080085import static org.junit.Assert.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070086import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080087import static org.onosproject.net.flow.FlowRuleEvent.Type.*;
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");
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080096 private static final ProviderId FOO_PID = new ProviderId("foo", "foo");
97
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070098 private static final DeviceId DID = DeviceId.deviceId("of:001");
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080099 private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002");
alshabibba5ac482014-10-02 17:15:20 -0700100 private static final int TIMEOUT = 10;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800101
102 private static final DefaultAnnotations ANNOTATIONS =
103 DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build();
104
105 private static final Device DEV =
106 new DefaultDevice(PID, DID, Type.SWITCH, "", "", "", "", null);
107 private static final Device FOO_DEV =
108 new DefaultDevice(FOO_PID, FOO_DID, Type.SWITCH, "", "", "", "", null, ANNOTATIONS);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700109
tom202175a2014-09-19 19:00:11 -0700110 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700111
112 protected FlowRuleService service;
113 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -0700114 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700115 protected TestProvider provider;
116 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -0700117 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700118
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800119 private TestDriverManager driverService;
120
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800121
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700122 @Before
123 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700124 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700125 mgr.store = new SimpleFlowRuleStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700126 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700127 mgr.deviceService = new TestDeviceService();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800128 mgr.mastershipService = new TestMastershipService();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800129 mgr.coreService = new TestCoreService();
130 mgr.operationsService = MoreExecutors.newDirectExecutorService();
131 mgr.deviceInstallers = MoreExecutors.newDirectExecutorService();
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700132 mgr.cfgService = new ComponentConfigAdapter();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700133 service = mgr;
134 registry = mgr;
135
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800136 driverService = new TestDriverManager();
137 driverService.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "",
138 ImmutableMap.of(FlowRuleProgrammable.class,
139 TestFlowRuleProgrammable.class),
140 ImmutableMap.of()));
141
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700142 mgr.activate(null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700143 mgr.addListener(listener);
144 provider = new TestProvider(PID);
alshabibbb8b1282014-09-22 17:00:18 -0700145 providerService = registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800146 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700147 assertTrue("provider should be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700148 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700149 }
150
151 @After
152 public void tearDown() {
153 registry.unregister(provider);
154 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700155 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700156 service.removeListener(listener);
157 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700158 injectEventDispatcher(mgr, null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 mgr.deviceService = null;
160 }
161
162 private FlowRule flowRule(int tsval, int trval) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800163 return flowRule(DID, tsval, trval);
164 }
165
166 private FlowRule flowRule(DeviceId did, int tsval, int trval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700167 TestSelector ts = new TestSelector(tsval);
168 TestTreatment tr = new TestTreatment(trval);
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700169 return DefaultFlowRule.builder()
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800170 .forDevice(did)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700171 .withSelector(ts)
172 .withTreatment(tr)
173 .withPriority(10)
174 .fromApp(appId)
175 .makeTemporary(TIMEOUT)
176 .build();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700177 }
178
alshabibbb8b1282014-09-22 17:00:18 -0700179 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700180 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700181 service.applyFlowRules(rule);
182
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700183 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700184 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700185 }
186
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700187 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700188 if (events == null) {
189 assertTrue("events generated", listener.events.isEmpty());
190 }
191
192 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700193 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700194 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800195 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700196 i++;
197 }
198
199 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700200 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700201
202 listener.events.clear();
203 }
204
205 private int flowCount() {
206 return Sets.newHashSet(service.getFlowEntries(DID)).size();
207 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700208
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700209 @Test
210 public void getFlowEntries() {
211 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700212 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700213 FlowRule f1 = addFlowRule(1);
214 FlowRule f2 = addFlowRule(2);
215
alshabib1c319ff2014-10-04 20:29:09 -0700216 FlowEntry fe1 = new DefaultFlowEntry(f1);
217 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700218 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700219
alshabib1c319ff2014-10-04 20:29:09 -0700220 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700221 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
222 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700223
224 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800225 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700226 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700227
alshabib1c319ff2014-10-04 20:29:09 -0700228 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
Charles Chan93fa7272016-01-26 22:27:02 -0800229 validateEvents(RULE_UPDATED, RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700230 }
231
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700232 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
233 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700234 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700235 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700236 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
237 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
238 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700239 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700240 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700241 return true;
242 }
243
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700244 @Test
245 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700246
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700247 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700248 FlowRule r2 = flowRule(2, 2);
249 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700250
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700251 assertTrue("store should be empty",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800252 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700253 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700254 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700255 assertTrue("Entries should be pending add.",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800256 validateState(ImmutableMap.of(
257 r1, FlowEntryState.PENDING_ADD,
258 r2, FlowEntryState.PENDING_ADD,
259 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700260 }
261
262 @Test
263 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700264 FlowRule f1 = addFlowRule(1);
265 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700266 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700267 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700268
alshabib1c319ff2014-10-04 20:29:09 -0700269 FlowEntry fe1 = new DefaultFlowEntry(f1);
270 FlowEntry fe2 = new DefaultFlowEntry(f2);
271 FlowEntry fe3 = new DefaultFlowEntry(f3);
272 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700273 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
274 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700275
alshabib1c319ff2014-10-04 20:29:09 -0700276 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700277 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700278 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700279 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700280 assertTrue("Entries should be pending remove.",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800281 validateState(ImmutableMap.of(
282 f1, FlowEntryState.PENDING_REMOVE,
283 f2, FlowEntryState.PENDING_REMOVE,
284 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700285
alshabib1c319ff2014-10-04 20:29:09 -0700286 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700287 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700288 }
289
290 @Test
291 public void flowRemoved() {
alshabibbb8b1282014-09-22 17:00:18 -0700292 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700293 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700294 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700295 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800296
alshabib1c319ff2014-10-04 20:29:09 -0700297 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700298 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800299
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800300 //FIXME modification of "stored" flow entry outside of store
alshabib1c319ff2014-10-04 20:29:09 -0700301 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800302
alshabib1c319ff2014-10-04 20:29:09 -0700303 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800304
alshabib3d643ec2014-10-22 18:33:00 -0700305 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800306 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700307
alshabib1c319ff2014-10-04 20:29:09 -0700308 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700309 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700310
alshabibba5ac482014-10-02 17:15:20 -0700311 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700312 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700313 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800314
alshabib1c319ff2014-10-04 20:29:09 -0700315 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
Charles Chan93fa7272016-01-26 22:27:02 -0800316 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
alshabibba5ac482014-10-02 17:15:20 -0700317
alshabib1c319ff2014-10-04 20:29:09 -0700318 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700319 validateEvents();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700320 }
321
alshabibbb8b1282014-09-22 17:00:18 -0700322 @Test
323 public void flowMetrics() {
324 FlowRule f1 = flowRule(1, 1);
325 FlowRule f2 = flowRule(2, 2);
326 FlowRule f3 = flowRule(3, 3);
327
alshabibba5ac482014-10-02 17:15:20 -0700328 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700329
alshabib1c319ff2014-10-04 20:29:09 -0700330 FlowEntry fe1 = new DefaultFlowEntry(f1);
331 FlowEntry fe2 = new DefaultFlowEntry(f2);
332
alshabib1c319ff2014-10-04 20:29:09 -0700333 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
334 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
335
336 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700337
338 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700339 validateState(ImmutableMap.of(
340 f1, FlowEntryState.ADDED,
341 f2, FlowEntryState.ADDED,
342 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700343
alshabib3d643ec2014-10-22 18:33:00 -0700344 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800345 RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700346 }
347
348 @Test
349 public void extraneousFlow() {
350 FlowRule f1 = flowRule(1, 1);
351 FlowRule f2 = flowRule(2, 2);
352 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700353 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700354
alshabib1c319ff2014-10-04 20:29:09 -0700355// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
356// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
357// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
358 FlowEntry fe1 = new DefaultFlowEntry(f1);
359 FlowEntry fe2 = new DefaultFlowEntry(f2);
360 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700361
alshabib1c319ff2014-10-04 20:29:09 -0700362
363 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700364
alshabib3d643ec2014-10-22 18:33:00 -0700365 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700366
367 }
368
369 /*
370 * Tests whether a rule that was marked for removal but no flowRemoved was received
371 * is indeed removed at the next stats update.
372 */
373 @Test
374 public void flowMissingRemove() {
375 FlowRule f1 = flowRule(1, 1);
376 FlowRule f2 = flowRule(2, 2);
377 FlowRule f3 = flowRule(3, 3);
378
alshabib1c319ff2014-10-04 20:29:09 -0700379// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
380// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
381
382 FlowEntry fe1 = new DefaultFlowEntry(f1);
383 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700384 mgr.applyFlowRules(f1, f2, f3);
385
386 mgr.removeFlowRules(f3);
387
alshabib1c319ff2014-10-04 20:29:09 -0700388 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700389
alshabib3d643ec2014-10-22 18:33:00 -0700390 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
391 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700392
393 }
394
395 @Test
396 public void getByAppId() {
397 FlowRule f1 = flowRule(1, 1);
398 FlowRule f2 = flowRule(2, 2);
399 mgr.applyFlowRules(f1, f2);
400
401 assertTrue("should have two rules",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800402 Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
alshabibbb42cad2014-09-25 11:43:05 -0700403 }
404
405 @Test
406 public void removeByAppId() {
407 FlowRule f1 = flowRule(1, 1);
408 FlowRule f2 = flowRule(2, 2);
409 mgr.applyFlowRules(f1, f2);
410
411
412 mgr.removeFlowRulesById(appId);
413
414 //only check that we are in pending remove. Events and actual remove state will
415 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700416 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700417 f1, FlowEntryState.PENDING_REMOVE,
418 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700419 }
420
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800421 @Test
422 public void fallbackBasics() {
423 FlowRule f1 = flowRule(FOO_DID, 1, 1);
424 flowRules.clear();
425 mgr.applyFlowRules(f1);
426 assertTrue("flow rule not applied", flowRules.contains(f1));
427
428 flowRules.clear();
429 mgr.removeFlowRules(f1);
430 assertTrue("flow rule not removed", flowRules.contains(f1));
431 }
432
433 @Test
434 public void fallbackFlowRemoved() {
435 FlowRule f1 = flowRule(FOO_DID, 1, 1);
436 mgr.applyFlowRules(f1);
437 flowRules.clear();
438 providerService.flowRemoved(new DefaultFlowEntry(f1));
439 assertTrue("flow rule not reapplied", flowRules.contains(f1));
440 }
441
442 @Test
443 public void fallbackExtraFlow() {
444 FlowRule f1 = flowRule(FOO_DID, 1, 1);
445 flowRules.clear();
446 providerService.pushFlowMetrics(FOO_DID, ImmutableList.of(new DefaultFlowEntry(f1)));
447 assertTrue("flow rule not removed", flowRules.contains(f1));
448 }
449
450 @Test
451 public void fallbackPoll() {
452 FlowRuleDriverProvider fallback = (FlowRuleDriverProvider) mgr.defaultProvider();
453 FlowRule f1 = flowRule(FOO_DID, 1, 1);
454 mgr.applyFlowRules(f1);
455 FlowEntry fe = mgr.getFlowEntries(FOO_DID).iterator().next();
456 assertEquals("incorrect state", FlowEntryState.PENDING_ADD, fe.state());
457
458 fallback.init(fallback.providerService, mgr.deviceService, mgr.mastershipService, 1);
459 TestTools.assertAfter(2000, () -> {
460 FlowEntry e = mgr.getFlowEntries(FOO_DID).iterator().next();
461 assertEquals("incorrect state", FlowEntryState.ADDED, e.state());
462 });
463 }
464
465
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700466 private static class TestListener implements FlowRuleListener {
467 final List<FlowRuleEvent> events = new ArrayList<>();
468
469 @Override
470 public void event(FlowRuleEvent event) {
471 events.add(event);
472 }
473 }
474
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800475 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700476 @Override
477 public int getDeviceCount() {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800478 return 2;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700479 }
480
481 @Override
482 public Iterable<Device> getDevices() {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800483 return ImmutableList.of(DEV, FOO_DEV);
484 }
485
486 @Override
487 public Iterable<Device> getAvailableDevices() {
488 return getDevices();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700489 }
490
491 @Override
492 public Device getDevice(DeviceId deviceId) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800493 return deviceId.equals(FOO_DID) ? FOO_DEV : DEV;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700494 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700495 }
496
497 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
498
499 protected TestProvider(ProviderId id) {
500 super(PID);
501 }
502
503 @Override
504 public void applyFlowRule(FlowRule... flowRules) {
505 }
506
507 @Override
508 public void removeFlowRule(FlowRule... flowRules) {
509 }
510
alshabiba68eb962014-09-24 20:34:13 -0700511 @Override
512 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
513 }
514
alshabib902d41b2014-10-07 16:52:05 -0700515 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800516 public void executeBatch(FlowRuleBatchOperation batch) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800517 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700518 }
519
alshabibcf369912014-10-13 14:16:42 -0700520 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700521 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700522
523 @Override
524 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800525 return false;
alshabibcf369912014-10-13 14:16:42 -0700526 }
527
528 @Override
529 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800530 return false;
alshabibcf369912014-10-13 14:16:42 -0700531 }
532
533 @Override
534 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800535 return true;
alshabibcf369912014-10-13 14:16:42 -0700536 }
537
538 @Override
539 public CompletedBatchOperation get()
540 throws InterruptedException, ExecutionException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800541 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700542 }
543
544 @Override
545 public CompletedBatchOperation get(long timeout, TimeUnit unit)
546 throws InterruptedException,
547 ExecutionException, TimeoutException {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800548 return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700549 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700550
551 @Override
552 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800553 if (isDone()) {
554 executor.execute(task);
555 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700556 }
alshabibcf369912014-10-13 14:16:42 -0700557 }
alshabiba68eb962014-09-24 20:34:13 -0700558
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700559 }
560
561 private class TestSelector implements TrafficSelector {
562
563 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700564 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700565
566 public TestSelector(int val) {
567 testval = val;
568 }
569
570 @Override
alshabibba5ac482014-10-02 17:15:20 -0700571 public Set<Criterion> criteria() {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700572 return null;
573 }
574
575 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700576 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800577 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700578 return null;
579 }
580
581 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700582 public int hashCode() {
583 return testval;
584 }
585
586 @Override
587 public boolean equals(Object o) {
588 if (o instanceof TestSelector) {
589 return this.testval == ((TestSelector) o).testval;
590 }
591 return false;
592 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700593
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700594 }
595
596 private class TestTreatment implements TrafficTreatment {
597
598 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700599 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700600
601 public TestTreatment(int val) {
602 testval = val;
603 }
604
605 @Override
alshabib346b5b32015-03-06 00:42:16 -0800606 public List<Instruction> deferred() {
607 return null;
608 }
609
610 @Override
611 public List<Instruction> immediate() {
612 return null;
613 }
614
615 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700616 public List<Instruction> allInstructions() {
617 return null;
618 }
619
620 @Override
alshabib346b5b32015-03-06 00:42:16 -0800621 public Instructions.TableTypeTransition tableTransition() {
622 return null;
623 }
624
625 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700626 public boolean clearedDeferred() {
627 return false;
alshabib346b5b32015-03-06 00:42:16 -0800628 }
629
630 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700631 public int hashCode() {
632 return testval;
633 }
634
635 @Override
636 public boolean equals(Object o) {
637 if (o instanceof TestTreatment) {
638 return this.testval == ((TestTreatment) o).testval;
639 }
640 return false;
641 }
642
Saurav Das86af8f12015-05-25 23:55:33 -0700643 @Override
644 public MetadataInstruction writeMetadata() {
645 return null;
646 }
647
alshabib10c810b2015-08-18 16:59:04 -0700648 @Override
649 public Instructions.MeterInstruction metered() {
650 return null;
651 }
652
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700653 }
654
alshabib92c65ad2014-10-08 21:56:05 -0700655 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800656 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700657 super(id, name);
658 }
659 }
660
Ray Milkeycc53abd2015-02-19 12:31:33 -0800661 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800662
663 @Override
664 public IdGenerator getIdGenerator(String topic) {
665 return new IdGenerator() {
666 private AtomicLong counter = new AtomicLong(0);
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800667
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800668 @Override
669 public long getNewId() {
670 return counter.getAndIncrement();
671 }
672 };
673 }
674 }
675
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800676 private class TestMastershipService extends MastershipServiceAdapter {
677 @Override
678 public MastershipRole getLocalRole(DeviceId deviceId) {
679 return MastershipRole.MASTER;
680 }
681 }
682
683 private class TestDriverManager extends DriverManager {
684 TestDriverManager() {
685 this.deviceService = mgr.deviceService;
686 activate();
687 }
688 }
689
690 static Collection<FlowRule> flowRules = new HashSet<>();
691
692 public static class TestFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
693
694 @Override
695 public Collection<FlowEntry> getFlowEntries() {
696 ImmutableList.Builder<FlowEntry> builder = ImmutableList.builder();
697 flowRules.stream().map(DefaultFlowEntry::new).forEach(builder::add);
698 return builder.build();
699 }
700
701 @Override
702 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
703 flowRules.addAll(rules);
704 return rules;
705 }
706
707 @Override
708 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
709 flowRules.addAll(rules);
710 return rules;
711 }
712 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700713}