Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 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 | package org.onosproject.net.flowobjective; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 20 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 21 | import org.onosproject.net.flow.TrafficSelector; |
| 22 | import org.onosproject.net.flow.TrafficTreatment; |
| 23 | import org.onosproject.net.flow.criteria.Criteria; |
| 24 | import org.onosproject.net.flow.criteria.Criterion; |
| 25 | |
| 26 | import static org.hamcrest.CoreMatchers.hasItem; |
| 27 | import static org.hamcrest.CoreMatchers.is; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 28 | import static org.hamcrest.CoreMatchers.nullValue; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 29 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.not; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 31 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 32 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 33 | import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY; |
| 34 | import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC; |
| 35 | import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 36 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
| 37 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE; |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Unit tests for forwarding objective class. |
| 41 | */ |
| 42 | public 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 89 | .withPriority(22) |
| 90 | .makeTemporary(5) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 91 | .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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 99 | 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 Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 104 | 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 109 | 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 Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests that forwarding objective objects are built correctly using the |
| 121 | * add() method. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testForwardingAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 125 | checkForwardingBase(baseForwardingBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 126 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 135 | checkForwardingBase(baseForwardingBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Tests that forwarding objective objects are built correctly using the |
| 140 | * remove() method. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testForwardingRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 144 | checkForwardingBase(baseForwardingBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 145 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 154 | checkForwardingBase(baseForwardingBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 155 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 179 | private void checkFilteringBase(FilteringObjective objective, |
| 180 | Objective.Operation op, |
| 181 | ObjectiveContext expectedContext) { |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 182 | 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 189 | 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 Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Tests that forwarding objective objects are built correctly using the |
| 200 | * add() method. |
| 201 | */ |
| 202 | @Test |
| 203 | public void testFilteringAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 204 | checkFilteringBase(baseFilteringBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 205 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 214 | checkFilteringBase(baseFilteringBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests that forwarding objective objects are built correctly using the |
| 219 | * remove() method. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testFilteringRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 223 | checkFilteringBase(baseFilteringBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 224 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 233 | checkFilteringBase(baseFilteringBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 234 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 248 | .makeTemporary(777) |
| 249 | .withPriority(33) |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 250 | .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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 258 | private void checkNextBase(NextObjective objective, |
| 259 | Objective.Operation op, |
| 260 | ObjectiveContext expectedContext) { |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 261 | 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 265 | 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 Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Tests that forwarding objective objects are built correctly using the |
| 278 | * add() method. |
| 279 | */ |
| 280 | @Test |
| 281 | public void testNextAdd() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 282 | checkNextBase(baseNextBuilder().add(), ADD, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 283 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 292 | checkNextBase(baseNextBuilder().add(context), ADD, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests that forwarding objective objects are built correctly using the |
| 297 | * remove() method. |
| 298 | */ |
| 299 | @Test |
| 300 | public void testNextRemove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 301 | checkNextBase(baseNextBuilder().remove(), REMOVE, null); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 302 | } |
| 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 Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 311 | checkNextBase(baseNextBuilder().remove(context), REMOVE, context); |
Ray Milkey | f410a0c | 2015-05-19 15:46:03 -0700 | [diff] [blame] | 312 | } |
| 313 | } |