blob: d8b511c4448e89881605ecc419d39fb900fef2b5 [file] [log] [blame]
Ray Milkey78081052014-11-05 10:38:12 -08001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
Ray Milkey78081052014-11-05 10:38:12 -080017
18import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.PortNumber;
Ray Milkey78081052014-11-05 10:38:12 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24
Ray Milkey0d338052014-11-21 16:40:12 -080025import com.google.common.testing.EqualsTester;
26
Ray Milkey78081052014-11-05 10:38:12 -080027import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey78081052014-11-05 10:38:12 -080028import static org.hamcrest.Matchers.equalTo;
29import static org.hamcrest.Matchers.instanceOf;
30import static org.hamcrest.Matchers.is;
31import static org.hamcrest.Matchers.not;
32import static org.hamcrest.Matchers.notNullValue;
33import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
34import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import static org.onosproject.net.PortNumber.portNumber;
Ray Milkey78081052014-11-05 10:38:12 -080036
37/**
38 * Unit tests for the Instructions class.
39 */
40public class InstructionsTest {
41
42 /**
43 * Checks that a Criterion object has the proper type, and then converts
44 * it to the proper type.
45 *
46 * @param instruction Instruction object to convert
47 * @param type Enumerated type value for the Criterion class
48 * @param clazz Desired Criterion class
49 * @param <T> The type the caller wants returned
50 * @return converted object
51 */
52 @SuppressWarnings("unchecked")
53 private <T> T checkAndConvert(Instruction instruction, Instruction.Type type, Class clazz) {
54 assertThat(instruction, is(notNullValue()));
55 assertThat(instruction.type(), is(equalTo(type)));
56 assertThat(instruction, instanceOf(clazz));
57 return (T) instruction;
58 }
59
60 /**
61 * Checks the equals() and toString() methods of a Criterion class.
62 *
63 * @param c1 first object to compare
64 * @param c1match object that should be equal to the first
65 * @param c2 object that should be not equal to the first
Ray Milkey78081052014-11-05 10:38:12 -080066 * @param <T> type of the arguments
67 */
68 private <T extends Instruction> void checkEqualsAndToString(T c1, T c1match,
Ray Milkey0d338052014-11-21 16:40:12 -080069 T c2) {
Ray Milkey78081052014-11-05 10:38:12 -080070
Ray Milkey0d338052014-11-21 16:40:12 -080071 new EqualsTester()
72 .addEqualityGroup(c1, c1match)
73 .addEqualityGroup(c2)
74 .testEquals();
Ray Milkey78081052014-11-05 10:38:12 -080075 }
76
77 /**
78 * Checks that Instructions is a proper utility class.
79 */
80 @Test
81 public void testInstructionsUtilityClass() {
82 assertThatClassIsUtility(Instructions.class);
83 }
84
85 /**
86 * Checks that the Instruction class implementations are immutable.
87 */
88 @Test
89 public void testImmutabilityOfInstructions() {
90 assertThatClassIsImmutable(Instructions.DropInstruction.class);
91 assertThatClassIsImmutable(Instructions.OutputInstruction.class);
92 assertThatClassIsImmutable(L0ModificationInstruction.ModLambdaInstruction.class);
93 assertThatClassIsImmutable(L2ModificationInstruction.ModEtherInstruction.class);
94 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class);
95 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class);
96 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class);
Ray Milkeyd03eda02015-01-09 14:58:48 -080097 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class);
98 assertThatClassIsImmutable(L2ModificationInstruction.PushHeaderInstructions.class);
Ray Milkey78081052014-11-05 10:38:12 -080099 }
100
101 // DropInstruction
102
103 private final Instructions.DropInstruction drop1 = Instructions.createDrop();
104 private final Instructions.DropInstruction drop2 = Instructions.createDrop();
105
106 /**
107 * Test the createDrop method.
108 */
109 @Test
110 public void testCreateDropMethod() {
111 Instructions.DropInstruction instruction = Instructions.createDrop();
112 checkAndConvert(instruction,
113 Instruction.Type.DROP,
114 Instructions.DropInstruction.class);
115 }
116
117 /**
118 * Test the equals() method of the DropInstruction class.
119 */
120
121 @Test
122 public void testDropInstructionEquals() throws Exception {
123 assertThat(drop1, is(equalTo(drop2)));
124 }
125
126 /**
127 * Test the hashCode() method of the DropInstruction class.
128 */
129
130 @Test
131 public void testDropInstructionHashCode() {
132 assertThat(drop1.hashCode(), is(equalTo(drop2.hashCode())));
133 }
134
135 // OutputInstruction
136
137 private final PortNumber port1 = portNumber(1);
138 private final PortNumber port2 = portNumber(2);
139 private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1);
140 private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1);
141 private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2);
142
143 /**
144 * Test the createOutput method.
145 */
146 @Test
147 public void testCreateOutputMethod() {
148 final Instruction instruction = Instructions.createOutput(port2);
149 final Instructions.OutputInstruction outputInstruction =
150 checkAndConvert(instruction,
151 Instruction.Type.OUTPUT,
152 Instructions.OutputInstruction.class);
153 assertThat(outputInstruction.port(), is(equalTo(port2)));
154 }
155
156
157 /**
158 * Test the equals() method of the OutputInstruction class.
159 */
160
161 @Test
162 public void testOutputInstructionEquals() throws Exception {
Ray Milkey0d338052014-11-21 16:40:12 -0800163 checkEqualsAndToString(output1, sameAsOutput1, output2);
Ray Milkey78081052014-11-05 10:38:12 -0800164 }
165
166 /**
167 * Test the hashCode() method of the OutputInstruction class.
168 */
169
170 @Test
171 public void testOutputInstructionHashCode() {
172 assertThat(output1.hashCode(), is(equalTo(sameAsOutput1.hashCode())));
173 assertThat(output1.hashCode(), is(not(equalTo(output2.hashCode()))));
174 }
175
176 // ModLambdaInstruction
177
178 private final short lambda1 = 1;
179 private final short lambda2 = 2;
180 private final Instruction lambdaInstruction1 = Instructions.modL0Lambda(lambda1);
181 private final Instruction sameAsLambdaInstruction1 = Instructions.modL0Lambda(lambda1);
182 private final Instruction lambdaInstruction2 = Instructions.modL0Lambda(lambda2);
183
184 /**
185 * Test the modL0Lambda method.
186 */
187 @Test
188 public void testCreateLambdaMethod() {
189 final Instruction instruction = Instructions.modL0Lambda(lambda1);
190 final L0ModificationInstruction.ModLambdaInstruction lambdaInstruction =
191 checkAndConvert(instruction,
192 Instruction.Type.L0MODIFICATION,
193 L0ModificationInstruction.ModLambdaInstruction.class);
194 assertThat(lambdaInstruction.lambda(), is(equalTo(lambda1)));
195 }
196
197
198 /**
199 * Test the equals() method of the ModLambdaInstruction class.
200 */
201
202 @Test
203 public void testModLambdaInstructionEquals() throws Exception {
204 checkEqualsAndToString(lambdaInstruction1,
205 sameAsLambdaInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800206 lambdaInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800207 }
208
209 /**
210 * Test the hashCode() method of the ModLambdaInstruction class.
211 */
212
213 @Test
214 public void testModLambdaInstructionHashCode() {
215 assertThat(lambdaInstruction1.hashCode(),
216 is(equalTo(sameAsLambdaInstruction1.hashCode())));
217 assertThat(lambdaInstruction1.hashCode(),
218 is(not(equalTo(lambdaInstruction2.hashCode()))));
219 }
220
221 // ModEtherInstruction
222
223 private static final String MAC1 = "00:00:00:00:00:01";
224 private static final String MAC2 = "00:00:00:00:00:02";
225 private final MacAddress mac1 = MacAddress.valueOf(MAC1);
226 private final MacAddress mac2 = MacAddress.valueOf(MAC2);
227 private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1);
228 private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1);
229 private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2);
230
231 /**
232 * Test the modL2Src method.
233 */
234 @Test
235 public void testModL2SrcMethod() {
236 final Instruction instruction = Instructions.modL2Src(mac1);
237 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
238 checkAndConvert(instruction,
239 Instruction.Type.L2MODIFICATION,
240 L2ModificationInstruction.ModEtherInstruction.class);
241 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
242 assertThat(modEtherInstruction.subtype(),
243 is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC)));
244 }
245
246 /**
247 * Test the modL2Dst method.
248 */
249 @Test
250 public void testModL2DstMethod() {
251 final Instruction instruction = Instructions.modL2Dst(mac1);
252 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
253 checkAndConvert(instruction,
254 Instruction.Type.L2MODIFICATION,
255 L2ModificationInstruction.ModEtherInstruction.class);
256 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
257 assertThat(modEtherInstruction.subtype(),
258 is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST)));
259 }
260
261 /**
262 * Test the equals() method of the ModEtherInstruction class.
263 */
264
265 @Test
266 public void testModEtherInstructionEquals() throws Exception {
267 checkEqualsAndToString(modEtherInstruction1,
268 sameAsModEtherInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800269 modEtherInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800270 }
271
272 /**
273 * Test the hashCode() method of the ModEtherInstruction class.
274 */
275
276 @Test
277 public void testModEtherInstructionHashCode() {
278 assertThat(modEtherInstruction1.hashCode(),
279 is(equalTo(sameAsModEtherInstruction1.hashCode())));
280 assertThat(modEtherInstruction1.hashCode(),
281 is(not(equalTo(modEtherInstruction2.hashCode()))));
282 }
283
284
285 // ModVlanIdInstruction
286
287 private final short vlan1 = 1;
288 private final short vlan2 = 2;
289 private final VlanId vlanId1 = VlanId.vlanId(vlan1);
290 private final VlanId vlanId2 = VlanId.vlanId(vlan2);
291 private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1);
292 private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1);
293 private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2);
294
295 /**
296 * Test the modVlanId method.
297 */
298 @Test
299 public void testModVlanIdMethod() {
300 final Instruction instruction = Instructions.modVlanId(vlanId1);
301 final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction =
302 checkAndConvert(instruction,
303 Instruction.Type.L2MODIFICATION,
304 L2ModificationInstruction.ModVlanIdInstruction.class);
305 assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1)));
306 assertThat(modEtherInstruction.subtype(),
307 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID)));
308 }
309
310 /**
311 * Test the equals() method of the ModVlanIdInstruction class.
312 */
313
314 @Test
315 public void testModVlanIdInstructionEquals() throws Exception {
316 checkEqualsAndToString(modVlanId1,
317 sameAsModVlanId1,
Ray Milkey0d338052014-11-21 16:40:12 -0800318 modVlanId2);
Ray Milkey78081052014-11-05 10:38:12 -0800319 }
320
321 /**
322 * Test the hashCode() method of the ModEtherInstruction class.
323 */
324
325 @Test
326 public void testModVlanIdInstructionHashCode() {
327 assertThat(modVlanId1.hashCode(),
328 is(equalTo(sameAsModVlanId1.hashCode())));
329 assertThat(modVlanId1.hashCode(),
330 is(not(equalTo(modVlanId2.hashCode()))));
331 }
332
333
334 // ModVlanPcpInstruction
335
336 private final byte vlanPcp1 = 1;
337 private final byte vlanPcp2 = 2;
338 private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
339 private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
340 private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2);
341
342 /**
343 * Test the modVlanPcp method.
344 */
345 @Test
346 public void testModVlanPcpMethod() {
347 final Instruction instruction = Instructions.modVlanPcp(vlanPcp1);
348 final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction =
349 checkAndConvert(instruction,
350 Instruction.Type.L2MODIFICATION,
351 L2ModificationInstruction.ModVlanPcpInstruction.class);
352 assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1)));
353 assertThat(modEtherInstruction.subtype(),
354 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP)));
355 }
356
357 /**
358 * Test the equals() method of the ModVlanPcpInstruction class.
359 */
360
361 @Test
362 public void testModVlanPcpInstructionEquals() throws Exception {
363 checkEqualsAndToString(modVlanPcp1,
364 sameAsModVlanPcp1,
Ray Milkey0d338052014-11-21 16:40:12 -0800365 modVlanPcp2);
Ray Milkey78081052014-11-05 10:38:12 -0800366 }
367
368 /**
369 * Test the hashCode() method of the ModEtherInstruction class.
370 */
371
372 @Test
373 public void testModVlanPcpInstructionHashCode() {
374 assertThat(modVlanPcp1.hashCode(),
375 is(equalTo(sameAsModVlanPcp1.hashCode())));
376 assertThat(modVlanPcp1.hashCode(),
377 is(not(equalTo(modVlanPcp2.hashCode()))));
378 }
379
380 // ModIPerInstruction
381
382 private static final String IP1 = "1.2.3.4/24";
383 private static final String IP2 = "5.6.7.8/24";
384 private IpAddress ip1 = IpPrefix.valueOf(IP1).address();
385 private IpAddress ip2 = IpPrefix.valueOf(IP2).address();
386 private final Instruction modIPInstruction1 = Instructions.modL3Src(ip1);
387 private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip1);
388 private final Instruction modIPInstruction2 = Instructions.modL3Src(ip2);
389
390 /**
391 * Test the modL3Src method.
392 */
393 @Test
394 public void testModL3SrcMethod() {
395 final Instruction instruction = Instructions.modL3Src(ip1);
396 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
397 checkAndConvert(instruction,
398 Instruction.Type.L3MODIFICATION,
399 L3ModificationInstruction.ModIPInstruction.class);
400 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
401 assertThat(modIPInstruction.subtype(),
402 is(equalTo(L3ModificationInstruction.L3SubType.IP_SRC)));
403 }
404
405 /**
406 * Test the modL3Dst method.
407 */
408 @Test
409 public void testModL3DstMethod() {
410 final Instruction instruction = Instructions.modL3Dst(ip1);
411 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
412 checkAndConvert(instruction,
413 Instruction.Type.L3MODIFICATION,
414 L3ModificationInstruction.ModIPInstruction.class);
415 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
416 assertThat(modIPInstruction.subtype(),
417 is(equalTo(L3ModificationInstruction.L3SubType.IP_DST)));
418 }
419
420 /**
421 * Test the equals() method of the ModEtherInstruction class.
422 */
423
424 @Test
425 public void testModIPInstructionEquals() throws Exception {
426 checkEqualsAndToString(modIPInstruction1,
427 sameAsModIPInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800428 modIPInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800429 }
430
431 /**
432 * Test the hashCode() method of the ModEtherInstruction class.
433 */
434
435 @Test
436 public void testModIPInstructionHashCode() {
437 assertThat(modIPInstruction1.hashCode(),
438 is(equalTo(sameAsModIPInstruction1.hashCode())));
439 assertThat(modIPInstruction1.hashCode(),
440 is(not(equalTo(modIPInstruction2.hashCode()))));
441 }
442
Ray Milkey0d338052014-11-21 16:40:12 -0800443 private Instruction modMplsLabelInstruction1 = Instructions.modMplsLabel(1);
444 private Instruction sameAsModMplsLabelInstruction1 = Instructions.modMplsLabel(1);
445 private Instruction modMplsLabelInstruction2 = Instructions.modMplsLabel(2);
446
447 /**
448 * Test the modMplsLabel method.
449 */
450 @Test
451 public void testModMplsMethod() {
452 final Instruction instruction = Instructions.modMplsLabel(33);
453 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
454 checkAndConvert(instruction,
455 Instruction.Type.L2MODIFICATION,
456 L2ModificationInstruction.ModMplsLabelInstruction.class);
457 assertThat(modMplsLabelInstruction.label(), is(equalTo(33)));
458 assertThat(modMplsLabelInstruction.subtype(),
459 is(equalTo(L2ModificationInstruction.L2SubType.MPLS_LABEL)));
460 }
461
462 /**
463 * Test the equals(), hashCode and toString() methods of the
464 * ModMplsLabelInstruction class.
465 */
466
467 @Test
468 public void testModMplsLabelInstructionEquals() throws Exception {
469 checkEqualsAndToString(modMplsLabelInstruction1,
470 sameAsModMplsLabelInstruction1,
471 modMplsLabelInstruction2);
472 }
Ray Milkey78081052014-11-05 10:38:12 -0800473
474}