blob: 68dc4899b4e38df4d8e1d1168557c650db996aaf [file] [log] [blame]
Ray Milkey78081052014-11-05 10:38:12 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Jian Li11260a02016-05-19 13:07:22 -070018import com.google.common.testing.EqualsTester;
Ray Milkey78081052014-11-05 10:38:12 -080019import org.junit.Test;
Ray Milkey3564a8c2015-12-14 11:43:12 -080020import org.onlab.packet.EthType;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.MplsLabel;
24import org.onlab.packet.TpPort;
25import org.onlab.packet.VlanId;
Frank Wange33e4ed2017-06-28 10:01:07 +080026import org.onlab.util.ImmutableByteSequence;
Ray Milkey3564a8c2015-12-14 11:43:12 -080027import org.onosproject.core.GroupId;
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -070028import org.onosproject.net.ChannelSpacing;
Ray Milkey3564a8c2015-12-14 11:43:12 -080029import org.onosproject.net.DeviceId;
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -070030import org.onosproject.net.GridType;
31import org.onosproject.net.Lambda;
Yafit Hadar52d81552015-10-07 12:26:52 +030032import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.PortNumber;
Cem Türker3baff672017-10-12 15:09:01 +030034import org.onosproject.net.flow.StatTriggerField;
35import org.onosproject.net.flow.StatTriggerFlag;
Ray Milkey3564a8c2015-12-14 11:43:12 -080036import org.onosproject.net.meter.MeterId;
Carmelo Cascone87892e22017-11-13 16:01:29 -080037import org.onosproject.net.pi.model.PiActionId;
38import org.onosproject.net.pi.model.PiActionParamId;
Frank Wange33e4ed2017-06-28 10:01:07 +080039import org.onosproject.net.pi.runtime.PiAction;
Frank Wange33e4ed2017-06-28 10:01:07 +080040import org.onosproject.net.pi.runtime.PiActionParam;
Frank Wange33e4ed2017-06-28 10:01:07 +080041import org.onosproject.net.pi.runtime.PiTableAction;
Ray Milkey78081052014-11-05 10:38:12 -080042
Cem Türker3baff672017-10-12 15:09:01 +030043import java.util.EnumMap;
Jian Li11260a02016-05-19 13:07:22 -070044import java.util.List;
Cem Türker3baff672017-10-12 15:09:01 +030045import java.util.Map;
Ray Milkey0d338052014-11-21 16:40:12 -080046
Ray Milkey78081052014-11-05 10:38:12 -080047import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey78081052014-11-05 10:38:12 -080048import static org.hamcrest.Matchers.equalTo;
49import static org.hamcrest.Matchers.instanceOf;
50import static org.hamcrest.Matchers.is;
51import static org.hamcrest.Matchers.not;
52import static org.hamcrest.Matchers.notNullValue;
53import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
54import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
Yafit Hadar52d81552015-10-07 12:26:52 +030055import static org.onosproject.net.OduSignalId.oduSignalId;
Ray Milkey3564a8c2015-12-14 11:43:12 -080056import static org.onosproject.net.PortNumber.portNumber;
Ray Milkey78081052014-11-05 10:38:12 -080057
58/**
59 * Unit tests for the Instructions class.
60 */
61public class InstructionsTest {
62
63 /**
Ray Milkey3564a8c2015-12-14 11:43:12 -080064 * Checks that an Instruction object has the proper type, and then converts
Ray Milkey78081052014-11-05 10:38:12 -080065 * it to the proper type.
66 *
67 * @param instruction Instruction object to convert
68 * @param type Enumerated type value for the Criterion class
69 * @param clazz Desired Criterion class
70 * @param <T> The type the caller wants returned
71 * @return converted object
72 */
73 @SuppressWarnings("unchecked")
74 private <T> T checkAndConvert(Instruction instruction, Instruction.Type type, Class clazz) {
75 assertThat(instruction, is(notNullValue()));
76 assertThat(instruction.type(), is(equalTo(type)));
77 assertThat(instruction, instanceOf(clazz));
78 return (T) instruction;
79 }
80
81 /**
Ray Milkey3564a8c2015-12-14 11:43:12 -080082 * Checks the equals() and toString() methods of a Instruction class.
Ray Milkey78081052014-11-05 10:38:12 -080083 *
84 * @param c1 first object to compare
85 * @param c1match object that should be equal to the first
86 * @param c2 object that should be not equal to the first
Ray Milkey78081052014-11-05 10:38:12 -080087 * @param <T> type of the arguments
88 */
89 private <T extends Instruction> void checkEqualsAndToString(T c1, T c1match,
Ray Milkey0d338052014-11-21 16:40:12 -080090 T c2) {
Ray Milkey78081052014-11-05 10:38:12 -080091
Ray Milkey0d338052014-11-21 16:40:12 -080092 new EqualsTester()
93 .addEqualityGroup(c1, c1match)
94 .addEqualityGroup(c2)
95 .testEquals();
Ray Milkey78081052014-11-05 10:38:12 -080096 }
97
98 /**
99 * Checks that Instructions is a proper utility class.
100 */
101 @Test
102 public void testInstructionsUtilityClass() {
103 assertThatClassIsUtility(Instructions.class);
104 }
105
106 /**
107 * Checks that the Instruction class implementations are immutable.
108 */
109 @Test
110 public void testImmutabilityOfInstructions() {
Ray Milkey78081052014-11-05 10:38:12 -0800111 assertThatClassIsImmutable(Instructions.OutputInstruction.class);
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -0700112 assertThatClassIsImmutable(L0ModificationInstruction.ModOchSignalInstruction.class);
Yafit Hadar52d81552015-10-07 12:26:52 +0300113 assertThatClassIsImmutable(L1ModificationInstruction.ModOduSignalIdInstruction.class);
Ray Milkey78081052014-11-05 10:38:12 -0800114 assertThatClassIsImmutable(L2ModificationInstruction.ModEtherInstruction.class);
115 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class);
116 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class);
117 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800118 assertThatClassIsImmutable(L3ModificationInstruction.ModIPv6FlowLabelInstruction.class);
Ray Milkeyd03eda02015-01-09 14:58:48 -0800119 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class);
Jian Li11260a02016-05-19 13:07:22 -0700120 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -0800121 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsBosInstruction.class);
122 assertThatClassIsImmutable(L2ModificationInstruction.ModMplsTtlInstruction.class);
123 assertThatClassIsImmutable(L2ModificationInstruction.ModTunnelIdInstruction.class);
Frank Wange33e4ed2017-06-28 10:01:07 +0800124 assertThatClassIsImmutable(PiInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -0800125 }
126
127 // NoActionInstruction
128
129 private final Instructions.NoActionInstruction noAction1 = Instructions.createNoAction();
130 private final Instructions.NoActionInstruction noAction2 = Instructions.createNoAction();
131
132 /**
133 * Test the createNoAction method.
134 */
135 @Test
136 public void testCreateNoActionMethod() {
137 Instructions.NoActionInstruction instruction = Instructions.createNoAction();
138 checkAndConvert(instruction,
139 Instruction.Type.NOACTION,
140 Instructions.NoActionInstruction.class);
141 }
142
143 /**
144 * Test the equals() method of the NoActionInstruction class.
145 */
146
147 @Test
148 public void testNoActionInstructionEquals() throws Exception {
149 new EqualsTester()
150 .addEqualityGroup(noAction1, noAction2)
151 .testEquals();
152 }
153
154 /**
155 * Test the hashCode() method of the NoActionInstruction class.
156 */
157
158 @Test
159 public void testNoActionInstructionHashCode() {
160 assertThat(noAction1.hashCode(), is(equalTo(noAction2.hashCode())));
Ray Milkey78081052014-11-05 10:38:12 -0800161 }
162
Ray Milkey78081052014-11-05 10:38:12 -0800163 // OutputInstruction
164
165 private final PortNumber port1 = portNumber(1);
166 private final PortNumber port2 = portNumber(2);
167 private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1);
168 private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1);
169 private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2);
170
171 /**
172 * Test the createOutput method.
173 */
174 @Test
175 public void testCreateOutputMethod() {
176 final Instruction instruction = Instructions.createOutput(port2);
177 final Instructions.OutputInstruction outputInstruction =
178 checkAndConvert(instruction,
179 Instruction.Type.OUTPUT,
180 Instructions.OutputInstruction.class);
181 assertThat(outputInstruction.port(), is(equalTo(port2)));
182 }
183
184
185 /**
186 * Test the equals() method of the OutputInstruction class.
187 */
188
189 @Test
190 public void testOutputInstructionEquals() throws Exception {
Ray Milkey0d338052014-11-21 16:40:12 -0800191 checkEqualsAndToString(output1, sameAsOutput1, output2);
Ray Milkey78081052014-11-05 10:38:12 -0800192 }
193
194 /**
195 * Test the hashCode() method of the OutputInstruction class.
196 */
197
198 @Test
199 public void testOutputInstructionHashCode() {
200 assertThat(output1.hashCode(), is(equalTo(sameAsOutput1.hashCode())));
201 assertThat(output1.hashCode(), is(not(equalTo(output2.hashCode()))));
202 }
203
204 // ModLambdaInstruction
205
Sho SHIMIZU5a5a7342015-05-05 16:26:11 -0700206 private final Lambda och1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
207 private final Lambda och2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
208 private final Instruction ochInstruction1 = Instructions.modL0Lambda(och1);
209 private final Instruction sameAsOchInstruction1 = Instructions.modL0Lambda(och1);
210 private final Instruction ochInstruction2 = Instructions.modL0Lambda(och2);
211
212 /**
213 * Test the modL0Lambda().
214 */
215 @Test
216 public void testModL0LambdaMethod() {
217 Instruction instruction = Instructions.modL0Lambda(och1);
218 L0ModificationInstruction.ModOchSignalInstruction ochInstruction =
219 checkAndConvert(instruction, Instruction.Type.L0MODIFICATION,
220 L0ModificationInstruction.ModOchSignalInstruction.class);
221 assertThat(ochInstruction.lambda(), is(och1));
222 }
223
224 /**
225 * Test the equals() method of the ModOchSignalInstruction class.
226 */
227 @Test
228 public void testModOchSignalInstructionEquals() {
229 checkEqualsAndToString(ochInstruction1, sameAsOchInstruction1, ochInstruction2);
230 }
231
232 /**
233 * Test the hashCode() method of the ModOchSignalInstruction class.
234 */
235 @Test
236 public void testModOchSignalInstructionHashCode() {
237 assertThat(ochInstruction1.hashCode(), is(sameAsOchInstruction1.hashCode()));
238 assertThat(ochInstruction1.hashCode(), is(not(ochInstruction2.hashCode())));
Ray Milkey78081052014-11-05 10:38:12 -0800239 }
240
Yafit Hadar52d81552015-10-07 12:26:52 +0300241 // ModOduSignalIdInstruction
242
243 private final OduSignalId odu1 = oduSignalId(1, 80, new byte[] {8, 7, 6, 5, 7, 6, 5, 7, 6, 5});
244 private final OduSignalId odu2 = oduSignalId(2, 80, new byte[] {1, 1, 2, 2, 1, 2, 2, 1, 2, 2});
245 private final Instruction oduInstruction1 = Instructions.modL1OduSignalId(odu1);
246 private final Instruction sameAsOduInstruction1 = Instructions.modL1OduSignalId(odu1);
247 private final Instruction oduInstruction2 = Instructions.modL1OduSignalId(odu2);
248
249 /**
250 * Test the modL1OduSignalId().
251 */
252 @Test
253 public void testModL1OduSignalIdMethod() {
254 Instruction instruction = Instructions.modL1OduSignalId(odu1);
255 L1ModificationInstruction.ModOduSignalIdInstruction oduInstruction =
256 checkAndConvert(instruction, Instruction.Type.L1MODIFICATION,
257 L1ModificationInstruction.ModOduSignalIdInstruction.class);
258 assertThat(oduInstruction.oduSignalId(), is(odu1));
259 }
260
261 /**
262 * Test the equals() method of the ModOduSignalInstruction class.
263 */
264 @Test
265 public void testModOduSignalIdInstructionEquals() {
266 checkEqualsAndToString(oduInstruction1, sameAsOduInstruction1, oduInstruction2);
267 }
268
269 /**
270 * Test the hashCode() method of the ModOduSignalInstruction class.
271 */
272 @Test
273 public void testModOduSignalIdInstructionHashCode() {
274 assertThat(oduInstruction1.hashCode(), is(sameAsOduInstruction1.hashCode()));
275 assertThat(oduInstruction1.hashCode(), is(not(oduInstruction2.hashCode())));
276 }
277
278
Ray Milkey78081052014-11-05 10:38:12 -0800279 // ModEtherInstruction
280
281 private static final String MAC1 = "00:00:00:00:00:01";
282 private static final String MAC2 = "00:00:00:00:00:02";
283 private final MacAddress mac1 = MacAddress.valueOf(MAC1);
284 private final MacAddress mac2 = MacAddress.valueOf(MAC2);
285 private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1);
286 private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1);
287 private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2);
288
289 /**
290 * Test the modL2Src method.
291 */
292 @Test
293 public void testModL2SrcMethod() {
294 final Instruction instruction = Instructions.modL2Src(mac1);
295 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
296 checkAndConvert(instruction,
297 Instruction.Type.L2MODIFICATION,
298 L2ModificationInstruction.ModEtherInstruction.class);
299 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
300 assertThat(modEtherInstruction.subtype(),
301 is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC)));
302 }
303
304 /**
305 * Test the modL2Dst method.
306 */
307 @Test
308 public void testModL2DstMethod() {
309 final Instruction instruction = Instructions.modL2Dst(mac1);
310 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
311 checkAndConvert(instruction,
312 Instruction.Type.L2MODIFICATION,
313 L2ModificationInstruction.ModEtherInstruction.class);
314 assertThat(modEtherInstruction.mac(), is(equalTo(mac1)));
315 assertThat(modEtherInstruction.subtype(),
316 is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST)));
317 }
318
319 /**
320 * Test the equals() method of the ModEtherInstruction class.
321 */
322
323 @Test
324 public void testModEtherInstructionEquals() throws Exception {
325 checkEqualsAndToString(modEtherInstruction1,
326 sameAsModEtherInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800327 modEtherInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800328 }
329
330 /**
331 * Test the hashCode() method of the ModEtherInstruction class.
332 */
333
334 @Test
335 public void testModEtherInstructionHashCode() {
336 assertThat(modEtherInstruction1.hashCode(),
337 is(equalTo(sameAsModEtherInstruction1.hashCode())));
338 assertThat(modEtherInstruction1.hashCode(),
339 is(not(equalTo(modEtherInstruction2.hashCode()))));
340 }
341
342
343 // ModVlanIdInstruction
344
345 private final short vlan1 = 1;
346 private final short vlan2 = 2;
347 private final VlanId vlanId1 = VlanId.vlanId(vlan1);
348 private final VlanId vlanId2 = VlanId.vlanId(vlan2);
349 private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1);
350 private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1);
351 private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2);
352
353 /**
354 * Test the modVlanId method.
355 */
356 @Test
357 public void testModVlanIdMethod() {
358 final Instruction instruction = Instructions.modVlanId(vlanId1);
359 final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction =
360 checkAndConvert(instruction,
361 Instruction.Type.L2MODIFICATION,
362 L2ModificationInstruction.ModVlanIdInstruction.class);
363 assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1)));
364 assertThat(modEtherInstruction.subtype(),
365 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID)));
366 }
367
368 /**
369 * Test the equals() method of the ModVlanIdInstruction class.
370 */
371
372 @Test
373 public void testModVlanIdInstructionEquals() throws Exception {
374 checkEqualsAndToString(modVlanId1,
375 sameAsModVlanId1,
Ray Milkey0d338052014-11-21 16:40:12 -0800376 modVlanId2);
Ray Milkey78081052014-11-05 10:38:12 -0800377 }
378
379 /**
380 * Test the hashCode() method of the ModEtherInstruction class.
381 */
382
383 @Test
384 public void testModVlanIdInstructionHashCode() {
385 assertThat(modVlanId1.hashCode(),
386 is(equalTo(sameAsModVlanId1.hashCode())));
387 assertThat(modVlanId1.hashCode(),
388 is(not(equalTo(modVlanId2.hashCode()))));
389 }
390
391
392 // ModVlanPcpInstruction
393
394 private final byte vlanPcp1 = 1;
395 private final byte vlanPcp2 = 2;
396 private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
397 private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1);
398 private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2);
399
400 /**
401 * Test the modVlanPcp method.
402 */
403 @Test
404 public void testModVlanPcpMethod() {
405 final Instruction instruction = Instructions.modVlanPcp(vlanPcp1);
406 final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction =
407 checkAndConvert(instruction,
408 Instruction.Type.L2MODIFICATION,
409 L2ModificationInstruction.ModVlanPcpInstruction.class);
410 assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1)));
411 assertThat(modEtherInstruction.subtype(),
412 is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP)));
413 }
414
415 /**
416 * Test the equals() method of the ModVlanPcpInstruction class.
417 */
418
419 @Test
420 public void testModVlanPcpInstructionEquals() throws Exception {
421 checkEqualsAndToString(modVlanPcp1,
422 sameAsModVlanPcp1,
Ray Milkey0d338052014-11-21 16:40:12 -0800423 modVlanPcp2);
Ray Milkey78081052014-11-05 10:38:12 -0800424 }
425
426 /**
Ray Milkey3564a8c2015-12-14 11:43:12 -0800427 * Test the hashCode() method of the ModVlanPcp class.
Ray Milkey78081052014-11-05 10:38:12 -0800428 */
429
430 @Test
431 public void testModVlanPcpInstructionHashCode() {
432 assertThat(modVlanPcp1.hashCode(),
433 is(equalTo(sameAsModVlanPcp1.hashCode())));
434 assertThat(modVlanPcp1.hashCode(),
435 is(not(equalTo(modVlanPcp2.hashCode()))));
436 }
437
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800438 // ModIPInstruction
Ray Milkey78081052014-11-05 10:38:12 -0800439
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800440 private static final String IP41 = "1.2.3.4";
441 private static final String IP42 = "5.6.7.8";
442 private IpAddress ip41 = IpAddress.valueOf(IP41);
443 private IpAddress ip42 = IpAddress.valueOf(IP42);
444 private final Instruction modIPInstruction1 = Instructions.modL3Src(ip41);
445 private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip41);
446 private final Instruction modIPInstruction2 = Instructions.modL3Src(ip42);
447
448 private static final String IP61 = "1111::2222";
449 private static final String IP62 = "3333::4444";
450 private IpAddress ip61 = IpAddress.valueOf(IP61);
451 private IpAddress ip62 = IpAddress.valueOf(IP62);
452 private final Instruction modIPv6Instruction1 =
453 Instructions.modL3IPv6Src(ip61);
454 private final Instruction sameAsModIPv6Instruction1 =
455 Instructions.modL3IPv6Src(ip61);
456 private final Instruction modIPv6Instruction2 =
457 Instructions.modL3IPv6Src(ip62);
Ray Milkey78081052014-11-05 10:38:12 -0800458
459 /**
460 * Test the modL3Src method.
461 */
462 @Test
463 public void testModL3SrcMethod() {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800464 final Instruction instruction = Instructions.modL3Src(ip41);
Ray Milkey78081052014-11-05 10:38:12 -0800465 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
466 checkAndConvert(instruction,
467 Instruction.Type.L3MODIFICATION,
468 L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800469 assertThat(modIPInstruction.ip(), is(equalTo(ip41)));
Ray Milkey78081052014-11-05 10:38:12 -0800470 assertThat(modIPInstruction.subtype(),
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800471 is(equalTo(L3ModificationInstruction.L3SubType.IPV4_SRC)));
Ray Milkey78081052014-11-05 10:38:12 -0800472 }
473
474 /**
475 * Test the modL3Dst method.
476 */
477 @Test
478 public void testModL3DstMethod() {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800479 final Instruction instruction = Instructions.modL3Dst(ip41);
Ray Milkey78081052014-11-05 10:38:12 -0800480 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
481 checkAndConvert(instruction,
482 Instruction.Type.L3MODIFICATION,
483 L3ModificationInstruction.ModIPInstruction.class);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800484 assertThat(modIPInstruction.ip(), is(equalTo(ip41)));
Ray Milkey78081052014-11-05 10:38:12 -0800485 assertThat(modIPInstruction.subtype(),
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800486 is(equalTo(L3ModificationInstruction.L3SubType.IPV4_DST)));
Ray Milkey78081052014-11-05 10:38:12 -0800487 }
488
489 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800490 * Test the modL3IPv6Src method.
Ray Milkey78081052014-11-05 10:38:12 -0800491 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800492 @Test
493 public void testModL3IPv6SrcMethod() {
494 final Instruction instruction = Instructions.modL3IPv6Src(ip61);
495 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
496 checkAndConvert(instruction,
497 Instruction.Type.L3MODIFICATION,
498 L3ModificationInstruction.ModIPInstruction.class);
499 assertThat(modIPInstruction.ip(), is(equalTo(ip61)));
500 assertThat(modIPInstruction.subtype(),
501 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_SRC)));
502 }
Ray Milkey78081052014-11-05 10:38:12 -0800503
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800504 /**
505 * Test the modL3IPv6Dst method.
506 */
507 @Test
508 public void testModL3IPv6DstMethod() {
509 final Instruction instruction = Instructions.modL3IPv6Dst(ip61);
510 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
511 checkAndConvert(instruction,
512 Instruction.Type.L3MODIFICATION,
513 L3ModificationInstruction.ModIPInstruction.class);
514 assertThat(modIPInstruction.ip(), is(equalTo(ip61)));
515 assertThat(modIPInstruction.subtype(),
516 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_DST)));
517 }
518
519 /**
520 * Test the equals() method of the ModIPInstruction class.
521 */
Ray Milkey78081052014-11-05 10:38:12 -0800522 @Test
523 public void testModIPInstructionEquals() throws Exception {
524 checkEqualsAndToString(modIPInstruction1,
525 sameAsModIPInstruction1,
Ray Milkey0d338052014-11-21 16:40:12 -0800526 modIPInstruction2);
Ray Milkey78081052014-11-05 10:38:12 -0800527 }
528
529 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800530 * Test the hashCode() method of the ModIPInstruction class.
Ray Milkey78081052014-11-05 10:38:12 -0800531 */
Ray Milkey78081052014-11-05 10:38:12 -0800532 @Test
533 public void testModIPInstructionHashCode() {
534 assertThat(modIPInstruction1.hashCode(),
535 is(equalTo(sameAsModIPInstruction1.hashCode())));
536 assertThat(modIPInstruction1.hashCode(),
537 is(not(equalTo(modIPInstruction2.hashCode()))));
538 }
539
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800540 private final int flowLabel1 = 0x11111;
541 private final int flowLabel2 = 0x22222;
542 private final Instruction modIPv6FlowLabelInstruction1 =
543 Instructions.modL3IPv6FlowLabel(flowLabel1);
544 private final Instruction sameAsModIPv6FlowLabelInstruction1 =
545 Instructions.modL3IPv6FlowLabel(flowLabel1);
546 private final Instruction modIPv6FlowLabelInstruction2 =
547 Instructions.modL3IPv6FlowLabel(flowLabel2);
548
549 /**
550 * Test the modL3IPv6FlowLabel method.
551 */
552 @Test
553 public void testModL3IPv6FlowLabelMethod() {
554 final Instruction instruction =
555 Instructions.modL3IPv6FlowLabel(flowLabel1);
556 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
557 modIPv6FlowLabelInstruction =
558 checkAndConvert(instruction,
559 Instruction.Type.L3MODIFICATION,
560 L3ModificationInstruction.ModIPv6FlowLabelInstruction.class);
561 assertThat(modIPv6FlowLabelInstruction.flowLabel(),
562 is(equalTo(flowLabel1)));
563 assertThat(modIPv6FlowLabelInstruction.subtype(),
564 is(equalTo(L3ModificationInstruction.L3SubType.IPV6_FLABEL)));
565 }
566
567 /**
568 * Test the equals() method of the ModIPv6FlowLabelInstruction class.
569 */
570 @Test
571 public void testModIPv6FlowLabelInstructionEquals() throws Exception {
572 checkEqualsAndToString(modIPv6FlowLabelInstruction1,
573 sameAsModIPv6FlowLabelInstruction1,
574 modIPv6FlowLabelInstruction2);
575 }
576
577 /**
578 * Test the hashCode() method of the ModIPv6FlowLabelInstruction class.
579 */
580 @Test
581 public void testModIPv6FlowLabelInstructionHashCode() {
582 assertThat(modIPv6FlowLabelInstruction1.hashCode(),
583 is(equalTo(sameAsModIPv6FlowLabelInstruction1.hashCode())));
584 assertThat(modIPv6FlowLabelInstruction1.hashCode(),
585 is(not(equalTo(modIPv6FlowLabelInstruction2.hashCode()))));
586 }
587
Michele Santuari4b6019e2014-12-19 11:31:45 +0100588 private Instruction modMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1));
589 private Instruction sameAsModMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1));
590 private Instruction modMplsLabelInstruction2 = Instructions.modMplsLabel(MplsLabel.mplsLabel(2));
Ray Milkey0d338052014-11-21 16:40:12 -0800591
592 /**
593 * Test the modMplsLabel method.
594 */
595 @Test
596 public void testModMplsMethod() {
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700597 final MplsLabel mplsLabel = MplsLabel.mplsLabel(33);
598 final Instruction instruction = Instructions.modMplsLabel(mplsLabel);
Ray Milkey0d338052014-11-21 16:40:12 -0800599 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
600 checkAndConvert(instruction,
601 Instruction.Type.L2MODIFICATION,
602 L2ModificationInstruction.ModMplsLabelInstruction.class);
Ray Milkey125572b2016-02-22 16:48:17 -0800603 assertThat(modMplsLabelInstruction.label(), is(equalTo(mplsLabel)));
Ray Milkey0d338052014-11-21 16:40:12 -0800604 assertThat(modMplsLabelInstruction.subtype(),
605 is(equalTo(L2ModificationInstruction.L2SubType.MPLS_LABEL)));
606 }
607
608 /**
Ray Milkey3564a8c2015-12-14 11:43:12 -0800609 * Test the equals(), hashCode() and toString() methods of the
Ray Milkey0d338052014-11-21 16:40:12 -0800610 * ModMplsLabelInstruction class.
611 */
Ray Milkey0d338052014-11-21 16:40:12 -0800612 @Test
613 public void testModMplsLabelInstructionEquals() throws Exception {
614 checkEqualsAndToString(modMplsLabelInstruction1,
615 sameAsModMplsLabelInstruction1,
616 modMplsLabelInstruction2);
617 }
Ray Milkey78081052014-11-05 10:38:12 -0800618
Hyunsun Moond0533e52015-07-28 18:12:43 -0700619 // ModTunnelIdInstruction
620
621 private final long tunnelId1 = 1L;
622 private final long tunnelId2 = 2L;
623 private final Instruction modTunnelId1 = Instructions.modTunnelId(tunnelId1);
624 private final Instruction sameAsModTunnelId1 = Instructions.modTunnelId(tunnelId1);
625 private final Instruction modTunnelId2 = Instructions.modTunnelId(tunnelId2);
626
627 /**
628 * Test the modTunnelId method.
629 */
630 @Test
631 public void testModTunnelIdMethod() {
632 final Instruction instruction = Instructions.modTunnelId(tunnelId1);
633 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
634 checkAndConvert(instruction, Instruction.Type.L2MODIFICATION,
635 L2ModificationInstruction.ModTunnelIdInstruction.class);
636 assertThat(modTunnelIdInstruction.tunnelId(), is(equalTo(tunnelId1)));
637 assertThat(modTunnelIdInstruction.subtype(),
638 is(equalTo(L2ModificationInstruction.L2SubType.TUNNEL_ID)));
639 }
640
641 /***
642 * Test the equals() method of the ModTunnelIdInstruction class.
643 */
644 @Test
645 public void testModTunnelIdInstructionEquals() throws Exception {
646 checkEqualsAndToString(modTunnelId1, sameAsModTunnelId1, modTunnelId2);
647 }
648
649 /**
650 * Test the hashCode() method of the ModTunnelIdInstruction class.
651 */
652 @Test
653 public void testModTunnelIdInstructionHashCode() {
654 assertThat(modTunnelId1.hashCode(), is(equalTo(sameAsModTunnelId1.hashCode())));
655 assertThat(modTunnelId1.hashCode(), is(not(equalTo(modTunnelId2.hashCode()))));
656 }
657
658 // ModTransportPortInstruction
659
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700660 private final TpPort tpPort1 = TpPort.tpPort(1);
661 private final TpPort tpPort2 = TpPort.tpPort(2);
662 private final Instruction modTransportPortInstruction1 = Instructions.modTcpSrc(tpPort1);
663 private final Instruction sameAsModTransportPortInstruction1 = Instructions.modTcpSrc(tpPort1);
664 private final Instruction modTransportPortInstruction2 = Instructions.modTcpSrc(tpPort2);
Hyunsun Moond0533e52015-07-28 18:12:43 -0700665
666 /**
667 * Test the modTcpSrc() method.
668 */
669 @Test
670 public void testModTcpSrcMethod() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700671 final Instruction instruction = Instructions.modTcpSrc(tpPort1);
Hyunsun Moond0533e52015-07-28 18:12:43 -0700672 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
673 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
674 L4ModificationInstruction.ModTransportPortInstruction.class);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700675 assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1)));
Hyunsun Moond0533e52015-07-28 18:12:43 -0700676 assertThat(modTransportPortInstruction.subtype(),
677 is(equalTo(L4ModificationInstruction.L4SubType.TCP_SRC)));
678 }
679
680 /**
681 * Test the modTcpDst() method.
682 */
683 @Test
684 public void testModTcpDstMethod() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700685 final Instruction instruction = Instructions.modTcpDst(tpPort1);
Hyunsun Moond0533e52015-07-28 18:12:43 -0700686 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
687 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
688 L4ModificationInstruction.ModTransportPortInstruction.class);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700689 assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1)));
Hyunsun Moond0533e52015-07-28 18:12:43 -0700690 assertThat(modTransportPortInstruction.subtype(),
691 is(equalTo(L4ModificationInstruction.L4SubType.TCP_DST)));
692 }
693
694 /**
695 * Test the modUdpSrc() method.
696 */
697 @Test
698 public void testModUdpSrcMethod() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700699 final Instruction instruction = Instructions.modUdpSrc(tpPort1);
Hyunsun Moond0533e52015-07-28 18:12:43 -0700700 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
701 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
702 L4ModificationInstruction.ModTransportPortInstruction.class);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700703 assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1)));
Hyunsun Moond0533e52015-07-28 18:12:43 -0700704 assertThat(modTransportPortInstruction.subtype(),
705 is(equalTo(L4ModificationInstruction.L4SubType.UDP_SRC)));
706 }
707
708 /**
709 * Test the modUdpDst() method.
710 */
711 @Test
712 public void testModUdpDstMethod() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700713 final Instruction instruction = Instructions.modUdpDst(tpPort1);
Hyunsun Moond0533e52015-07-28 18:12:43 -0700714 final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction =
715 checkAndConvert(instruction, Instruction.Type.L4MODIFICATION,
716 L4ModificationInstruction.ModTransportPortInstruction.class);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700717 assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1)));
Hyunsun Moond0533e52015-07-28 18:12:43 -0700718 assertThat(modTransportPortInstruction.subtype(),
719 is(equalTo(L4ModificationInstruction.L4SubType.UDP_DST)));
720 }
721
722 /**
723 * Test the equals() method of the ModTransportPortInstruction class.
724 */
725 @Test
726 public void testModTransportPortInstructionEquals() throws Exception {
727 checkEqualsAndToString(modTransportPortInstruction1,
728 sameAsModTransportPortInstruction1,
729 modTransportPortInstruction2);
730 }
731
732 /**
733 * Test the hashCode() method of the ModTransportPortInstruction class.
734 */
735 @Test
736 public void testModTransportPortInstructionHashCode() {
737 assertThat(modTransportPortInstruction1.hashCode(),
738 is(equalTo(sameAsModTransportPortInstruction1.hashCode())));
739 assertThat(modTransportPortInstruction1.hashCode(),
740 is(not(equalTo(modTransportPortInstruction2.hashCode()))));
741 }
Ray Milkey3564a8c2015-12-14 11:43:12 -0800742
743 // GroupInstruction
744
Yi Tsengfa394de2017-02-01 11:26:40 -0800745 private final GroupId groupId1 = new GroupId(1);
746 private final GroupId groupId2 = new GroupId(2);
Ray Milkey3564a8c2015-12-14 11:43:12 -0800747 private final Instruction groupInstruction1 = Instructions.createGroup(groupId1);
748 private final Instruction sameAsGroupInstruction1 = Instructions.createGroup(groupId1);
749 private final Instruction groupInstruction2 = Instructions.createGroup(groupId2);
750
751 /**
752 * Test the create group method.
753 */
754 @Test
755 public void testCreateGroupMethod() {
756 final Instruction instruction = Instructions.createGroup(groupId1);
757 final Instructions.GroupInstruction groupInstruction =
758 checkAndConvert(instruction,
759 Instruction.Type.GROUP,
760 Instructions.GroupInstruction.class);
761 assertThat(groupInstruction.groupId(), is(equalTo(groupId1)));
762 }
763
764 /**
765 * Test the equals() method of the GroupInstruction class.
766 */
767
768 @Test
769 public void testGroupInstructionEquals() {
770 checkEqualsAndToString(groupInstruction1,
771 sameAsGroupInstruction1,
772 groupInstruction2);
773 }
774
775 // SetQueueInstruction
776
777 private final Instruction setQueueInstruction1 = Instructions.setQueue(1, port1);
778 private final Instruction sameAsSetQueueInstruction1 = Instructions.setQueue(1, port1);
779 private final Instruction setQueueInstruction2 = Instructions.setQueue(1, port2);
780
781 /**
782 * Test the set queue method.
783 */
784 @Test
785 public void testSetQueueMethod() {
786 final Instruction instruction = Instructions.setQueue(2, port2);
787 final Instructions.SetQueueInstruction setQueueInstruction =
788 checkAndConvert(instruction,
789 Instruction.Type.QUEUE,
790 Instructions.SetQueueInstruction.class);
791 assertThat(setQueueInstruction.queueId(), is(2L));
792 assertThat(setQueueInstruction.port(), is(port2));
793 }
794
795 /**
796 * Test the equals() method of the SetQueueInstruction class.
797 */
798 @Test
799 public void testSetQueueInstructionEquals() {
800 checkEqualsAndToString(setQueueInstruction1,
801 sameAsSetQueueInstruction1,
802 setQueueInstruction2);
803 }
804
805 // MeterInstruction
806
807 MeterId meterId1 = MeterId.meterId(1);
808 MeterId meterId2 = MeterId.meterId(2);
809 private final Instruction meterInstruction1 = Instructions.meterTraffic(meterId1);
810 private final Instruction sameAsMeterInstruction1 = Instructions.meterTraffic(meterId1);
811 private final Instruction meterInstruction2 = Instructions.meterTraffic(meterId2);
812
813 /**
814 * Test the meter traffic method.
815 */
816 @Test
817 public void testMeterTrafficMethod() {
818 final Instruction instruction = Instructions.meterTraffic(meterId1);
819 final Instructions.MeterInstruction meterInstruction =
820 checkAndConvert(instruction,
821 Instruction.Type.METER,
822 Instructions.MeterInstruction.class);
823 assertThat(meterInstruction.meterId(), is(meterId1));
824 }
825
826 /**
827 * Test the equals() method of the MeterInstruction class.
828 */
829 @Test
830 public void testMeterTrafficInstructionEquals() {
831 checkEqualsAndToString(meterInstruction1,
832 sameAsMeterInstruction1,
833 meterInstruction2);
834 }
835
Cem Türker3baff672017-10-12 15:09:01 +0300836 private long packetCountValue1 = 5L;
837 private long byteCountValue1 = 10L;
838 private long packetCountValue2 = 10L;
839 private long byteCountValue2 = 5L;
840 private StatTriggerFlag flag1 = StatTriggerFlag.ONLY_FIRST;
841 private StatTriggerFlag flag2 = StatTriggerFlag.PERIODIC;
842 Map<StatTriggerField, Long> statTriggerFieldMap1 = new EnumMap<StatTriggerField, Long>(StatTriggerField.class) {
843 {
844 put(StatTriggerField.BYTE_COUNT, packetCountValue1);
845 put(StatTriggerField.PACKET_COUNT, byteCountValue1);
846 }
847 };
848 Map<StatTriggerField, Long> statTriggerFieldMap2 = new EnumMap<StatTriggerField, Long>(StatTriggerField.class) {
849 {
850 put(StatTriggerField.BYTE_COUNT, packetCountValue2);
851 put(StatTriggerField.PACKET_COUNT, byteCountValue2);
852 }
853 };
854
855 final Instruction statInstruction1 = Instructions.statTrigger(statTriggerFieldMap1, flag1);
856 final Instruction statInstruction1Same = Instructions.statTrigger(statTriggerFieldMap1, flag1);
857 final Instruction statInstruction2 = Instructions.statTrigger(statTriggerFieldMap2, flag2);
858
859 @Test
860 public void testStatTriggerTrafficMethod() {
861 final Instruction instruction = Instructions.statTrigger(statTriggerFieldMap1, flag1);
862 final Instructions.StatTriggerInstruction statTriggerInstruction =
863 checkAndConvert(instruction,
864 Instruction.Type.STAT_TRIGGER,
865 Instructions.StatTriggerInstruction.class);
866 assertThat(statTriggerInstruction.getStatTriggerFieldMap(), is(equalTo(statTriggerFieldMap1)));
867 assertThat(statTriggerInstruction.getStatTriggerFlag(), is(equalTo(flag1)));
868 assertThat(statTriggerInstruction.getStatTriggerFieldMap(), is(not(equalTo(statTriggerFieldMap2))));
869 assertThat(statTriggerInstruction.getStatTriggerFlag(), is(not(equalTo(flag2))));
870 }
871
872 @Test
873 public void testStatTriggerTrafficInstructionEquals() {
874 checkEqualsAndToString(statInstruction1,
875 statInstruction1Same,
876 statInstruction2);
877 }
878
Ray Milkey3564a8c2015-12-14 11:43:12 -0800879 // TableTypeTransition
880
881 private final Instruction transitionInstruction1 = Instructions.transition(1);
882 private final Instruction sameAsTransitionInstruction1 = Instructions.transition(1);
883 private final Instruction transitionInstruction2 = Instructions.transition(2);
884
885 /**
886 * Test the transition method.
887 */
888 @Test
889 public void testTransitionMethod() {
890 final Instruction instruction = Instructions.transition(1);
891 final Instructions.TableTypeTransition tableInstruction =
892 checkAndConvert(instruction,
893 Instruction.Type.TABLE,
894 Instructions.TableTypeTransition.class);
895 assertThat(tableInstruction.tableId(), is(1));
896 }
897
898 /**
899 * Test the equals() method of the TableTypeTransition class.
900 */
901 @Test
902 public void testTableTypeTransitionInstructionEquals() {
903 checkEqualsAndToString(transitionInstruction1,
904 sameAsTransitionInstruction1,
905 transitionInstruction2);
906 }
907
908 // MetadataInstruction
909
910 long metadata1 = 111L;
911 long metadataMask1 = 222L;
912 long metadata2 = 333L;
913 long metadataMask2 = 444L;
914
915 private final Instruction metadataInstruction1 =
916 Instructions.writeMetadata(metadata1, metadataMask1);
917 private final Instruction sameAsMetadataInstruction1 =
918 Instructions.writeMetadata(metadata1, metadataMask1);
919 private final Instruction metadataInstruction2 =
920 Instructions.writeMetadata(metadata2, metadataMask2);
921
922 /**
923 * Test the write metadata method.
924 */
925 @Test
926 public void testWriteMetadataMethod() {
927 final Instruction instruction =
928 Instructions.writeMetadata(metadata1, metadataMask1);
929 final Instructions.MetadataInstruction metadataInstruction =
930 checkAndConvert(instruction,
931 Instruction.Type.METADATA,
932 Instructions.MetadataInstruction.class);
933 assertThat(metadataInstruction.metadata(), is(metadata1));
934 assertThat(metadataInstruction.metadataMask(), is(metadataMask1));
935 }
936
937 /**
938 * Test the equals() method of the MetadataInstruction class.
939 */
940 @Test
941 public void testInstructionEquals() {
942 checkEqualsAndToString(metadataInstruction1,
943 sameAsMetadataInstruction1,
944 metadataInstruction2);
945 }
946
947 // ExtensionInstructionWrapper
948
949 class MockExtensionTreatment implements ExtensionTreatment {
950 int type;
951
952 MockExtensionTreatment(int type) {
953 this.type = type;
954 }
955 @Override
956 public ExtensionTreatmentType type() {
957 return new ExtensionTreatmentType(type);
958 }
959
960 @Override
961 public <T> void setPropertyValue(String key, T value) throws ExtensionPropertyException {
962
963 }
964
965 @Override
966 public <T> T getPropertyValue(String key) throws ExtensionPropertyException {
967 return null;
968 }
969
970 @Override
971 public List<String> getProperties() {
972 return null;
973 }
974
975 @Override
976 public byte[] serialize() {
977 return new byte[0];
978 }
979
980 @Override
981 public void deserialize(byte[] data) {
982
983 }
984 }
985
986 ExtensionTreatment extensionTreatment1 = new MockExtensionTreatment(111);
987 ExtensionTreatment extensionTreatment2 = new MockExtensionTreatment(222);
988
989 DeviceId deviceId1 = DeviceId.deviceId("of:1");
990 DeviceId deviceId2 = DeviceId.deviceId("of:2");
991
992 private final Instruction extensionInstruction1 =
993 Instructions.extension(extensionTreatment1, deviceId1);
994 private final Instruction sameAsExtensionInstruction1 =
995 Instructions.extension(extensionTreatment1, deviceId1);
996 private final Instruction extensionInstruction2 =
997 Instructions.extension(extensionTreatment2, deviceId2);
998
999 /**
1000 * Test the extension method.
1001 */
1002 @Test
1003 public void testExtensionMethod() {
1004 final Instruction instruction =
1005 Instructions.extension(extensionTreatment1, deviceId1);
1006 final Instructions.ExtensionInstructionWrapper extensionInstructionWrapper =
1007 checkAndConvert(instruction,
1008 Instruction.Type.EXTENSION,
1009 Instructions.ExtensionInstructionWrapper.class);
1010 assertThat(extensionInstructionWrapper.deviceId(), is(deviceId1));
1011 assertThat(extensionInstructionWrapper.extensionInstruction(), is(extensionTreatment1));
1012 }
1013
1014 /**
1015 * Test the equals() method of the ExtensionInstructionWrapper class.
1016 */
1017 @Test
1018 public void testExtensionInstructionWrapperEquals() {
1019 checkEqualsAndToString(extensionInstruction1,
1020 sameAsExtensionInstruction1,
1021 extensionInstruction2);
1022 }
1023
Jian Li11260a02016-05-19 13:07:22 -07001024 // ModMplsHeaderInstructions
Ray Milkey3564a8c2015-12-14 11:43:12 -08001025
1026 private final EthType ethType1 = new EthType(1);
1027 private final EthType ethType2 = new EthType(2);
Jian Li11260a02016-05-19 13:07:22 -07001028 private final Instruction modMplsHeaderInstruction1 = Instructions.popMpls(ethType1);
1029 private final Instruction sameAsModMplsHeaderInstruction1 = Instructions.popMpls(ethType1);
1030 private final Instruction modMplsHeaderInstruction2 = Instructions.popMpls(ethType2);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001031
1032 /**
1033 * Test the pushMpls method.
1034 */
1035 @Test
1036 public void testPushMplsMethod() {
1037 final Instruction instruction = Instructions.pushMpls();
Jian Li11260a02016-05-19 13:07:22 -07001038 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstruction =
Ray Milkey3564a8c2015-12-14 11:43:12 -08001039 checkAndConvert(instruction,
1040 Instruction.Type.L2MODIFICATION,
Jian Li11260a02016-05-19 13:07:22 -07001041 L2ModificationInstruction.ModMplsHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001042 assertThat(pushHeaderInstruction.ethernetType().toString(),
Yi Tseng603d0552017-04-14 14:06:44 -07001043 is(EthType.EtherType.MPLS_UNICAST.toString()));
Ray Milkey3564a8c2015-12-14 11:43:12 -08001044 assertThat(pushHeaderInstruction.subtype(),
1045 is(L2ModificationInstruction.L2SubType.MPLS_PUSH));
1046 }
1047
1048 /**
1049 * Test the popMpls method.
1050 */
1051 @Test
1052 public void testPopMplsMethod() {
1053 final Instruction instruction = Instructions.popMpls();
Jian Li11260a02016-05-19 13:07:22 -07001054 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstruction =
Ray Milkey3564a8c2015-12-14 11:43:12 -08001055 checkAndConvert(instruction,
1056 Instruction.Type.L2MODIFICATION,
Jian Li11260a02016-05-19 13:07:22 -07001057 L2ModificationInstruction.ModMplsHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001058 assertThat(pushHeaderInstruction.ethernetType().toString(),
Yi Tseng603d0552017-04-14 14:06:44 -07001059 is(EthType.EtherType.MPLS_UNICAST.toString()));
Ray Milkey3564a8c2015-12-14 11:43:12 -08001060 assertThat(pushHeaderInstruction.subtype(),
1061 is(L2ModificationInstruction.L2SubType.MPLS_POP));
1062 }
1063
1064 /**
1065 * Test the popMpls(EtherType) method.
1066 */
1067 @Test
1068 public void testPopMplsEthertypeMethod() {
1069 final Instruction instruction = Instructions.popMpls(new EthType(1));
Jian Li11260a02016-05-19 13:07:22 -07001070 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstruction =
Ray Milkey3564a8c2015-12-14 11:43:12 -08001071 checkAndConvert(instruction,
1072 Instruction.Type.L2MODIFICATION,
Jian Li11260a02016-05-19 13:07:22 -07001073 L2ModificationInstruction.ModMplsHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001074 assertThat(pushHeaderInstruction.ethernetType().toShort(), is((short) 1));
1075 assertThat(pushHeaderInstruction.subtype(),
1076 is(L2ModificationInstruction.L2SubType.MPLS_POP));
1077 }
1078
1079 /**
1080 * Test the pushVlan method.
1081 */
1082 @Test
1083 public void testPushVlanMethod() {
1084 final Instruction instruction = Instructions.pushVlan();
Jian Li11260a02016-05-19 13:07:22 -07001085 final L2ModificationInstruction.ModVlanHeaderInstruction pushHeaderInstruction =
Ray Milkey3564a8c2015-12-14 11:43:12 -08001086 checkAndConvert(instruction,
1087 Instruction.Type.L2MODIFICATION,
Jian Li11260a02016-05-19 13:07:22 -07001088 L2ModificationInstruction.ModVlanHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001089 assertThat(pushHeaderInstruction.ethernetType().toString(),
1090 is(EthType.EtherType.VLAN.toString()));
1091 assertThat(pushHeaderInstruction.subtype(),
1092 is(L2ModificationInstruction.L2SubType.VLAN_PUSH));
1093 }
1094
1095 /**
1096 * Tests the equals(), hashCode() and toString() methods of the
Jian Li11260a02016-05-19 13:07:22 -07001097 * ModMplsHeaderInstructions class.
Ray Milkey3564a8c2015-12-14 11:43:12 -08001098 */
1099
1100 @Test
Jian Li11260a02016-05-19 13:07:22 -07001101 public void testModMplsHeaderInstructionsEquals() {
1102 checkEqualsAndToString(modMplsHeaderInstruction1,
1103 sameAsModMplsHeaderInstruction1,
1104 modMplsHeaderInstruction2);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001105 }
1106
1107 // ModMplsTtlInstruction
1108
1109 private final Instruction modMplsTtlInstruction1 = Instructions.decMplsTtl();
1110 private final Instruction sameAsModMplsTtlInstruction1 = Instructions.decMplsTtl();
1111
1112 /**
1113 * Test the modMplsBos() method.
1114 */
1115 @Test
1116 public void testDecMplsTtlMethod() {
1117 final Instruction instruction = Instructions.decMplsTtl();
1118 final L2ModificationInstruction.ModMplsTtlInstruction modMplsTtlInstruction =
1119 checkAndConvert(instruction,
1120 Instruction.Type.L2MODIFICATION,
1121 L2ModificationInstruction.ModMplsTtlInstruction.class);
1122 assertThat(modMplsTtlInstruction.subtype(),
1123 is(L2ModificationInstruction.L2SubType.DEC_MPLS_TTL));
1124 }
1125
1126 /**
1127 * Tests the equals(), hashCode() and toString() methods of the
Jian Li11260a02016-05-19 13:07:22 -07001128 * ModMplsTtlInstructions class.
Ray Milkey3564a8c2015-12-14 11:43:12 -08001129 */
1130
1131 @Test
1132 public void testMplsTtlInstructionsEquals() {
1133 new EqualsTester()
1134 .addEqualityGroup(modMplsTtlInstruction1, sameAsModMplsTtlInstruction1)
1135 .testEquals();
1136 }
1137
1138 // ModMplsBosInstruction
1139
1140 private final Instruction modMplsBosInstruction1 = Instructions.modMplsBos(true);
1141 private final Instruction sameAsModMplsBosInstruction1 = Instructions.modMplsBos(true);
1142 private final Instruction modMplsBosInstruction2 = Instructions.modMplsBos(false);
1143
1144 /**
1145 * Test the modMplsBos() method.
1146 */
1147 @Test
1148 public void testModMplsBosMethod() {
1149 final Instruction instruction = Instructions.modMplsBos(true);
1150 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
1151 checkAndConvert(instruction,
1152 Instruction.Type.L2MODIFICATION,
1153 L2ModificationInstruction.ModMplsBosInstruction.class);
1154 assertThat(modMplsBosInstruction.subtype(),
1155 is(L2ModificationInstruction.L2SubType.MPLS_BOS));
1156 assertThat(modMplsBosInstruction.mplsBos(), is(true));
1157 }
1158
1159 /**
1160 * Tests the equals(), hashCode() and toString() methods of the
Jian Li11260a02016-05-19 13:07:22 -07001161 * ModMplsBosInstructions class.
Ray Milkey3564a8c2015-12-14 11:43:12 -08001162 */
1163
1164 @Test
1165 public void testMplsBosInstructionsEquals() {
1166 checkEqualsAndToString(modMplsBosInstruction1,
1167 sameAsModMplsBosInstruction1,
1168 modMplsBosInstruction2);
1169 }
1170
Jian Li11260a02016-05-19 13:07:22 -07001171 // ModVlanHeaderInstruction
Ray Milkey3564a8c2015-12-14 11:43:12 -08001172
Jian Li11260a02016-05-19 13:07:22 -07001173 private final Instruction modVlanHeaderInstruction1 = Instructions.popVlan();
1174 private final Instruction sameAsModVlanHeaderInstruction1 = Instructions.popVlan();
Ray Milkey3564a8c2015-12-14 11:43:12 -08001175
1176 /**
1177 * Test the popVlan method.
1178 */
1179 @Test
1180 public void testPopVlanMethod() {
1181 final Instruction instruction = Instructions.popVlan();
Jian Li11260a02016-05-19 13:07:22 -07001182 final L2ModificationInstruction.ModVlanHeaderInstruction popVlanInstruction =
Ray Milkey3564a8c2015-12-14 11:43:12 -08001183 checkAndConvert(instruction,
1184 Instruction.Type.L2MODIFICATION,
Jian Li11260a02016-05-19 13:07:22 -07001185 L2ModificationInstruction.ModVlanHeaderInstruction.class);
Ray Milkey3564a8c2015-12-14 11:43:12 -08001186 assertThat(popVlanInstruction.subtype(),
1187 is(L2ModificationInstruction.L2SubType.VLAN_POP));
1188 }
1189
1190 /**
1191 * Tests the equals(), hashCode() and toString() methods of the
Jian Li11260a02016-05-19 13:07:22 -07001192 * ModVlanHeaderInstructions class.
Ray Milkey3564a8c2015-12-14 11:43:12 -08001193 */
1194
1195 @Test
Jian Li11260a02016-05-19 13:07:22 -07001196 public void testModVlanHeaderInstructionsEquals() {
Ray Milkey3564a8c2015-12-14 11:43:12 -08001197 new EqualsTester()
Jian Li11260a02016-05-19 13:07:22 -07001198 .addEqualityGroup(modVlanHeaderInstruction1, sameAsModVlanHeaderInstruction1)
Ray Milkey3564a8c2015-12-14 11:43:12 -08001199 .testEquals();
1200 }
1201
1202 // ModArpIPInstruction
1203
1204 private final Instruction modArpIPInstruction1 = Instructions.modArpSpa(ip41);
1205 private final Instruction sameAsModArpIPInstruction1 = Instructions.modArpSpa(ip41);
1206 private final Instruction modArpIPInstruction2 = Instructions.modArpSpa(ip42);
1207
1208 /**
1209 * Test the modArpSpa() method.
1210 */
1211 @Test
1212 public void testModArpSpaMethod() {
1213 final Instruction instruction = Instructions.modArpSpa(ip41);
1214 final L3ModificationInstruction.ModArpIPInstruction modArpIPInstruction =
1215 checkAndConvert(instruction,
1216 Instruction.Type.L3MODIFICATION,
1217 L3ModificationInstruction.ModArpIPInstruction.class);
1218 assertThat(modArpIPInstruction.subtype(),
1219 is(L3ModificationInstruction.L3SubType.ARP_SPA));
1220 assertThat(modArpIPInstruction.ip(), is(ip41));
1221 }
1222
1223 /**
1224 * Tests the equals(), hashCode() and toString() methods of the
1225 * ModArpIPInstruction class.
1226 */
1227
1228 @Test
1229 public void testModArpIPInstructionEquals() {
1230 checkEqualsAndToString(modArpIPInstruction1,
1231 sameAsModArpIPInstruction1,
1232 modArpIPInstruction2);
1233 }
1234
1235 // ModArpEthInstruction
1236
1237 private final Instruction modArpEthInstruction1 = Instructions.modArpSha(mac1);
1238 private final Instruction sameAsModArpEthInstruction1 = Instructions.modArpSha(mac1);
1239 private final Instruction modArpEthInstruction2 = Instructions.modArpSha(mac2);
1240
1241 /**
1242 * Test the modArpSha() method.
1243 */
1244 @Test
1245 public void testModArpShaMethod() {
1246 final Instruction instruction = Instructions.modArpSha(mac1);
1247 final L3ModificationInstruction.ModArpEthInstruction modArpEthInstruction =
1248 checkAndConvert(instruction,
1249 Instruction.Type.L3MODIFICATION,
1250 L3ModificationInstruction.ModArpEthInstruction.class);
1251 assertThat(modArpEthInstruction.subtype(),
1252 is(L3ModificationInstruction.L3SubType.ARP_SHA));
1253 assertThat(modArpEthInstruction.mac(), is(mac1));
1254 }
1255
1256 /**
1257 * Tests the equals(), hashCode() and toString() methods of the
1258 * ModArpIPInstruction class.
1259 */
1260
1261 @Test
1262 public void testModArpEthInstructionEquals() {
1263 checkEqualsAndToString(modArpEthInstruction1,
1264 sameAsModArpEthInstruction1,
1265 modArpEthInstruction2);
1266 }
1267
1268 // ModArpOpInstruction
1269
1270 private final Instruction modArpOpInstruction1 = Instructions.modL3ArpOp((short) 1);
1271 private final Instruction sameAsModArpOpInstruction1 = Instructions.modL3ArpOp((short) 1);
1272 private final Instruction modArpOpInstruction2 = Instructions.modL3ArpOp((short) 2);
1273
1274 /**
1275 * Test the modL3ArpOp() method.
1276 */
1277 @Test
1278 public void testModArpModL3ArpOpMethod() {
1279 final Instruction instruction = Instructions.modL3ArpOp((short) 1);
1280 final L3ModificationInstruction.ModArpOpInstruction modArpEthInstruction =
1281 checkAndConvert(instruction,
1282 Instruction.Type.L3MODIFICATION,
1283 L3ModificationInstruction.ModArpOpInstruction.class);
1284 assertThat(modArpEthInstruction.subtype(),
1285 is(L3ModificationInstruction.L3SubType.ARP_OP));
1286 assertThat(modArpEthInstruction.op(), is(1L));
1287 }
1288
1289 /**
1290 * Tests the equals(), hashCode() and toString() methods of the
1291 * ModArpIPInstruction class.
1292 */
1293
1294 @Test
1295 public void testModArpOpInstructionEquals() {
1296 checkEqualsAndToString(modArpOpInstruction1,
1297 sameAsModArpOpInstruction1,
1298 modArpOpInstruction2);
1299 }
1300
1301 // ModTtlInstruction
1302
1303 private final Instruction modArpTtlInstruction1 = Instructions.copyTtlIn();
1304 private final Instruction sameAsModArpTtlInstruction1 = Instructions.copyTtlIn();
1305 private final Instruction modArpTtlInstruction2 = Instructions.copyTtlOut();
1306 private final Instruction modArpTtlInstruction3 = Instructions.decNwTtl();
1307
1308 /**
1309 * Test the copyTtlIn() method.
1310 */
1311 @Test
1312 public void testCopyTtlInMethod() {
1313 final Instruction instruction = Instructions.copyTtlIn();
1314 final L3ModificationInstruction.ModTtlInstruction modTtlInstruction =
1315 checkAndConvert(instruction,
1316 Instruction.Type.L3MODIFICATION,
1317 L3ModificationInstruction.ModTtlInstruction.class);
1318 assertThat(modTtlInstruction.subtype(),
1319 is(L3ModificationInstruction.L3SubType.TTL_IN));
1320 }
1321
1322 /**
1323 * Test the copyTtlOut() method.
1324 */
1325 @Test
1326 public void testCopyTtlOutMethod() {
1327 final Instruction instruction = Instructions.copyTtlOut();
1328 final L3ModificationInstruction.ModTtlInstruction modTtlInstruction =
1329 checkAndConvert(instruction,
1330 Instruction.Type.L3MODIFICATION,
1331 L3ModificationInstruction.ModTtlInstruction.class);
1332 assertThat(modTtlInstruction.subtype(),
1333 is(L3ModificationInstruction.L3SubType.TTL_OUT));
1334 }
1335
1336 /**
1337 * Test the decNwTtl() method.
1338 */
1339 @Test
1340 public void testDecNwTtlOutMethod() {
1341 final Instruction instruction = Instructions.decNwTtl();
1342 final L3ModificationInstruction.ModTtlInstruction modTtlInstruction =
1343 checkAndConvert(instruction,
1344 Instruction.Type.L3MODIFICATION,
1345 L3ModificationInstruction.ModTtlInstruction.class);
1346 assertThat(modTtlInstruction.subtype(),
1347 is(L3ModificationInstruction.L3SubType.DEC_TTL));
1348 }
1349
1350 /**
1351 * Tests the equals(), hashCode() and toString() methods of the
1352 * ModArpIPInstruction class.
1353 */
1354
1355 @Test
1356 public void testModTtlInstructionEquals() {
1357 new EqualsTester()
1358 .addEqualityGroup(modArpTtlInstruction1, sameAsModArpTtlInstruction1)
1359 .addEqualityGroup(modArpTtlInstruction2)
1360 .addEqualityGroup(modArpTtlInstruction3)
1361 .testEquals();
1362 }
1363
Frank Wange33e4ed2017-06-28 10:01:07 +08001364 // PiInstruction
1365 PiTableAction piTableAction1 = PiAction.builder()
1366 .withId(PiActionId.of("set_egress_port_0"))
1367 .withParameter(new PiActionParam(PiActionParamId.of("port"),
1368 ImmutableByteSequence.copyFrom(10))).build();
1369 PiTableAction piTableAction2 = PiAction.builder()
1370 .withId(PiActionId.of("set_egress_port_0"))
1371 .withParameter(new PiActionParam(PiActionParamId.of("port"),
1372 ImmutableByteSequence.copyFrom(20))).build();
1373 private final Instruction piSetEgressPortInstruction1 = new PiInstruction(piTableAction1);
1374 private final Instruction sameAsPiSetEgressPortInstruction1 = new PiInstruction(piTableAction1);
1375 private final Instruction piSetEgressPortInstruction2 = new PiInstruction(piTableAction2);
1376
1377 /**
1378 * Test the PiInstruction() method.
1379 */
1380 @Test
1381 public void testPiMethod() {
1382 final Instruction instruction = new PiInstruction(piTableAction1);
1383 final PiInstruction piInstruction = checkAndConvert(instruction,
1384 Instruction.Type.PROTOCOL_INDEPENDENT, PiInstruction.class);
1385
1386 assertThat(piInstruction.action(), is(piTableAction1));
1387 assertThat(piInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
1388 }
1389
1390 /**
1391 * Tests the equals(), hashCode() and toString() methods of the
1392 * PiInstruction class.
1393 */
1394
1395 @Test
1396 public void testPiInstructionEquals() {
1397 new EqualsTester()
1398 .addEqualityGroup(piSetEgressPortInstruction1, sameAsPiSetEgressPortInstruction1)
1399 .addEqualityGroup(piSetEgressPortInstruction2)
1400 .testEquals();
1401 }
1402
Ray Milkey78081052014-11-05 10:38:12 -08001403}