blob: 10c5a63712e728dd6470cfe51eb56cdafc963ba2 [file] [log] [blame]
Ray Milkeyf19b7152014-11-21 10:56:52 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkeyf19b7152014-11-21 10:56:52 -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;
Ray Milkeyf19b7152014-11-21 10:56:52 -080017
18import java.util.Set;
19
20import org.hamcrest.Description;
21import org.hamcrest.Factory;
22import org.hamcrest.Matcher;
23import org.hamcrest.TypeSafeMatcher;
24import org.junit.Test;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080025import org.onlab.packet.Ip6Address;
Ray Milkeyf19b7152014-11-21 10:56:52 -080026import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010028import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070029import org.onlab.packet.TpPort;
Ray Milkeyf19b7152014-11-21 10:56:52 -080030import org.onlab.packet.VlanId;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070031import org.onosproject.net.IndexedLambda;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.criteria.Criteria;
34import org.onosproject.net.flow.criteria.Criterion;
Ray Milkeyf19b7152014-11-21 10:56:52 -080035
36import com.google.common.testing.EqualsTester;
37
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.hasSize;
40import static org.hamcrest.Matchers.notNullValue;
41import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import static org.onosproject.net.flow.criteria.Criterion.Type;
Ray Milkeyf19b7152014-11-21 10:56:52 -080043
44/**
45 * Unit tests for default traffic selector class.
46 */
47public class DefaultTrafficSelectorTest {
48
49 /**
50 * Checks that the DefaultFlowRule class is immutable.
51 */
52 @Test
53 public void testImmutability() {
54 assertThatClassIsImmutable(DefaultTrafficSelector.class);
55 }
56
57 /**
58 * Tests equals(), hashCode() and toString() methods.
59 */
60 @Test
61 public void testEquals() {
62 final short one = 1;
63 final short two = 2;
64
Sho SHIMIZU260439d2015-06-30 16:35:34 -070065 final TrafficSelector selector1 = DefaultTrafficSelector.builder()
66 .add(Criteria.matchLambda(new IndexedLambda(one)))
67 .build();
68 final TrafficSelector sameAsSelector1 = DefaultTrafficSelector.builder()
69 .add(Criteria.matchLambda(new IndexedLambda(one)))
70 .build();
71 final TrafficSelector selector2 = DefaultTrafficSelector.builder()
72 .add(Criteria.matchLambda(new IndexedLambda(two)))
73 .build();
Ray Milkeyf19b7152014-11-21 10:56:52 -080074
75 new EqualsTester()
76 .addEqualityGroup(selector1, sameAsSelector1)
77 .addEqualityGroup(selector2)
78 .testEquals();
79 }
80
81 /**
Ray Milkeyff517732014-12-01 10:40:14 -080082 * Hamcrest matcher to check that a selector contains a
83 * Criterion with the specified type.
Ray Milkeyf19b7152014-11-21 10:56:52 -080084 */
85 public static final class CriterionExistsMatcher
86 extends TypeSafeMatcher<TrafficSelector> {
87 private final Criterion.Type type;
88
Ray Milkeyff517732014-12-01 10:40:14 -080089 /**
90 * Constructs a matcher for the given criterion type.
91 *
92 * @param typeValue criterion type to match
93 */
Ray Milkeyf19b7152014-11-21 10:56:52 -080094 public CriterionExistsMatcher(Criterion.Type typeValue) {
95 type = typeValue;
96 }
97
98 @Override
99 public boolean matchesSafely(TrafficSelector selector) {
100 final Set<Criterion> criteria = selector.criteria();
101
102 return notNullValue().matches(criteria) &&
103 hasSize(1).matches(criteria) &&
104 notNullValue().matches(selector.getCriterion(type));
105 }
106
107 @Override
108 public void describeTo(Description description) {
109 description.appendText("a criterion with type \" ").
110 appendText(type.toString()).
111 appendText("\"");
112 }
113 }
114
115
116 /**
Ray Milkeyff517732014-12-01 10:40:14 -0800117 * Creates a criterion type matcher. Returns a matcher
Ray Milkeyf19b7152014-11-21 10:56:52 -0800118 * for a criterion with the given type.
119 *
120 * @param type type of Criterion to match
121 * @return Matcher object
122 */
123 @Factory
124 public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
125 return new CriterionExistsMatcher(type);
126 }
127
128
129 /**
130 * Tests the builder functions that add specific criteria.
131 */
132 @Test
133 public void testCriteriaCreation() {
134 TrafficSelector selector;
135
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800136 final long longValue = 0x12345678;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800137 final int intValue = 22;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800138 final short shortValue = 33;
139 final byte byteValue = 44;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800140 final byte dscpValue = 0xf;
141 final byte ecnValue = 3;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800142 final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800143 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800144 final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64");
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800145 final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800146
147 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800148 .matchInPort(PortNumber.portNumber(11)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800149 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
150
151 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800152 .matchInPhyPort(PortNumber.portNumber(11)).build();
153 assertThat(selector, hasCriterionWithType(Type.IN_PHY_PORT));
154
155 selector = DefaultTrafficSelector.builder()
156 .matchMetadata(longValue).build();
157 assertThat(selector, hasCriterionWithType(Type.METADATA));
158
159 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800160 .matchEthDst(macValue).build();
161 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800162
163 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800164 .matchEthSrc(macValue).build();
165 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800166
167 selector = DefaultTrafficSelector.builder()
168 .matchEthType(shortValue).build();
169 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
170
171 selector = DefaultTrafficSelector.builder()
172 .matchVlanId(VlanId.vlanId(shortValue)).build();
173 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
174
175 selector = DefaultTrafficSelector.builder()
176 .matchVlanPcp(byteValue).build();
177 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
178
179 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800180 .matchIPDscp(dscpValue).build();
181 assertThat(selector, hasCriterionWithType(Type.IP_DSCP));
182
183 selector = DefaultTrafficSelector.builder()
184 .matchIPEcn(ecnValue).build();
185 assertThat(selector, hasCriterionWithType(Type.IP_ECN));
186
187 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800188 .matchIPProtocol(byteValue).build();
189 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
190
191 selector = DefaultTrafficSelector.builder()
192 .matchIPSrc(ipPrefixValue).build();
193 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
194
195 selector = DefaultTrafficSelector.builder()
196 .matchIPDst(ipPrefixValue).build();
197 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
198
199 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700200 .matchTcpSrc(TpPort.tpPort(intValue)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800201 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
202
203 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700204 .matchTcpDst(TpPort.tpPort(intValue)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800205 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
206
207 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700208 .matchUdpSrc(TpPort.tpPort(intValue)).build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800209 assertThat(selector, hasCriterionWithType(Type.UDP_SRC));
210
211 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700212 .matchUdpDst(TpPort.tpPort(intValue)).build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800213 assertThat(selector, hasCriterionWithType(Type.UDP_DST));
214
215 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700216 .matchSctpSrc(TpPort.tpPort(intValue)).build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800217 assertThat(selector, hasCriterionWithType(Type.SCTP_SRC));
218
219 selector = DefaultTrafficSelector.builder()
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700220 .matchSctpDst(TpPort.tpPort(intValue)).build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800221 assertThat(selector, hasCriterionWithType(Type.SCTP_DST));
222
223 selector = DefaultTrafficSelector.builder()
224 .matchIcmpType(byteValue).build();
225 assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE));
226
227 selector = DefaultTrafficSelector.builder()
228 .matchIcmpCode(byteValue).build();
229 assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE));
230
231 selector = DefaultTrafficSelector.builder()
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800232 .matchIPv6Src(ipv6PrefixValue).build();
233 assertThat(selector, hasCriterionWithType(Type.IPV6_SRC));
234
235 selector = DefaultTrafficSelector.builder()
236 .matchIPv6Dst(ipv6PrefixValue).build();
237 assertThat(selector, hasCriterionWithType(Type.IPV6_DST));
238
239 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800240 .matchIPv6FlowLabel(intValue).build();
241 assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL));
242
243 selector = DefaultTrafficSelector.builder()
244 .matchIcmpv6Type(byteValue).build();
245 assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE));
246
247 selector = DefaultTrafficSelector.builder()
248 .matchIPv6NDTargetAddress(ipv6AddressValue).build();
249 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET));
250
251 selector = DefaultTrafficSelector.builder()
252 .matchIPv6NDSourceLinkLayerAddress(macValue).build();
253 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL));
254
255 selector = DefaultTrafficSelector.builder()
256 .matchIPv6NDTargetLinkLayerAddress(macValue).build();
257 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL));
258
259 selector = DefaultTrafficSelector.builder()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100260 .matchMplsLabel(MplsLabel.mplsLabel(3)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800261 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
262
263 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800264 .matchIPv6ExthdrFlags(Criterion.IPv6ExthdrFlags.NONEXT.getValue()).build();
265 assertThat(selector, hasCriterionWithType(Type.IPV6_EXTHDR));
266
267 selector = DefaultTrafficSelector.builder()
Sho SHIMIZU260439d2015-06-30 16:35:34 -0700268 .add(Criteria.matchLambda(new IndexedLambda(shortValue))).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800269 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800270 }
271}