blob: e232647e46311a6b7667c02e2bd4e587327c4e4d [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 Radoslavov320e6c92015-02-02 16:51:58 -0800130 final int intValue = 22;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800131 final short shortValue = 33;
132 final byte byteValue = 44;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800133 final MacAddress macValue = MacAddress.valueOf("11:22:33:44:55:66");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800134 final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800135 final IpPrefix ipv6PrefixValue = IpPrefix.valueOf("fe80::1/64");
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800136 final Ip6Address ipv6AddressValue = Ip6Address.valueOf("fe80::1");
Ray Milkeyf19b7152014-11-21 10:56:52 -0800137
138 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800139 .matchInPort(PortNumber.portNumber(11)).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800140 assertThat(selector, hasCriterionWithType(Type.IN_PORT));
141
142 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800143 .matchEthDst(macValue).build();
144 assertThat(selector, hasCriterionWithType(Type.ETH_DST));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800145
146 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800147 .matchEthSrc(macValue).build();
148 assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800149
150 selector = DefaultTrafficSelector.builder()
151 .matchEthType(shortValue).build();
152 assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
153
154 selector = DefaultTrafficSelector.builder()
155 .matchVlanId(VlanId.vlanId(shortValue)).build();
156 assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
157
158 selector = DefaultTrafficSelector.builder()
159 .matchVlanPcp(byteValue).build();
160 assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
161
162 selector = DefaultTrafficSelector.builder()
163 .matchIPProtocol(byteValue).build();
164 assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
165
166 selector = DefaultTrafficSelector.builder()
167 .matchIPSrc(ipPrefixValue).build();
168 assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
169
170 selector = DefaultTrafficSelector.builder()
171 .matchIPDst(ipPrefixValue).build();
172 assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
173
174 selector = DefaultTrafficSelector.builder()
175 .matchTcpSrc(shortValue).build();
176 assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
177
178 selector = DefaultTrafficSelector.builder()
179 .matchTcpDst(shortValue).build();
180 assertThat(selector, hasCriterionWithType(Type.TCP_DST));
181
182 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800183 .matchUdpSrc(shortValue).build();
184 assertThat(selector, hasCriterionWithType(Type.UDP_SRC));
185
186 selector = DefaultTrafficSelector.builder()
187 .matchUdpDst(shortValue).build();
188 assertThat(selector, hasCriterionWithType(Type.UDP_DST));
189
190 selector = DefaultTrafficSelector.builder()
191 .matchSctpSrc(shortValue).build();
192 assertThat(selector, hasCriterionWithType(Type.SCTP_SRC));
193
194 selector = DefaultTrafficSelector.builder()
195 .matchSctpDst(shortValue).build();
196 assertThat(selector, hasCriterionWithType(Type.SCTP_DST));
197
198 selector = DefaultTrafficSelector.builder()
199 .matchIcmpType(byteValue).build();
200 assertThat(selector, hasCriterionWithType(Type.ICMPV4_TYPE));
201
202 selector = DefaultTrafficSelector.builder()
203 .matchIcmpCode(byteValue).build();
204 assertThat(selector, hasCriterionWithType(Type.ICMPV4_CODE));
205
206 selector = DefaultTrafficSelector.builder()
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800207 .matchIPv6Src(ipv6PrefixValue).build();
208 assertThat(selector, hasCriterionWithType(Type.IPV6_SRC));
209
210 selector = DefaultTrafficSelector.builder()
211 .matchIPv6Dst(ipv6PrefixValue).build();
212 assertThat(selector, hasCriterionWithType(Type.IPV6_DST));
213
214 selector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800215 .matchIPv6FlowLabel(intValue).build();
216 assertThat(selector, hasCriterionWithType(Type.IPV6_FLABEL));
217
218 selector = DefaultTrafficSelector.builder()
219 .matchIcmpv6Type(byteValue).build();
220 assertThat(selector, hasCriterionWithType(Type.ICMPV6_TYPE));
221
222 selector = DefaultTrafficSelector.builder()
223 .matchIPv6NDTargetAddress(ipv6AddressValue).build();
224 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TARGET));
225
226 selector = DefaultTrafficSelector.builder()
227 .matchIPv6NDSourceLinkLayerAddress(macValue).build();
228 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_SLL));
229
230 selector = DefaultTrafficSelector.builder()
231 .matchIPv6NDTargetLinkLayerAddress(macValue).build();
232 assertThat(selector, hasCriterionWithType(Type.IPV6_ND_TLL));
233
234 selector = DefaultTrafficSelector.builder()
Ray Milkeyf19b7152014-11-21 10:56:52 -0800235 .matchMplsLabel(3).build();
236 assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
237
238 selector = DefaultTrafficSelector.builder()
239 .matchLambda(shortValue).build();
240 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
241
242 selector = DefaultTrafficSelector.builder()
243 .matchOpticalSignalType(shortValue).build();
244 assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800245 }
246
247 /**
248 * Tests the traffic selector builder.
249 */
250 @Test
251 public void testTrafficSelectorBuilder() {
252 TrafficSelector selector;
253 final short shortValue = 33;
Ray Milkeyf19b7152014-11-21 10:56:52 -0800254
255 final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800256 .matchLambda(shortValue).build();
Ray Milkeyf19b7152014-11-21 10:56:52 -0800257 selector = DefaultTrafficSelector.builder(baseSelector)
258 .build();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800259 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
Ray Milkeyf19b7152014-11-21 10:56:52 -0800260
261 final Criterion criterion = Criteria.matchLambda(shortValue);
262 selector = DefaultTrafficSelector.builder()
263 .add(criterion).build();
264 assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
265 }
266}