blob: b43393b9b58914b6b06e20de2adb6449231bd5b5 [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
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.containsString;
27import static org.hamcrest.Matchers.equalTo;
28import static org.hamcrest.Matchers.instanceOf;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.not;
31import static org.hamcrest.Matchers.notNullValue;
32import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
33import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
34import static org.onlab.onos.net.PortNumber.portNumber;
35
36/**
37 * Unit tests for the Instructions class.
38 */
39public class InstructionsTest {
40
41 /**
42 * Checks that a Criterion object has the proper type, and then converts
43 * it to the proper type.
44 *
45 * @param instruction Instruction object to convert
46 * @param type Enumerated type value for the Criterion class
47 * @param clazz Desired Criterion class
48 * @param <T> The type the caller wants returned
49 * @return converted object
50 */
51 @SuppressWarnings("unchecked")
52 private <T> T checkAndConvert(Instruction instruction, Instruction.Type type, Class clazz) {
53 assertThat(instruction, is(notNullValue()));
54 assertThat(instruction.type(), is(equalTo(type)));
55 assertThat(instruction, instanceOf(clazz));
56 return (T) instruction;
57 }
58
59 /**
60 * Checks the equals() and toString() methods of a Criterion class.
61 *
62 * @param c1 first object to compare
63 * @param c1match object that should be equal to the first
64 * @param c2 object that should be not equal to the first
65 * @param clazz Class object for the Criterion subclass
66 * @param <T> type of the arguments
67 */
68 private <T extends Instruction> void checkEqualsAndToString(T c1, T c1match,
69 T c2, Class clazz) {
70 assertThat(c1, instanceOf(clazz));
71 assertThat(c1match, instanceOf(clazz));
72 assertThat(c2, instanceOf(clazz));
73
74 assertThat(c1, is(equalTo(c1match)));
75 assertThat(c1, is(not(equalTo(c2))));
76 assertThat(c1, is(not(equalTo(new Object()))));
77
78 // Make sure the toString() output is unique and correct.
79 assertThat(c1.toString(), containsString("{"));
80 assertThat(c1.toString(), equalTo(c1match.toString()));
81 assertThat(c1.toString(), not(equalTo(c2.toString())));
82 }
83
84 /**
85 * Checks that Instructions is a proper utility class.
86 */
87 @Test
88 public void testInstructionsUtilityClass() {
89 assertThatClassIsUtility(Instructions.class);
90 }
91
92 /**
93 * Checks that the Instruction class implementations are immutable.
94 */
95 @Test
96 public void testImmutabilityOfInstructions() {
97 assertThatClassIsImmutable(Instructions.DropInstruction.class);
98 assertThatClassIsImmutable(Instructions.OutputInstruction.class);
99 assertThatClassIsImmutable(L0ModificationInstruction.ModLambdaInstruction.class);
100 assertThatClassIsImmutable(L2ModificationInstruction.ModEtherInstruction.class);
101 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class);
102 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class);
103 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class);
104 }
105
106 // DropInstruction
107
108 private final Instructions.DropInstruction drop1 = Instructions.createDrop();
109 private final Instructions.DropInstruction drop2 = Instructions.createDrop();
110
111 /**
112 * Test the createDrop method.
113 */
114 @Test
115 public void testCreateDropMethod() {
116 Instructions.DropInstruction instruction = Instructions.createDrop();
117 checkAndConvert(instruction,
118 Instruction.Type.DROP,
119 Instructions.DropInstruction.class);
120 }
121
122 /**
123 * Test the equals() method of the DropInstruction class.
124 */
125
126 @Test
127 public void testDropInstructionEquals() throws Exception {
128 assertThat(drop1, is(equalTo(drop2)));
129 }
130
131 /**
132 * Test the hashCode() method of the DropInstruction class.
133 */
134
135 @Test
136 public void testDropInstructionHashCode() {
137 assertThat(drop1.hashCode(), is(equalTo(drop2.hashCode())));
138 }
139
140 // OutputInstruction
141
142 private final PortNumber port1 = portNumber(1);
143 private final PortNumber port2 = portNumber(2);
144 private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1);
145 private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1);
146 private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2);
147
148 /**
149 * Test the createOutput method.
150 */
151 @Test
152 public void testCreateOutputMethod() {
153 final Instruction instruction = Instructions.createOutput(port2);
154 final Instructions.OutputInstruction outputInstruction =
155 checkAndConvert(instruction,
156 Instruction.Type.OUTPUT,
157 Instructions.OutputInstruction.class);
158 assertThat(outputInstruction.port(), is(equalTo(port2)));
159 }
160
161
162 /**
163 * Test the equals() method of the OutputInstruction class.
164 */
165
166 @Test
167 public void testOutputInstructionEquals() throws Exception {
168 checkEqualsAndToString(output1, sameAsOutput1, output2,
169 Instructions.OutputInstruction.class);
170 }
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
184 private final short lambda1 = 1;
185 private final short lambda2 = 2;
186 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);
200 assertThat(lambdaInstruction.lambda(), is(equalTo(lambda1)));
201 }
202
203
204 /**
205 * Test the equals() method of the ModLambdaInstruction class.
206 */
207
208 @Test
209 public void testModLambdaInstructionEquals() throws Exception {
210 checkEqualsAndToString(lambdaInstruction1,
211 sameAsLambdaInstruction1,
212 lambdaInstruction2,
213 L0ModificationInstruction.ModLambdaInstruction.class);
214 }
215
216 /**
217 * Test the hashCode() method of the ModLambdaInstruction class.
218 */
219
220 @Test
221 public void testModLambdaInstructionHashCode() {
222 assertThat(lambdaInstruction1.hashCode(),
223 is(equalTo(sameAsLambdaInstruction1.hashCode())));
224 assertThat(lambdaInstruction1.hashCode(),
225 is(not(equalTo(lambdaInstruction2.hashCode()))));
226 }
227
228 // ModEtherInstruction
229
230 private static final String MAC1 = "00:00:00:00:00:01";
231 private static final String MAC2 = "00:00:00:00:00:02";
232 private final MacAddress mac1 = MacAddress.valueOf(MAC1);
233 private final MacAddress mac2 = MacAddress.valueOf(MAC2);
234 private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1);
235 private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1);
236 private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2);
237
238 /**
239 * Test the modL2Src method.
240 */
241 @Test
242 public void testModL2SrcMethod() {
243 final Instruction instruction = Instructions.modL2Src(mac1);
244 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
245 checkAndConvert(instruction,
246 Instruction.Type.L2MODIFICATION,
247 L2ModificationInstruction.ModEtherInstruction.class);
248 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
249 assertThat(modEtherInstruction.subtype(),
250 is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC)));
251 }
252
253 /**
254 * Test the modL2Dst method.
255 */
256 @Test
257 public void testModL2DstMethod() {
258 final Instruction instruction = Instructions.modL2Dst(mac1);
259 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
260 checkAndConvert(instruction,
261 Instruction.Type.L2MODIFICATION,
262 L2ModificationInstruction.ModEtherInstruction.class);
263 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
264 assertThat(modEtherInstruction.subtype(),
265 is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST)));
266 }
267
268 /**
269 * Test the equals() method of the ModEtherInstruction class.
270 */
271
272 @Test
273 public void testModEtherInstructionEquals() throws Exception {
274 checkEqualsAndToString(modEtherInstruction1,
275 sameAsModEtherInstruction1,
276 modEtherInstruction2,
277 L2ModificationInstruction.ModEtherInstruction.class);
278 }
279
280 /**
281 * Test the hashCode() method of the ModEtherInstruction class.
282 */
283
284 @Test
285 public void testModEtherInstructionHashCode() {
286 assertThat(modEtherInstruction1.hashCode(),
287 is(equalTo(sameAsModEtherInstruction1.hashCode())));
288 assertThat(modEtherInstruction1.hashCode(),
289 is(not(equalTo(modEtherInstruction2.hashCode()))));
290 }
291
292
293 // ModVlanIdInstruction
294
295 private final short vlan1 = 1;
296 private final short vlan2 = 2;
297 private final VlanId vlanId1 = VlanId.vlanId(vlan1);
298 private final VlanId vlanId2 = VlanId.vlanId(vlan2);
299 private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1);
300 private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1);
301 private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2);
302
303 /**
304 * Test the modVlanId method.
305 */
306 @Test
307 public void testModVlanIdMethod() {
308 final Instruction instruction = Instructions.modVlanId(vlanId1);
309 final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction =
310 checkAndConvert(instruction,
311 Instruction.Type.L2MODIFICATION,
312 L2ModificationInstruction.ModVlanIdInstruction.class);
313 assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1)));
314 assertThat(modEtherInstruction.subtype(),
315 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID)));
316 }
317
318 /**
319 * Test the equals() method of the ModVlanIdInstruction class.
320 */
321
322 @Test
323 public void testModVlanIdInstructionEquals() throws Exception {
324 checkEqualsAndToString(modVlanId1,
325 sameAsModVlanId1,
326 modVlanId2,
327 L2ModificationInstruction.ModVlanIdInstruction.class);
328 }
329
330 /**
331 * Test the hashCode() method of the ModEtherInstruction class.
332 */
333
334 @Test
335 public void testModVlanIdInstructionHashCode() {
336 assertThat(modVlanId1.hashCode(),
337 is(equalTo(sameAsModVlanId1.hashCode())));
338 assertThat(modVlanId1.hashCode(),
339 is(not(equalTo(modVlanId2.hashCode()))));
340 }
341
342
343 // ModVlanPcpInstruction
344
345 private final byte vlanPcp1 = 1;
346 private final byte vlanPcp2 = 2;
347 private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
348 private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
349 private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2);
350
351 /**
352 * Test the modVlanPcp method.
353 */
354 @Test
355 public void testModVlanPcpMethod() {
356 final Instruction instruction = Instructions.modVlanPcp(vlanPcp1);
357 final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction =
358 checkAndConvert(instruction,
359 Instruction.Type.L2MODIFICATION,
360 L2ModificationInstruction.ModVlanPcpInstruction.class);
361 assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1)));
362 assertThat(modEtherInstruction.subtype(),
363 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP)));
364 }
365
366 /**
367 * Test the equals() method of the ModVlanPcpInstruction class.
368 */
369
370 @Test
371 public void testModVlanPcpInstructionEquals() throws Exception {
372 checkEqualsAndToString(modVlanPcp1,
373 sameAsModVlanPcp1,
374 modVlanPcp2,
375 L2ModificationInstruction.ModVlanPcpInstruction.class);
376 }
377
378 /**
379 * Test the hashCode() method of the ModEtherInstruction class.
380 */
381
382 @Test
383 public void testModVlanPcpInstructionHashCode() {
384 assertThat(modVlanPcp1.hashCode(),
385 is(equalTo(sameAsModVlanPcp1.hashCode())));
386 assertThat(modVlanPcp1.hashCode(),
387 is(not(equalTo(modVlanPcp2.hashCode()))));
388 }
389
390 // ModIPerInstruction
391
392 private static final String IP1 = "1.2.3.4/24";
393 private static final String IP2 = "5.6.7.8/24";
394 private IpAddress ip1 = IpPrefix.valueOf(IP1).address();
395 private IpAddress ip2 = IpPrefix.valueOf(IP2).address();
396 private final Instruction modIPInstruction1 = Instructions.modL3Src(ip1);
397 private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip1);
398 private final Instruction modIPInstruction2 = Instructions.modL3Src(ip2);
399
400 /**
401 * Test the modL3Src method.
402 */
403 @Test
404 public void testModL3SrcMethod() {
405 final Instruction instruction = Instructions.modL3Src(ip1);
406 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
407 checkAndConvert(instruction,
408 Instruction.Type.L3MODIFICATION,
409 L3ModificationInstruction.ModIPInstruction.class);
410 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
411 assertThat(modIPInstruction.subtype(),
412 is(equalTo(L3ModificationInstruction.L3SubType.IP_SRC)));
413 }
414
415 /**
416 * Test the modL3Dst method.
417 */
418 @Test
419 public void testModL3DstMethod() {
420 final Instruction instruction = Instructions.modL3Dst(ip1);
421 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
422 checkAndConvert(instruction,
423 Instruction.Type.L3MODIFICATION,
424 L3ModificationInstruction.ModIPInstruction.class);
425 assertThat(modIPInstruction.ip(), is(equalTo(ip1)));
426 assertThat(modIPInstruction.subtype(),
427 is(equalTo(L3ModificationInstruction.L3SubType.IP_DST)));
428 }
429
430 /**
431 * Test the equals() method of the ModEtherInstruction class.
432 */
433
434 @Test
435 public void testModIPInstructionEquals() throws Exception {
436 checkEqualsAndToString(modIPInstruction1,
437 sameAsModIPInstruction1,
438 modIPInstruction2,
439 L3ModificationInstruction.ModIPInstruction.class);
440 }
441
442 /**
443 * Test the hashCode() method of the ModEtherInstruction class.
444 */
445
446 @Test
447 public void testModIPInstructionHashCode() {
448 assertThat(modIPInstruction1.hashCode(),
449 is(equalTo(sameAsModIPInstruction1.hashCode())));
450 assertThat(modIPInstruction1.hashCode(),
451 is(not(equalTo(modIPInstruction2.hashCode()))));
452 }
453
454
455}