Ray Milkey | f19b715 | 2014-11-21 10:56:52 -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 | f19b715 | 2014-11-21 10:56:52 -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 | 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; |
Sho SHIMIZU | 260439d | 2015-06-30 16:35:34 -0700 | [diff] [blame] | 25 | import org.onosproject.net.IndexedLambda; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.criteria.Criteria; |
| 28 | import org.onosproject.net.flow.criteria.Criterion; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 29 | import org.onlab.packet.Ip6Address; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 32 | import org.onlab.packet.MplsLabel; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 33 | import org.onlab.packet.VlanId; |
| 34 | |
| 35 | import com.google.common.testing.EqualsTester; |
| 36 | |
| 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.Matchers.hasSize; |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.flow.criteria.Criterion.Type; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Unit tests for default traffic selector class. |
| 45 | */ |
| 46 | public class DefaultTrafficSelectorTest { |
| 47 | |
| 48 | /** |
| 49 | * Checks that the DefaultFlowRule class is immutable. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testImmutability() { |
| 53 | assertThatClassIsImmutable(DefaultTrafficSelector.class); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Tests equals(), hashCode() and toString() methods. |
| 58 | */ |
| 59 | @Test |
| 60 | public void testEquals() { |
| 61 | final short one = 1; |
| 62 | final short two = 2; |
| 63 | |
Sho SHIMIZU | 260439d | 2015-06-30 16:35:34 -0700 | [diff] [blame] | 64 | final TrafficSelector selector1 = DefaultTrafficSelector.builder() |
| 65 | .add(Criteria.matchLambda(new IndexedLambda(one))) |
| 66 | .build(); |
| 67 | final TrafficSelector sameAsSelector1 = DefaultTrafficSelector.builder() |
| 68 | .add(Criteria.matchLambda(new IndexedLambda(one))) |
| 69 | .build(); |
| 70 | final TrafficSelector selector2 = DefaultTrafficSelector.builder() |
| 71 | .add(Criteria.matchLambda(new IndexedLambda(two))) |
| 72 | .build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 73 | |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(selector1, sameAsSelector1) |
| 76 | .addEqualityGroup(selector2) |
| 77 | .testEquals(); |
| 78 | } |
| 79 | |
| 80 | /** |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 81 | * Hamcrest matcher to check that a selector contains a |
| 82 | * Criterion with the specified type. |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 83 | */ |
| 84 | public static final class CriterionExistsMatcher |
| 85 | extends TypeSafeMatcher<TrafficSelector> { |
| 86 | private final Criterion.Type type; |
| 87 | |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Constructs a matcher for the given criterion type. |
| 90 | * |
| 91 | * @param typeValue criterion type to match |
| 92 | */ |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 93 | public CriterionExistsMatcher(Criterion.Type typeValue) { |
| 94 | type = typeValue; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean matchesSafely(TrafficSelector selector) { |
| 99 | final Set<Criterion> criteria = selector.criteria(); |
| 100 | |
| 101 | return notNullValue().matches(criteria) && |
| 102 | hasSize(1).matches(criteria) && |
| 103 | notNullValue().matches(selector.getCriterion(type)); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void describeTo(Description description) { |
| 108 | description.appendText("a criterion with type \" "). |
| 109 | appendText(type.toString()). |
| 110 | appendText("\""); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
Ray Milkey | ff51773 | 2014-12-01 10:40:14 -0800 | [diff] [blame] | 116 | * Creates a criterion type matcher. Returns a matcher |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 117 | * for a criterion with the given type. |
| 118 | * |
| 119 | * @param type type of Criterion to match |
| 120 | * @return Matcher object |
| 121 | */ |
| 122 | @Factory |
| 123 | public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) { |
| 124 | return new CriterionExistsMatcher(type); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Tests the builder functions that add specific criteria. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testCriteriaCreation() { |
| 133 | TrafficSelector selector; |
| 134 | |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 135 | final long longValue = 0x12345678; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 136 | final int intValue = 22; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 137 | final short shortValue = 33; |
| 138 | final byte byteValue = 44; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 139 | final byte dscpValue = 0xf; |
| 140 | final byte ecnValue = 3; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 141 | final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66"); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 142 | final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24"); |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 143 | final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64"); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 144 | final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1"); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 145 | |
| 146 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 147 | .matchInPort(PortNumber.portNumber(11)).build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 148 | assertThat(selector, hasCriterionWithType(Type.IN_PORT)); |
| 149 | |
| 150 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 151 | .matchInPhyPort(PortNumber.portNumber(11)).build(); |
| 152 | assertThat(selector, hasCriterionWithType(Type.IN_PHY_PORT)); |
| 153 | |
| 154 | selector = DefaultTrafficSelector.builder() |
| 155 | .matchMetadata(longValue).build(); |
| 156 | assertThat(selector, hasCriterionWithType(Type.METADATA)); |
| 157 | |
| 158 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 159 | .matchEthDst(macValue).build(); |
| 160 | assertThat(selector, hasCriterionWithType(Type.ETH_DST)); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 161 | |
| 162 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 163 | .matchEthSrc(macValue).build(); |
| 164 | assertThat(selector, hasCriterionWithType(Type.ETH_SRC)); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 165 | |
| 166 | selector = DefaultTrafficSelector.builder() |
| 167 | .matchEthType(shortValue).build(); |
| 168 | assertThat(selector, hasCriterionWithType(Type.ETH_TYPE)); |
| 169 | |
| 170 | selector = DefaultTrafficSelector.builder() |
| 171 | .matchVlanId(VlanId.vlanId(shortValue)).build(); |
| 172 | assertThat(selector, hasCriterionWithType(Type.VLAN_VID)); |
| 173 | |
| 174 | selector = DefaultTrafficSelector.builder() |
| 175 | .matchVlanPcp(byteValue).build(); |
| 176 | assertThat(selector, hasCriterionWithType(Type.VLAN_PCP)); |
| 177 | |
| 178 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 179 | .matchIPDscp(dscpValue).build(); |
| 180 | assertThat(selector, hasCriterionWithType(Type.IP_DSCP)); |
| 181 | |
| 182 | selector = DefaultTrafficSelector.builder() |
| 183 | .matchIPEcn(ecnValue).build(); |
| 184 | assertThat(selector, hasCriterionWithType(Type.IP_ECN)); |
| 185 | |
| 186 | selector = DefaultTrafficSelector.builder() |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 187 | .matchIPProtocol(byteValue).build(); |
| 188 | assertThat(selector, hasCriterionWithType(Type.IP_PROTO)); |
| 189 | |
| 190 | selector = DefaultTrafficSelector.builder() |
| 191 | .matchIPSrc(ipPrefixValue).build(); |
| 192 | assertThat(selector, hasCriterionWithType(Type.IPV4_SRC)); |
| 193 | |
| 194 | selector = DefaultTrafficSelector.builder() |
| 195 | .matchIPDst(ipPrefixValue).build(); |
| 196 | assertThat(selector, hasCriterionWithType(Type.IPV4_DST)); |
| 197 | |
| 198 | selector = DefaultTrafficSelector.builder() |
| 199 | .matchTcpSrc(shortValue).build(); |
| 200 | assertThat(selector, hasCriterionWithType(Type.TCP_SRC)); |
| 201 | |
| 202 | selector = DefaultTrafficSelector.builder() |
| 203 | .matchTcpDst(shortValue).build(); |
| 204 | assertThat(selector, hasCriterionWithType(Type.TCP_DST)); |
| 205 | |
| 206 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 207 | .matchUdpSrc(shortValue).build(); |
| 208 | assertThat(selector, hasCriterionWithType(Type.UDP_SRC)); |
| 209 | |
| 210 | selector = DefaultTrafficSelector.builder() |
| 211 | .matchUdpDst(shortValue).build(); |
| 212 | assertThat(selector, hasCriterionWithType(Type.UDP_DST)); |
| 213 | |
| 214 | selector = DefaultTrafficSelector.builder() |
| 215 | .matchSctpSrc(shortValue).build(); |
| 216 | assertThat(selector, hasCriterionWithType(Type.SCTP_SRC)); |
| 217 | |
| 218 | selector = DefaultTrafficSelector.builder() |
| 219 | .matchSctpDst(shortValue).build(); |
| 220 | assertThat(selector, hasCriterionWithType(Type.SCTP_DST)); |
| 221 | |
| 222 | selector = DefaultTrafficSelector.builder() |
| 223 | .matchIcmpType(byteValue).build(); |
| 224 | assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE)); |
| 225 | |
| 226 | selector = DefaultTrafficSelector.builder() |
| 227 | .matchIcmpCode(byteValue).build(); |
| 228 | assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE)); |
| 229 | |
| 230 | selector = DefaultTrafficSelector.builder() |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 231 | .matchIPv6Src(ipv6PrefixValue).build(); |
| 232 | assertThat(selector, hasCriterionWithType(Type.IPV6_SRC)); |
| 233 | |
| 234 | selector = DefaultTrafficSelector.builder() |
| 235 | .matchIPv6Dst(ipv6PrefixValue).build(); |
| 236 | assertThat(selector, hasCriterionWithType(Type.IPV6_DST)); |
| 237 | |
| 238 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 239 | .matchIPv6FlowLabel(intValue).build(); |
| 240 | assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL)); |
| 241 | |
| 242 | selector = DefaultTrafficSelector.builder() |
| 243 | .matchIcmpv6Type(byteValue).build(); |
| 244 | assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE)); |
| 245 | |
| 246 | selector = DefaultTrafficSelector.builder() |
| 247 | .matchIPv6NDTargetAddress(ipv6AddressValue).build(); |
| 248 | assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET)); |
| 249 | |
| 250 | selector = DefaultTrafficSelector.builder() |
| 251 | .matchIPv6NDSourceLinkLayerAddress(macValue).build(); |
| 252 | assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL)); |
| 253 | |
| 254 | selector = DefaultTrafficSelector.builder() |
| 255 | .matchIPv6NDTargetLinkLayerAddress(macValue).build(); |
| 256 | assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL)); |
| 257 | |
| 258 | selector = DefaultTrafficSelector.builder() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 259 | .matchMplsLabel(MplsLabel.mplsLabel(3)).build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 260 | assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL)); |
| 261 | |
| 262 | selector = DefaultTrafficSelector.builder() |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 263 | .matchIPv6ExthdrFlags(Criterion.IPv6ExthdrFlags.NONEXT.getValue()).build(); |
| 264 | assertThat(selector, hasCriterionWithType(Type.IPV6_EXTHDR)); |
| 265 | |
| 266 | selector = DefaultTrafficSelector.builder() |
Sho SHIMIZU | 260439d | 2015-06-30 16:35:34 -0700 | [diff] [blame] | 267 | .add(Criteria.matchLambda(new IndexedLambda(shortValue))).build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 268 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGID)); |
| 269 | |
| 270 | selector = DefaultTrafficSelector.builder() |
Sho SHIMIZU | 260439d | 2015-06-30 16:35:34 -0700 | [diff] [blame] | 271 | .add(Criteria.matchOpticalSignalType(shortValue)).build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 272 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE)); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Tests the traffic selector builder. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testTrafficSelectorBuilder() { |
| 280 | TrafficSelector selector; |
| 281 | final short shortValue = 33; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 282 | |
| 283 | final TrafficSelector baseSelector = DefaultTrafficSelector.builder() |
Sho SHIMIZU | 260439d | 2015-06-30 16:35:34 -0700 | [diff] [blame] | 284 | .add(Criteria.matchLambda(new IndexedLambda(shortValue))).build(); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 285 | selector = DefaultTrafficSelector.builder(baseSelector) |
| 286 | .build(); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 287 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGID)); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 288 | |
| 289 | final Criterion criterion = Criteria.matchLambda(shortValue); |
| 290 | selector = DefaultTrafficSelector.builder() |
| 291 | .add(criterion).build(); |
| 292 | assertThat(selector, hasCriterionWithType(Type.OCH_SIGID)); |
| 293 | } |
| 294 | } |