blob: e33e9d7f1a4ec056aa8356269040f8fdc786221b [file] [log] [blame]
Ray Milkey78081052014-11-05 10:38:12 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey78081052014-11-05 10:38:12 -08003 *
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;
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -070019import org.onosproject.net.ChannelSpacing;
20import org.onosproject.net.GridType;
Ray Milkey85571eb2015-06-25 09:45:46 -070021import org.onosproject.net.IndexedLambda;
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -070022import org.onosproject.net.Lambda;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.PortNumber;
Ray Milkey78081052014-11-05 10:38:12 -080024import org.onlab.packet.IpAddress;
Ray Milkey78081052014-11-05 10:38:12 -080025import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010026import org.onlab.packet.MplsLabel;
Ray Milkey78081052014-11-05 10:38:12 -080027import org.onlab.packet.VlanId;
28
Ray Milkey0d338052014-11-21 16:40:12 -080029import com.google.common.testing.EqualsTester;
30
Ray Milkey78081052014-11-05 10:38:12 -080031import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey78081052014-11-05 10:38:12 -080032import static org.hamcrest.Matchers.equalTo;
33import static org.hamcrest.Matchers.instanceOf;
34import static org.hamcrest.Matchers.is;
35import static org.hamcrest.Matchers.not;
36import static org.hamcrest.Matchers.notNullValue;
37import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
38import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import static org.onosproject.net.PortNumber.portNumber;
Ray Milkey78081052014-11-05 10:38:12 -080040
41/**
42 * Unit tests for the Instructions class.
43 */
44public class InstructionsTest {
45
46 /**
47 * Checks that a Criterion object has the proper type, and then converts
48 * it to the proper type.
49 *
50 * @param instruction Instruction object to convert
51 * @param type Enumerated type value for the Criterion class
52 * @param clazz Desired Criterion class
53 * @param <T> The type the caller wants returned
54 * @return converted object
55 */
56 @SuppressWarnings("unchecked")
57 private <T> T checkAndConvert(Instruction instruction, Instruction.Type type, Class clazz) {
58 assertThat(instruction, is(notNullValue()));
59 assertThat(instruction.type(), is(equalTo(type)));
60 assertThat(instruction, instanceOf(clazz));
61 return (T) instruction;
62 }
63
64 /**
65 * Checks the equals() and toString() methods of a Criterion class.
66 *
67 * @param c1 first object to compare
68 * @param c1match object that should be equal to the first
69 * @param c2 object that should be not equal to the first
Ray Milkey78081052014-11-05 10:38:12 -080070 * @param <T> type of the arguments
71 */
72 private <T extends Instruction> void checkEqualsAndToString(T c1, T c1match,
Ray Milkey0d338052014-11-21 16:40:12 -080073 T c2) {
Ray Milkey78081052014-11-05 10:38:12 -080074
Ray Milkey0d338052014-11-21 16:40:12 -080075 new EqualsTester()
76 .addEqualityGroup(c1, c1match)
77 .addEqualityGroup(c2)
78 .testEquals();
Ray Milkey78081052014-11-05 10:38:12 -080079 }
80
81 /**
82 * Checks that Instructions is a proper utility class.
83 */
84 @Test
85 public void testInstructionsUtilityClass() {
86 assertThatClassIsUtility(Instructions.class);
87 }
88
89 /**
90 * Checks that the Instruction class implementations are immutable.
91 */
92 @Test
93 public void testImmutabilityOfInstructions() {
94 assertThatClassIsImmutable(Instructions.DropInstruction.class);
95 assertThatClassIsImmutable(Instructions.OutputInstruction.class);
96 assertThatClassIsImmutable(L0ModificationInstruction.ModLambdaInstruction.class);
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -070097 assertThatClassIsImmutable(L0ModificationInstruction.ModOchSignalInstruction.class);
Ray Milkey78081052014-11-05 10:38:12 -080098 assertThatClassIsImmutable(L2ModificationInstruction.ModEtherInstruction.class);
99 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class);
100 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class);
101 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800102 assertThatClassIsImmutable(L3ModificationInstruction.ModIPv6FlowLabelInstruction.class);
Ray Milkeyd03eda02015-01-09 14:58:48 -0800103 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class);
104 assertThatClassIsImmutable(L2ModificationInstruction.PushHeaderInstructions.class);
Ray Milkey78081052014-11-05 10:38:12 -0800105 }
106
107 // DropInstruction
108
109 private final Instructions.DropInstruction drop1 = Instructions.createDrop();
110 private final Instructions.DropInstruction drop2 = Instructions.createDrop();
111
112 /**
113 * Test the createDrop method.
114 */
115 @Test
116 public void testCreateDropMethod() {
117 Instructions.DropInstruction instruction = Instructions.createDrop();
118 checkAndConvert(instruction,
119 Instruction.Type.DROP,
120 Instructions.DropInstruction.class);
121 }
122
123 /**
124 * Test the equals() method of the DropInstruction class.
125 */
126
127 @Test
128 public void testDropInstructionEquals() throws Exception {
129 assertThat(drop1, is(equalTo(drop2)));
130 }
131
132 /**
133 * Test the hashCode() method of the DropInstruction class.
134 */
135
136 @Test
137 public void testDropInstructionHashCode() {
138 assertThat(drop1.hashCode(), is(equalTo(drop2.hashCode())));
139 }
140
141 // OutputInstruction
142
143 private final PortNumber port1 = portNumber(1);
144 private final PortNumber port2 = portNumber(2);
145 private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1);
146 private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1);
147 private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2);
148
149 /**
150 * Test the createOutput method.
151 */
152 @Test
153 public void testCreateOutputMethod() {
154 final Instruction instruction = Instructions.createOutput(port2);
155 final Instructions.OutputInstruction outputInstruction =
156 checkAndConvert(instruction,
157 Instruction.Type.OUTPUT,
158 Instructions.OutputInstruction.class);
159 assertThat(outputInstruction.port(), is(equalTo(port2)));
160 }
161
162
163 /**
164 * Test the equals() method of the OutputInstruction class.
165 */
166
167 @Test
168 public void testOutputInstructionEquals() throws Exception {
Ray Milkey0d338052014-11-21 16:40:12 -0800169 checkEqualsAndToString(output1, sameAsOutput1, output2);
Ray Milkey78081052014-11-05 10:38:12 -0800170 }
171
172 /**
173 * Test the hashCode() method of the OutputInstruction class.
174 */
175
176 @Test
177 public void testOutputInstructionHashCode() {
178 assertThat(output1.hashCode(), is(equalTo(sameAsOutput1.hashCode())));
179 assertThat(output1.hashCode(), is(not(equalTo(output2.hashCode()))));
180 }
181
182 // ModLambdaInstruction
183
Ray Milkey85571eb2015-06-25 09:45:46 -0700184 private final IndexedLambda lambda1 = new IndexedLambda(1);
185 private final IndexedLambda lambda2 = new IndexedLambda(2);
Ray Milkey78081052014-11-05 10:38:12 -0800186 private final Instruction lambdaInstruction1 = Instructions.modL0Lambda(lambda1);
187 private final Instruction sameAsLambdaInstruction1 = Instructions.modL0Lambda(lambda1);
188 private final Instruction lambdaInstruction2 = Instructions.modL0Lambda(lambda2);
189
190 /**
191 * Test the modL0Lambda method.
192 */
193 @Test
194 public void testCreateLambdaMethod() {
195 final Instruction instruction = Instructions.modL0Lambda(lambda1);
196 final L0ModificationInstruction.ModLambdaInstruction lambdaInstruction =
197 checkAndConvert(instruction,
198 Instruction.Type.L0MODIFICATION,
199 L0ModificationInstruction.ModLambdaInstruction.class);
Ray Milkey85571eb2015-06-25 09:45:46 -0700200 assertThat(lambdaInstruction.lambda(), is(equalTo((short) lambda1.index())));
Ray Milkey78081052014-11-05 10:38:12 -0800201 }
202
Ray Milkey78081052014-11-05 10:38:12 -0800203 /**
204 * Test the equals() method of the ModLambdaInstruction class.
205 */
206
207 @Test
208 public void testModLambdaInstructionEquals() throws Exception {
209 checkEqualsAndToString(lambdaInstruction1,
210 sameAsLambdaInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800211 lambdaInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800212 }
213
214 /**
215 * Test the hashCode() method of the ModLambdaInstruction class.
216 */
217
218 @Test
219 public void testModLambdaInstructionHashCode() {
220 assertThat(lambdaInstruction1.hashCode(),
221 is(equalTo(sameAsLambdaInstruction1.hashCode())));
222 assertThat(lambdaInstruction1.hashCode(),
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -0700223 is(not(equalTo(lambdaInstruction2.hashCode()))));
224 }
225
226 private final Lambda och1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
227 private final Lambda och2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
228 private final Instruction ochInstruction1 = Instructions.modL0Lambda(och1);
229 private final Instruction sameAsOchInstruction1 = Instructions.modL0Lambda(och1);
230 private final Instruction ochInstruction2 = Instructions.modL0Lambda(och2);
231
232 /**
233 * Test the modL0Lambda().
234 */
235 @Test
236 public void testModL0LambdaMethod() {
237 Instruction instruction = Instructions.modL0Lambda(och1);
238 L0ModificationInstruction.ModOchSignalInstruction ochInstruction =
239 checkAndConvert(instruction, Instruction.Type.L0MODIFICATION,
240 L0ModificationInstruction.ModOchSignalInstruction.class);
241 assertThat(ochInstruction.lambda(), is(och1));
242 }
243
244 /**
245 * Test the equals() method of the ModOchSignalInstruction class.
246 */
247 @Test
248 public void testModOchSignalInstructionEquals() {
249 checkEqualsAndToString(ochInstruction1, sameAsOchInstruction1, ochInstruction2);
250 }
251
252 /**
253 * Test the hashCode() method of the ModOchSignalInstruction class.
254 */
255 @Test
256 public void testModOchSignalInstructionHashCode() {
257 assertThat(ochInstruction1.hashCode(), is(sameAsOchInstruction1.hashCode()));
258 assertThat(ochInstruction1.hashCode(), is(not(ochInstruction2.hashCode())));
Ray Milkey78081052014-11-05 10:38:12 -0800259 }
260
261 // ModEtherInstruction
262
263 private static final String MAC1 = "00:00:00:00:00:01";
264 private static final String MAC2 = "00:00:00:00:00:02";
265 private final MacAddress mac1 = MacAddress.valueOf(MAC1);
266 private final MacAddress mac2 = MacAddress.valueOf(MAC2);
267 private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1);
268 private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1);
269 private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2);
270
271 /**
272 * Test the modL2Src method.
273 */
274 @Test
275 public void testModL2SrcMethod() {
276 final Instruction instruction = Instructions.modL2Src(mac1);
277 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
278 checkAndConvert(instruction,
279 Instruction.Type.L2MODIFICATION,
280 L2ModificationInstruction.ModEtherInstruction.class);
281 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
282 assertThat(modEtherInstruction.subtype(),
283 is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC)));
284 }
285
286 /**
287 * Test the modL2Dst method.
288 */
289 @Test
290 public void testModL2DstMethod() {
291 final Instruction instruction = Instructions.modL2Dst(mac1);
292 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
293 checkAndConvert(instruction,
294 Instruction.Type.L2MODIFICATION,
295 L2ModificationInstruction.ModEtherInstruction.class);
296 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
297 assertThat(modEtherInstruction.subtype(),
298 is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST)));
299 }
300
301 /**
302 * Test the equals() method of the ModEtherInstruction class.
303 */
304
305 @Test
306 public void testModEtherInstructionEquals() throws Exception {
307 checkEqualsAndToString(modEtherInstruction1,
308 sameAsModEtherInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800309 modEtherInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800310 }
311
312 /**
313 * Test the hashCode() method of the ModEtherInstruction class.
314 */
315
316 @Test
317 public void testModEtherInstructionHashCode() {
318 assertThat(modEtherInstruction1.hashCode(),
319 is(equalTo(sameAsModEtherInstruction1.hashCode())));
320 assertThat(modEtherInstruction1.hashCode(),
321 is(not(equalTo(modEtherInstruction2.hashCode()))));
322 }
323
324
325 // ModVlanIdInstruction
326
327 private final short vlan1 = 1;
328 private final short vlan2 = 2;
329 private final VlanId vlanId1 = VlanId.vlanId(vlan1);
330 private final VlanId vlanId2 = VlanId.vlanId(vlan2);
331 private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1);
332 private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1);
333 private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2);
334
335 /**
336 * Test the modVlanId method.
337 */
338 @Test
339 public void testModVlanIdMethod() {
340 final Instruction instruction = Instructions.modVlanId(vlanId1);
341 final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction =
342 checkAndConvert(instruction,
343 Instruction.Type.L2MODIFICATION,
344 L2ModificationInstruction.ModVlanIdInstruction.class);
345 assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1)));
346 assertThat(modEtherInstruction.subtype(),
347 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID)));
348 }
349
350 /**
351 * Test the equals() method of the ModVlanIdInstruction class.
352 */
353
354 @Test
355 public void testModVlanIdInstructionEquals() throws Exception {
356 checkEqualsAndToString(modVlanId1,
357 sameAsModVlanId1,
Ray Milkey0d338052014-11-21 16:40:12 -0800358 modVlanId2);
Ray Milkey78081052014-11-05 10:38:12 -0800359 }
360
361 /**
362 * Test the hashCode() method of the ModEtherInstruction class.
363 */
364
365 @Test
366 public void testModVlanIdInstructionHashCode() {
367 assertThat(modVlanId1.hashCode(),
368 is(equalTo(sameAsModVlanId1.hashCode())));
369 assertThat(modVlanId1.hashCode(),
370 is(not(equalTo(modVlanId2.hashCode()))));
371 }
372
373
374 // ModVlanPcpInstruction
375
376 private final byte vlanPcp1 = 1;
377 private final byte vlanPcp2 = 2;
378 private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
379 private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
380 private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2);
381
382 /**
383 * Test the modVlanPcp method.
384 */
385 @Test
386 public void testModVlanPcpMethod() {
387 final Instruction instruction = Instructions.modVlanPcp(vlanPcp1);
388 final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction =
389 checkAndConvert(instruction,
390 Instruction.Type.L2MODIFICATION,
391 L2ModificationInstruction.ModVlanPcpInstruction.class);
392 assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1)));
393 assertThat(modEtherInstruction.subtype(),
394 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP)));
395 }
396
397 /**
398 * Test the equals() method of the ModVlanPcpInstruction class.
399 */
400
401 @Test
402 public void testModVlanPcpInstructionEquals() throws Exception {
403 checkEqualsAndToString(modVlanPcp1,
404 sameAsModVlanPcp1,
Ray Milkey0d338052014-11-21 16:40:12 -0800405 modVlanPcp2);
Ray Milkey78081052014-11-05 10:38:12 -0800406 }
407
408 /**
409 * Test the hashCode() method of the ModEtherInstruction class.
410 */
411
412 @Test
413 public void testModVlanPcpInstructionHashCode() {
414 assertThat(modVlanPcp1.hashCode(),
415 is(equalTo(sameAsModVlanPcp1.hashCode())));
416 assertThat(modVlanPcp1.hashCode(),
417 is(not(equalTo(modVlanPcp2.hashCode()))));
418 }
419
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800420 // ModIPInstruction
Ray Milkey78081052014-11-05 10:38:12 -0800421
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800422 private static final String IP41 = "1.2.3.4";
423 private static final String IP42 = "5.6.7.8";
424 private IpAddress ip41 = IpAddress.valueOf(IP41);
425 private IpAddress ip42 = IpAddress.valueOf(IP42);
426 private final Instruction modIPInstruction1 = Instructions.modL3Src(ip41);
427 private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip41);
428 private final Instruction modIPInstruction2 = Instructions.modL3Src(ip42);
429
430 private static final String IP61 = "1111::2222";
431 private static final String IP62 = "3333::4444";
432 private IpAddress ip61 = IpAddress.valueOf(IP61);
433 private IpAddress ip62 = IpAddress.valueOf(IP62);
434 private final Instruction modIPv6Instruction1 =
435 Instructions.modL3IPv6Src(ip61);
436 private final Instruction sameAsModIPv6Instruction1 =
437 Instructions.modL3IPv6Src(ip61);
438 private final Instruction modIPv6Instruction2 =
439 Instructions.modL3IPv6Src(ip62);
Ray Milkey78081052014-11-05 10:38:12 -0800440
441 /**
442 * Test the modL3Src method.
443 */
444 @Test
445 public void testModL3SrcMethod() {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800446 final Instruction instruction = Instructions.modL3Src(ip41);
Ray Milkey78081052014-11-05 10:38:12 -0800447 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
448 checkAndConvert(instruction,
449 Instruction.Type.L3MODIFICATION,
450 L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800451 assertThat(modIPInstruction.ip(), is(equalTo(ip41)));
Ray Milkey78081052014-11-05 10:38:12 -0800452 assertThat(modIPInstruction.subtype(),
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800453 is(equalTo(L3ModificationInstruction.L3SubType.IPV4_SRC)));
Ray Milkey78081052014-11-05 10:38:12 -0800454 }
455
456 /**
457 * Test the modL3Dst method.
458 */
459 @Test
460 public void testModL3DstMethod() {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800461 final Instruction instruction = Instructions.modL3Dst(ip41);
Ray Milkey78081052014-11-05 10:38:12 -0800462 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
463 checkAndConvert(instruction,
464 Instruction.Type.L3MODIFICATION,
465 L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800466 assertThat(modIPInstruction.ip(), is(equalTo(ip41)));
Ray Milkey78081052014-11-05 10:38:12 -0800467 assertThat(modIPInstruction.subtype(),
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800468 is(equalTo(L3ModificationInstruction.L3SubType.IPV4_DST)));
Ray Milkey78081052014-11-05 10:38:12 -0800469 }
470
471 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800472 * Test the modL3IPv6Src method.
Ray Milkey78081052014-11-05 10:38:12 -0800473 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800474 @Test
475 public void testModL3IPv6SrcMethod() {
476 final Instruction instruction = Instructions.modL3IPv6Src(ip61);
477 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
478 checkAndConvert(instruction,
479 Instruction.Type.L3MODIFICATION,
480 L3ModificationInstruction.ModIPInstruction.class);
481 assertThat(modIPInstruction.ip(), is(equalTo(ip61)));
482 assertThat(modIPInstruction.subtype(),
483 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_SRC)));
484 }
Ray Milkey78081052014-11-05 10:38:12 -0800485
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800486 /**
487 * Test the modL3IPv6Dst method.
488 */
489 @Test
490 public void testModL3IPv6DstMethod() {
491 final Instruction instruction = Instructions.modL3IPv6Dst(ip61);
492 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
493 checkAndConvert(instruction,
494 Instruction.Type.L3MODIFICATION,
495 L3ModificationInstruction.ModIPInstruction.class);
496 assertThat(modIPInstruction.ip(), is(equalTo(ip61)));
497 assertThat(modIPInstruction.subtype(),
498 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_DST)));
499 }
500
501 /**
502 * Test the equals() method of the ModIPInstruction class.
503 */
Ray Milkey78081052014-11-05 10:38:12 -0800504 @Test
505 public void testModIPInstructionEquals() throws Exception {
506 checkEqualsAndToString(modIPInstruction1,
507 sameAsModIPInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800508 modIPInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800509 }
510
511 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800512 * Test the hashCode() method of the ModIPInstruction class.
Ray Milkey78081052014-11-05 10:38:12 -0800513 */
Ray Milkey78081052014-11-05 10:38:12 -0800514 @Test
515 public void testModIPInstructionHashCode() {
516 assertThat(modIPInstruction1.hashCode(),
517 is(equalTo(sameAsModIPInstruction1.hashCode())));
518 assertThat(modIPInstruction1.hashCode(),
519 is(not(equalTo(modIPInstruction2.hashCode()))));
520 }
521
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800522 private final int flowLabel1 = 0x11111;
523 private final int flowLabel2 = 0x22222;
524 private final Instruction modIPv6FlowLabelInstruction1 =
525 Instructions.modL3IPv6FlowLabel(flowLabel1);
526 private final Instruction sameAsModIPv6FlowLabelInstruction1 =
527 Instructions.modL3IPv6FlowLabel(flowLabel1);
528 private final Instruction modIPv6FlowLabelInstruction2 =
529 Instructions.modL3IPv6FlowLabel(flowLabel2);
530
531 /**
532 * Test the modL3IPv6FlowLabel method.
533 */
534 @Test
535 public void testModL3IPv6FlowLabelMethod() {
536 final Instruction instruction =
537 Instructions.modL3IPv6FlowLabel(flowLabel1);
538 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
539 modIPv6FlowLabelInstruction =
540 checkAndConvert(instruction,
541 Instruction.Type.L3MODIFICATION,
542 L3ModificationInstruction.ModIPv6FlowLabelInstruction.class);
543 assertThat(modIPv6FlowLabelInstruction.flowLabel(),
544 is(equalTo(flowLabel1)));
545 assertThat(modIPv6FlowLabelInstruction.subtype(),
546 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_FLABEL)));
547 }
548
549 /**
550 * Test the equals() method of the ModIPv6FlowLabelInstruction class.
551 */
552 @Test
553 public void testModIPv6FlowLabelInstructionEquals() throws Exception {
554 checkEqualsAndToString(modIPv6FlowLabelInstruction1,
555 sameAsModIPv6FlowLabelInstruction1,
556 modIPv6FlowLabelInstruction2);
557 }
558
559 /**
560 * Test the hashCode() method of the ModIPv6FlowLabelInstruction class.
561 */
562 @Test
563 public void testModIPv6FlowLabelInstructionHashCode() {
564 assertThat(modIPv6FlowLabelInstruction1.hashCode(),
565 is(equalTo(sameAsModIPv6FlowLabelInstruction1.hashCode())));
566 assertThat(modIPv6FlowLabelInstruction1.hashCode(),
567 is(not(equalTo(modIPv6FlowLabelInstruction2.hashCode()))));
568 }
569
Michele Santuari4b6019e2014-12-19 11:31:45 +0100570 private Instruction modMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1));
571 private Instruction sameAsModMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1));
572 private Instruction modMplsLabelInstruction2 = Instructions.modMplsLabel(MplsLabel.mplsLabel(2));
Ray Milkey0d338052014-11-21 16:40:12 -0800573
574 /**
575 * Test the modMplsLabel method.
576 */
577 @Test
578 public void testModMplsMethod() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100579 final Instruction instruction = Instructions.modMplsLabel(MplsLabel.mplsLabel(33));
Ray Milkey0d338052014-11-21 16:40:12 -0800580 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
581 checkAndConvert(instruction,
582 Instruction.Type.L2MODIFICATION,
583 L2ModificationInstruction.ModMplsLabelInstruction.class);
584 assertThat(modMplsLabelInstruction.label(), is(equalTo(33)));
585 assertThat(modMplsLabelInstruction.subtype(),
586 is(equalTo(L2ModificationInstruction.L2SubType.MPLS_LABEL)));
587 }
588
589 /**
590 * Test the equals(), hashCode and toString() methods of the
591 * ModMplsLabelInstruction class.
592 */
Ray Milkey0d338052014-11-21 16:40:12 -0800593 @Test
594 public void testModMplsLabelInstructionEquals() throws Exception {
595 checkEqualsAndToString(modMplsLabelInstruction1,
596 sameAsModMplsLabelInstruction1,
597 modMplsLabelInstruction2);
598 }
Ray Milkey78081052014-11-05 10:38:12 -0800599
Hyunsun Moond0533e52015-07-28 18:12:43 -0700600 // ModTunnelIdInstruction
601
602 private final long tunnelId1 = 1L;
603 private final long tunnelId2 = 2L;
604 private final Instruction modTunnelId1 = Instructions.modTunnelId(tunnelId1);
605 private final Instruction sameAsModTunnelId1 = Instructions.modTunnelId(tunnelId1);
606 private final Instruction modTunnelId2 = Instructions.modTunnelId(tunnelId2);
607
608 /**
609 * Test the modTunnelId method.
610 */
611 @Test
612 public void testModTunnelIdMethod() {
613 final Instruction instruction = Instructions.modTunnelId(tunnelId1);
614 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
615 checkAndConvert(instruction, Instruction.Type.L2MODIFICATION,
616 L2ModificationInstruction.ModTunnelIdInstruction.class);
617 assertThat(modTunnelIdInstruction.tunnelId(), is(equalTo(tunnelId1)));
618 assertThat(modTunnelIdInstruction.subtype(),
619 is(equalTo(L2ModificationInstruction.L2SubType.TUNNEL_ID)));
620 }
621
622 /***
623 * Test the equals() method of the ModTunnelIdInstruction class.
624 */
625 @Test
626 public void testModTunnelIdInstructionEquals() throws Exception {
627 checkEqualsAndToString(modTunnelId1, sameAsModTunnelId1, modTunnelId2);
628 }
629
630 /**
631 * Test the hashCode() method of the ModTunnelIdInstruction class.
632 */
633 @Test
634 public void testModTunnelIdInstructionHashCode() {
635 assertThat(modTunnelId1.hashCode(), is(equalTo(sameAsModTunnelId1.hashCode())));
636 assertThat(modTunnelId1.hashCode(), is(not(equalTo(modTunnelId2.hashCode()))));
637 }
638
639 // ModTransportPortInstruction
640
641 private final short l4port1 = 1;
642 private final short l4port2 = 2;
643 private final Instruction modTransportPortInstruction1 = Instructions.modTcpSrc(l4port1);
644 private final Instruction sameAsModTransportPortInstruction1 = Instructions.modTcpSrc(l4port1);
645 private final Instruction modTransportPortInstruction2 = Instructions.modTcpSrc(l4port2);
646
647 /**
648 * Test the modTcpSrc() method.
649 */
650 @Test
651 public void testModTcpSrcMethod() {
652 final Instruction instruction = Instructions.modTcpSrc(l4port1);
653 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
654 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
655 L4ModificationInstruction.ModTransportPortInstruction.class);
656 assertThat(modTransportPortInstruction.port(), is(equalTo(l4port1)));
657 assertThat(modTransportPortInstruction.subtype(),
658 is(equalTo(L4ModificationInstruction.L4SubType.TCP_SRC)));
659 }
660
661 /**
662 * Test the modTcpDst() method.
663 */
664 @Test
665 public void testModTcpDstMethod() {
666 final Instruction instruction = Instructions.modTcpDst(l4port1);
667 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
668 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
669 L4ModificationInstruction.ModTransportPortInstruction.class);
670 assertThat(modTransportPortInstruction.port(), is(equalTo(l4port1)));
671 assertThat(modTransportPortInstruction.subtype(),
672 is(equalTo(L4ModificationInstruction.L4SubType.TCP_DST)));
673 }
674
675 /**
676 * Test the modUdpSrc() method.
677 */
678 @Test
679 public void testModUdpSrcMethod() {
680 final Instruction instruction = Instructions.modUdpSrc(l4port1);
681 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
682 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
683 L4ModificationInstruction.ModTransportPortInstruction.class);
684 assertThat(modTransportPortInstruction.port(), is(equalTo(l4port1)));
685 assertThat(modTransportPortInstruction.subtype(),
686 is(equalTo(L4ModificationInstruction.L4SubType.UDP_SRC)));
687 }
688
689 /**
690 * Test the modUdpDst() method.
691 */
692 @Test
693 public void testModUdpDstMethod() {
694 final Instruction instruction = Instructions.modUdpDst(l4port1);
695 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
696 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
697 L4ModificationInstruction.ModTransportPortInstruction.class);
698 assertThat(modTransportPortInstruction.port(), is(equalTo(l4port1)));
699 assertThat(modTransportPortInstruction.subtype(),
700 is(equalTo(L4ModificationInstruction.L4SubType.UDP_DST)));
701 }
702
703 /**
704 * Test the equals() method of the ModTransportPortInstruction class.
705 */
706 @Test
707 public void testModTransportPortInstructionEquals() throws Exception {
708 checkEqualsAndToString(modTransportPortInstruction1,
709 sameAsModTransportPortInstruction1,
710 modTransportPortInstruction2);
711 }
712
713 /**
714 * Test the hashCode() method of the ModTransportPortInstruction class.
715 */
716 @Test
717 public void testModTransportPortInstructionHashCode() {
718 assertThat(modTransportPortInstruction1.hashCode(),
719 is(equalTo(sameAsModTransportPortInstruction1.hashCode())));
720 assertThat(modTransportPortInstruction1.hashCode(),
721 is(not(equalTo(modTransportPortInstruction2.hashCode()))));
722 }
Ray Milkey78081052014-11-05 10:38:12 -0800723}