Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.Factory; |
| 22 | import org.hamcrest.Matcher; |
| 23 | import org.hamcrest.TypeSafeMatcher; |
| 24 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.flow.criteria.Criteria; |
| 27 | import org.onosproject.net.flow.criteria.Criterion; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
| 31 | |
| 32 | import com.google.common.testing.EqualsTester; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.hasSize; |
| 36 | import static org.hamcrest.Matchers.notNullValue; |
| 37 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import static org.onosproject.net.flow.criteria.Criterion.Type; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unit tests for default traffic selector class. |
| 42 | */ |
| 43 | public class DefaultTrafficSelectorTest { |
| 44 | |
| 45 | /** |
| 46 | * Checks that the DefaultFlowRule class is immutable. |
| 47 | */ |
| 48 | @Test |
| 49 | public void testImmutability() { |
| 50 | assertThatClassIsImmutable(DefaultTrafficSelector.class); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tests equals(), hashCode() and toString() methods. |
| 55 | */ |
| 56 | @Test |
| 57 | public void testEquals() { |
| 58 | final short one = 1; |
| 59 | final short two = 2; |
| 60 | |
| 61 | final TrafficSelector selector1 = |
| 62 | DefaultTrafficSelector.builder().matchLambda(one).build(); |
| 63 | final TrafficSelector sameAsSelector1 = |
| 64 | DefaultTrafficSelector.builder().matchLambda(one).build(); |
| 65 | final TrafficSelector selector2 = |
| 66 | DefaultTrafficSelector.builder().matchLambda(two).build(); |
| 67 | |
| 68 | new EqualsTester() |
| 69 | .addEqualityGroup(selector1, sameAsSelector1) |
| 70 | .addEqualityGroup(selector2) |
| 71 | .testEquals(); |
| 72 | } |
| 73 | |
| 74 | /** |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 75 | * Hamcrest matcher to check that a selector contains a |
| 76 | * Criterion with the specified type. |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 77 | */ |
| 78 | public static final class CriterionExistsMatcher |
| 79 | extends TypeSafeMatcher<TrafficSelector> { |
| 80 | private final Criterion.Type type; |
| 81 | |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Constructs a matcher for the given criterion type. |
| 84 | * |
| 85 | * @param typeValue criterion type to match |
| 86 | */ |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 87 | public CriterionExistsMatcher(Criterion.Type typeValue) { |
| 88 | type = typeValue; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean matchesSafely(TrafficSelector selector) { |
| 93 | final Set<Criterion> criteria = selector.criteria(); |
| 94 | |
| 95 | return notNullValue().matches(criteria) && |
| 96 | hasSize(1).matches(criteria) && |
| 97 | notNullValue().matches(selector.getCriterion(type)); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void describeTo(Description description) { |
| 102 | description.appendText("a criterion with type \" "). |
| 103 | appendText(type.toString()). |
| 104 | appendText("\""); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 110 | * Creates a criterion type matcher. Returns a matcher |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 111 | * for a criterion with the given type. |
| 112 | * |
| 113 | * @param type type of Criterion to match |
| 114 | * @return Matcher object |
| 115 | */ |
| 116 | @Factory |
| 117 | public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) { |
| 118 | return new CriterionExistsMatcher(type); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Tests the builder functions that add specific criteria. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testCriteriaCreation() { |
| 127 | TrafficSelector selector; |
| 128 | |
| 129 | final short shortValue = 33; |
| 130 | final byte byteValue = 44; |
| 131 | final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24"); |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 132 | final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64"); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 133 | |
| 134 | selector = DefaultTrafficSelector.builder() |
| 135 | .matchInport(PortNumber.portNumber(11)).build(); |
| 136 | assertThat(selector, hasCriterionWithType(Type.IN_PORT)); |
| 137 | |
| 138 | selector = DefaultTrafficSelector.builder() |
| 139 | .matchEthSrc(MacAddress.BROADCAST).build(); |
| 140 | assertThat(selector, hasCriterionWithType(Type.ETH_SRC)); |
| 141 | |
| 142 | selector = DefaultTrafficSelector.builder() |
| 143 | .matchEthDst(MacAddress.BROADCAST).build(); |
| 144 | assertThat(selector, hasCriterionWithType(Type.ETH_DST)); |
| 145 | |
| 146 | selector = DefaultTrafficSelector.builder() |
| 147 | .matchEthType(shortValue).build(); |
| 148 | assertThat(selector, hasCriterionWithType(Type.ETH_TYPE)); |
| 149 | |
| 150 | selector = DefaultTrafficSelector.builder() |
| 151 | .matchVlanId(VlanId.vlanId(shortValue)).build(); |
| 152 | assertThat(selector, hasCriterionWithType(Type.VLAN_VID)); |
| 153 | |
| 154 | selector = DefaultTrafficSelector.builder() |
| 155 | .matchVlanPcp(byteValue).build(); |
| 156 | assertThat(selector, hasCriterionWithType(Type.VLAN_PCP)); |
| 157 | |
| 158 | selector = DefaultTrafficSelector.builder() |
| 159 | .matchIPProtocol(byteValue).build(); |
| 160 | assertThat(selector, hasCriterionWithType(Type.IP_PROTO)); |
| 161 | |
| 162 | selector = DefaultTrafficSelector.builder() |
| 163 | .matchIPSrc(ipPrefixValue).build(); |
| 164 | assertThat(selector, hasCriterionWithType(Type.IPV4_SRC)); |
| 165 | |
| 166 | selector = DefaultTrafficSelector.builder() |
| 167 | .matchIPDst(ipPrefixValue).build(); |
| 168 | assertThat(selector, hasCriterionWithType(Type.IPV4_DST)); |
| 169 | |
| 170 | selector = DefaultTrafficSelector.builder() |
| 171 | .matchTcpSrc(shortValue).build(); |
| 172 | assertThat(selector, hasCriterionWithType(Type.TCP_SRC)); |
| 173 | |
| 174 | selector = DefaultTrafficSelector.builder() |
| 175 | .matchTcpDst(shortValue).build(); |
| 176 | assertThat(selector, hasCriterionWithType(Type.TCP_DST)); |
| 177 | |
| 178 | selector = DefaultTrafficSelector.builder() |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 179 | .matchIPv6Src(ipv6PrefixValue).build(); |
| 180 | assertThat(selector, hasCriterionWithType(Type.IPV6_SRC)); |
| 181 | |
| 182 | selector = DefaultTrafficSelector.builder() |
| 183 | .matchIPv6Dst(ipv6PrefixValue).build(); |
| 184 | assertThat(selector, hasCriterionWithType(Type.IPV6_DST)); |
| 185 | |
| 186 | selector = DefaultTrafficSelector.builder() |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 187 | .matchMplsLabel(3).build(); |
| 188 | assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL)); |
| 189 | |
| 190 | selector = DefaultTrafficSelector.builder() |
| 191 | .matchLambda(shortValue).build(); |
| 192 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGID)); |
| 193 | |
| 194 | selector = DefaultTrafficSelector.builder() |
| 195 | .matchOpticalSignalType(shortValue).build(); |
| 196 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE)); |
| 197 | |
| 198 | final TrafficSelector baseSelector = DefaultTrafficSelector.builder() |
| 199 | .matchOpticalSignalType(shortValue).build(); |
| 200 | selector = DefaultTrafficSelector.builder(baseSelector) |
| 201 | .build(); |
| 202 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE)); |
| 203 | |
| 204 | final Criterion criterion = Criteria.matchLambda(shortValue); |
| 205 | selector = DefaultTrafficSelector.builder() |
| 206 | .add(criterion).build(); |
| 207 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGID)); |
| 208 | } |
| 209 | } |