blob: b1d757219876c35ca6d19214fffd191dce3f3d5f [file] [log] [blame]
yoonseon6b972c32016-12-06 16:45:03 -08001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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;
44import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
yoonseonbd8a93d2016-12-07 15:51:21 -080045import org.onosproject.incubator.store.virtual.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;
yoonseon6b972c32016-12-06 16:45:03 -080052import org.onosproject.net.flow.FlowRuleBatchOperation;
53import 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;
62import org.onosproject.net.intent.FakeIntentManager;
63import org.onosproject.net.intent.TestableIntentService;
64import org.onosproject.net.provider.ProviderId;
65import org.onosproject.store.service.TestStorageService;
66
67import java.util.ArrayList;
68import java.util.Collections;
yoonseonbd8a93d2016-12-07 15:51:21 -080069import java.util.HashMap;
yoonseon6b972c32016-12-06 16:45:03 -080070import java.util.List;
yoonseonbd8a93d2016-12-07 15:51:21 -080071import java.util.Map;
yoonseon6b972c32016-12-06 16:45:03 -080072import java.util.Set;
yoonseon6b972c32016-12-06 16:45:03 -080073
74import static org.junit.Assert.*;
yoonseonbd8a93d2016-12-07 15:51:21 -080075import static org.onosproject.net.flow.FlowRuleEvent.Type.*;
yoonseon6b972c32016-12-06 16:45:03 -080076
yoonseon322c9c32016-12-07 16:47:02 -080077public class VirtualNetworkFlowRuleManagerTest extends VirtualNetworkTestUtil {
yoonseon6b972c32016-12-06 16:45:03 -080078 private static final int TIMEOUT = 10;
79
80 private VirtualNetworkManager manager;
81 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
82 private TestableIntentService intentService = new FakeIntentManager();
83 private ServiceDirectory testDirectory;
84 private VirtualNetworkFlowRuleStore flowRuleStore;
yoonseonbd8a93d2016-12-07 15:51:21 -080085 private VirtualProviderManager providerRegistryService;
86
87 private EventDeliveryService eventDeliveryService;
yoonseon6b972c32016-12-06 16:45:03 -080088
89 private VirtualNetworkFlowRuleManager vnetFlowRuleService1;
90 private VirtualNetworkFlowRuleManager vnetFlowRuleService2;
91
92 private VirtualFlowRuleProvider provider = new TestProvider();
yoonseonbd8a93d2016-12-07 15:51:21 -080093 private VirtualFlowRuleProviderService providerService1;
94 private VirtualFlowRuleProviderService providerService2;
yoonseon6b972c32016-12-06 16:45:03 -080095
96 protected TestFlowRuleListener listener1 = new TestFlowRuleListener();
97 protected TestFlowRuleListener listener2 = new TestFlowRuleListener();
98
yoonseon6b972c32016-12-06 16:45:03 -080099 private VirtualNetwork vnet1;
100 private VirtualNetwork vnet2;
101
102 private ApplicationId appId;
103
yoonseon6b972c32016-12-06 16:45:03 -0800104 @Before
105 public void setUp() throws Exception {
106 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
107
108 CoreService coreService = new TestCoreService();
yoonseonc6a69272017-01-12 18:22:20 -0800109 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
yoonseon6b972c32016-12-06 16:45:03 -0800110 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
111 virtualNetworkManagerStore.activate();
112
yoonseonbd8a93d2016-12-07 15:51:21 -0800113 flowRuleStore = new SimpleVirtualFlowRuleStore();
yoonseon6b972c32016-12-06 16:45:03 -0800114
115 providerRegistryService = new VirtualProviderManager();
116 providerRegistryService.registerProvider(provider);
117
118 manager = new VirtualNetworkManager();
119 manager.store = virtualNetworkManagerStore;
120 manager.intentService = intentService;
121 TestUtils.setField(manager, "coreService", coreService);
yoonseonbd8a93d2016-12-07 15:51:21 -0800122
123 eventDeliveryService = new TestEventDispatcher();
124 NetTestTools.injectEventDispatcher(manager, eventDeliveryService);
yoonseon6b972c32016-12-06 16:45:03 -0800125
126 appId = new TestApplicationId("FlowRuleManagerTest");
127
yoonseon6b972c32016-12-06 16:45:03 -0800128 testDirectory = new TestServiceDirectory()
129 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
130 .add(CoreService.class, coreService)
131 .add(VirtualProviderRegistryService.class, providerRegistryService)
yoonseonbd8a93d2016-12-07 15:51:21 -0800132 .add(EventDeliveryService.class, eventDeliveryService)
yoonseon6b972c32016-12-06 16:45:03 -0800133 .add(VirtualNetworkFlowRuleStore.class, flowRuleStore);
yoonseonc6a69272017-01-12 18:22:20 -0800134 TestUtils.setField(manager, "serviceDirectory", testDirectory);
yoonseon6b972c32016-12-06 16:45:03 -0800135
yoonseonc6a69272017-01-12 18:22:20 -0800136 manager.activate();
yoonseon6b972c32016-12-06 16:45:03 -0800137
yoonseon322c9c32016-12-07 16:47:02 -0800138 vnet1 = setupVirtualNetworkTopology(manager, TID1);
139 vnet2 = setupVirtualNetworkTopology(manager, TID2);
yoonseon6b972c32016-12-06 16:45:03 -0800140
yoonseonc6a69272017-01-12 18:22:20 -0800141 vnetFlowRuleService1 = new VirtualNetworkFlowRuleManager(manager, vnet1.id());
142 vnetFlowRuleService2 = new VirtualNetworkFlowRuleManager(manager, vnet2.id());
yoonseon6b972c32016-12-06 16:45:03 -0800143 vnetFlowRuleService1.addListener(listener1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800144 vnetFlowRuleService2.addListener(listener2);
yoonseon6b972c32016-12-06 16:45:03 -0800145
146 vnetFlowRuleService1.operationsService = MoreExecutors.newDirectExecutorService();
147 vnetFlowRuleService2.operationsService = MoreExecutors.newDirectExecutorService();
148 vnetFlowRuleService1.deviceInstallers = MoreExecutors.newDirectExecutorService();
149 vnetFlowRuleService2.deviceInstallers = MoreExecutors.newDirectExecutorService();
yoonseonbd8a93d2016-12-07 15:51:21 -0800150
151 providerService1 = (VirtualFlowRuleProviderService)
Yoonseon Hanc70b4e02016-10-20 15:24:33 -0700152 providerRegistryService.getProviderService(vnet1.id(), VirtualFlowRuleProvider.class);
yoonseonbd8a93d2016-12-07 15:51:21 -0800153 providerService2 = (VirtualFlowRuleProviderService)
Yoonseon Hanc70b4e02016-10-20 15:24:33 -0700154 providerRegistryService.getProviderService(vnet2.id(), VirtualFlowRuleProvider.class);
yoonseon6b972c32016-12-06 16:45:03 -0800155 }
156
157 @After
158 public void tearDown() {
159 manager.deactivate();
160 virtualNetworkManagerStore.deactivate();
161 }
162
yoonseon6b972c32016-12-06 16:45:03 -0800163 private FlowRule flowRule(int tsval, int trval) {
yoonseon322c9c32016-12-07 16:47:02 -0800164 return flowRule(VDID1, tsval, trval);
yoonseon6b972c32016-12-06 16:45:03 -0800165 }
166
167 private FlowRule flowRule(DeviceId did, int tsval, int trval) {
168 TestSelector ts = new TestSelector(tsval);
169 TestTreatment tr = new TestTreatment(trval);
170 return DefaultFlowRule.builder()
171 .forDevice(did)
172 .withSelector(ts)
173 .withTreatment(tr)
174 .withPriority(10)
175 .fromApp(appId)
176 .makeTemporary(TIMEOUT)
177 .build();
178 }
179
180 private FlowRule addFlowRule(int hval) {
181 FlowRule rule = flowRule(hval, hval);
182 vnetFlowRuleService1.applyFlowRules(rule);
183
yoonseon322c9c32016-12-07 16:47:02 -0800184 assertNotNull("rule should be found", vnetFlowRuleService1.getFlowEntries(VDID1));
yoonseon6b972c32016-12-06 16:45:03 -0800185 return rule;
186 }
187
188 private int flowCount(FlowRuleService service) {
189 List<FlowEntry> entries = Lists.newArrayList();
yoonseon322c9c32016-12-07 16:47:02 -0800190 service.getFlowEntries(VDID1).forEach(entries::add);
yoonseon6b972c32016-12-06 16:45:03 -0800191 return entries.size();
192 }
193
194 @Test
195 public void getFlowEntries() {
196 assertTrue("store should be empty",
yoonseon322c9c32016-12-07 16:47:02 -0800197 Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(VDID1)).isEmpty());
yoonseon6b972c32016-12-06 16:45:03 -0800198 assertTrue("store should be empty",
yoonseon322c9c32016-12-07 16:47:02 -0800199 Sets.newHashSet(vnetFlowRuleService2.getFlowEntries(VDID1)).isEmpty());
yoonseonbd8a93d2016-12-07 15:51:21 -0800200
yoonseon6b972c32016-12-06 16:45:03 -0800201 FlowRule f1 = addFlowRule(1);
202 FlowRule f2 = addFlowRule(2);
203
204 FlowEntry fe1 = new DefaultFlowEntry(f1);
205 FlowEntry fe2 = new DefaultFlowEntry(f2);
206
207 assertEquals("2 rules should exist", 2, flowCount(vnetFlowRuleService1));
208 assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800209
yoonseon322c9c32016-12-07 16:47:02 -0800210 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800211 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
212 RULE_ADDED, RULE_ADDED);
213
214 addFlowRule(1);
215 assertEquals("should still be 2 rules", 2, flowCount(vnetFlowRuleService1));
216 System.err.println("events :" + listener1.events);
217 assertEquals("0 rules should exist", 0, flowCount(vnetFlowRuleService2));
218
yoonseon322c9c32016-12-07 16:47:02 -0800219 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1));
yoonseonbd8a93d2016-12-07 15:51:21 -0800220 validateEvents(listener1, RULE_UPDATED, RULE_UPDATED);
221 }
222
223 @Test
224 public void applyFlowRules() {
225 FlowRule r1 = flowRule(1, 1);
226 FlowRule r2 = flowRule(2, 2);
227 FlowRule r3 = flowRule(3, 3);
228
229 assertTrue("store should be empty",
230 Sets.newHashSet(vnetFlowRuleService1.getFlowEntries(DID1)).isEmpty());
231 vnetFlowRuleService1.applyFlowRules(r1, r2, r3);
232 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
233 assertTrue("Entries should be pending add.",
234 validateState(ImmutableMap.of(
235 r1, FlowEntry.FlowEntryState.PENDING_ADD,
236 r2, FlowEntry.FlowEntryState.PENDING_ADD,
237 r3, FlowEntry.FlowEntryState.PENDING_ADD)));
238 }
239
240 @Test
241 public void purgeFlowRules() {
242 FlowRule f1 = addFlowRule(1);
243 FlowRule f2 = addFlowRule(2);
244 FlowRule f3 = addFlowRule(3);
245 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
246 FlowEntry fe1 = new DefaultFlowEntry(f1);
247 FlowEntry fe2 = new DefaultFlowEntry(f2);
248 FlowEntry fe3 = new DefaultFlowEntry(f3);
yoonseon322c9c32016-12-07 16:47:02 -0800249 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800250 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
251 RULE_ADDED, RULE_ADDED, RULE_ADDED);
yoonseon322c9c32016-12-07 16:47:02 -0800252 vnetFlowRuleService1.purgeFlowRules(VDID1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800253 assertEquals("0 rule should exist", 0, flowCount(vnetFlowRuleService1));
254 }
255
256 @Test
257 public void removeFlowRules() {
258 FlowRule f1 = addFlowRule(1);
259 FlowRule f2 = addFlowRule(2);
260 FlowRule f3 = addFlowRule(3);
261 assertEquals("3 rules should exist", 3, flowCount(vnetFlowRuleService1));
262
263 FlowEntry fe1 = new DefaultFlowEntry(f1);
264 FlowEntry fe2 = new DefaultFlowEntry(f2);
265 FlowEntry fe3 = new DefaultFlowEntry(f3);
yoonseon322c9c32016-12-07 16:47:02 -0800266 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800267 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
268 RULE_ADDED, RULE_ADDED, RULE_ADDED);
269
270 vnetFlowRuleService1.removeFlowRules(f1, f2);
271 //removing from north, so no events generated
272 validateEvents(listener1, RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
273 assertEquals("3 rule should exist", 3, flowCount(vnetFlowRuleService1));
274 assertTrue("Entries should be pending remove.",
275 validateState(ImmutableMap.of(
276 f1, FlowEntry.FlowEntryState.PENDING_REMOVE,
277 f2, FlowEntry.FlowEntryState.PENDING_REMOVE,
278 f3, FlowEntry.FlowEntryState.ADDED)));
279
280 vnetFlowRuleService1.removeFlowRules(f1);
281 assertEquals("3 rule should still exist", 3, flowCount(vnetFlowRuleService1));
282 }
283
284 @Test
285 public void flowRemoved() {
286 FlowRule f1 = addFlowRule(1);
287 FlowRule f2 = addFlowRule(2);
288 StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
289 FlowEntry fe2 = new DefaultFlowEntry(f2);
290
yoonseon322c9c32016-12-07 16:47:02 -0800291 providerService1.pushFlowMetrics(VDID1, ImmutableList.of(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800292 vnetFlowRuleService1.removeFlowRules(f1);
293
294 //FIXME modification of "stored" flow entry outside of store
295 fe1.setState(FlowEntry.FlowEntryState.REMOVED);
296
297 providerService1.flowRemoved(fe1);
298
299 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
300 RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
301
302 providerService1.flowRemoved(fe1);
303 validateEvents(listener1);
304
305 FlowRule f3 = flowRule(3, 3);
306 FlowEntry fe3 = new DefaultFlowEntry(f3);
307 vnetFlowRuleService1.applyFlowRules(f3);
308
yoonseon322c9c32016-12-07 16:47:02 -0800309 providerService1.pushFlowMetrics(VDID1, Collections.singletonList(fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800310 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
311
312 providerService1.flowRemoved(fe3);
313 validateEvents(listener1);
314 }
315
316 @Test
317 public void extraneousFlow() {
318 FlowRule f1 = flowRule(1, 1);
319 FlowRule f2 = flowRule(2, 2);
320 FlowRule f3 = flowRule(3, 3);
321 vnetFlowRuleService1.applyFlowRules(f1, f2);
322
323 FlowEntry fe1 = new DefaultFlowEntry(f1);
324 FlowEntry fe2 = new DefaultFlowEntry(f2);
325 FlowEntry fe3 = new DefaultFlowEntry(f3);
326
327
yoonseon322c9c32016-12-07 16:47:02 -0800328 providerService1.pushFlowMetrics(VDID1, Lists.newArrayList(fe1, fe2, fe3));
yoonseonbd8a93d2016-12-07 15:51:21 -0800329
330 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
331 RULE_ADDED, RULE_ADDED);
332 }
333
334 /*
335 * Tests whether a rule that was marked for removal but no flowRemoved was received
336 * is indeed removed at the next stats update.
337 */
338 @Test
339 public void flowMissingRemove() {
340 FlowRule f1 = flowRule(1, 1);
341 FlowRule f2 = flowRule(2, 2);
342 FlowRule f3 = flowRule(3, 3);
343
344 FlowEntry fe1 = new DefaultFlowEntry(f1);
345 FlowEntry fe2 = new DefaultFlowEntry(f2);
346 vnetFlowRuleService1.applyFlowRules(f1, f2, f3);
347
348 vnetFlowRuleService1.removeFlowRules(f3);
349
yoonseon322c9c32016-12-07 16:47:02 -0800350 providerService1.pushFlowMetrics(VDID1, Lists.newArrayList(fe1, fe2));
yoonseonbd8a93d2016-12-07 15:51:21 -0800351
352 validateEvents(listener1, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
353 RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED);
354 }
355
356 @Test
357 public void removeByAppId() {
358 FlowRule f1 = flowRule(1, 1);
359 FlowRule f2 = flowRule(2, 2);
360 vnetFlowRuleService1.applyFlowRules(f1, f2);
361
362 vnetFlowRuleService1.removeFlowRulesById(appId);
363
364 //only check that we are in pending remove. Events and actual remove state will
365 // be set by flowRemoved call.
366 validateState(ImmutableMap.of(
367 f1, FlowEntry.FlowEntryState.PENDING_REMOVE,
368 f2, FlowEntry.FlowEntryState.PENDING_REMOVE));
369 }
370
371 //TODO:Tests for fallback
372
373 private boolean validateState(Map<FlowRule, FlowEntry.FlowEntryState> expected) {
374 Map<FlowRule, FlowEntry.FlowEntryState> expectedToCheck = new HashMap<>(expected);
yoonseon322c9c32016-12-07 16:47:02 -0800375 Iterable<FlowEntry> rules = vnetFlowRuleService1.getFlowEntries(VDID1);
yoonseonbd8a93d2016-12-07 15:51:21 -0800376 for (FlowEntry f : rules) {
377 assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f));
378 assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state());
379 expectedToCheck.remove(f);
380 }
381 assertEquals(Collections.emptySet(), expectedToCheck.entrySet());
382 return true;
yoonseon6b972c32016-12-06 16:45:03 -0800383 }
384
385 private class TestSelector implements TrafficSelector {
386
387 //for controlling hashcode uniqueness;
388 private final int testval;
389
390 public TestSelector(int val) {
391 testval = val;
392 }
393
394 @Override
395 public Set<Criterion> criteria() {
396 return Collections.emptySet();
397 }
398
399 @Override
400 public Criterion getCriterion(
401 org.onosproject.net.flow.criteria.Criterion.Type type) {
402 return null;
403 }
404
405 @Override
406 public int hashCode() {
407 return testval;
408 }
409
410 @Override
411 public boolean equals(Object o) {
412 if (o instanceof TestSelector) {
413 return this.testval == ((TestSelector) o).testval;
414 }
415 return false;
416 }
yoonseon6b972c32016-12-06 16:45:03 -0800417 }
418
419 private class TestTreatment implements TrafficTreatment {
420
421 //for controlling hashcode uniqueness;
422 private final int testval;
423
424 public TestTreatment(int val) {
425 testval = val;
426 }
427
428 @Override
429 public List<Instruction> deferred() {
430 return null;
431 }
432
433 @Override
434 public List<Instruction> immediate() {
435 return null;
436 }
437
438 @Override
439 public List<Instruction> allInstructions() {
440 return null;
441 }
442
443 @Override
444 public Instructions.TableTypeTransition tableTransition() {
445 return null;
446 }
447
448 @Override
449 public boolean clearedDeferred() {
450 return false;
451 }
452
453 @Override
454 public int hashCode() {
455 return testval;
456 }
457
458 @Override
459 public boolean equals(Object o) {
460 if (o instanceof TestTreatment) {
461 return this.testval == ((TestTreatment) o).testval;
462 }
463 return false;
464 }
465
466 @Override
467 public Instructions.MetadataInstruction writeMetadata() {
468 return null;
469 }
470
471 @Override
472 public Instructions.MeterInstruction metered() {
473 return null;
474 }
475 }
476
yoonseon6b972c32016-12-06 16:45:03 -0800477 private void validateEvents(TestFlowRuleListener listener, FlowRuleEvent.Type... events) {
478 if (events == null) {
479 assertTrue("events generated", listener.events.isEmpty());
480 }
481
482 int i = 0;
483 System.err.println("events :" + listener.events);
484 for (FlowRuleEvent e : listener.events) {
485 assertEquals("unexpected event", events[i], e.type());
486 i++;
487 }
488
489 assertEquals("mispredicted number of events",
490 events.length, listener.events.size());
491
492 listener.events.clear();
493 }
494
495 private class TestFlowRuleListener implements FlowRuleListener {
496
497 public final List<FlowRuleEvent> events = new ArrayList<>();
498
499 @Override
500 public void event(FlowRuleEvent event) {
yoonseonbd8a93d2016-12-07 15:51:21 -0800501 events.add(event);
yoonseon6b972c32016-12-06 16:45:03 -0800502 }
503 }
504
505 private class TestProvider extends AbstractVirtualProvider
506 implements VirtualFlowRuleProvider {
507
508 protected TestProvider() {
509 super(new ProviderId("test", "org.onosproject.virtual.testprovider"));
510 }
511
512 @Override
513 public void applyFlowRule(NetworkId networkId, FlowRule... flowRules) {
514
515 }
516
517 @Override
518 public void removeFlowRule(NetworkId networkId, FlowRule... flowRules) {
519
520 }
521
522 @Override
523 public void executeBatch(NetworkId networkId, FlowRuleBatchOperation batch) {
524
525 }
526 }
527}