blob: 850582b0272dc0d43bd442a60b2c577652582135 [file] [log] [blame]
Ray Milkeyf410a0c2015-05-19 15:46:03 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.flowobjective;
17
18import org.junit.Test;
19import org.onosproject.net.flow.DefaultTrafficSelector;
20import org.onosproject.net.flow.DefaultTrafficTreatment;
21import org.onosproject.net.flow.TrafficSelector;
22import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.net.flow.criteria.Criteria;
24import org.onosproject.net.flow.criteria.Criterion;
25
26import static org.hamcrest.CoreMatchers.hasItem;
27import static org.hamcrest.CoreMatchers.is;
Ray Milkey810d6e72015-08-14 10:35:06 -070028import static org.hamcrest.CoreMatchers.nullValue;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070029import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey810d6e72015-08-14 10:35:06 -070030import static org.hamcrest.Matchers.not;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070031import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32import static org.onosproject.net.NetTestTools.APP_ID;
33import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY;
34import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
35import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Ray Milkey810d6e72015-08-14 10:35:06 -070036import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
37import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;
Ray Milkeyf410a0c2015-05-19 15:46:03 -070038
39/**
40 * Unit tests for forwarding objective class.
41 */
42public class ObjectiveTest {
43
44 private final TrafficTreatment treatment =
45 DefaultTrafficTreatment.emptyTreatment();
46 private final TrafficSelector selector =
47 DefaultTrafficSelector.emptySelector();
48 private final Criterion criterion = Criteria.dummy();
49 private final Criterion key = Criteria.dummy();
50
51 /**
52 * Mock objective context.
53 */
54 private static class MockObjectiveContext implements ObjectiveContext {
55 @Override
56 public void onSuccess(Objective objective) {
57 // stub
58 }
59
60 @Override
61 public void onError(Objective objective, ObjectiveError error) {
62 // stub
63 }
64 }
65
66 /**
67 * Checks immutability of objective classes.
68 */
69 @Test
70 public void testImmutability() {
71 assertThatClassIsImmutable(DefaultFilteringObjective.class);
72 assertThatClassIsImmutable(DefaultForwardingObjective.class);
73 assertThatClassIsImmutable(DefaultNextObjective.class);
74 }
75
76 // Forwarding Objectives
77
78 /**
79 * Makes a forwarding objective builder with a set of default values.
80 *
81 * @return forwarding objective builder
82 */
83 private ForwardingObjective.Builder baseForwardingBuilder() {
84 return DefaultForwardingObjective.builder()
85 .withSelector(selector)
86 .withTreatment(treatment)
87 .withFlag(SPECIFIC)
88 .fromApp(APP_ID)
Ray Milkey810d6e72015-08-14 10:35:06 -070089 .withPriority(22)
90 .makeTemporary(5)
Ray Milkeyf410a0c2015-05-19 15:46:03 -070091 .nextStep(33);
92 }
93
94 /**
95 * Checks the default values of a forwarding objective object.
96 *
97 * @param objective forwarding objective to check
98 */
Ray Milkey810d6e72015-08-14 10:35:06 -070099 private void checkForwardingBase(ForwardingObjective objective,
100 Objective.Operation op,
101 ObjectiveContext expectedContext) {
102 assertThat(objective.permanent(), is(false));
103 assertThat(objective.timeout(), is(5));
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700104 assertThat(objective.selector(), is(selector));
105 assertThat(objective.treatment(), is(treatment));
106 assertThat(objective.flag(), is(SPECIFIC));
107 assertThat(objective.appId(), is(APP_ID));
108 assertThat(objective.nextId(), is(33));
Ray Milkey810d6e72015-08-14 10:35:06 -0700109 assertThat(objective.id(), is(not(0)));
110 assertThat(objective.priority(), is(22));
111 assertThat(objective.op(), is(op));
112 if (objective.context().isPresent()) {
113 assertThat(objective.context().get(), is(expectedContext));
114 } else {
115 assertThat(expectedContext, nullValue());
116 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700117 }
118
119 /**
120 * Tests that forwarding objective objects are built correctly using the
121 * add() method.
122 */
123 @Test
124 public void testForwardingAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700125 checkForwardingBase(baseForwardingBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700126 }
127
128 /**
129 * Tests that forwarding objective objects are built correctly using the
130 * add(context) method.
131 */
132 @Test
133 public void testForwardingAddWithContext() {
134 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700135 checkForwardingBase(baseForwardingBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700136 }
137
138 /**
139 * Tests that forwarding objective objects are built correctly using the
140 * remove() method.
141 */
142 @Test
143 public void testForwardingRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700144 checkForwardingBase(baseForwardingBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700145 }
146
147 /**
148 * Tests that forwarding objective objects are built correctly using the
149 * remove(context) method.
150 */
151 @Test
152 public void testForwardingRemoveWithContext() {
153 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700154 checkForwardingBase(baseForwardingBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700155 }
156
157 // Filtering objectives
158
159 /**
160 * Makes a filtering objective builder with a set of default values.
161 *
162 * @return filtering objective builder
163 */
164 private FilteringObjective.Builder baseFilteringBuilder() {
165 return DefaultFilteringObjective.builder()
166 .withKey(key)
167 .withPriority(5)
168 .addCondition(criterion)
169 .fromApp(APP_ID)
170 .makeTemporary(2)
171 .deny();
172 }
173
174 /**
175 * Checks the default values of a filtering objective object.
176 *
177 * @param objective filtering objective to check
178 */
Ray Milkey810d6e72015-08-14 10:35:06 -0700179 private void checkFilteringBase(FilteringObjective objective,
180 Objective.Operation op,
181 ObjectiveContext expectedContext) {
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700182 assertThat(objective.key(), is(key));
183 assertThat(objective.conditions(), hasItem(criterion));
184 assertThat(objective.permanent(), is(false));
185 assertThat(objective.timeout(), is(2));
186 assertThat(objective.priority(), is(5));
187 assertThat(objective.appId(), is(APP_ID));
188 assertThat(objective.type(), is(DENY));
Ray Milkey810d6e72015-08-14 10:35:06 -0700189 assertThat(objective.id(), is(not(0)));
190 assertThat(objective.op(), is(op));
191 if (objective.context().isPresent()) {
192 assertThat(objective.context().get(), is(expectedContext));
193 } else {
194 assertThat(expectedContext, nullValue());
195 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700196 }
197
198 /**
199 * Tests that forwarding objective objects are built correctly using the
200 * add() method.
201 */
202 @Test
203 public void testFilteringAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700204 checkFilteringBase(baseFilteringBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700205 }
206
207 /**
208 * Tests that forwarding objective objects are built correctly using the
209 * add(context) method.
210 */
211 @Test
212 public void testFilteringAddWithContext() {
213 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700214 checkFilteringBase(baseFilteringBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700215 }
216
217 /**
218 * Tests that forwarding objective objects are built correctly using the
219 * remove() method.
220 */
221 @Test
222 public void testFilteringRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700223 checkFilteringBase(baseFilteringBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700224 }
225
226 /**
227 * Tests that forwarding objective objects are built correctly using the
228 * remove(context) method.
229 */
230 @Test
231 public void testFilteringRemoveWithContext() {
232 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700233 checkFilteringBase(baseFilteringBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700234 }
235
236 // Next objectives
237
238 /**
239 * Makes a next objective builder with a set of default values.
240 *
241 * @return next objective builder
242 */
243 private NextObjective.Builder baseNextBuilder() {
244 return DefaultNextObjective.builder()
245 .addTreatment(treatment)
246 .withId(12)
247 .withType(HASHED)
Ray Milkey810d6e72015-08-14 10:35:06 -0700248 .makeTemporary(777)
249 .withPriority(33)
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700250 .fromApp(APP_ID);
251 }
252
253 /**
254 * Checks the default values of a next objective object.
255 *
256 * @param objective next objective to check
257 */
Ray Milkey810d6e72015-08-14 10:35:06 -0700258 private void checkNextBase(NextObjective objective,
259 Objective.Operation op,
260 ObjectiveContext expectedContext) {
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700261 assertThat(objective.id(), is(12));
262 assertThat(objective.appId(), is(APP_ID));
263 assertThat(objective.type(), is(HASHED));
264 assertThat(objective.next(), hasItem(treatment));
Ray Milkey810d6e72015-08-14 10:35:06 -0700265 assertThat(objective.permanent(), is(false));
266 assertThat(objective.timeout(), is(0));
267 assertThat(objective.priority(), is(0));
268 assertThat(objective.op(), is(op));
269 if (objective.context().isPresent()) {
270 assertThat(objective.context().get(), is(expectedContext));
271 } else {
272 assertThat(expectedContext, nullValue());
273 }
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700274 }
275
276 /**
277 * Tests that forwarding objective objects are built correctly using the
278 * add() method.
279 */
280 @Test
281 public void testNextAdd() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700282 checkNextBase(baseNextBuilder().add(), ADD, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700283 }
284
285 /**
286 * Tests that forwarding objective objects are built correctly using the
287 * add(context) method.
288 */
289 @Test
290 public void testNextAddWithContext() {
291 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700292 checkNextBase(baseNextBuilder().add(context), ADD, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700293 }
294
295 /**
296 * Tests that forwarding objective objects are built correctly using the
297 * remove() method.
298 */
299 @Test
300 public void testNextRemove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700301 checkNextBase(baseNextBuilder().remove(), REMOVE, null);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700302 }
303
304 /**
305 * Tests that forwarding objective objects are built correctly using the
306 * remove(context) method.
307 */
308 @Test
309 public void testNextRemoveWithContext() {
310 ObjectiveContext context = new MockObjectiveContext();
Ray Milkey810d6e72015-08-14 10:35:06 -0700311 checkNextBase(baseNextBuilder().remove(context), REMOVE, context);
Ray Milkeyf410a0c2015-05-19 15:46:03 -0700312 }
313}