Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -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 | 97f8f10 | 2014-11-14 14:18:23 -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; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 17 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 19 | import org.junit.Test; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 23 | import org.onosproject.net.IndexedLambda; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.flow.instructions.Instruction; |
| 26 | import org.onosproject.net.flow.instructions.Instructions; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 27 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 28 | import java.util.List; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 29 | |
| 30 | import static org.hamcrest.CoreMatchers.equalTo; |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.hasSize; |
| 33 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Unit tests for the DefaultTrafficTreatment class. |
| 37 | */ |
| 38 | public class DefaultTrafficTreatmentTest { |
| 39 | |
| 40 | // Tests for the nested Builder class |
| 41 | |
| 42 | /** |
| 43 | * Tests that the Builder constructors return equivalent objects |
| 44 | * when given the same data. |
| 45 | */ |
| 46 | @Test |
| 47 | public void testTreatmentBuilderConstructors() { |
| 48 | final TrafficTreatment treatment1 = |
| 49 | DefaultTrafficTreatment.builder() |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 50 | .add(Instructions.modL0Lambda(new IndexedLambda(4))) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 51 | .build(); |
| 52 | final TrafficTreatment treatment2 = |
| 53 | DefaultTrafficTreatment.builder(treatment1).build(); |
| 54 | assertThat(treatment1, is(equalTo(treatment2))); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tests methods defined on the Builder. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testBuilderMethods() { |
| 62 | final Instruction instruction1 = |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 63 | Instructions.modL0Lambda(new IndexedLambda(4)); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 64 | |
| 65 | final TrafficTreatment.Builder builder1 = |
| 66 | DefaultTrafficTreatment.builder() |
| 67 | .add(instruction1) |
| 68 | .setEthDst(MacAddress.BROADCAST) |
| 69 | .setEthSrc(MacAddress.BROADCAST) |
| 70 | .setIpDst(IpAddress.valueOf("1.1.1.1")) |
| 71 | .setIpSrc(IpAddress.valueOf("2.2.2.2")) |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame] | 72 | .add(Instructions.modL0Lambda(new IndexedLambda(4))) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 73 | .setOutput(PortNumber.portNumber(2)) |
| 74 | .setVlanId(VlanId.vlanId((short) 4)) |
| 75 | .setVlanPcp((byte) 3); |
| 76 | |
| 77 | final TrafficTreatment treatment1 = builder1.build(); |
| 78 | |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 79 | final List<Instruction> instructions1 = treatment1.immediate(); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 80 | assertThat(instructions1, hasSize(9)); |
| 81 | |
| 82 | builder1.drop(); |
| 83 | builder1.add(instruction1); |
| 84 | |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 85 | final List<Instruction> instructions2 = builder1.build().immediate(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 86 | assertThat(instructions2, hasSize(11)); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 87 | |
| 88 | builder1.deferred() |
| 89 | .popVlan() |
| 90 | .pushVlan() |
| 91 | .setVlanId(VlanId.vlanId((short) 5)); |
| 92 | |
| 93 | final List<Instruction> instructions3 = builder1.build().immediate(); |
| 94 | assertThat(instructions3, hasSize(11)); |
| 95 | final List<Instruction> instructions4 = builder1.build().deferred(); |
| 96 | assertThat(instructions4, hasSize(3)); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests equals(), hashCode() and toString() methods of |
| 101 | * DefaultTrafficTreatment. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testEquals() { |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 105 | final IndexedLambda lambda1 = new IndexedLambda(4); |
| 106 | final IndexedLambda lambda2 = new IndexedLambda(5); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 107 | final TrafficTreatment treatment1 = |
| 108 | DefaultTrafficTreatment.builder() |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 109 | .add(Instructions.modL0Lambda(lambda1)) |
| 110 | .build(); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 111 | final TrafficTreatment sameAsTreatment1 = |
| 112 | DefaultTrafficTreatment.builder() |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 113 | .add(Instructions.modL0Lambda(lambda1)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 114 | .build(); |
| 115 | final TrafficTreatment treatment2 = |
| 116 | DefaultTrafficTreatment.builder() |
Ray Milkey | 85571eb | 2015-06-25 09:45:46 -0700 | [diff] [blame] | 117 | .add(Instructions.modL0Lambda(lambda2)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 118 | .build(); |
| 119 | new EqualsTester() |
| 120 | .addEqualityGroup(treatment1, sameAsTreatment1) |
| 121 | .addEqualityGroup(treatment2) |
| 122 | .testEquals(); |
| 123 | } |
| 124 | } |