blob: 3fb62a7dc4713e83540200d38475bae804e78457 [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;
31import org.onlab.packet.VlanId;
32
33import com.google.common.testing.EqualsTester;
34
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.Matchers.hasSize;
37import static org.hamcrest.Matchers.notNullValue;
38import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import static org.onosproject.net.flow.criteria.Criterion.Type;
Ray Milkeyf19b7152014-11-21 10:56:52 -080040
41/**
42 * Unit tests for default traffic selector class.
43 */
44public class DefaultTrafficSelectorTest {
45
46 /**
47 * Checks that the DefaultFlowRule class is immutable.
48 */
49 @Test
50 public void testImmutability() {
51 assertThatClassIsImmutable(DefaultTrafficSelector.class);
52 }
53
54 /**
55 * Tests equals(), hashCode() and toString() methods.
56 */
57 @Test
58 public void testEquals() {
59 final short one = 1;
60 final short two = 2;
61
62 final TrafficSelector selector1 =
63 DefaultTrafficSelector.builder().matchLambda(one).build();
64 final TrafficSelector sameAsSelector1 =
65 DefaultTrafficSelector.builder().matchLambda(one).build();
66 final TrafficSelector selector2 =
67 DefaultTrafficSelector.builder().matchLambda(two).build();
68
69 new EqualsTester()
70 .addEqualityGroup(selector1, sameAsSelector1)
71 .addEqualityGroup(selector2)
72 .testEquals();
73 }
74
75 /**
Ray Milkeyff517732014-12-01 10:40:14 -080076 * Hamcrest matcher to check that a selector contains a
77 * Criterion with the specified type.
Ray Milkeyf19b7152014-11-21 10:56:52 -080078 */
79 public static final class CriterionExistsMatcher
80 extends TypeSafeMatcher<TrafficSelector> {
81 private final Criterion.Type type;
82
Ray Milkeyff517732014-12-01 10:40:14 -080083 /**
84 * Constructs a matcher for the given criterion type.
85 *
86 * @param typeValue criterion type to match
87 */
Ray Milkeyf19b7152014-11-21 10:56:52 -080088 public CriterionExistsMatcher(Criterion.Type typeValue) {
89 type = typeValue;
90 }
91
92 @Override
93 public boolean matchesSafely(TrafficSelector selector) {
94 final Set<Criterion> criteria = selector.criteria();
95
96 return notNullValue().matches(criteria) &&
97 hasSize(1).matches(criteria) &&
98 notNullValue().matches(selector.getCriterion(type));
99 }
100
101 @Override
102 public void describeTo(Description description) {
103 description.appendText("a criterion with type \" ").
104 appendText(type.toString()).
105 appendText("\"");
106 }
107 }
108
109
110 /**
Ray Milkeyff517732014-12-01 10:40:14 -0800111 * Creates a criterion type matcher. Returns a matcher
Ray Milkeyf19b7152014-11-21 10:56:52 -0800112 * for a criterion with the given type.
113 *
114 * @param type type of Criterion to match
115 * @return Matcher object
116 */
117 @Factory
118 public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
119 return new CriterionExistsMatcher(type);
120 }
121
122
123 /**
124 * Tests the builder functions that add specific criteria.
125 */
126 @Test
127 public void testCriteriaCreation() {
128 TrafficSelector selector;
129
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800130 final long longValue = 0x12345678;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800131 final int intValue = 22;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800132 final short shortValue = 33;
133 final byte byteValue = 44;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800134 final byte dscpValue = 0xf;
135 final byte ecnValue = 3;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800136 final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800137 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800138 final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64");
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800139 final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800140
141 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800142 .matchInPort(PortNumber.portNumber(11)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800143 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
144
145 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800146 .matchInPhyPort(PortNumber.portNumber(11)).build();
147 assertThat(selector, hasCriterionWithType(Type.IN_PHY_PORT));
148
149 selector = DefaultTrafficSelector.builder()
150 .matchMetadata(longValue).build();
151 assertThat(selector, hasCriterionWithType(Type.METADATA));
152
153 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800154 .matchEthDst(macValue).build();
155 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800156
157 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800158 .matchEthSrc(macValue).build();
159 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800160
161 selector = DefaultTrafficSelector.builder()
162 .matchEthType(shortValue).build();
163 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
164
165 selector = DefaultTrafficSelector.builder()
166 .matchVlanId(VlanId.vlanId(shortValue)).build();
167 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
168
169 selector = DefaultTrafficSelector.builder()
170 .matchVlanPcp(byteValue).build();
171 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
172
173 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800174 .matchIPDscp(dscpValue).build();
175 assertThat(selector, hasCriterionWithType(Type.IP_DSCP));
176
177 selector = DefaultTrafficSelector.builder()
178 .matchIPEcn(ecnValue).build();
179 assertThat(selector, hasCriterionWithType(Type.IP_ECN));
180
181 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800182 .matchIPProtocol(byteValue).build();
183 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
184
185 selector = DefaultTrafficSelector.builder()
186 .matchIPSrc(ipPrefixValue).build();
187 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
188
189 selector = DefaultTrafficSelector.builder()
190 .matchIPDst(ipPrefixValue).build();
191 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
192
193 selector = DefaultTrafficSelector.builder()
194 .matchTcpSrc(shortValue).build();
195 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
196
197 selector = DefaultTrafficSelector.builder()
198 .matchTcpDst(shortValue).build();
199 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
200
201 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800202 .matchUdpSrc(shortValue).build();
203 assertThat(selector, hasCriterionWithType(Type.UDP_SRC));
204
205 selector = DefaultTrafficSelector.builder()
206 .matchUdpDst(shortValue).build();
207 assertThat(selector, hasCriterionWithType(Type.UDP_DST));
208
209 selector = DefaultTrafficSelector.builder()
210 .matchSctpSrc(shortValue).build();
211 assertThat(selector, hasCriterionWithType(Type.SCTP_SRC));
212
213 selector = DefaultTrafficSelector.builder()
214 .matchSctpDst(shortValue).build();
215 assertThat(selector, hasCriterionWithType(Type.SCTP_DST));
216
217 selector = DefaultTrafficSelector.builder()
218 .matchIcmpType(byteValue).build();
219 assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE));
220
221 selector = DefaultTrafficSelector.builder()
222 .matchIcmpCode(byteValue).build();
223 assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE));
224
225 selector = DefaultTrafficSelector.builder()
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800226 .matchIPv6Src(ipv6PrefixValue).build();
227 assertThat(selector, hasCriterionWithType(Type.IPV6_SRC));
228
229 selector = DefaultTrafficSelector.builder()
230 .matchIPv6Dst(ipv6PrefixValue).build();
231 assertThat(selector, hasCriterionWithType(Type.IPV6_DST));
232
233 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800234 .matchIPv6FlowLabel(intValue).build();
235 assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL));
236
237 selector = DefaultTrafficSelector.builder()
238 .matchIcmpv6Type(byteValue).build();
239 assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE));
240
241 selector = DefaultTrafficSelector.builder()
242 .matchIPv6NDTargetAddress(ipv6AddressValue).build();
243 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET));
244
245 selector = DefaultTrafficSelector.builder()
246 .matchIPv6NDSourceLinkLayerAddress(macValue).build();
247 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL));
248
249 selector = DefaultTrafficSelector.builder()
250 .matchIPv6NDTargetLinkLayerAddress(macValue).build();
251 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL));
252
253 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800254 .matchMplsLabel(3).build();
255 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
256
257 selector = DefaultTrafficSelector.builder()
258 .matchLambda(shortValue).build();
259 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
260
261 selector = DefaultTrafficSelector.builder()
262 .matchOpticalSignalType(shortValue).build();
263 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800264 }
265
266 /**
267 * Tests the traffic selector builder.
268 */
269 @Test
270 public void testTrafficSelectorBuilder() {
271 TrafficSelector selector;
272 final short shortValue = 33;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800273
274 final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800275 .matchLambda(shortValue).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800276 selector = DefaultTrafficSelector.builder(baseSelector)
277 .build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800278 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800279
280 final Criterion criterion = Criteria.matchLambda(shortValue);
281 selector = DefaultTrafficSelector.builder()
282 .add(criterion).build();
283 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
284 }
285}