blob: 037b67f52068af14b67a453f4327c605dadf4364 [file] [log] [blame]
yoonseon6b972c32016-12-06 16:45:03 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
yoonseon6b972c32016-12-06 16:45:03 -08003 *
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 */
16
17package org.onosproject.incubator.net.virtual.impl;
18
yoonseonbd8a93d2016-12-07 15:51:21 -080019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
yoonseon6b972c32016-12-06 16:45:03 -080021import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import com.google.common.util.concurrent.MoreExecutors;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.junit.TestUtils;
28import org.onlab.osgi.ServiceDirectory;
29import org.onlab.osgi.TestServiceDirectory;
yoonseon6b972c32016-12-06 16:45:03 -080030import org.onosproject.TestApplicationId;
31import org.onosproject.common.event.impl.TestEventDispatcher;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
yoonseonbd8a93d2016-12-07 15:51:21 -080034import org.onosproject.event.EventDeliveryService;
yoonseon6b972c32016-12-06 16:45:03 -080035import org.onosproject.incubator.net.virtual.NetworkId;
yoonseon6b972c32016-12-06 16:45:03 -080036import org.onosproject.incubator.net.virtual.VirtualNetwork;
37import org.onosproject.incubator.net.virtual.VirtualNetworkFlowRuleStore;
38import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
39import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager;
40import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider;
41import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProvider;
yoonseonbd8a93d2016-12-07 15:51:21 -080042import org.onosproject.incubator.net.virtual.provider.VirtualFlowRuleProviderService;
yoonseon6b972c32016-12-06 16:45:03 -080043import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080044import org.onosproject.incubator.net.virtual.store.impl.DistributedVirtualNetworkStore;
45import org.onosproject.incubator.net.virtual.store.impl.SimpleVirtualFlowRuleStore;
yoonseon6b972c32016-12-06 16:45:03 -080046import org.onosproject.net.DeviceId;
yoonseon6b972c32016-12-06 16:45:03 -080047import org.onosproject.net.NetTestTools;
yoonseon6b972c32016-12-06 16:45:03 -080048import org.onosproject.net.flow.DefaultFlowEntry;
49import org.onosproject.net.flow.DefaultFlowRule;
50import org.onosproject.net.flow.FlowEntry;
yoonseon6b972c32016-12-06 16:45:03 -080051import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070052import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
yoonseon6b972c32016-12-06 16:45:03 -080053import org.onosproject.net.flow.FlowRuleEvent;
54import org.onosproject.net.flow.FlowRuleListener;
55import org.onosproject.net.flow.FlowRuleService;
yoonseon6b972c32016-12-06 16:45:03 -080056import org.onosproject.net.flow.StoredFlowEntry;
yoonseon6b972c32016-12-06 16:45:03 -080057import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criterion;
60import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions;
yoonseon6b972c32016-12-06 16:45:03 -080062import org.onosproject.net.provider.ProviderId;
63import org.onosproject.store.service.TestStorageService;
64
65import java.util.ArrayList;
66import java.util.Collections;
yoonseonbd8a93d2016-12-07 15:51:21 -080067import java.util.HashMap;
yoonseon6b972c32016-12-06 16:45:03 -080068import java.util.List;
yoonseonbd8a93d2016-12-07 15:51:21 -080069import java.util.Map;
yoonseon6b972c32016-12-06 16:45:03 -080070import java.util.Set;
yoonseon6b972c32016-12-06 16:45:03 -080071
72import static org.junit.Assert.*;
yoonseonbd8a93d2016-12-07 15:51:21 -080073import static org.onosproject.net.flow.FlowRuleEvent.Type.*;
yoonseon6b972c32016-12-06 16:45:03 -080074
yoonseon322c9c32016-12-07 16:47:02 -080075public class VirtualNetworkFlowRuleManagerTest extends VirtualNetworkTestUtil {
yoonseon6b972c32016-12-06 16:45:03 -080076 private static final int TIMEOUT = 10;
77
78 private VirtualNetworkManager manager;
79 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
yoonseon6b972c32016-12-06 16:45:03 -080080 private ServiceDirectory testDirectory;
81 private VirtualNetworkFlowRuleStore flowRuleStore;
yoonseonbd8a93d2016-12-07 15:51:21 -080082 private VirtualProviderManager providerRegistryService;
83
84 private EventDeliveryService eventDeliveryService;
yoonseon6b972c32016-12-06 16:45:03 -080085
86 private VirtualNetworkFlowRuleManager vnetFlowRuleService1;
87 private VirtualNetworkFlowRuleManager vnetFlowRuleService2;
88
89 private VirtualFlowRuleProvider provider = new TestProvider();
yoonseonbd8a93d2016-12-07 15:51:21 -080090 private VirtualFlowRuleProviderService providerService1;
91 private VirtualFlowRuleProviderService providerService2;
yoonseon6b972c32016-12-06 16:45:03 -080092
93 protected TestFlowRuleListener listener1 = new TestFlowRuleListener();
94 protected TestFlowRuleListener listener2 = new TestFlowRuleListener();
95
yoonseon6b972c32016-12-06 16:45:03 -080096 private VirtualNetwork vnet1;
97 private VirtualNetwork vnet2;
98
99 private ApplicationId appId;
100
yoonseon6b972c32016-12-06 16:45:03 -0800101 @Before
102 public void setUp() throws Exception {
103 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
104
105 CoreService coreService = new TestCoreService();
yoonseonc6a69272017-01-12 18:22:20 -0800106 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
yoonseon6b972c32016-12-06 16:45:03 -0800107 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
108 virtualNetworkManagerStore.activate();
109
yoonseonbd8a93d2016-12-07 15:51:21 -0800110 flowRuleStore = new SimpleVirtualFlowRuleStore();
yoonseon6b972c32016-12-06 16:45:03 -0800111
112 providerRegistryService = new VirtualProviderManager();
113 providerRegistryService.registerProvider(provider);
114
115 manager = new VirtualNetworkManager();
116 manager.store = virtualNetworkManagerStore;
yoonseon6b972c32016-12-06 16:45:03 -0800117 TestUtils.setField(manager, "coreService", coreService);
yoonseonbd8a93d2016-12-07 15:51:21 -0800118
119 eventDeliveryService = new TestEventDispatcher();
120 NetTestTools.injectEventDispatcher(manager, eventDeliveryService);
yoonseon6b972c32016-12-06 16:45:03 -0800121
122 appId = new TestApplicationId("FlowRuleManagerTest");
123
yoonseon6b972c32016-12-06 16:45:03 -0800124 testDirectory = new TestServiceDirectory()
125 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
126 .add(CoreService.class, coreService)
127 .add(VirtualProviderRegistryService.class, providerRegistryService)
yoonseonbd8a93d2016-12-07 15:51:21 -0800128 .add(EventDeliveryService.class, eventDeliveryService)
yoonseon6b972c32016-12-06 16:45:03 -0800129 .add(VirtualNetworkFlowRuleStore.class, flowRuleStore);
yoonseonc6a69272017-01-12 18:22:20 -0800130 TestUtils.setField(manager, "serviceDirectory", testDirectory);
yoonseon6b972c32016-12-06 16:45:03 -0800131
yoonseonc6a69272017-01-12 18:22:20 -0800132 manager.activate();
yoonseon6b972c32016-12-06 16:45:03 -0800133
yoonseon322c9c32016-12-07 16:47:02 -0800134 vnet1 = setupVirtualNetworkTopology(manager, TID1);
135 vnet2 = setupVirtualNetworkTopology(manager, TID2);
yoonseon6b972c32016-12-06 16:45:03 -0800136
yoonseonc6a69272017-01-12 18:22:20 -0800137 vnetFlowRuleService1 = new VirtualNetworkFlowRuleManager(manager, vnet1.id());
138 vnetFlowRuleService2 = new VirtualNetworkFlowRuleManager(manager, vnet2.id());
yoonseon6b972c32016-12-06 16:45:03 -0800139 vnetFlowRuleService1.addListener(listener1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800140 vnetFlowRuleService2.addListener(listener2);
yoonseon6b972c32016-12-06 16:45:03 -0800141
142 vnetFlowRuleService1.operationsService = MoreExecutors.newDirectExecutorService();
143 vnetFlowRuleService2.operationsService = MoreExecutors.newDirectExecutorService();
144 vnetFlowRuleService1.deviceInstallers = MoreExecutors.newDirectExecutorService();
145 vnetFlowRuleService2.deviceInstallers = MoreExecutors.newDirectExecutorService();
yoonseonbd8a93d2016-12-07 15:51:21 -0800146
147 providerService1 = (VirtualFlowRuleProviderService)
Yoonseon Hanc70b4e02016-10-20 15:24:33 -0700148 providerRegistryService.getProviderService(vnet1.id(), VirtualFlowRuleProvider.class);
yoonseonbd8a93d2016-12-07 15:51:21 -0800149 providerService2 = (VirtualFlowRuleProviderService)
Yoonseon Hanc70b4e02016-10-20 15:24:33 -0700150 providerRegistryService.getProviderService(vnet2.id(), VirtualFlowRuleProvider.class);
yoonseon6b972c32016-12-06 16:45:03 -0800151 }
152
153 @After
154 public void tearDown() {
155 manager.deactivate();
156 virtualNetworkManagerStore.deactivate();
157 }
158
yoonseon6b972c32016-12-06 16:45:03 -0800159 private FlowRule flowRule(int tsval, int trval) {
yoonseon322c9c32016-12-07 16:47:02 -0800160 return flowRule(VDID1, tsval, trval);
yoonseon6b972c32016-12-06 16:45:03 -0800161 }
162
163 private FlowRule flowRule(DeviceId did, int tsval, int trval) {
164 TestSelector ts = new TestSelector(tsval);
165 TestTreatment tr = new TestTreatment(trval);
166 return DefaultFlowRule.builder()
167 .forDevice(did)
168 .withSelector(ts)
169 .withTreatment(tr)
170 .withPriority(10)
171 .fromApp(appId)
172 .makeTemporary(TIMEOUT)
173 .build();
174 }
175
176 private FlowRule addFlowRule(int hval) {
177 FlowRule rule = flowRule(hval, hval);
178 vnetFlowRuleService1.applyFlowRules(rule);
179
yoonseon322c9c32016-12-07 16:47:02 -0800180 assertNotNull("rule should be found", vnetFlowRuleService1.getFlowEntries(VDID1));
yoonseon6b972c32016-12-06 16:45:03 -0800181 return rule;
182 }
183
184 private int flowCount(FlowRuleService service) {
185 List<FlowEntry> entries = Lists.newArrayList();
yoonseon322c9c32016-12-07 16:47:02 -0800186 service.getFlowEntries(VDID1).forEach(entries::add);
yoonseon6b972c32016-12-06 16:45:03 -0800187 return entries.size();
188 }
189
190 @Test
191 public void getFlowEntries() {
192 assertTrue("store should be empty",
yoonseon322c9c32016-12-07 16:47:02 -0800193 Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(VDID1)).isEmpty());
yoonseon6b972c32016-12-06 16:45:03 -0800194 assertTrue("store should be empty",
yoonseon322c9c32016-12-07 16:47:02 -0800195 Sets.newHashSet(vnetFlowRuleService2.getFlowEntries(VDID1)).isEmpty());
yoonseonbd8a93d2016-12-07 15:51:21 -0800196
yoonseon6b972c32016-12-06 16:45:03 -0800197 FlowRule f1 = addFlowRule(1);
198 FlowRule f2 = addFlowRule(2);
199
200 FlowEntry fe1 = new DefaultFlowEntry(f1);
201 FlowEntry fe2 = new DefaultFlowEntry(f2);
202
203 assertEquals("2 rules should exist", 2, flowCount(vnetFlowRuleService1));
204 assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800205
yoonseon322c9c32016-12-07 16:47:02 -0800206 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800207 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
208 RULE_ADDED, RULE_ADDED);
209
210 addFlowRule(1);
211 assertEquals("should still be 2 rules", 2, flowCount(vnetFlowRuleService1));
212 System.err.println("events :" + listener1.events);
213 assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2));
214
yoonseon322c9c32016-12-07 16:47:02 -0800215 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1));
yoonseonbd8a93d2016-12-07 15:51:21 -0800216 validateEvents(listener1, RULE_UPDATED, RULE_UPDATED);
217 }
218
219 @Test
220 public void applyFlowRules() {
221 FlowRule r1 = flowRule(1, 1);
222 FlowRule r2 = flowRule(2, 2);
223 FlowRule r3 = flowRule(3, 3);
224
225 assertTrue("store should be empty",
226 Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(DID1)).isEmpty());
227 vnetFlowRuleService1.applyFlowRules(r1, r2, r3);
228 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
229 assertTrue("Entries should be pending add.",
230 validateState(ImmutableMap.of(
231 r1, FlowEntry.FlowEntryState.PENDING_ADD,
232 r2, FlowEntry.FlowEntryState.PENDING_ADD,
233 r3, FlowEntry.FlowEntryState.PENDING_ADD)));
234 }
235
236 @Test
237 public void purgeFlowRules() {
238 FlowRule f1 = addFlowRule(1);
239 FlowRule f2 = addFlowRule(2);
240 FlowRule f3 = addFlowRule(3);
241 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
242 FlowEntry fe1 = new DefaultFlowEntry(f1);
243 FlowEntry fe2 = new DefaultFlowEntry(f2);
244 FlowEntry fe3 = new DefaultFlowEntry(f3);
yoonseon322c9c32016-12-07 16:47:02 -0800245 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800246 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
247 RULE_ADDED, RULE_ADDED, RULE_ADDED);
yoonseon322c9c32016-12-07 16:47:02 -0800248 vnetFlowRuleService1.purgeFlowRules(VDID1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800249 assertEquals("0 rule should exist", 0, flowCount(vnetFlowRuleService1));
250 }
251
252 @Test
253 public void removeFlowRules() {
254 FlowRule f1 = addFlowRule(1);
255 FlowRule f2 = addFlowRule(2);
256 FlowRule f3 = addFlowRule(3);
257 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
258
259 FlowEntry fe1 = new DefaultFlowEntry(f1);
260 FlowEntry fe2 = new DefaultFlowEntry(f2);
261 FlowEntry fe3 = new DefaultFlowEntry(f3);
yoonseon322c9c32016-12-07 16:47:02 -0800262 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800263 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
264 RULE_ADDED, RULE_ADDED, RULE_ADDED);
265
266 vnetFlowRuleService1.removeFlowRules(f1, f2);
267 //removing from north, so no events generated
268 validateEvents(listener1, RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
269 assertEquals("3 rule should exist", 3, flowCount(vnetFlowRuleService1));
270 assertTrue("Entries should be pending remove.",
271 validateState(ImmutableMap.of(
272 f1, FlowEntry.FlowEntryState.PENDING_REMOVE,
273 f2, FlowEntry.FlowEntryState.PENDING_REMOVE,
274 f3, FlowEntry.FlowEntryState.ADDED)));
275
276 vnetFlowRuleService1.removeFlowRules(f1);
277 assertEquals("3 rule should still exist", 3, flowCount(vnetFlowRuleService1));
278 }
279
280 @Test
281 public void flowRemoved() {
282 FlowRule f1 = addFlowRule(1);
283 FlowRule f2 = addFlowRule(2);
284 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
285 FlowEntry fe2 = new DefaultFlowEntry(f2);
286
yoonseon322c9c32016-12-07 16:47:02 -0800287 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800288 vnetFlowRuleService1.removeFlowRules(f1);
289
290 //FIXME modification of "stored" flow entry outside of store
291 fe1.setState(FlowEntry.FlowEntryState.REMOVED);
292
293 providerService1.flowRemoved(fe1);
294
295 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
296 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
297
298 providerService1.flowRemoved(fe1);
299 validateEvents(listener1);
300
301 FlowRule f3 = flowRule(3, 3);
302 FlowEntry fe3 = new DefaultFlowEntry(f3);
303 vnetFlowRuleService1.applyFlowRules(f3);
304
yoonseon322c9c32016-12-07 16:47:02 -0800305 providerService1.pushFlowMetrics(VDID1, Collections.singletonList(fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800306 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
307
308 providerService1.flowRemoved(fe3);
309 validateEvents(listener1);
310 }
311
312 @Test
313 public void extraneousFlow() {
314 FlowRule f1 = flowRule(1, 1);
315 FlowRule f2 = flowRule(2, 2);
316 FlowRule f3 = flowRule(3, 3);
317 vnetFlowRuleService1.applyFlowRules(f1, f2);
318
319 FlowEntry fe1 = new DefaultFlowEntry(f1);
320 FlowEntry fe2 = new DefaultFlowEntry(f2);
321 FlowEntry fe3 = new DefaultFlowEntry(f3);
322
323
yoonseon322c9c32016-12-07 16:47:02 -0800324 providerService1.pushFlowMetrics(VDID1, Lists.newArrayList(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800325
326 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
327 RULE_ADDED, RULE_ADDED);
328 }
329
330 /*
331 * Tests whether a rule that was marked for removal but no flowRemoved was received
332 * is indeed removed at the next stats update.
333 */
334 @Test
335 public void flowMissingRemove() {
336 FlowRule f1 = flowRule(1, 1);
337 FlowRule f2 = flowRule(2, 2);
338 FlowRule f3 = flowRule(3, 3);
339
340 FlowEntry fe1 = new DefaultFlowEntry(f1);
341 FlowEntry fe2 = new DefaultFlowEntry(f2);
342 vnetFlowRuleService1.applyFlowRules(f1, f2, f3);
343
344 vnetFlowRuleService1.removeFlowRules(f3);
345
yoonseon322c9c32016-12-07 16:47:02 -0800346 providerService1.pushFlowMetrics(VDID1, Lists.newArrayList(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800347
348 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
349 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
350 }
351
352 @Test
353 public void removeByAppId() {
354 FlowRule f1 = flowRule(1, 1);
355 FlowRule f2 = flowRule(2, 2);
356 vnetFlowRuleService1.applyFlowRules(f1, f2);
357
358 vnetFlowRuleService1.removeFlowRulesById(appId);
359
360 //only check that we are in pending remove. Events and actual remove state will
361 // be set by flowRemoved call.
362 validateState(ImmutableMap.of(
363 f1, FlowEntry.FlowEntryState.PENDING_REMOVE,
364 f2, FlowEntry.FlowEntryState.PENDING_REMOVE));
365 }
366
367 //TODO:Tests for fallback
368
369 private boolean validateState(Map<FlowRule, FlowEntry.FlowEntryState> expected) {
370 Map<FlowRule, FlowEntry.FlowEntryState> expectedToCheck = new HashMap<>(expected);
yoonseon322c9c32016-12-07 16:47:02 -0800371 Iterable<FlowEntry> rules = vnetFlowRuleService1.getFlowEntries(VDID1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800372 for (FlowEntry f : rules) {
373 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
374 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
375 expectedToCheck.remove(f);
376 }
377 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
378 return true;
yoonseon6b972c32016-12-06 16:45:03 -0800379 }
380
381 private class TestSelector implements TrafficSelector {
382
383 //for controlling hashcode uniqueness;
384 private final int testval;
385
386 public TestSelector(int val) {
387 testval = val;
388 }
389
390 @Override
391 public Set<Criterion> criteria() {
392 return Collections.emptySet();
393 }
394
395 @Override
396 public Criterion getCriterion(
397 org.onosproject.net.flow.criteria.Criterion.Type type) {
398 return null;
399 }
400
401 @Override
402 public int hashCode() {
403 return testval;
404 }
405
406 @Override
407 public boolean equals(Object o) {
408 if (o instanceof TestSelector) {
409 return this.testval == ((TestSelector) o).testval;
410 }
411 return false;
412 }
yoonseon6b972c32016-12-06 16:45:03 -0800413 }
414
415 private class TestTreatment implements TrafficTreatment {
416
417 //for controlling hashcode uniqueness;
418 private final int testval;
419
420 public TestTreatment(int val) {
421 testval = val;
422 }
423
424 @Override
425 public List<Instruction> deferred() {
426 return null;
427 }
428
429 @Override
430 public List<Instruction> immediate() {
431 return null;
432 }
433
434 @Override
435 public List<Instruction> allInstructions() {
436 return null;
437 }
438
439 @Override
440 public Instructions.TableTypeTransition tableTransition() {
441 return null;
442 }
443
444 @Override
445 public boolean clearedDeferred() {
446 return false;
447 }
448
449 @Override
450 public int hashCode() {
451 return testval;
452 }
453
454 @Override
455 public boolean equals(Object o) {
456 if (o instanceof TestTreatment) {
457 return this.testval == ((TestTreatment) o).testval;
458 }
459 return false;
460 }
461
462 @Override
463 public Instructions.MetadataInstruction writeMetadata() {
464 return null;
465 }
466
467 @Override
Cem Türker3baff672017-10-12 15:09:01 +0300468 public Instructions.StatTriggerInstruction statTrigger() {
469 return null;
470 }
471
472 @Override
yoonseon6b972c32016-12-06 16:45:03 -0800473 public Instructions.MeterInstruction metered() {
474 return null;
475 }
cansu.toprak409289d2017-10-27 10:04:05 +0300476
477 @Override
478 public Set<Instructions.MeterInstruction> meters() {
479 return Sets.newHashSet();
480 }
yoonseon6b972c32016-12-06 16:45:03 -0800481 }
482
yoonseon6b972c32016-12-06 16:45:03 -0800483 private void validateEvents(TestFlowRuleListener listener, FlowRuleEvent.Type... events) {
484 if (events == null) {
485 assertTrue("events generated", listener.events.isEmpty());
486 }
487
488 int i = 0;
489 System.err.println("events :" + listener.events);
490 for (FlowRuleEvent e : listener.events) {
491 assertEquals("unexpected event", events[i], e.type());
492 i++;
493 }
494
495 assertEquals("mispredicted number of events",
496 events.length, listener.events.size());
497
498 listener.events.clear();
499 }
500
501 private class TestFlowRuleListener implements FlowRuleListener {
502
503 public final List<FlowRuleEvent> events = new ArrayList<>();
504
505 @Override
506 public void event(FlowRuleEvent event) {
yoonseonbd8a93d2016-12-07 15:51:21 -0800507 events.add(event);
yoonseon6b972c32016-12-06 16:45:03 -0800508 }
509 }
510
511 private class TestProvider extends AbstractVirtualProvider
512 implements VirtualFlowRuleProvider {
513
514 protected TestProvider() {
515 super(new ProviderId("test", "org.onosproject.virtual.testprovider"));
516 }
517
518 @Override
519 public void applyFlowRule(NetworkId networkId, FlowRule... flowRules) {
520
521 }
522
523 @Override
524 public void removeFlowRule(NetworkId networkId, FlowRule... flowRules) {
525
526 }
527
528 @Override
529 public void executeBatch(NetworkId networkId, FlowRuleBatchOperation batch) {
530
531 }
532 }
533}