blob: 34acd421f1ae12ae06a3afec2d138d5cf9705844 [file] [log] [blame]
Ray Milkeyf410a0c2015-05-19 15:46:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyf410a0c2015-05-19 15:46:03 -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 */
16package org.onosproject.net.flowobjective;
17
Ray Milkey3004c7d2017-12-13 09:43:20 -080018import com.google.common.testing.EqualsTester;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070019import org.junit.Test;
Ray Milkey3004c7d2017-12-13 09:43:20 -080020import org.onosproject.TestApplicationId;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070021import org.onosproject.net.flow.DefaultTrafficSelector;
22import org.onosproject.net.flow.DefaultTrafficTreatment;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.criteria.Criteria;
26import org.onosproject.net.flow.criteria.Criterion;
27
Ray Milkey3004c7d2017-12-13 09:43:20 -080028import static org.hamcrest.CoreMatchers.equalTo;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070029import static org.hamcrest.CoreMatchers.hasItem;
30import static org.hamcrest.CoreMatchers.is;
Ray Milkey810d6e72015-08-14 10:35:06 -070031import static org.hamcrest.CoreMatchers.nullValue;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070032import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey810d6e72015-08-14 10:35:06 -070033import static org.hamcrest.Matchers.not;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070034import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
35import static org.onosproject.net.NetTestTools.APP_ID;
36import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY;
37import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Ray Milkey3004c7d2017-12-13 09:43:20 -080038import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.VERSATILE;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070039import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Ray Milkey810d6e72015-08-14 10:35:06 -070040import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
Ray Milkey3004c7d2017-12-13 09:43:20 -080041import static org.onosproject.net.flowobjective.Objective.Operation.ADD_TO_EXISTING;
Ray Milkey810d6e72015-08-14 10:35:06 -070042import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;
Ray Milkey3004c7d2017-12-13 09:43:20 -080043import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE_FROM_EXISTING;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070044
45/**
46 * Unit tests for forwarding objective class.
47 */
48public class ObjectiveTest {
49
50 private final TrafficTreatment treatment =
51 DefaultTrafficTreatment.emptyTreatment();
52 private final TrafficSelector selector =
53 DefaultTrafficSelector.emptySelector();
54 private final Criterion criterion = Criteria.dummy();
55 private final Criterion key = Criteria.dummy();
56
57 /**
Ray Milkeyf410a0c2015-05-19 15:46:03 -070058 * Checks immutability of objective classes.
59 */
60 @Test
61 public void testImmutability() {
62 assertThatClassIsImmutable(DefaultFilteringObjective.class);
63 assertThatClassIsImmutable(DefaultForwardingObjective.class);
64 assertThatClassIsImmutable(DefaultNextObjective.class);
65 }
66
67 // Forwarding Objectives
68
69 /**
70 * Makes a forwarding objective builder with a set of default values.
71 *
72 * @return forwarding objective builder
73 */
74 private ForwardingObjective.Builder baseForwardingBuilder() {
75 return DefaultForwardingObjective.builder()
76 .withSelector(selector)
77 .withTreatment(treatment)
78 .withFlag(SPECIFIC)
Ray Milkey3004c7d2017-12-13 09:43:20 -080079 .withMeta(selector)
Ray Milkeyf410a0c2015-05-19 15:46:03 -070080 .fromApp(APP_ID)
Ray Milkey810d6e72015-08-14 10:35:06 -070081 .withPriority(22)
82 .makeTemporary(5)
Ray Milkeyf410a0c2015-05-19 15:46:03 -070083 .nextStep(33);
84 }
85
86 /**
87 * Checks the default values of a forwarding objective object.
88 *
89 * @param objective forwarding objective to check
90 */
Ray Milkey810d6e72015-08-14 10:35:06 -070091 private void checkForwardingBase(ForwardingObjective objective,
92 Objective.Operation op,
93 ObjectiveContext expectedContext) {
94 assertThat(objective.permanent(), is(false));
95 assertThat(objective.timeout(), is(5));
Ray Milkeyf410a0c2015-05-19 15:46:03 -070096 assertThat(objective.selector(), is(selector));
Ray Milkey3004c7d2017-12-13 09:43:20 -080097 assertThat(objective.meta(), is(selector));
Ray Milkeyf410a0c2015-05-19 15:46:03 -070098 assertThat(objective.treatment(), is(treatment));
99 assertThat(objective.flag(), is(SPECIFIC));
100 assertThat(objective.appId(), is(APP_ID));
101 assertThat(objective.nextId(), is(33));
Ray Milkey810d6e72015-08-14 10:35:06 -0700102 assertThat(objective.id(), is(not(0)));
103 assertThat(objective.priority(), is(22));
104 assertThat(objective.op(), is(op));
105 if (objective.context().isPresent()) {
106 assertThat(objective.context().get(), is(expectedContext));
107 } else {
108 assertThat(expectedContext, nullValue());
109 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700110 }
111
112 /**
113 * Tests that forwarding objective objects are built correctly using the
114 * add() method.
115 */
116 @Test
117 public void testForwardingAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700118 checkForwardingBase(baseForwardingBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700119 }
120
121 /**
122 * Tests that forwarding objective objects are built correctly using the
123 * add(context) method.
124 */
125 @Test
126 public void testForwardingAddWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800127 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700128 checkForwardingBase(baseForwardingBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700129 }
130
131 /**
132 * Tests that forwarding objective objects are built correctly using the
133 * remove() method.
134 */
135 @Test
136 public void testForwardingRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700137 checkForwardingBase(baseForwardingBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700138 }
139
140 /**
141 * Tests that forwarding objective objects are built correctly using the
142 * remove(context) method.
143 */
144 @Test
145 public void testForwardingRemoveWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800146 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700147 checkForwardingBase(baseForwardingBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700148 }
149
150 // Filtering objectives
151
152 /**
153 * Makes a filtering objective builder with a set of default values.
154 *
155 * @return filtering objective builder
156 */
157 private FilteringObjective.Builder baseFilteringBuilder() {
158 return DefaultFilteringObjective.builder()
159 .withKey(key)
160 .withPriority(5)
Ray Milkey3004c7d2017-12-13 09:43:20 -0800161 .withMeta(treatment)
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700162 .addCondition(criterion)
163 .fromApp(APP_ID)
164 .makeTemporary(2)
165 .deny();
166 }
167
168 /**
169 * Checks the default values of a filtering objective object.
170 *
171 * @param objective filtering objective to check
172 */
Ray Milkey810d6e72015-08-14 10:35:06 -0700173 private void checkFilteringBase(FilteringObjective objective,
174 Objective.Operation op,
175 ObjectiveContext expectedContext) {
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700176 assertThat(objective.key(), is(key));
177 assertThat(objective.conditions(), hasItem(criterion));
Ray Milkey3004c7d2017-12-13 09:43:20 -0800178 assertThat(objective.meta(), is(treatment));
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700179 assertThat(objective.permanent(), is(false));
180 assertThat(objective.timeout(), is(2));
181 assertThat(objective.priority(), is(5));
182 assertThat(objective.appId(), is(APP_ID));
183 assertThat(objective.type(), is(DENY));
Ray Milkey810d6e72015-08-14 10:35:06 -0700184 assertThat(objective.id(), is(not(0)));
185 assertThat(objective.op(), is(op));
186 if (objective.context().isPresent()) {
187 assertThat(objective.context().get(), is(expectedContext));
188 } else {
189 assertThat(expectedContext, nullValue());
190 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700191 }
192
193 /**
194 * Tests that forwarding objective objects are built correctly using the
195 * add() method.
196 */
197 @Test
198 public void testFilteringAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700199 checkFilteringBase(baseFilteringBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700200 }
201
202 /**
203 * Tests that forwarding objective objects are built correctly using the
204 * add(context) method.
205 */
206 @Test
207 public void testFilteringAddWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800208 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700209 checkFilteringBase(baseFilteringBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700210 }
211
212 /**
213 * Tests that forwarding objective objects are built correctly using the
214 * remove() method.
215 */
216 @Test
217 public void testFilteringRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700218 checkFilteringBase(baseFilteringBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700219 }
220
221 /**
222 * Tests that forwarding objective objects are built correctly using the
223 * remove(context) method.
224 */
225 @Test
226 public void testFilteringRemoveWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800227 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700228 checkFilteringBase(baseFilteringBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700229 }
230
231 // Next objectives
232
233 /**
234 * Makes a next objective builder with a set of default values.
235 *
236 * @return next objective builder
237 */
238 private NextObjective.Builder baseNextBuilder() {
239 return DefaultNextObjective.builder()
240 .addTreatment(treatment)
241 .withId(12)
242 .withType(HASHED)
Ray Milkey3004c7d2017-12-13 09:43:20 -0800243 .withMeta(selector)
Ray Milkey810d6e72015-08-14 10:35:06 -0700244 .makeTemporary(777)
245 .withPriority(33)
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700246 .fromApp(APP_ID);
247 }
248
249 /**
250 * Checks the default values of a next objective object.
251 *
252 * @param objective next objective to check
253 */
Ray Milkey810d6e72015-08-14 10:35:06 -0700254 private void checkNextBase(NextObjective objective,
255 Objective.Operation op,
256 ObjectiveContext expectedContext) {
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700257 assertThat(objective.id(), is(12));
258 assertThat(objective.appId(), is(APP_ID));
259 assertThat(objective.type(), is(HASHED));
260 assertThat(objective.next(), hasItem(treatment));
Ray Milkey3004c7d2017-12-13 09:43:20 -0800261 assertThat(objective.meta(), is(selector));
Ray Milkey810d6e72015-08-14 10:35:06 -0700262 assertThat(objective.permanent(), is(false));
263 assertThat(objective.timeout(), is(0));
264 assertThat(objective.priority(), is(0));
265 assertThat(objective.op(), is(op));
266 if (objective.context().isPresent()) {
267 assertThat(objective.context().get(), is(expectedContext));
268 } else {
269 assertThat(expectedContext, nullValue());
270 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700271 }
272
273 /**
274 * Tests that forwarding objective objects are built correctly using the
275 * add() method.
276 */
277 @Test
278 public void testNextAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700279 checkNextBase(baseNextBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700280 }
281
282 /**
283 * Tests that forwarding objective objects are built correctly using the
284 * add(context) method.
285 */
286 @Test
287 public void testNextAddWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800288 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700289 checkNextBase(baseNextBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700290 }
291
292 /**
293 * Tests that forwarding objective objects are built correctly using the
294 * remove() method.
295 */
296 @Test
297 public void testNextRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700298 checkNextBase(baseNextBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700299 }
300
301 /**
302 * Tests that forwarding objective objects are built correctly using the
303 * remove(context) method.
304 */
305 @Test
306 public void testNextRemoveWithContext() {
Ray Milkey3004c7d2017-12-13 09:43:20 -0800307 ObjectiveContext context = new DefaultObjectiveContext(null, null);
Ray Milkey810d6e72015-08-14 10:35:06 -0700308 checkNextBase(baseNextBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700309 }
Ray Milkey3004c7d2017-12-13 09:43:20 -0800310
311 /**
312 * Tests equality, hash, and toString operations on next objectives.
313 */
314 @Test
315 public void testEqualsNextObjective() {
316 NextObjective next1 = baseNextBuilder().add();
317 NextObjective sameAsNext1 = next1.copy().add();
318 NextObjective next2 = baseNextBuilder().verify();
319 NextObjective next3 = baseNextBuilder().fromApp(new TestApplicationId("foo2")).verify();
320 NextObjective next4 = baseNextBuilder().withType(NextObjective.Type.FAILOVER).verify();
321 NextObjective next5 = baseNextBuilder().addTreatment(DefaultTrafficTreatment.emptyTreatment()).verify();
322 new EqualsTester()
323 .addEqualityGroup(next1, sameAsNext1)
324 .addEqualityGroup(next2)
325 .addEqualityGroup(next3)
326 .addEqualityGroup(next4)
327 .addEqualityGroup(next5)
328 .testEquals();
329 }
330
331 /**
332 * Tests add to an existing next objective.
333 */
334 @Test
335 public void testToFromExistingNextObjective() {
336 NextObjective next1 = baseNextBuilder().addToExisting();
337
338 checkNextBase(next1, ADD_TO_EXISTING, null);
339 }
340
341 /**
342 * Tests remove from an existing next objective.
343 */
344 @Test
345 public void testRemoveFromExistingNextObjective() {
346 NextObjective next1 = baseNextBuilder().removeFromExisting();
347
348 checkNextBase(next1, REMOVE_FROM_EXISTING, null);
349 }
350
351 /**
352 * Tests equality, hash, and toString operations on filtering objectives.
353 */
354 @Test
355 public void testEqualsFilteringObjective() {
356 FilteringObjective filter1 = baseFilteringBuilder().add();
357 FilteringObjective sameAsFilter1 = filter1.copy().add();
358 FilteringObjective filter2 = baseFilteringBuilder().permit().add();
359 FilteringObjective filter3 = baseFilteringBuilder().fromApp(new TestApplicationId("foo2")).add();
360 FilteringObjective filter4 = baseFilteringBuilder().permit().remove();
361 FilteringObjective filter5 = baseFilteringBuilder().withPriority(55).add();
362 FilteringObjective filter6 = baseFilteringBuilder().makePermanent().add();
363 new EqualsTester()
364 .addEqualityGroup(filter1, sameAsFilter1)
365 .addEqualityGroup(filter2)
366 .addEqualityGroup(filter3)
367 .addEqualityGroup(filter4)
368 .addEqualityGroup(filter5)
369 .addEqualityGroup(filter6)
370 .testEquals();
371 }
372
373 /**
374 * Tests add to an existing filtering objective.
375 */
376 @Test
377 public void testToFromExistingFilteringObjective() {
378 FilteringObjective filter1 = baseFilteringBuilder().add(null);
379
380 checkFilteringBase(filter1, ADD, null);
381 }
382
383 /**
384 * Tests remove from an existing filtering objective.
385 */
386 @Test
387 public void testRemoveFromExistingFilteringObjective() {
388 FilteringObjective filter1 = baseFilteringBuilder().remove(null);
389
390 checkFilteringBase(filter1, REMOVE, null);
391 }
392
393 /**
394 * Tests equality, hash, and toString operations on filtering objectives.
395 */
396 @Test
397 public void testEqualsForwardingObjective() {
398 ForwardingObjective forward1 = baseForwardingBuilder().add();
399 ForwardingObjective sameAsForward1 = forward1.copy().add();
400 ForwardingObjective forward2 = baseForwardingBuilder().withFlag(VERSATILE).add();
401 ForwardingObjective forward3 = baseForwardingBuilder().fromApp(new TestApplicationId("foo2")).add();
402 ForwardingObjective forward4 = baseForwardingBuilder().remove();
403 ForwardingObjective forward5 = baseForwardingBuilder().withPriority(55).add();
404 ForwardingObjective forward6 = baseForwardingBuilder().makePermanent().add();
405 new EqualsTester()
406 .addEqualityGroup(forward1, sameAsForward1)
407 .addEqualityGroup(forward2)
408 .addEqualityGroup(forward3)
409 .addEqualityGroup(forward4)
410 .addEqualityGroup(forward5)
411 .addEqualityGroup(forward6)
412 .testEquals();
413 }
414
415 /**
416 * Tests add to an existing forwarding objective.
417 */
418 @Test
419 public void testToFromExistingForwardingObjective() {
420 ForwardingObjective forward1 = baseForwardingBuilder().add(null);
421
422 checkForwardingBase(forward1, ADD, null);
423 }
424
425 /**
426 * Tests remove from an existing forwarding objective.
427 */
428 @Test
429 public void testRemoveFromExistingForwardingObjective() {
430 ForwardingObjective forward1 = baseForwardingBuilder().remove(null);
431
432 checkForwardingBase(forward1, REMOVE, null);
433 }
434
435
436 enum ContextType {
437 BOTH,
438 ERROR_ONLY,
439 SUCCESS_ONLY
440 }
441
442 class ObjectiveContextAdapter implements ObjectiveContext {
443 DefaultObjectiveContext context;
444 int successCount = 0;
445 int errorCount = 0;
446 ObjectiveError objectiveError = ObjectiveError.UNKNOWN;
447 Objective objectiveInError = null;
448
449 ObjectiveContextAdapter(ContextType type) {
450 switch (type) {
451
452 case ERROR_ONLY:
453 context = new DefaultObjectiveContext(
454 (failedObjective, error) -> {
455 errorCount++;
456 objectiveInError = failedObjective;
457 objectiveError = error;
458 });
459 break;
460
461 case SUCCESS_ONLY:
462 context = new DefaultObjectiveContext(
463 (successfulObjective) -> successCount++);
464 break;
465
466 case BOTH:
467 default:
468 context = new DefaultObjectiveContext(
469 (successfulObjective) -> successCount++,
470 (failedObjective, error) -> {
471 errorCount++;
472 objectiveInError = failedObjective;
473 objectiveError = error;
474 });
475 break;
476 }
477 }
478
479 @Override
480 public void onSuccess(Objective objective) {
481 context.onSuccess(objective);
482 }
483
484 @Override
485 public void onError(Objective objective, ObjectiveError error) {
486 context.onError(objective, error);
487 }
488
489 int successCount() {
490 return successCount;
491 }
492
493 int errorCount() {
494 return errorCount;
495 }
496
497 ObjectiveError objectiveError() {
498 return objectiveError;
499 }
500
501 Objective objectiveInError() {
502 return objectiveInError;
503 }
504 }
505
506 /**
507 * Tests default objective context.
508 */
509 @Test
510 public void testDefaultContext() {
511 Objective objective = baseFilteringBuilder().add();
512 ObjectiveContextAdapter context;
513
514 context = new ObjectiveContextAdapter(ContextType.BOTH);
515 context.onSuccess(objective);
516 assertThat(context.successCount(), is(1));
517 assertThat(context.errorCount(), is(0));
518 assertThat(context.objectiveError(), is(ObjectiveError.UNKNOWN));
519 assertThat(context.objectiveInError(), nullValue());
520
521 context = new ObjectiveContextAdapter(ContextType.BOTH);
522 context.onError(objective, ObjectiveError.UNSUPPORTED);
523 assertThat(context.successCount(), is(0));
524 assertThat(context.errorCount(), is(1));
525 assertThat(context.objectiveError(), is(ObjectiveError.UNSUPPORTED));
526 assertThat(context.objectiveInError(), equalTo(objective));
527
528 context = new ObjectiveContextAdapter(ContextType.SUCCESS_ONLY);
529 context.onSuccess(objective);
530 assertThat(context.successCount(), is(1));
531 assertThat(context.errorCount(), is(0));
532 assertThat(context.objectiveError(), is(ObjectiveError.UNKNOWN));
533 assertThat(context.objectiveInError(), nullValue());
534
535 context = new ObjectiveContextAdapter(ContextType.ERROR_ONLY);
536 context.onError(objective, ObjectiveError.UNSUPPORTED);
537 assertThat(context.successCount(), is(0));
538 assertThat(context.errorCount(), is(1));
539 assertThat(context.objectiveError(), is(ObjectiveError.UNSUPPORTED));
540 assertThat(context.objectiveInError(), equalTo(objective));
541 }
542
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700543}