blob: 437dcf677df15320fc2c10242a89454c8fe5d9ae [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 */
16package org.onlab.onos.net.flow.instructions;
17
18import org.junit.Test;
19import org.onlab.onos.net.PortNumber;
20import 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;
35import static org.onlab.onos.net.PortNumber.portNumber;
36
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);
97 }
98
99 // DropInstruction
100
101 private final Instructions.DropInstruction drop1 = Instructions.createDrop();
102 private final Instructions.DropInstruction drop2 = Instructions.createDrop();
103
104 /**
105 * Test the createDrop method.
106 */
107 @Test
108 public void testCreateDropMethod() {
109 Instructions.DropInstruction instruction = Instructions.createDrop();
110 checkAndConvert(instruction,
111 Instruction.Type.DROP,
112 Instructions.DropInstruction.class);
113 }
114
115 /**
116 * Test the equals() method of the DropInstruction class.
117 */
118
119 @Test
120 public void testDropInstructionEquals() throws Exception {
121 assertThat(drop1, is(equalTo(drop2)));
122 }
123
124 /**
125 * Test the hashCode() method of the DropInstruction class.
126 */
127
128 @Test
129 public void testDropInstructionHashCode() {
130 assertThat(drop1.hashCode(), is(equalTo(drop2.hashCode())));
131 }
132
133 // OutputInstruction
134
135 private final PortNumber port1 = portNumber(1);
136 private final PortNumber port2 = portNumber(2);
137 private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1);
138 private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1);
139 private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2);
140
141 /**
142 * Test the createOutput method.
143 */
144 @Test
145 public void testCreateOutputMethod() {
146 final Instruction instruction = Instructions.createOutput(port2);
147 final Instructions.OutputInstruction outputInstruction =
148 checkAndConvert(instruction,
149 Instruction.Type.OUTPUT,
150 Instructions.OutputInstruction.class);
151 assertThat(outputInstruction.port(), is(equalTo(port2)));
152 }
153
154
155 /**
156 * Test the equals() method of the OutputInstruction class.
157 */
158
159 @Test
160 public void testOutputInstructionEquals() throws Exception {
Ray Milkey0d338052014-11-21 16:40:12 -0800161 checkEqualsAndToString(output1, sameAsOutput1, output2);
Ray Milkey78081052014-11-05 10:38:12 -0800162 }
163
164 /**
165 * Test the hashCode() method of the OutputInstruction class.
166 */
167
168 @Test
169 public void testOutputInstructionHashCode() {
170 assertThat(output1.hashCode(), is(equalTo(sameAsOutput1.hashCode())));
171 assertThat(output1.hashCode(), is(not(equalTo(output2.hashCode()))));
172 }
173
174 // ModLambdaInstruction
175
176 private final short lambda1 = 1;
177 private final short lambda2 = 2;
178 private final Instruction lambdaInstruction1 = Instructions.modL0Lambda(lambda1);
179 private final Instruction sameAsLambdaInstruction1 = Instructions.modL0Lambda(lambda1);
180 private final Instruction lambdaInstruction2 = Instructions.modL0Lambda(lambda2);
181
182 /**
183 * Test the modL0Lambda method.
184 */
185 @Test
186 public void testCreateLambdaMethod() {
187 final Instruction instruction = Instructions.modL0Lambda(lambda1);
188 final L0ModificationInstruction.ModLambdaInstruction lambdaInstruction =
189 checkAndConvert(instruction,
190 Instruction.Type.L0MODIFICATION,
191 L0ModificationInstruction.ModLambdaInstruction.class);
192 assertThat(lambdaInstruction.lambda(), is(equalTo(lambda1)));
193 }
194
195
196 /**
197 * Test the equals() method of the ModLambdaInstruction class.
198 */
199
200 @Test
201 public void testModLambdaInstructionEquals() throws Exception {
202 checkEqualsAndToString(lambdaInstruction1,
203 sameAsLambdaInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800204 lambdaInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800205 }
206
207 /**
208 * Test the hashCode() method of the ModLambdaInstruction class.
209 */
210
211 @Test
212 public void testModLambdaInstructionHashCode() {
213 assertThat(lambdaInstruction1.hashCode(),
214 is(equalTo(sameAsLambdaInstruction1.hashCode())));
215 assertThat(lambdaInstruction1.hashCode(),
216 is(not(equalTo(lambdaInstruction2.hashCode()))));
217 }
218
219 // ModEtherInstruction
220
221 private static final String MAC1 = "00:00:00:00:00:01";
222 private static final String MAC2 = "00:00:00:00:00:02";
223 private final MacAddress mac1 = MacAddress.valueOf(MAC1);
224 private final MacAddress mac2 = MacAddress.valueOf(MAC2);
225 private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1);
226 private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1);
227 private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2);
228
229 /**
230 * Test the modL2Src method.
231 */
232 @Test
233 public void testModL2SrcMethod() {
234 final Instruction instruction = Instructions.modL2Src(mac1);
235 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
236 checkAndConvert(instruction,
237 Instruction.Type.L2MODIFICATION,
238 L2ModificationInstruction.ModEtherInstruction.class);
239 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
240 assertThat(modEtherInstruction.subtype(),
241 is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC)));
242 }
243
244 /**
245 * Test the modL2Dst method.
246 */
247 @Test
248 public void testModL2DstMethod() {
249 final Instruction instruction = Instructions.modL2Dst(mac1);
250 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
251 checkAndConvert(instruction,
252 Instruction.Type.L2MODIFICATION,
253 L2ModificationInstruction.ModEtherInstruction.class);
254 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
255 assertThat(modEtherInstruction.subtype(),
256 is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST)));
257 }
258
259 /**
260 * Test the equals() method of the ModEtherInstruction class.
261 */
262
263 @Test
264 public void testModEtherInstructionEquals() throws Exception {
265 checkEqualsAndToString(modEtherInstruction1,
266 sameAsModEtherInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800267 modEtherInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800268 }
269
270 /**
271 * Test the hashCode() method of the ModEtherInstruction class.
272 */
273
274 @Test
275 public void testModEtherInstructionHashCode() {
276 assertThat(modEtherInstruction1.hashCode(),
277 is(equalTo(sameAsModEtherInstruction1.hashCode())));
278 assertThat(modEtherInstruction1.hashCode(),
279 is(not(equalTo(modEtherInstruction2.hashCode()))));
280 }
281
282
283 // ModVlanIdInstruction
284
285 private final short vlan1 = 1;
286 private final short vlan2 = 2;
287 private final VlanId vlanId1 = VlanId.vlanId(vlan1);
288 private final VlanId vlanId2 = VlanId.vlanId(vlan2);
289 private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1);
290 private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1);
291 private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2);
292
293 /**
294 * Test the modVlanId method.
295 */
296 @Test
297 public void testModVlanIdMethod() {
298 final Instruction instruction = Instructions.modVlanId(vlanId1);
299 final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction =
300 checkAndConvert(instruction,
301 Instruction.Type.L2MODIFICATION,
302 L2ModificationInstruction.ModVlanIdInstruction.class);
303 assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1)));
304 assertThat(modEtherInstruction.subtype(),
305 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID)));
306 }
307
308 /**
309 * Test the equals() method of the ModVlanIdInstruction class.
310 */
311
312 @Test
313 public void testModVlanIdInstructionEquals() throws Exception {
314 checkEqualsAndToString(modVlanId1,
315 sameAsModVlanId1,
Ray Milkey0d338052014-11-21 16:40:12 -0800316 modVlanId2);
Ray Milkey78081052014-11-05 10:38:12 -0800317 }
318
319 /**
320 * Test the hashCode() method of the ModEtherInstruction class.
321 */
322
323 @Test
324 public void testModVlanIdInstructionHashCode() {
325 assertThat(modVlanId1.hashCode(),
326 is(equalTo(sameAsModVlanId1.hashCode())));
327 assertThat(modVlanId1.hashCode(),
328 is(not(equalTo(modVlanId2.hashCode()))));
329 }
330
331
332 // ModVlanPcpInstruction
333
334 private final byte vlanPcp1 = 1;
335 private final byte vlanPcp2 = 2;
336 private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
337 private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
338 private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2);
339
340 /**
341 * Test the modVlanPcp method.
342 */
343 @Test
344 public void testModVlanPcpMethod() {
345 final Instruction instruction = Instructions.modVlanPcp(vlanPcp1);
346 final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction =
347 checkAndConvert(instruction,
348 Instruction.Type.L2MODIFICATION,
349 L2ModificationInstruction.ModVlanPcpInstruction.class);
350 assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1)));
351 assertThat(modEtherInstruction.subtype(),
352 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP)));
353 }
354
355 /**
356 * Test the equals() method of the ModVlanPcpInstruction class.
357 */
358
359 @Test
360 public void testModVlanPcpInstructionEquals() throws Exception {
361 checkEqualsAndToString(modVlanPcp1,
362 sameAsModVlanPcp1,
Ray Milkey0d338052014-11-21 16:40:12 -0800363 modVlanPcp2);
Ray Milkey78081052014-11-05 10:38:12 -0800364 }
365
366 /**
367 * Test the hashCode() method of the ModEtherInstruction class.
368 */
369
370 @Test
371 public void testModVlanPcpInstructionHashCode() {
372 assertThat(modVlanPcp1.hashCode(),
373 is(equalTo(sameAsModVlanPcp1.hashCode())));
374 assertThat(modVlanPcp1.hashCode(),
375 is(not(equalTo(modVlanPcp2.hashCode()))));
376 }
377
378 // ModIPerInstruction
379
380 private static final String IP1 = "1.2.3.4/24";
381 private static final String IP2 = "5.6.7.8/24";
382 private IpAddress ip1 = IpPrefix.valueOf(IP1).address();
383 private IpAddress ip2 = IpPrefix.valueOf(IP2).address();
384 private final Instruction modIPInstruction1 = Instructions.modL3Src(ip1);
385 private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip1);
386 private final Instruction modIPInstruction2 = Instructions.modL3Src(ip2);
387
388 /**
389 * Test the modL3Src method.
390 */
391 @Test
392 public void testModL3SrcMethod() {
393 final Instruction instruction = Instructions.modL3Src(ip1);
394 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
395 checkAndConvert(instruction,
396 Instruction.Type.L3MODIFICATION,
397 L3ModificationInstruction.ModIPInstruction.class);
398 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
399 assertThat(modIPInstruction.subtype(),
400 is(equalTo(L3ModificationInstruction.L3SubType.IP_SRC)));
401 }
402
403 /**
404 * Test the modL3Dst method.
405 */
406 @Test
407 public void testModL3DstMethod() {
408 final Instruction instruction = Instructions.modL3Dst(ip1);
409 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
410 checkAndConvert(instruction,
411 Instruction.Type.L3MODIFICATION,
412 L3ModificationInstruction.ModIPInstruction.class);
413 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
414 assertThat(modIPInstruction.subtype(),
415 is(equalTo(L3ModificationInstruction.L3SubType.IP_DST)));
416 }
417
418 /**
419 * Test the equals() method of the ModEtherInstruction class.
420 */
421
422 @Test
423 public void testModIPInstructionEquals() throws Exception {
424 checkEqualsAndToString(modIPInstruction1,
425 sameAsModIPInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800426 modIPInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800427 }
428
429 /**
430 * Test the hashCode() method of the ModEtherInstruction class.
431 */
432
433 @Test
434 public void testModIPInstructionHashCode() {
435 assertThat(modIPInstruction1.hashCode(),
436 is(equalTo(sameAsModIPInstruction1.hashCode())));
437 assertThat(modIPInstruction1.hashCode(),
438 is(not(equalTo(modIPInstruction2.hashCode()))));
439 }
440
Ray Milkey0d338052014-11-21 16:40:12 -0800441 private Instruction modMplsLabelInstruction1 = Instructions.modMplsLabel(1);
442 private Instruction sameAsModMplsLabelInstruction1 = Instructions.modMplsLabel(1);
443 private Instruction modMplsLabelInstruction2 = Instructions.modMplsLabel(2);
444
445 /**
446 * Test the modMplsLabel method.
447 */
448 @Test
449 public void testModMplsMethod() {
450 final Instruction instruction = Instructions.modMplsLabel(33);
451 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
452 checkAndConvert(instruction,
453 Instruction.Type.L2MODIFICATION,
454 L2ModificationInstruction.ModMplsLabelInstruction.class);
455 assertThat(modMplsLabelInstruction.label(), is(equalTo(33)));
456 assertThat(modMplsLabelInstruction.subtype(),
457 is(equalTo(L2ModificationInstruction.L2SubType.MPLS_LABEL)));
458 }
459
460 /**
461 * Test the equals(), hashCode and toString() methods of the
462 * ModMplsLabelInstruction class.
463 */
464
465 @Test
466 public void testModMplsLabelInstructionEquals() throws Exception {
467 checkEqualsAndToString(modMplsLabelInstruction1,
468 sameAsModMplsLabelInstruction1,
469 modMplsLabelInstruction2);
470 }
Ray Milkey78081052014-11-05 10:38:12 -0800471
472}