Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.MplsLabel; |
| 22 | import org.onlab.packet.TpPort; |
| 23 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | 5a5a734 | 2015-05-05 16:26:11 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ChannelSpacing; |
| 25 | import org.onosproject.net.GridType; |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 26 | import org.onosproject.net.IndexedLambda; |
Sho SHIMIZU | 5a5a734 | 2015-05-05 16:26:11 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Lambda; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 29 | |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 30 | import com.google.common.testing.EqualsTester; |
| 31 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 32 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 33 | import static org.hamcrest.Matchers.equalTo; |
| 34 | import static org.hamcrest.Matchers.instanceOf; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | import static org.hamcrest.Matchers.not; |
| 37 | import static org.hamcrest.Matchers.notNullValue; |
| 38 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 39 | import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Unit tests for the Instructions class. |
| 44 | */ |
| 45 | public class InstructionsTest { |
| 46 | |
| 47 | /** |
| 48 | * Checks that a Criterion object has the proper type, and then converts |
| 49 | * it to the proper type. |
| 50 | * |
| 51 | * @param instruction Instruction object to convert |
| 52 | * @param type Enumerated type value for the Criterion class |
| 53 | * @param clazz Desired Criterion class |
| 54 | * @param <T> The type the caller wants returned |
| 55 | * @return converted object |
| 56 | */ |
| 57 | @SuppressWarnings("unchecked") |
| 58 | private <T> T checkAndConvert(Instruction instruction, Instruction.Type type, Class clazz) { |
| 59 | assertThat(instruction, is(notNullValue())); |
| 60 | assertThat(instruction.type(), is(equalTo(type))); |
| 61 | assertThat(instruction, instanceOf(clazz)); |
| 62 | return (T) instruction; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Checks the equals() and toString() methods of a Criterion class. |
| 67 | * |
| 68 | * @param c1 first object to compare |
| 69 | * @param c1match object that should be equal to the first |
| 70 | * @param c2 object that should be not equal to the first |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 71 | * @param <T> type of the arguments |
| 72 | */ |
| 73 | private <T extends Instruction> void checkEqualsAndToString(T c1, T c1match, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 74 | T c2) { |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 75 | |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 76 | new EqualsTester() |
| 77 | .addEqualityGroup(c1, c1match) |
| 78 | .addEqualityGroup(c2) |
| 79 | .testEquals(); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks that Instructions is a proper utility class. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testInstructionsUtilityClass() { |
| 87 | assertThatClassIsUtility(Instructions.class); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks that the Instruction class implementations are immutable. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testImmutabilityOfInstructions() { |
| 95 | assertThatClassIsImmutable(Instructions.DropInstruction.class); |
| 96 | assertThatClassIsImmutable(Instructions.OutputInstruction.class); |
| 97 | assertThatClassIsImmutable(L0ModificationInstruction.ModLambdaInstruction.class); |
Sho SHIMIZU | 5a5a734 | 2015-05-05 16:26:11 -0700 | [diff] [blame] | 98 | assertThatClassIsImmutable(L0ModificationInstruction.ModOchSignalInstruction.class); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 99 | assertThatClassIsImmutable(L2ModificationInstruction.ModEtherInstruction.class); |
| 100 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class); |
| 101 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class); |
| 102 | assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 103 | assertThatClassIsImmutable(L3ModificationInstruction.ModIPv6FlowLabelInstruction.class); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 104 | assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class); |
| 105 | assertThatClassIsImmutable(L2ModificationInstruction.PushHeaderInstructions.class); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // DropInstruction |
| 109 | |
| 110 | private final Instructions.DropInstruction drop1 = Instructions.createDrop(); |
| 111 | private final Instructions.DropInstruction drop2 = Instructions.createDrop(); |
| 112 | |
| 113 | /** |
| 114 | * Test the createDrop method. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testCreateDropMethod() { |
| 118 | Instructions.DropInstruction instruction = Instructions.createDrop(); |
| 119 | checkAndConvert(instruction, |
| 120 | Instruction.Type.DROP, |
| 121 | Instructions.DropInstruction.class); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test the equals() method of the DropInstruction class. |
| 126 | */ |
| 127 | |
| 128 | @Test |
| 129 | public void testDropInstructionEquals() throws Exception { |
| 130 | assertThat(drop1, is(equalTo(drop2))); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Test the hashCode() method of the DropInstruction class. |
| 135 | */ |
| 136 | |
| 137 | @Test |
| 138 | public void testDropInstructionHashCode() { |
| 139 | assertThat(drop1.hashCode(), is(equalTo(drop2.hashCode()))); |
| 140 | } |
| 141 | |
| 142 | // OutputInstruction |
| 143 | |
| 144 | private final PortNumber port1 = portNumber(1); |
| 145 | private final PortNumber port2 = portNumber(2); |
| 146 | private final Instructions.OutputInstruction output1 = Instructions.createOutput(port1); |
| 147 | private final Instructions.OutputInstruction sameAsOutput1 = Instructions.createOutput(port1); |
| 148 | private final Instructions.OutputInstruction output2 = Instructions.createOutput(port2); |
| 149 | |
| 150 | /** |
| 151 | * Test the createOutput method. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testCreateOutputMethod() { |
| 155 | final Instruction instruction = Instructions.createOutput(port2); |
| 156 | final Instructions.OutputInstruction outputInstruction = |
| 157 | checkAndConvert(instruction, |
| 158 | Instruction.Type.OUTPUT, |
| 159 | Instructions.OutputInstruction.class); |
| 160 | assertThat(outputInstruction.port(), is(equalTo(port2))); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Test the equals() method of the OutputInstruction class. |
| 166 | */ |
| 167 | |
| 168 | @Test |
| 169 | public void testOutputInstructionEquals() throws Exception { |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 170 | checkEqualsAndToString(output1, sameAsOutput1, output2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Test the hashCode() method of the OutputInstruction class. |
| 175 | */ |
| 176 | |
| 177 | @Test |
| 178 | public void testOutputInstructionHashCode() { |
| 179 | assertThat(output1.hashCode(), is(equalTo(sameAsOutput1.hashCode()))); |
| 180 | assertThat(output1.hashCode(), is(not(equalTo(output2.hashCode())))); |
| 181 | } |
| 182 | |
| 183 | // ModLambdaInstruction |
| 184 | |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 185 | private final IndexedLambda lambda1 = new IndexedLambda(1); |
| 186 | private final IndexedLambda lambda2 = new IndexedLambda(2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 187 | private final Instruction lambdaInstruction1 = Instructions.modL0Lambda(lambda1); |
| 188 | private final Instruction sameAsLambdaInstruction1 = Instructions.modL0Lambda(lambda1); |
| 189 | private final Instruction lambdaInstruction2 = Instructions.modL0Lambda(lambda2); |
| 190 | |
| 191 | /** |
| 192 | * Test the modL0Lambda method. |
| 193 | */ |
| 194 | @Test |
| 195 | public void testCreateLambdaMethod() { |
| 196 | final Instruction instruction = Instructions.modL0Lambda(lambda1); |
| 197 | final L0ModificationInstruction.ModLambdaInstruction lambdaInstruction = |
| 198 | checkAndConvert(instruction, |
| 199 | Instruction.Type.L0MODIFICATION, |
| 200 | L0ModificationInstruction.ModLambdaInstruction.class); |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 201 | assertThat(lambdaInstruction.lambda(), is(equalTo((short) lambda1.index()))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 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, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 212 | lambdaInstruction2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Test the hashCode() method of the ModLambdaInstruction class. |
| 217 | */ |
| 218 | |
| 219 | @Test |
| 220 | public void testModLambdaInstructionHashCode() { |
| 221 | assertThat(lambdaInstruction1.hashCode(), |
| 222 | is(equalTo(sameAsLambdaInstruction1.hashCode()))); |
| 223 | assertThat(lambdaInstruction1.hashCode(), |
Sho SHIMIZU | 5a5a734 | 2015-05-05 16:26:11 -0700 | [diff] [blame] | 224 | is(not(equalTo(lambdaInstruction2.hashCode())))); |
| 225 | } |
| 226 | |
| 227 | private final Lambda och1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8); |
| 228 | private final Lambda och2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8); |
| 229 | private final Instruction ochInstruction1 = Instructions.modL0Lambda(och1); |
| 230 | private final Instruction sameAsOchInstruction1 = Instructions.modL0Lambda(och1); |
| 231 | private final Instruction ochInstruction2 = Instructions.modL0Lambda(och2); |
| 232 | |
| 233 | /** |
| 234 | * Test the modL0Lambda(). |
| 235 | */ |
| 236 | @Test |
| 237 | public void testModL0LambdaMethod() { |
| 238 | Instruction instruction = Instructions.modL0Lambda(och1); |
| 239 | L0ModificationInstruction.ModOchSignalInstruction ochInstruction = |
| 240 | checkAndConvert(instruction, Instruction.Type.L0MODIFICATION, |
| 241 | L0ModificationInstruction.ModOchSignalInstruction.class); |
| 242 | assertThat(ochInstruction.lambda(), is(och1)); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Test the equals() method of the ModOchSignalInstruction class. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testModOchSignalInstructionEquals() { |
| 250 | checkEqualsAndToString(ochInstruction1, sameAsOchInstruction1, ochInstruction2); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Test the hashCode() method of the ModOchSignalInstruction class. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testModOchSignalInstructionHashCode() { |
| 258 | assertThat(ochInstruction1.hashCode(), is(sameAsOchInstruction1.hashCode())); |
| 259 | assertThat(ochInstruction1.hashCode(), is(not(ochInstruction2.hashCode()))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // ModEtherInstruction |
| 263 | |
| 264 | private static final String MAC1 = "00:00:00:00:00:01"; |
| 265 | private static final String MAC2 = "00:00:00:00:00:02"; |
| 266 | private final MacAddress mac1 = MacAddress.valueOf(MAC1); |
| 267 | private final MacAddress mac2 = MacAddress.valueOf(MAC2); |
| 268 | private final Instruction modEtherInstruction1 = Instructions.modL2Src(mac1); |
| 269 | private final Instruction sameAsModEtherInstruction1 = Instructions.modL2Src(mac1); |
| 270 | private final Instruction modEtherInstruction2 = Instructions.modL2Src(mac2); |
| 271 | |
| 272 | /** |
| 273 | * Test the modL2Src method. |
| 274 | */ |
| 275 | @Test |
| 276 | public void testModL2SrcMethod() { |
| 277 | final Instruction instruction = Instructions.modL2Src(mac1); |
| 278 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
| 279 | checkAndConvert(instruction, |
| 280 | Instruction.Type.L2MODIFICATION, |
| 281 | L2ModificationInstruction.ModEtherInstruction.class); |
| 282 | assertThat(modEtherInstruction.mac(), is(equalTo(mac1))); |
| 283 | assertThat(modEtherInstruction.subtype(), |
| 284 | is(equalTo(L2ModificationInstruction.L2SubType.ETH_SRC))); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Test the modL2Dst method. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testModL2DstMethod() { |
| 292 | final Instruction instruction = Instructions.modL2Dst(mac1); |
| 293 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
| 294 | checkAndConvert(instruction, |
| 295 | Instruction.Type.L2MODIFICATION, |
| 296 | L2ModificationInstruction.ModEtherInstruction.class); |
| 297 | assertThat(modEtherInstruction.mac(), is(equalTo(mac1))); |
| 298 | assertThat(modEtherInstruction.subtype(), |
| 299 | is(equalTo(L2ModificationInstruction.L2SubType.ETH_DST))); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Test the equals() method of the ModEtherInstruction class. |
| 304 | */ |
| 305 | |
| 306 | @Test |
| 307 | public void testModEtherInstructionEquals() throws Exception { |
| 308 | checkEqualsAndToString(modEtherInstruction1, |
| 309 | sameAsModEtherInstruction1, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 310 | modEtherInstruction2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Test the hashCode() method of the ModEtherInstruction class. |
| 315 | */ |
| 316 | |
| 317 | @Test |
| 318 | public void testModEtherInstructionHashCode() { |
| 319 | assertThat(modEtherInstruction1.hashCode(), |
| 320 | is(equalTo(sameAsModEtherInstruction1.hashCode()))); |
| 321 | assertThat(modEtherInstruction1.hashCode(), |
| 322 | is(not(equalTo(modEtherInstruction2.hashCode())))); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | // ModVlanIdInstruction |
| 327 | |
| 328 | private final short vlan1 = 1; |
| 329 | private final short vlan2 = 2; |
| 330 | private final VlanId vlanId1 = VlanId.vlanId(vlan1); |
| 331 | private final VlanId vlanId2 = VlanId.vlanId(vlan2); |
| 332 | private final Instruction modVlanId1 = Instructions.modVlanId(vlanId1); |
| 333 | private final Instruction sameAsModVlanId1 = Instructions.modVlanId(vlanId1); |
| 334 | private final Instruction modVlanId2 = Instructions.modVlanId(vlanId2); |
| 335 | |
| 336 | /** |
| 337 | * Test the modVlanId method. |
| 338 | */ |
| 339 | @Test |
| 340 | public void testModVlanIdMethod() { |
| 341 | final Instruction instruction = Instructions.modVlanId(vlanId1); |
| 342 | final L2ModificationInstruction.ModVlanIdInstruction modEtherInstruction = |
| 343 | checkAndConvert(instruction, |
| 344 | Instruction.Type.L2MODIFICATION, |
| 345 | L2ModificationInstruction.ModVlanIdInstruction.class); |
| 346 | assertThat(modEtherInstruction.vlanId(), is(equalTo(vlanId1))); |
| 347 | assertThat(modEtherInstruction.subtype(), |
| 348 | is(equalTo(L2ModificationInstruction.L2SubType.VLAN_ID))); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Test the equals() method of the ModVlanIdInstruction class. |
| 353 | */ |
| 354 | |
| 355 | @Test |
| 356 | public void testModVlanIdInstructionEquals() throws Exception { |
| 357 | checkEqualsAndToString(modVlanId1, |
| 358 | sameAsModVlanId1, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 359 | modVlanId2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Test the hashCode() method of the ModEtherInstruction class. |
| 364 | */ |
| 365 | |
| 366 | @Test |
| 367 | public void testModVlanIdInstructionHashCode() { |
| 368 | assertThat(modVlanId1.hashCode(), |
| 369 | is(equalTo(sameAsModVlanId1.hashCode()))); |
| 370 | assertThat(modVlanId1.hashCode(), |
| 371 | is(not(equalTo(modVlanId2.hashCode())))); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | // ModVlanPcpInstruction |
| 376 | |
| 377 | private final byte vlanPcp1 = 1; |
| 378 | private final byte vlanPcp2 = 2; |
| 379 | private final Instruction modVlanPcp1 = Instructions.modVlanPcp(vlanPcp1); |
| 380 | private final Instruction sameAsModVlanPcp1 = Instructions.modVlanPcp(vlanPcp1); |
| 381 | private final Instruction modVlanPcp2 = Instructions.modVlanPcp(vlanPcp2); |
| 382 | |
| 383 | /** |
| 384 | * Test the modVlanPcp method. |
| 385 | */ |
| 386 | @Test |
| 387 | public void testModVlanPcpMethod() { |
| 388 | final Instruction instruction = Instructions.modVlanPcp(vlanPcp1); |
| 389 | final L2ModificationInstruction.ModVlanPcpInstruction modEtherInstruction = |
| 390 | checkAndConvert(instruction, |
| 391 | Instruction.Type.L2MODIFICATION, |
| 392 | L2ModificationInstruction.ModVlanPcpInstruction.class); |
| 393 | assertThat(modEtherInstruction.vlanPcp(), is(equalTo(vlanPcp1))); |
| 394 | assertThat(modEtherInstruction.subtype(), |
| 395 | is(equalTo(L2ModificationInstruction.L2SubType.VLAN_PCP))); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Test the equals() method of the ModVlanPcpInstruction class. |
| 400 | */ |
| 401 | |
| 402 | @Test |
| 403 | public void testModVlanPcpInstructionEquals() throws Exception { |
| 404 | checkEqualsAndToString(modVlanPcp1, |
| 405 | sameAsModVlanPcp1, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 406 | modVlanPcp2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Test the hashCode() method of the ModEtherInstruction class. |
| 411 | */ |
| 412 | |
| 413 | @Test |
| 414 | public void testModVlanPcpInstructionHashCode() { |
| 415 | assertThat(modVlanPcp1.hashCode(), |
| 416 | is(equalTo(sameAsModVlanPcp1.hashCode()))); |
| 417 | assertThat(modVlanPcp1.hashCode(), |
| 418 | is(not(equalTo(modVlanPcp2.hashCode())))); |
| 419 | } |
| 420 | |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 421 | // ModIPInstruction |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 422 | |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 423 | private static final String IP41 = "1.2.3.4"; |
| 424 | private static final String IP42 = "5.6.7.8"; |
| 425 | private IpAddress ip41 = IpAddress.valueOf(IP41); |
| 426 | private IpAddress ip42 = IpAddress.valueOf(IP42); |
| 427 | private final Instruction modIPInstruction1 = Instructions.modL3Src(ip41); |
| 428 | private final Instruction sameAsModIPInstruction1 = Instructions.modL3Src(ip41); |
| 429 | private final Instruction modIPInstruction2 = Instructions.modL3Src(ip42); |
| 430 | |
| 431 | private static final String IP61 = "1111::2222"; |
| 432 | private static final String IP62 = "3333::4444"; |
| 433 | private IpAddress ip61 = IpAddress.valueOf(IP61); |
| 434 | private IpAddress ip62 = IpAddress.valueOf(IP62); |
| 435 | private final Instruction modIPv6Instruction1 = |
| 436 | Instructions.modL3IPv6Src(ip61); |
| 437 | private final Instruction sameAsModIPv6Instruction1 = |
| 438 | Instructions.modL3IPv6Src(ip61); |
| 439 | private final Instruction modIPv6Instruction2 = |
| 440 | Instructions.modL3IPv6Src(ip62); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * Test the modL3Src method. |
| 444 | */ |
| 445 | @Test |
| 446 | public void testModL3SrcMethod() { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 447 | final Instruction instruction = Instructions.modL3Src(ip41); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 448 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 449 | checkAndConvert(instruction, |
| 450 | Instruction.Type.L3MODIFICATION, |
| 451 | L3ModificationInstruction.ModIPInstruction.class); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 452 | assertThat(modIPInstruction.ip(), is(equalTo(ip41))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 453 | assertThat(modIPInstruction.subtype(), |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 454 | is(equalTo(L3ModificationInstruction.L3SubType.IPV4_SRC))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Test the modL3Dst method. |
| 459 | */ |
| 460 | @Test |
| 461 | public void testModL3DstMethod() { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 462 | final Instruction instruction = Instructions.modL3Dst(ip41); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 463 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 464 | checkAndConvert(instruction, |
| 465 | Instruction.Type.L3MODIFICATION, |
| 466 | L3ModificationInstruction.ModIPInstruction.class); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 467 | assertThat(modIPInstruction.ip(), is(equalTo(ip41))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 468 | assertThat(modIPInstruction.subtype(), |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 469 | is(equalTo(L3ModificationInstruction.L3SubType.IPV4_DST))); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 473 | * Test the modL3IPv6Src method. |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 474 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 475 | @Test |
| 476 | public void testModL3IPv6SrcMethod() { |
| 477 | final Instruction instruction = Instructions.modL3IPv6Src(ip61); |
| 478 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 479 | checkAndConvert(instruction, |
| 480 | Instruction.Type.L3MODIFICATION, |
| 481 | L3ModificationInstruction.ModIPInstruction.class); |
| 482 | assertThat(modIPInstruction.ip(), is(equalTo(ip61))); |
| 483 | assertThat(modIPInstruction.subtype(), |
| 484 | is(equalTo(L3ModificationInstruction.L3SubType.IPV6_SRC))); |
| 485 | } |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 486 | |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 487 | /** |
| 488 | * Test the modL3IPv6Dst method. |
| 489 | */ |
| 490 | @Test |
| 491 | public void testModL3IPv6DstMethod() { |
| 492 | final Instruction instruction = Instructions.modL3IPv6Dst(ip61); |
| 493 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 494 | checkAndConvert(instruction, |
| 495 | Instruction.Type.L3MODIFICATION, |
| 496 | L3ModificationInstruction.ModIPInstruction.class); |
| 497 | assertThat(modIPInstruction.ip(), is(equalTo(ip61))); |
| 498 | assertThat(modIPInstruction.subtype(), |
| 499 | is(equalTo(L3ModificationInstruction.L3SubType.IPV6_DST))); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Test the equals() method of the ModIPInstruction class. |
| 504 | */ |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 505 | @Test |
| 506 | public void testModIPInstructionEquals() throws Exception { |
| 507 | checkEqualsAndToString(modIPInstruction1, |
| 508 | sameAsModIPInstruction1, |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 509 | modIPInstruction2); |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 513 | * Test the hashCode() method of the ModIPInstruction class. |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 514 | */ |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 515 | @Test |
| 516 | public void testModIPInstructionHashCode() { |
| 517 | assertThat(modIPInstruction1.hashCode(), |
| 518 | is(equalTo(sameAsModIPInstruction1.hashCode()))); |
| 519 | assertThat(modIPInstruction1.hashCode(), |
| 520 | is(not(equalTo(modIPInstruction2.hashCode())))); |
| 521 | } |
| 522 | |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 523 | private final int flowLabel1 = 0x11111; |
| 524 | private final int flowLabel2 = 0x22222; |
| 525 | private final Instruction modIPv6FlowLabelInstruction1 = |
| 526 | Instructions.modL3IPv6FlowLabel(flowLabel1); |
| 527 | private final Instruction sameAsModIPv6FlowLabelInstruction1 = |
| 528 | Instructions.modL3IPv6FlowLabel(flowLabel1); |
| 529 | private final Instruction modIPv6FlowLabelInstruction2 = |
| 530 | Instructions.modL3IPv6FlowLabel(flowLabel2); |
| 531 | |
| 532 | /** |
| 533 | * Test the modL3IPv6FlowLabel method. |
| 534 | */ |
| 535 | @Test |
| 536 | public void testModL3IPv6FlowLabelMethod() { |
| 537 | final Instruction instruction = |
| 538 | Instructions.modL3IPv6FlowLabel(flowLabel1); |
| 539 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 540 | modIPv6FlowLabelInstruction = |
| 541 | checkAndConvert(instruction, |
| 542 | Instruction.Type.L3MODIFICATION, |
| 543 | L3ModificationInstruction.ModIPv6FlowLabelInstruction.class); |
| 544 | assertThat(modIPv6FlowLabelInstruction.flowLabel(), |
| 545 | is(equalTo(flowLabel1))); |
| 546 | assertThat(modIPv6FlowLabelInstruction.subtype(), |
| 547 | is(equalTo(L3ModificationInstruction.L3SubType.IPV6_FLABEL))); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Test the equals() method of the ModIPv6FlowLabelInstruction class. |
| 552 | */ |
| 553 | @Test |
| 554 | public void testModIPv6FlowLabelInstructionEquals() throws Exception { |
| 555 | checkEqualsAndToString(modIPv6FlowLabelInstruction1, |
| 556 | sameAsModIPv6FlowLabelInstruction1, |
| 557 | modIPv6FlowLabelInstruction2); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Test the hashCode() method of the ModIPv6FlowLabelInstruction class. |
| 562 | */ |
| 563 | @Test |
| 564 | public void testModIPv6FlowLabelInstructionHashCode() { |
| 565 | assertThat(modIPv6FlowLabelInstruction1.hashCode(), |
| 566 | is(equalTo(sameAsModIPv6FlowLabelInstruction1.hashCode()))); |
| 567 | assertThat(modIPv6FlowLabelInstruction1.hashCode(), |
| 568 | is(not(equalTo(modIPv6FlowLabelInstruction2.hashCode())))); |
| 569 | } |
| 570 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 571 | private Instruction modMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1)); |
| 572 | private Instruction sameAsModMplsLabelInstruction1 = Instructions.modMplsLabel(MplsLabel.mplsLabel(1)); |
| 573 | private Instruction modMplsLabelInstruction2 = Instructions.modMplsLabel(MplsLabel.mplsLabel(2)); |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 574 | |
| 575 | /** |
| 576 | * Test the modMplsLabel method. |
| 577 | */ |
| 578 | @Test |
| 579 | public void testModMplsMethod() { |
HIGUCHI Yuta | 04b49fc | 2015-08-28 09:58:58 -0700 | [diff] [blame^] | 580 | final MplsLabel mplsLabel = MplsLabel.mplsLabel(33); |
| 581 | final Instruction instruction = Instructions.modMplsLabel(mplsLabel); |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 582 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
| 583 | checkAndConvert(instruction, |
| 584 | Instruction.Type.L2MODIFICATION, |
| 585 | L2ModificationInstruction.ModMplsLabelInstruction.class); |
HIGUCHI Yuta | 04b49fc | 2015-08-28 09:58:58 -0700 | [diff] [blame^] | 586 | assertThat(modMplsLabelInstruction.mplsLabel(), is(equalTo(mplsLabel))); |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 587 | assertThat(modMplsLabelInstruction.subtype(), |
| 588 | is(equalTo(L2ModificationInstruction.L2SubType.MPLS_LABEL))); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Test the equals(), hashCode and toString() methods of the |
| 593 | * ModMplsLabelInstruction class. |
| 594 | */ |
Ray Milkey | 0d33805 | 2014-11-21 16:40:12 -0800 | [diff] [blame] | 595 | @Test |
| 596 | public void testModMplsLabelInstructionEquals() throws Exception { |
| 597 | checkEqualsAndToString(modMplsLabelInstruction1, |
| 598 | sameAsModMplsLabelInstruction1, |
| 599 | modMplsLabelInstruction2); |
| 600 | } |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 601 | |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 602 | // ModTunnelIdInstruction |
| 603 | |
| 604 | private final long tunnelId1 = 1L; |
| 605 | private final long tunnelId2 = 2L; |
| 606 | private final Instruction modTunnelId1 = Instructions.modTunnelId(tunnelId1); |
| 607 | private final Instruction sameAsModTunnelId1 = Instructions.modTunnelId(tunnelId1); |
| 608 | private final Instruction modTunnelId2 = Instructions.modTunnelId(tunnelId2); |
| 609 | |
| 610 | /** |
| 611 | * Test the modTunnelId method. |
| 612 | */ |
| 613 | @Test |
| 614 | public void testModTunnelIdMethod() { |
| 615 | final Instruction instruction = Instructions.modTunnelId(tunnelId1); |
| 616 | final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction = |
| 617 | checkAndConvert(instruction, Instruction.Type.L2MODIFICATION, |
| 618 | L2ModificationInstruction.ModTunnelIdInstruction.class); |
| 619 | assertThat(modTunnelIdInstruction.tunnelId(), is(equalTo(tunnelId1))); |
| 620 | assertThat(modTunnelIdInstruction.subtype(), |
| 621 | is(equalTo(L2ModificationInstruction.L2SubType.TUNNEL_ID))); |
| 622 | } |
| 623 | |
| 624 | /*** |
| 625 | * Test the equals() method of the ModTunnelIdInstruction class. |
| 626 | */ |
| 627 | @Test |
| 628 | public void testModTunnelIdInstructionEquals() throws Exception { |
| 629 | checkEqualsAndToString(modTunnelId1, sameAsModTunnelId1, modTunnelId2); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Test the hashCode() method of the ModTunnelIdInstruction class. |
| 634 | */ |
| 635 | @Test |
| 636 | public void testModTunnelIdInstructionHashCode() { |
| 637 | assertThat(modTunnelId1.hashCode(), is(equalTo(sameAsModTunnelId1.hashCode()))); |
| 638 | assertThat(modTunnelId1.hashCode(), is(not(equalTo(modTunnelId2.hashCode())))); |
| 639 | } |
| 640 | |
| 641 | // ModTransportPortInstruction |
| 642 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 643 | private final TpPort tpPort1 = TpPort.tpPort(1); |
| 644 | private final TpPort tpPort2 = TpPort.tpPort(2); |
| 645 | private final Instruction modTransportPortInstruction1 = Instructions.modTcpSrc(tpPort1); |
| 646 | private final Instruction sameAsModTransportPortInstruction1 = Instructions.modTcpSrc(tpPort1); |
| 647 | private final Instruction modTransportPortInstruction2 = Instructions.modTcpSrc(tpPort2); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 648 | |
| 649 | /** |
| 650 | * Test the modTcpSrc() method. |
| 651 | */ |
| 652 | @Test |
| 653 | public void testModTcpSrcMethod() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 654 | final Instruction instruction = Instructions.modTcpSrc(tpPort1); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 655 | final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction = |
| 656 | checkAndConvert(instruction, Instruction.Type.L4MODIFICATION, |
| 657 | L4ModificationInstruction.ModTransportPortInstruction.class); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 658 | assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1))); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 659 | assertThat(modTransportPortInstruction.subtype(), |
| 660 | is(equalTo(L4ModificationInstruction.L4SubType.TCP_SRC))); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Test the modTcpDst() method. |
| 665 | */ |
| 666 | @Test |
| 667 | public void testModTcpDstMethod() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 668 | final Instruction instruction = Instructions.modTcpDst(tpPort1); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 669 | final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction = |
| 670 | checkAndConvert(instruction, Instruction.Type.L4MODIFICATION, |
| 671 | L4ModificationInstruction.ModTransportPortInstruction.class); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 672 | assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1))); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 673 | assertThat(modTransportPortInstruction.subtype(), |
| 674 | is(equalTo(L4ModificationInstruction.L4SubType.TCP_DST))); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Test the modUdpSrc() method. |
| 679 | */ |
| 680 | @Test |
| 681 | public void testModUdpSrcMethod() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 682 | final Instruction instruction = Instructions.modUdpSrc(tpPort1); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 683 | final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction = |
| 684 | checkAndConvert(instruction, Instruction.Type.L4MODIFICATION, |
| 685 | L4ModificationInstruction.ModTransportPortInstruction.class); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 686 | assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1))); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 687 | assertThat(modTransportPortInstruction.subtype(), |
| 688 | is(equalTo(L4ModificationInstruction.L4SubType.UDP_SRC))); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Test the modUdpDst() method. |
| 693 | */ |
| 694 | @Test |
| 695 | public void testModUdpDstMethod() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 696 | final Instruction instruction = Instructions.modUdpDst(tpPort1); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 697 | final L4ModificationInstruction.ModTransportPortInstruction modTransportPortInstruction = |
| 698 | checkAndConvert(instruction, Instruction.Type.L4MODIFICATION, |
| 699 | L4ModificationInstruction.ModTransportPortInstruction.class); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 700 | assertThat(modTransportPortInstruction.port(), is(equalTo(tpPort1))); |
Hyunsun Moon | d0533e5 | 2015-07-28 18:12:43 -0700 | [diff] [blame] | 701 | assertThat(modTransportPortInstruction.subtype(), |
| 702 | is(equalTo(L4ModificationInstruction.L4SubType.UDP_DST))); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Test the equals() method of the ModTransportPortInstruction class. |
| 707 | */ |
| 708 | @Test |
| 709 | public void testModTransportPortInstructionEquals() throws Exception { |
| 710 | checkEqualsAndToString(modTransportPortInstruction1, |
| 711 | sameAsModTransportPortInstruction1, |
| 712 | modTransportPortInstruction2); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Test the hashCode() method of the ModTransportPortInstruction class. |
| 717 | */ |
| 718 | @Test |
| 719 | public void testModTransportPortInstructionHashCode() { |
| 720 | assertThat(modTransportPortInstruction1.hashCode(), |
| 721 | is(equalTo(sameAsModTransportPortInstruction1.hashCode()))); |
| 722 | assertThat(modTransportPortInstruction1.hashCode(), |
| 723 | is(not(equalTo(modTransportPortInstruction2.hashCode())))); |
| 724 | } |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 725 | } |