blob: 38013888336f4db3dae32e2432287ffeb3f3125e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +000028import org.onlab.packet.Ip4Address;
29import org.onlab.packet.IpAddress;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070030import org.onosproject.cfg.ComponentConfigAdapter;
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +000031import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.ControllerNode;
33import org.onosproject.cluster.NodeId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080034import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.core.ApplicationId;
Ray Milkeycc53abd2015-02-19 12:31:33 -080036import org.onosproject.core.CoreServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.core.DefaultApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080038import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080039import org.onosproject.mastership.MastershipServiceAdapter;
40import org.onosproject.net.AnnotationKeys;
41import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.DefaultDevice;
43import org.onosproject.net.Device;
44import org.onosproject.net.Device.Type;
45import org.onosproject.net.DeviceId;
46import org.onosproject.net.MastershipRole;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070047import org.onosproject.net.config.NetworkConfigServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.device.DeviceServiceAdapter;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080049import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.driver.DefaultDriver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070051import org.onosproject.net.driver.DriverRegistry;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080052import org.onosproject.net.driver.impl.DriverManager;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070053import org.onosproject.net.driver.impl.DriverRegistryManager;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.flow.CompletedBatchOperation;
55import org.onosproject.net.flow.DefaultFlowEntry;
56import org.onosproject.net.flow.DefaultFlowRule;
57import org.onosproject.net.flow.FlowEntry;
58import org.onosproject.net.flow.FlowEntry.FlowEntryState;
59import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080060import org.onosproject.net.flow.FlowRuleEvent;
61import org.onosproject.net.flow.FlowRuleListener;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080062import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.flow.FlowRuleProvider;
64import org.onosproject.net.flow.FlowRuleProviderRegistry;
65import org.onosproject.net.flow.FlowRuleProviderService;
66import org.onosproject.net.flow.FlowRuleService;
67import org.onosproject.net.flow.StoredFlowEntry;
68import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
70import org.onosproject.net.flow.criteria.Criterion;
71import org.onosproject.net.flow.instructions.Instruction;
alshabib346b5b32015-03-06 00:42:16 -080072import org.onosproject.net.flow.instructions.Instructions;
Saurav Das86af8f12015-05-25 23:55:33 -070073import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070074import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
75import org.onosproject.net.pi.PiPipeconfServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080076import org.onosproject.net.provider.AbstractProvider;
77import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070078import org.onosproject.store.trivial.SimpleFlowRuleStore;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070079
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070080import java.util.ArrayList;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080081import java.util.Collection;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070082import java.util.Collections;
83import java.util.HashMap;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080084import java.util.HashSet;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070085import java.util.List;
86import java.util.Map;
87import java.util.Set;
88import java.util.concurrent.ExecutionException;
89import java.util.concurrent.Executor;
90import java.util.concurrent.TimeUnit;
91import java.util.concurrent.TimeoutException;
92import java.util.concurrent.atomic.AtomicLong;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070093
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +000094import static org.easymock.EasyMock.*;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070095import static org.junit.Assert.assertEquals;
96import static org.junit.Assert.assertFalse;
97import static org.junit.Assert.assertNotNull;
98import static org.junit.Assert.assertTrue;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070099import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700100import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADDED;
101import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
102import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
103import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
104import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700105
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700106/**
107 * Test codifying the flow rule service & flow rule provider service contracts.
108 */
tom202175a2014-09-19 19:00:11 -0700109public class FlowRuleManagerTest {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700110
alshabib92c65ad2014-10-08 21:56:05 -0700111
tomf6ab2152014-09-18 12:08:29 -0700112 private static final ProviderId PID = new ProviderId("of", "foo");
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800113 private static final ProviderId FOO_PID = new ProviderId("foo", "foo");
114
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700115 private static final DeviceId DID = DeviceId.deviceId("of:001");
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800116 private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002");
alshabibba5ac482014-10-02 17:15:20 -0700117 private static final int TIMEOUT = 10;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800118
119 private static final DefaultAnnotations ANNOTATIONS =
120 DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build();
121
122 private static final Device DEV =
123 new DefaultDevice(PID, DID, Type.SWITCH, "", "", "", "", null);
124 private static final Device FOO_DEV =
125 new DefaultDevice(FOO_PID, FOO_DID, Type.SWITCH, "", "", "", "", null, ANNOTATIONS);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700126
tom202175a2014-09-19 19:00:11 -0700127 private FlowRuleManager mgr;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700128
129 protected FlowRuleService service;
130 protected FlowRuleProviderRegistry registry;
alshabibbb8b1282014-09-22 17:00:18 -0700131 protected FlowRuleProviderService providerService;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700132 protected TestProvider provider;
133 protected TestListener listener = new TestListener();
alshabiba68eb962014-09-24 20:34:13 -0700134 private ApplicationId appId;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700135
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800136 private TestDriverManager driverService;
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +0000137 private static final String NODE_ID = "1";
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800138
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700139 @Before
140 public void setUp() {
tom202175a2014-09-19 19:00:11 -0700141 mgr = new FlowRuleManager();
tombe988312014-09-19 18:38:47 -0700142 mgr.store = new SimpleFlowRuleStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700143 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700144 mgr.deviceService = new TestDeviceService();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800145 mgr.mastershipService = new TestMastershipService();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800146 mgr.coreService = new TestCoreService();
147 mgr.operationsService = MoreExecutors.newDirectExecutorService();
148 mgr.deviceInstallers = MoreExecutors.newDirectExecutorService();
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700149 mgr.cfgService = new ComponentConfigAdapter();
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +0000150
151 ClusterService mockClusterService = createMock(ClusterService.class);
152 NodeId nodeId = new NodeId(NODE_ID);
153 MockControllerNode mockControllerNode = new MockControllerNode(nodeId);
154 expect(mockClusterService.getLocalNode())
155 .andReturn(mockControllerNode).anyTimes();
156 replay(mockClusterService);
157 mgr.clusterService = mockClusterService;
158
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700159 service = mgr;
160 registry = mgr;
161
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700162 DriverRegistryManager driverRegistry = new DriverRegistryManager();
163 driverService = new TestDriverManager(driverRegistry);
164 driverRegistry.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "",
165 ImmutableMap.of(FlowRuleProgrammable.class,
166 TestFlowRuleProgrammable.class),
167 ImmutableMap.of()));
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800168
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700169 mgr.activate(null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700170 mgr.addListener(listener);
171 provider = new TestProvider(PID);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700172 providerService = this.registry.register(provider);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800173 appId = new TestApplicationId(0, "FlowRuleManagerTest");
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700174 assertTrue("provider should be registered",
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700175 this.registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700176 }
177
178 @After
179 public void tearDown() {
180 registry.unregister(provider);
181 assertFalse("provider should not be registered",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700182 registry.getProviders().contains(provider.id()));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700183 service.removeListener(listener);
184 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700185 injectEventDispatcher(mgr, null);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700186 mgr.deviceService = null;
187 }
188
189 private FlowRule flowRule(int tsval, int trval) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800190 return flowRule(DID, tsval, trval);
191 }
192
193 private FlowRule flowRule(DeviceId did, int tsval, int trval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700194 TestSelector ts = new TestSelector(tsval);
195 TestTreatment tr = new TestTreatment(trval);
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700196 return DefaultFlowRule.builder()
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800197 .forDevice(did)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700198 .withSelector(ts)
199 .withTreatment(tr)
200 .withPriority(10)
201 .fromApp(appId)
202 .makeTemporary(TIMEOUT)
203 .build();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700204 }
205
alshabibbb8b1282014-09-22 17:00:18 -0700206 private FlowRule addFlowRule(int hval) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700207 FlowRule rule = flowRule(hval, hval);
alshabibba5ac482014-10-02 17:15:20 -0700208 service.applyFlowRules(rule);
209
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700210 assertNotNull("rule should be found", service.getFlowEntries(DID));
alshabibbb8b1282014-09-22 17:00:18 -0700211 return rule;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700212 }
213
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700214 private void validateEvents(FlowRuleEvent.Type... events) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700215 if (events == null) {
216 assertTrue("events generated", listener.events.isEmpty());
217 }
218
219 int i = 0;
alshabibbb42cad2014-09-25 11:43:05 -0700220 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700221 for (FlowRuleEvent e : listener.events) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800222 assertEquals("unexpected event", events[i], e.type());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700223 i++;
224 }
225
226 assertEquals("mispredicted number of events",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700227 events.length, listener.events.size());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700228
229 listener.events.clear();
230 }
231
232 private int flowCount() {
233 return Sets.newHashSet(service.getFlowEntries(DID)).size();
234 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700235
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700236 @Test
237 public void getFlowEntries() {
238 assertTrue("store should be empty",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700239 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabibba5ac482014-10-02 17:15:20 -0700240 FlowRule f1 = addFlowRule(1);
241 FlowRule f2 = addFlowRule(2);
242
alshabib1c319ff2014-10-04 20:29:09 -0700243 FlowEntry fe1 = new DefaultFlowEntry(f1);
244 FlowEntry fe2 = new DefaultFlowEntry(f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700245 assertEquals("2 rules should exist", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700246
alshabib1c319ff2014-10-04 20:29:09 -0700247 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabib3d643ec2014-10-22 18:33:00 -0700248 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
249 RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700250
251 addFlowRule(1);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800252 System.err.println("events :" + listener.events);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700253 assertEquals("should still be 2 rules", 2, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700254
alshabib1c319ff2014-10-04 20:29:09 -0700255 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
Charles Chan93fa7272016-01-26 22:27:02 -0800256 validateEvents(RULE_UPDATED, RULE_UPDATED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700257 }
258
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700259 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
260 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
alshabib1c319ff2014-10-04 20:29:09 -0700261 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
alshabib1c319ff2014-10-04 20:29:09 -0700262 for (FlowEntry f : rules) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700263 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
264 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
265 expectedToCheck.remove(f);
alshabibbb8b1282014-09-22 17:00:18 -0700266 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700267 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
alshabibbb8b1282014-09-22 17:00:18 -0700268 return true;
269 }
270
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700271 @Test
272 public void applyFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700273
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700274 FlowRule r1 = flowRule(1, 1);
alshabiba68eb962014-09-24 20:34:13 -0700275 FlowRule r2 = flowRule(2, 2);
276 FlowRule r3 = flowRule(3, 3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700277
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700278 assertTrue("store should be empty",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800279 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
alshabib219ebaa2014-09-22 15:41:24 -0700280 mgr.applyFlowRules(r1, r2, r3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700281 assertEquals("3 rules should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700282 assertTrue("Entries should be pending add.",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800283 validateState(ImmutableMap.of(
284 r1, FlowEntryState.PENDING_ADD,
285 r2, FlowEntryState.PENDING_ADD,
286 r3, FlowEntryState.PENDING_ADD)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700287 }
288
289 @Test
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530290 public void purgeFlowRules() {
291 FlowRule f1 = addFlowRule(1);
292 FlowRule f2 = addFlowRule(2);
293 FlowRule f3 = addFlowRule(3);
294 assertEquals("3 rules should exist", 3, flowCount());
295 FlowEntry fe1 = new DefaultFlowEntry(f1);
296 FlowEntry fe2 = new DefaultFlowEntry(f2);
297 FlowEntry fe3 = new DefaultFlowEntry(f3);
298 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
299 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700300 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530301 mgr.purgeFlowRules(DID);
302 assertEquals("0 rule should exist", 0, flowCount());
303 }
304
305 @Test
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700306 public void removeFlowRules() {
alshabibbb8b1282014-09-22 17:00:18 -0700307 FlowRule f1 = addFlowRule(1);
308 FlowRule f2 = addFlowRule(2);
alshabibba5ac482014-10-02 17:15:20 -0700309 FlowRule f3 = addFlowRule(3);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700310 assertEquals("3 rules should exist", 3, flowCount());
alshabibba5ac482014-10-02 17:15:20 -0700311
alshabib1c319ff2014-10-04 20:29:09 -0700312 FlowEntry fe1 = new DefaultFlowEntry(f1);
313 FlowEntry fe2 = new DefaultFlowEntry(f2);
314 FlowEntry fe3 = new DefaultFlowEntry(f3);
315 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
alshabib3d643ec2014-10-22 18:33:00 -0700316 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
317 RULE_ADDED, RULE_ADDED, RULE_ADDED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700318
alshabib1c319ff2014-10-04 20:29:09 -0700319 mgr.removeFlowRules(f1, f2);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700320 //removing from north, so no events generated
alshabib3d643ec2014-10-22 18:33:00 -0700321 validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
alshabib219ebaa2014-09-22 15:41:24 -0700322 assertEquals("3 rule should exist", 3, flowCount());
alshabibbb8b1282014-09-22 17:00:18 -0700323 assertTrue("Entries should be pending remove.",
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800324 validateState(ImmutableMap.of(
325 f1, FlowEntryState.PENDING_REMOVE,
326 f2, FlowEntryState.PENDING_REMOVE,
327 f3, FlowEntryState.ADDED)));
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700328
alshabib1c319ff2014-10-04 20:29:09 -0700329 mgr.removeFlowRules(f1);
alshabib219ebaa2014-09-22 15:41:24 -0700330 assertEquals("3 rule should still exist", 3, flowCount());
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700331 }
332
333 @Test
334 public void flowRemoved() {
alshabibbb8b1282014-09-22 17:00:18 -0700335 FlowRule f1 = addFlowRule(1);
alshabibba5ac482014-10-02 17:15:20 -0700336 FlowRule f2 = addFlowRule(2);
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700337 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
alshabib1c319ff2014-10-04 20:29:09 -0700338 FlowEntry fe2 = new DefaultFlowEntry(f2);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800339
alshabib1c319ff2014-10-04 20:29:09 -0700340 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
alshabiba68eb962014-09-24 20:34:13 -0700341 service.removeFlowRules(f1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800342
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800343 //FIXME modification of "stored" flow entry outside of store
alshabib1c319ff2014-10-04 20:29:09 -0700344 fe1.setState(FlowEntryState.REMOVED);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800345
alshabib1c319ff2014-10-04 20:29:09 -0700346 providerService.flowRemoved(fe1);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800347
alshabib3d643ec2014-10-22 18:33:00 -0700348 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800349 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700350
alshabib1c319ff2014-10-04 20:29:09 -0700351 providerService.flowRemoved(fe1);
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700352 validateEvents();
alshabibbb42cad2014-09-25 11:43:05 -0700353
alshabibba5ac482014-10-02 17:15:20 -0700354 FlowRule f3 = flowRule(3, 3);
alshabib1c319ff2014-10-04 20:29:09 -0700355 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibba5ac482014-10-02 17:15:20 -0700356 service.applyFlowRules(f3);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800357
alshabib1c319ff2014-10-04 20:29:09 -0700358 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
Charles Chan93fa7272016-01-26 22:27:02 -0800359 validateEvents(RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
alshabibba5ac482014-10-02 17:15:20 -0700360
alshabib1c319ff2014-10-04 20:29:09 -0700361 providerService.flowRemoved(fe3);
alshabibbb42cad2014-09-25 11:43:05 -0700362 validateEvents();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700363 }
364
alshabibbb8b1282014-09-22 17:00:18 -0700365 @Test
366 public void flowMetrics() {
367 FlowRule f1 = flowRule(1, 1);
368 FlowRule f2 = flowRule(2, 2);
369 FlowRule f3 = flowRule(3, 3);
370
alshabibba5ac482014-10-02 17:15:20 -0700371 mgr.applyFlowRules(f1, f2, f3);
alshabibbb8b1282014-09-22 17:00:18 -0700372
alshabib1c319ff2014-10-04 20:29:09 -0700373 FlowEntry fe1 = new DefaultFlowEntry(f1);
374 FlowEntry fe2 = new DefaultFlowEntry(f2);
375
alshabib1c319ff2014-10-04 20:29:09 -0700376 //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
377 //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
378
379 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb8b1282014-09-22 17:00:18 -0700380
381 assertTrue("Entries should be added.",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700382 validateState(ImmutableMap.of(
383 f1, FlowEntryState.ADDED,
384 f2, FlowEntryState.ADDED,
385 f3, FlowEntryState.PENDING_ADD)));
alshabibbb42cad2014-09-25 11:43:05 -0700386
alshabib3d643ec2014-10-22 18:33:00 -0700387 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530388 RULE_ADDED, RULE_ADDED, RULE_ADD_REQUESTED);
alshabibbb42cad2014-09-25 11:43:05 -0700389 }
390
391 @Test
392 public void extraneousFlow() {
393 FlowRule f1 = flowRule(1, 1);
394 FlowRule f2 = flowRule(2, 2);
395 FlowRule f3 = flowRule(3, 3);
alshabibba5ac482014-10-02 17:15:20 -0700396 mgr.applyFlowRules(f1, f2);
alshabibbb42cad2014-09-25 11:43:05 -0700397
alshabib1c319ff2014-10-04 20:29:09 -0700398// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
399// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
400// FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
401 FlowEntry fe1 = new DefaultFlowEntry(f1);
402 FlowEntry fe2 = new DefaultFlowEntry(f2);
403 FlowEntry fe3 = new DefaultFlowEntry(f3);
alshabibbb42cad2014-09-25 11:43:05 -0700404
alshabib1c319ff2014-10-04 20:29:09 -0700405
406 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3));
alshabibbb42cad2014-09-25 11:43:05 -0700407
alshabib3d643ec2014-10-22 18:33:00 -0700408 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED);
alshabibbb42cad2014-09-25 11:43:05 -0700409
410 }
411
412 /*
413 * Tests whether a rule that was marked for removal but no flowRemoved was received
414 * is indeed removed at the next stats update.
415 */
416 @Test
417 public void flowMissingRemove() {
418 FlowRule f1 = flowRule(1, 1);
419 FlowRule f2 = flowRule(2, 2);
420 FlowRule f3 = flowRule(3, 3);
421
alshabib1c319ff2014-10-04 20:29:09 -0700422// FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
423// FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
424
425 FlowEntry fe1 = new DefaultFlowEntry(f1);
426 FlowEntry fe2 = new DefaultFlowEntry(f2);
alshabibbb42cad2014-09-25 11:43:05 -0700427 mgr.applyFlowRules(f1, f2, f3);
428
429 mgr.removeFlowRules(f3);
430
alshabib1c319ff2014-10-04 20:29:09 -0700431 providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2));
alshabibbb42cad2014-09-25 11:43:05 -0700432
alshabib3d643ec2014-10-22 18:33:00 -0700433 validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
434 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
alshabibbb42cad2014-09-25 11:43:05 -0700435
436 }
437
438 @Test
alshabibbb42cad2014-09-25 11:43:05 -0700439 public void removeByAppId() {
440 FlowRule f1 = flowRule(1, 1);
441 FlowRule f2 = flowRule(2, 2);
442 mgr.applyFlowRules(f1, f2);
443
444
445 mgr.removeFlowRulesById(appId);
446
447 //only check that we are in pending remove. Events and actual remove state will
448 // be set by flowRemoved call.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700449 validateState(ImmutableMap.of(
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700450 f1, FlowEntryState.PENDING_REMOVE,
451 f2, FlowEntryState.PENDING_REMOVE));
alshabibbb8b1282014-09-22 17:00:18 -0700452 }
453
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800454 @Test
455 public void fallbackBasics() {
456 FlowRule f1 = flowRule(FOO_DID, 1, 1);
457 flowRules.clear();
458 mgr.applyFlowRules(f1);
459 assertTrue("flow rule not applied", flowRules.contains(f1));
460
461 flowRules.clear();
462 mgr.removeFlowRules(f1);
463 assertTrue("flow rule not removed", flowRules.contains(f1));
464 }
465
466 @Test
467 public void fallbackFlowRemoved() {
468 FlowRule f1 = flowRule(FOO_DID, 1, 1);
469 mgr.applyFlowRules(f1);
470 flowRules.clear();
471 providerService.flowRemoved(new DefaultFlowEntry(f1));
472 assertTrue("flow rule not reapplied", flowRules.contains(f1));
473 }
474
475 @Test
476 public void fallbackExtraFlow() {
477 FlowRule f1 = flowRule(FOO_DID, 1, 1);
478 flowRules.clear();
479 providerService.pushFlowMetrics(FOO_DID, ImmutableList.of(new DefaultFlowEntry(f1)));
480 assertTrue("flow rule not removed", flowRules.contains(f1));
481 }
482
483 @Test
484 public void fallbackPoll() {
485 FlowRuleDriverProvider fallback = (FlowRuleDriverProvider) mgr.defaultProvider();
486 FlowRule f1 = flowRule(FOO_DID, 1, 1);
487 mgr.applyFlowRules(f1);
488 FlowEntry fe = mgr.getFlowEntries(FOO_DID).iterator().next();
489 assertEquals("incorrect state", FlowEntryState.PENDING_ADD, fe.state());
490
491 fallback.init(fallback.providerService, mgr.deviceService, mgr.mastershipService, 1);
492 TestTools.assertAfter(2000, () -> {
493 FlowEntry e = mgr.getFlowEntries(FOO_DID).iterator().next();
494 assertEquals("incorrect state", FlowEntryState.ADDED, e.state());
495 });
496 }
497
498
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700499 private static class TestListener implements FlowRuleListener {
500 final List<FlowRuleEvent> events = new ArrayList<>();
501
502 @Override
503 public void event(FlowRuleEvent event) {
504 events.add(event);
505 }
506 }
507
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800508 private static class TestDeviceService extends DeviceServiceAdapter {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700509 @Override
510 public int getDeviceCount() {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800511 return 2;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700512 }
513
514 @Override
515 public Iterable<Device> getDevices() {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800516 return ImmutableList.of(DEV, FOO_DEV);
517 }
518
519 @Override
520 public Iterable<Device> getAvailableDevices() {
521 return getDevices();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700522 }
523
524 @Override
525 public Device getDevice(DeviceId deviceId) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800526 return deviceId.equals(FOO_DID) ? FOO_DEV : DEV;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700527 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700528 }
529
530 private class TestProvider extends AbstractProvider implements FlowRuleProvider {
531
532 protected TestProvider(ProviderId id) {
533 super(PID);
534 }
535
536 @Override
537 public void applyFlowRule(FlowRule... flowRules) {
538 }
539
540 @Override
541 public void removeFlowRule(FlowRule... flowRules) {
542 }
543
alshabiba68eb962014-09-24 20:34:13 -0700544 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800545 public void executeBatch(FlowRuleBatchOperation batch) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800546 // TODO: need to call batchOperationComplete
alshabib902d41b2014-10-07 16:52:05 -0700547 }
548
alshabibcf369912014-10-13 14:16:42 -0700549 private class TestInstallationFuture
Madan Jampani117aaae2014-10-23 10:04:05 -0700550 implements ListenableFuture<CompletedBatchOperation> {
alshabibcf369912014-10-13 14:16:42 -0700551
552 @Override
553 public boolean cancel(boolean mayInterruptIfRunning) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800554 return false;
alshabibcf369912014-10-13 14:16:42 -0700555 }
556
557 @Override
558 public boolean isCancelled() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800559 return false;
alshabibcf369912014-10-13 14:16:42 -0700560 }
561
562 @Override
563 public boolean isDone() {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800564 return true;
alshabibcf369912014-10-13 14:16:42 -0700565 }
566
567 @Override
568 public CompletedBatchOperation get()
569 throws InterruptedException, ExecutionException {
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700570 return new CompletedBatchOperation(true, Collections.emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700571 }
572
573 @Override
574 public CompletedBatchOperation get(long timeout, TimeUnit unit)
575 throws InterruptedException,
576 ExecutionException, TimeoutException {
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700577 return new CompletedBatchOperation(true, Collections.emptySet(), null);
alshabibcf369912014-10-13 14:16:42 -0700578 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700579
580 @Override
581 public void addListener(Runnable task, Executor executor) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800582 if (isDone()) {
583 executor.execute(task);
584 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700585 }
alshabibcf369912014-10-13 14:16:42 -0700586 }
alshabiba68eb962014-09-24 20:34:13 -0700587
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700588 }
589
590 private class TestSelector implements TrafficSelector {
591
592 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700593 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700594
595 public TestSelector(int val) {
596 testval = val;
597 }
598
599 @Override
alshabibba5ac482014-10-02 17:15:20 -0700600 public Set<Criterion> criteria() {
Hyunsun Moon7bfbc1c2016-04-05 16:40:43 -0700601 return Collections.emptySet();
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700602 }
603
604 @Override
Jonathan Hart936c49d2014-10-23 16:38:59 -0700605 public Criterion getCriterion(
Brian O'Connorabafb502014-12-02 22:26:20 -0800606 org.onosproject.net.flow.criteria.Criterion.Type type) {
Jonathan Hart936c49d2014-10-23 16:38:59 -0700607 return null;
608 }
609
610 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700611 public int hashCode() {
612 return testval;
613 }
614
615 @Override
616 public boolean equals(Object o) {
617 if (o instanceof TestSelector) {
618 return this.testval == ((TestSelector) o).testval;
619 }
620 return false;
621 }
Jonathan Hart936c49d2014-10-23 16:38:59 -0700622
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700623 }
624
625 private class TestTreatment implements TrafficTreatment {
626
627 //for controlling hashcode uniqueness;
alshabib97044902014-09-18 14:52:16 -0700628 private final int testval;
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700629
630 public TestTreatment(int val) {
631 testval = val;
632 }
633
634 @Override
alshabib346b5b32015-03-06 00:42:16 -0800635 public List<Instruction> deferred() {
636 return null;
637 }
638
639 @Override
640 public List<Instruction> immediate() {
641 return null;
642 }
643
644 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700645 public List<Instruction> allInstructions() {
646 return null;
647 }
648
649 @Override
alshabib346b5b32015-03-06 00:42:16 -0800650 public Instructions.TableTypeTransition tableTransition() {
651 return null;
652 }
653
654 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700655 public boolean clearedDeferred() {
656 return false;
alshabib346b5b32015-03-06 00:42:16 -0800657 }
658
659 @Override
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700660 public int hashCode() {
661 return testval;
662 }
663
664 @Override
665 public boolean equals(Object o) {
666 if (o instanceof TestTreatment) {
667 return this.testval == ((TestTreatment) o).testval;
668 }
669 return false;
670 }
671
Saurav Das86af8f12015-05-25 23:55:33 -0700672 @Override
673 public MetadataInstruction writeMetadata() {
674 return null;
675 }
676
alshabib10c810b2015-08-18 16:59:04 -0700677 @Override
Cem Türker3baff672017-10-12 15:09:01 +0300678 public Instructions.StatTriggerInstruction statTrigger() {
679 return null;
680 }
681
682 @Override
alshabib10c810b2015-08-18 16:59:04 -0700683 public Instructions.MeterInstruction metered() {
684 return null;
685 }
686
cansu.toprak409289d2017-10-27 10:04:05 +0300687 @Override
688 public Set<Instructions.MeterInstruction> meters() {
689 return Sets.newHashSet();
690 }
691
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700692 }
693
alshabib92c65ad2014-10-08 21:56:05 -0700694 public class TestApplicationId extends DefaultApplicationId {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800695 public TestApplicationId(int id, String name) {
alshabib92c65ad2014-10-08 21:56:05 -0700696 super(id, name);
697 }
698 }
699
Ray Milkeycc53abd2015-02-19 12:31:33 -0800700 private class TestCoreService extends CoreServiceAdapter {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800701
702 @Override
703 public IdGenerator getIdGenerator(String topic) {
704 return new IdGenerator() {
705 private AtomicLong counter = new AtomicLong(0);
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800706
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800707 @Override
708 public long getNewId() {
709 return counter.getAndIncrement();
710 }
711 };
712 }
713 }
714
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800715 private class TestMastershipService extends MastershipServiceAdapter {
716 @Override
717 public MastershipRole getLocalRole(DeviceId deviceId) {
718 return MastershipRole.MASTER;
719 }
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +0000720
721 @Override
722 public NodeId getMasterFor(DeviceId deviceId) {
723 return new NodeId(NODE_ID);
724 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800725 }
726
727 private class TestDriverManager extends DriverManager {
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700728 TestDriverManager(DriverRegistry registry) {
729 this.registry = registry;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800730 this.deviceService = mgr.deviceService;
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700731 this.pipeconfService = new PiPipeconfServiceAdapter();
732 this.networkConfigService = new NetworkConfigServiceAdapter();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800733 activate();
734 }
735 }
736
737 static Collection<FlowRule> flowRules = new HashSet<>();
738
739 public static class TestFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
740
741 @Override
742 public Collection<FlowEntry> getFlowEntries() {
743 ImmutableList.Builder<FlowEntry> builder = ImmutableList.builder();
744 flowRules.stream().map(DefaultFlowEntry::new).forEach(builder::add);
745 return builder.build();
746 }
747
748 @Override
749 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
750 flowRules.addAll(rules);
751 return rules;
752 }
753
754 @Override
755 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
756 flowRules.addAll(rules);
757 return rules;
758 }
759 }
Pier Luigi Ventred8a923c2020-02-20 11:25:31 +0000760
761 private static class MockControllerNode implements ControllerNode {
762 final NodeId id;
763
764 public MockControllerNode(NodeId id) {
765 this.id = id;
766 }
767
768 @Override
769 public NodeId id() {
770 return this.id;
771 }
772
773 @Override
774 public Ip4Address ip() {
775 return Ip4Address.valueOf("127.0.0.1");
776 }
777
778 @Override
779 public IpAddress ip(boolean resolve) {
780 return null;
781 }
782
783 @Override
784 public String host() {
785 return null;
786 }
787
788 @Override
789 public int tcpPort() {
790 return 0;
791 }
792 }
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700793}