blob: 81e0d5152dccfaf8c5fca1ea88a835c82446c6d5 [file] [log] [blame]
Ray Milkeyf19b7152014-11-21 10:56:52 -08001/*
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'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;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.criteria.Criteria;
27import org.onosproject.net.flow.criteria.Criterion;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080028import org.onlab.packet.Ip6Address;
Ray Milkeyf19b7152014-11-21 10:56:52 -080029import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010031import org.onlab.packet.MplsLabel;
Ray Milkeyf19b7152014-11-21 10:56:52 -080032import org.onlab.packet.VlanId;
33
34import com.google.common.testing.EqualsTester;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.hasSize;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.flow.criteria.Criterion.Type;
Ray Milkeyf19b7152014-11-21 10:56:52 -080041
42/**
43 * Unit tests for default traffic selector class.
44 */
45public class DefaultTrafficSelectorTest {
46
47 /**
48 * Checks that the DefaultFlowRule class is immutable.
49 */
50 @Test
51 public void testImmutability() {
52 assertThatClassIsImmutable(DefaultTrafficSelector.class);
53 }
54
55 /**
56 * Tests equals(), hashCode() and toString() methods.
57 */
58 @Test
59 public void testEquals() {
60 final short one = 1;
61 final short two = 2;
62
63 final TrafficSelector selector1 =
64 DefaultTrafficSelector.builder().matchLambda(one).build();
65 final TrafficSelector sameAsSelector1 =
66 DefaultTrafficSelector.builder().matchLambda(one).build();
67 final TrafficSelector selector2 =
68 DefaultTrafficSelector.builder().matchLambda(two).build();
69
70 new EqualsTester()
71 .addEqualityGroup(selector1, sameAsSelector1)
72 .addEqualityGroup(selector2)
73 .testEquals();
74 }
75
76 /**
Ray Milkeyff517732014-12-01 10:40:14 -080077 * Hamcrest matcher to check that a selector contains a
78 * Criterion with the specified type.
Ray Milkeyf19b7152014-11-21 10:56:52 -080079 */
80 public static final class CriterionExistsMatcher
81 extends TypeSafeMatcher<TrafficSelector> {
82 private final Criterion.Type type;
83
Ray Milkeyff517732014-12-01 10:40:14 -080084 /**
85 * Constructs a matcher for the given criterion type.
86 *
87 * @param typeValue criterion type to match
88 */
Ray Milkeyf19b7152014-11-21 10:56:52 -080089 public CriterionExistsMatcher(Criterion.Type typeValue) {
90 type = typeValue;
91 }
92
93 @Override
94 public boolean matchesSafely(TrafficSelector selector) {
95 final Set<Criterion> criteria = selector.criteria();
96
97 return notNullValue().matches(criteria) &&
98 hasSize(1).matches(criteria) &&
99 notNullValue().matches(selector.getCriterion(type));
100 }
101
102 @Override
103 public void describeTo(Description description) {
104 description.appendText("a criterion with type \" ").
105 appendText(type.toString()).
106 appendText("\"");
107 }
108 }
109
110
111 /**
Ray Milkeyff517732014-12-01 10:40:14 -0800112 * Creates a criterion type matcher. Returns a matcher
Ray Milkeyf19b7152014-11-21 10:56:52 -0800113 * for a criterion with the given type.
114 *
115 * @param type type of Criterion to match
116 * @return Matcher object
117 */
118 @Factory
119 public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
120 return new CriterionExistsMatcher(type);
121 }
122
123
124 /**
125 * Tests the builder functions that add specific criteria.
126 */
127 @Test
128 public void testCriteriaCreation() {
129 TrafficSelector selector;
130
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800131 final long longValue = 0x12345678;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800132 final int intValue = 22;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800133 final short shortValue = 33;
134 final byte byteValue = 44;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800135 final byte dscpValue = 0xf;
136 final byte ecnValue = 3;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800137 final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800138 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800139 final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64");
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800140 final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800141
142 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800143 .matchInPort(PortNumber.portNumber(11)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800144 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
145
146 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800147 .matchInPhyPort(PortNumber.portNumber(11)).build();
148 assertThat(selector, hasCriterionWithType(Type.IN_PHY_PORT));
149
150 selector = DefaultTrafficSelector.builder()
151 .matchMetadata(longValue).build();
152 assertThat(selector, hasCriterionWithType(Type.METADATA));
153
154 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800155 .matchEthDst(macValue).build();
156 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800157
158 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800159 .matchEthSrc(macValue).build();
160 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800161
162 selector = DefaultTrafficSelector.builder()
163 .matchEthType(shortValue).build();
164 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
165
166 selector = DefaultTrafficSelector.builder()
167 .matchVlanId(VlanId.vlanId(shortValue)).build();
168 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
169
170 selector = DefaultTrafficSelector.builder()
171 .matchVlanPcp(byteValue).build();
172 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
173
174 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800175 .matchIPDscp(dscpValue).build();
176 assertThat(selector, hasCriterionWithType(Type.IP_DSCP));
177
178 selector = DefaultTrafficSelector.builder()
179 .matchIPEcn(ecnValue).build();
180 assertThat(selector, hasCriterionWithType(Type.IP_ECN));
181
182 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800183 .matchIPProtocol(byteValue).build();
184 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
185
186 selector = DefaultTrafficSelector.builder()
187 .matchIPSrc(ipPrefixValue).build();
188 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
189
190 selector = DefaultTrafficSelector.builder()
191 .matchIPDst(ipPrefixValue).build();
192 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
193
194 selector = DefaultTrafficSelector.builder()
195 .matchTcpSrc(shortValue).build();
196 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
197
198 selector = DefaultTrafficSelector.builder()
199 .matchTcpDst(shortValue).build();
200 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
201
202 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800203 .matchUdpSrc(shortValue).build();
204 assertThat(selector, hasCriterionWithType(Type.UDP_SRC));
205
206 selector = DefaultTrafficSelector.builder()
207 .matchUdpDst(shortValue).build();
208 assertThat(selector, hasCriterionWithType(Type.UDP_DST));
209
210 selector = DefaultTrafficSelector.builder()
211 .matchSctpSrc(shortValue).build();
212 assertThat(selector, hasCriterionWithType(Type.SCTP_SRC));
213
214 selector = DefaultTrafficSelector.builder()
215 .matchSctpDst(shortValue).build();
216 assertThat(selector, hasCriterionWithType(Type.SCTP_DST));
217
218 selector = DefaultTrafficSelector.builder()
219 .matchIcmpType(byteValue).build();
220 assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE));
221
222 selector = DefaultTrafficSelector.builder()
223 .matchIcmpCode(byteValue).build();
224 assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE));
225
226 selector = DefaultTrafficSelector.builder()
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800227 .matchIPv6Src(ipv6PrefixValue).build();
228 assertThat(selector, hasCriterionWithType(Type.IPV6_SRC));
229
230 selector = DefaultTrafficSelector.builder()
231 .matchIPv6Dst(ipv6PrefixValue).build();
232 assertThat(selector, hasCriterionWithType(Type.IPV6_DST));
233
234 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800235 .matchIPv6FlowLabel(intValue).build();
236 assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL));
237
238 selector = DefaultTrafficSelector.builder()
239 .matchIcmpv6Type(byteValue).build();
240 assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE));
241
242 selector = DefaultTrafficSelector.builder()
243 .matchIPv6NDTargetAddress(ipv6AddressValue).build();
244 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET));
245
246 selector = DefaultTrafficSelector.builder()
247 .matchIPv6NDSourceLinkLayerAddress(macValue).build();
248 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL));
249
250 selector = DefaultTrafficSelector.builder()
251 .matchIPv6NDTargetLinkLayerAddress(macValue).build();
252 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL));
253
254 selector = DefaultTrafficSelector.builder()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100255 .matchMplsLabel(MplsLabel.mplsLabel(3)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800256 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
257
258 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800259 .matchIPv6ExthdrFlags(Criterion.IPv6ExthdrFlags.NONEXT.getValue()).build();
260 assertThat(selector, hasCriterionWithType(Type.IPV6_EXTHDR));
261
262 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800263 .matchLambda(shortValue).build();
264 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
265
266 selector = DefaultTrafficSelector.builder()
267 .matchOpticalSignalType(shortValue).build();
268 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800269 }
270
271 /**
272 * Tests the traffic selector builder.
273 */
274 @Test
275 public void testTrafficSelectorBuilder() {
276 TrafficSelector selector;
277 final short shortValue = 33;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800278
279 final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800280 .matchLambda(shortValue).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800281 selector = DefaultTrafficSelector.builder(baseSelector)
282 .build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800283 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800284
285 final Criterion criterion = Criteria.matchLambda(shortValue);
286 selector = DefaultTrafficSelector.builder()
287 .add(criterion).build();
288 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
289 }
290}