blob: e553e6624b23073b7ebaef45ab33211cfe86fb8d [file] [log] [blame]
Ray Milkey33d90232014-11-04 10:49:00 -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 */
16package org.onlab.onos.net.flow.criteria;
17
18import org.junit.Test;
19import org.onlab.onos.net.PortNumber;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23
Ray Milkeyfff20b52014-11-06 14:45:10 -080024import com.google.common.testing.EqualsTester;
25
Ray Milkey33d90232014-11-04 10:49:00 -080026import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey33d90232014-11-04 10:49:00 -080027import static org.hamcrest.Matchers.equalTo;
28import static org.hamcrest.Matchers.instanceOf;
29import static org.hamcrest.Matchers.is;
Ray Milkey33d90232014-11-04 10:49:00 -080030import static org.hamcrest.Matchers.notNullValue;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
33import static org.onlab.onos.net.PortNumber.portNumber;
34
35/**
36 * Unit tests for the Criteria class and its subclasses.
37 */
38public class CriteriaTest {
39
40 final PortNumber port1 = portNumber(1);
41 final PortNumber port2 = portNumber(2);
42
43 Criterion matchInPort1 = Criteria.matchInPort(port1);
44 Criterion sameAsMatchInPort1 = Criteria.matchInPort(port1);
45 Criterion matchInPort2 = Criteria.matchInPort(port2);
46
47 Criterion matchTcpPort1 = Criteria.matchTcpSrc((short) 1);
48 Criterion sameAsMatchTcpPort1 = Criteria.matchTcpSrc((short) 1);
49 Criterion matchTcpPort2 = Criteria.matchTcpDst((short) 2);
50
51 private static final String MAC1 = "00:00:00:00:00:01";
52 private static final String MAC2 = "00:00:00:00:00:02";
53 private MacAddress mac1 = new MacAddress(MAC1.getBytes());
54 private MacAddress mac2 = new MacAddress(MAC2.getBytes());
55 Criterion matchEth1 = Criteria.matchEthSrc(mac1);
56 Criterion sameAsMatchEth1 = Criteria.matchEthSrc(mac1);
57 Criterion matchEth2 = Criteria.matchEthDst(mac2);
58
59 short ethType1 = 1;
60 short ethType2 = 2;
61 Criterion matchEthType1 = Criteria.matchEthType(ethType1);
62 Criterion sameAsMatchEthType1 = Criteria.matchEthType(ethType1);
63 Criterion matchEthType2 = Criteria.matchEthType(ethType2);
64
65 short vlan1 = 1;
66 short vlan2 = 2;
67 VlanId vlanId1 = VlanId.vlanId(vlan1);
68 VlanId vlanId2 = VlanId.vlanId(vlan2);
69 Criterion matchVlanId1 = Criteria.matchVlanId(vlanId1);
70 Criterion sameAsMatchVlanId1 = Criteria.matchVlanId(vlanId1);
71 Criterion matchVlanId2 = Criteria.matchVlanId(vlanId2);
72
73 byte vlanPcp1 = 1;
74 byte vlanPcp2 = 2;
75 Criterion matchVlanPcp1 = Criteria.matchVlanPcp(vlanPcp1);
76 Criterion sameAsMatchVlanPcp1 = Criteria.matchVlanPcp(vlanPcp1);
77 Criterion matchVlanPcp2 = Criteria.matchVlanPcp(vlanPcp2);
78
79 byte protocol1 = 1;
80 byte protocol2 = 2;
81 Criterion matchIpProtocol1 = Criteria.matchIPProtocol(protocol1);
82 Criterion sameAsMatchIpProtocol1 = Criteria.matchIPProtocol(protocol1);
83 Criterion matchIpProtocol2 = Criteria.matchIPProtocol(protocol2);
84
85 private static final String IP1 = "1.2.3.4/24";
86 private static final String IP2 = "5.6.7.8/24";
87 private IpPrefix ip1 = IpPrefix.valueOf(IP1);
88 private IpPrefix ip2 = IpPrefix.valueOf(IP2);
89 Criterion matchIp1 = Criteria.matchIPSrc(ip1);
90 Criterion sameAsMatchIp1 = Criteria.matchIPSrc(ip1);
91 Criterion matchIp2 = Criteria.matchIPSrc(ip2);
92
93 short lambda1 = 1;
94 short lambda2 = 2;
95 Criterion matchLambda1 = Criteria.matchLambda(lambda1);
96 Criterion sameAsMatchLambda1 = Criteria.matchLambda(lambda1);
97 Criterion matchLambda2 = Criteria.matchLambda(lambda2);
98
Ray Milkey9d078652014-11-05 10:35:54 -080099 short signalLambda1 = 1;
100 short signalLambda2 = 2;
Ray Milkey33d90232014-11-04 10:49:00 -0800101 Criterion matchSignalLambda1 = Criteria.matchOpticalSignalType(signalLambda1);
102 Criterion sameAsMatchSignalLambda1 = Criteria.matchOpticalSignalType(signalLambda1);
103 Criterion matchSignalLambda2 = Criteria.matchOpticalSignalType(signalLambda2);
104
105 /**
106 * Checks that a Criterion object has the proper type, and then converts
107 * it to the proper type.
108 *
109 * @param criterion Criterion object to convert
110 * @param type Enumerated type value for the Criterion class
111 * @param clazz Desired Criterion class
112 * @param <T> The type the caller wants returned
113 * @return converted object
114 */
115 @SuppressWarnings("unchecked")
116 private <T> T checkAndConvert(Criterion criterion, Criterion.Type type, Class clazz) {
117 assertThat(criterion, is(notNullValue()));
118 assertThat(criterion.type(), is(equalTo(type)));
Ray Milkey78081052014-11-05 10:38:12 -0800119 assertThat(criterion, instanceOf(clazz));
Ray Milkey33d90232014-11-04 10:49:00 -0800120 return (T) criterion;
121 }
122
123 /**
Ray Milkey33d90232014-11-04 10:49:00 -0800124 * Check that the Criteria class is a valid utility class.
125 */
126 @Test
127 public void testCriteriaUtility() {
128 assertThatClassIsUtility(Criteria.class);
129 }
130
131 /**
132 * Check that the Criteria implementations are immutable.
133 */
134 @Test
135 public void testCriteriaImmutability() {
136 assertThatClassIsImmutable(Criteria.PortCriterion.class);
137 assertThatClassIsImmutable(Criteria.EthCriterion.class);
138 assertThatClassIsImmutable(Criteria.EthTypeCriterion.class);
139 assertThatClassIsImmutable(Criteria.IPCriterion.class);
140 assertThatClassIsImmutable(Criteria.IPProtocolCriterion.class);
141 assertThatClassIsImmutable(Criteria.VlanPcpCriterion.class);
142 assertThatClassIsImmutable(Criteria.VlanIdCriterion.class);
143 assertThatClassIsImmutable(Criteria.TcpPortCriterion.class);
144 assertThatClassIsImmutable(Criteria.LambdaCriterion.class);
145 assertThatClassIsImmutable(Criteria.OpticalSignalTypeCriterion.class);
146 }
147
148 // PortCriterion class
149
150 /**
151 * Test the matchInPort method.
152 */
153 @Test
154 public void testMatchInPortMethod() {
155 PortNumber p1 = portNumber(1);
156 Criterion matchInPort = Criteria.matchInPort(p1);
157 Criteria.PortCriterion portCriterion =
158 checkAndConvert(matchInPort,
159 Criterion.Type.IN_PORT,
160 Criteria.PortCriterion.class);
161 assertThat(portCriterion.port(), is(equalTo(p1)));
162 }
163
164 /**
165 * Test the equals() method of the PortCriterion class.
166 */
167 @Test
168 public void testPortCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800169 new EqualsTester()
170 .addEqualityGroup(matchInPort1, sameAsMatchInPort1)
171 .addEqualityGroup(matchInPort2)
172 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800173 }
174
175 // EthCriterion class
176
177 /**
178 * Test the matchEthSrc method.
179 */
180 @Test
181 public void testMatchEthSrcMethod() {
182 Criterion matchEthSrc = Criteria.matchEthSrc(mac1);
183 Criteria.EthCriterion ethCriterion =
184 checkAndConvert(matchEthSrc,
185 Criterion.Type.ETH_SRC,
186 Criteria.EthCriterion.class);
187 assertThat(ethCriterion.mac(), is(mac1));
188 }
189
190 /**
191 * Test the matchEthDst method.
192 */
193 @Test
194 public void testMatchEthDstMethod() {
195 Criterion matchTcpDst = Criteria.matchEthDst(mac1);
196 Criteria.EthCriterion ethCriterion =
197 checkAndConvert(matchTcpDst,
198 Criterion.Type.ETH_DST,
199 Criteria.EthCriterion.class);
200 assertThat(ethCriterion.mac(), is(equalTo(mac1)));
201 }
202
203 /**
204 * Test the equals() method of the EthCriterion class.
205 */
206 @Test
207 public void testEthCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800208 new EqualsTester()
209 .addEqualityGroup(matchEth1, sameAsMatchEth1)
210 .addEqualityGroup(matchEth2)
211 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800212 }
213
Ray Milkey33d90232014-11-04 10:49:00 -0800214 // TcpPortCriterion class
215
216 /**
217 * Test the matchTcpSrc method.
218 */
219 @Test
220 public void testMatchTcpSrcMethod() {
221 Criterion matchTcpSrc = Criteria.matchTcpSrc((short) 1);
222 Criteria.TcpPortCriterion tcpPortCriterion =
223 checkAndConvert(matchTcpSrc,
224 Criterion.Type.TCP_SRC,
225 Criteria.TcpPortCriterion.class);
226 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
227 }
228
229 /**
230 * Test the matchTcpDst method.
231 */
232 @Test
233 public void testMatchTcpDstMethod() {
234 Criterion matchTcpDst = Criteria.matchTcpDst((short) 1);
235 Criteria.TcpPortCriterion tcpPortCriterion =
236 checkAndConvert(matchTcpDst,
237 Criterion.Type.TCP_DST,
238 Criteria.TcpPortCriterion.class);
239 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
240 }
241
242 /**
243 * Test the equals() method of the TcpPortCriterion class.
244 */
245 @Test
246 public void testTcpPortCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800247 new EqualsTester()
248 .addEqualityGroup(matchTcpPort1, sameAsMatchTcpPort1)
249 .addEqualityGroup(matchTcpPort2)
250 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800251 }
252
Ray Milkey33d90232014-11-04 10:49:00 -0800253 // EthTypeCriterion class
254
255 /**
256 * Test the matchEthType method.
257 */
258 @Test
259 public void testMatchEthTypeMethod() {
260 Short ethType = 12;
261 Criterion matchEthType = Criteria.matchEthType(ethType);
262 Criteria.EthTypeCriterion ethTypeCriterion =
263 checkAndConvert(matchEthType,
264 Criterion.Type.ETH_TYPE,
265 Criteria.EthTypeCriterion.class);
266 assertThat(ethTypeCriterion.ethType(), is(equalTo(ethType)));
267 }
268
269 /**
270 * Test the equals() method of the EthTypeCriterion class.
271 */
272 @Test
273 public void testEthTypeCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800274 new EqualsTester()
275 .addEqualityGroup(matchEthType1, sameAsMatchEthType1)
276 .addEqualityGroup(matchEthType2)
277 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800278 }
279
Ray Milkey33d90232014-11-04 10:49:00 -0800280 // VlanIdCriterion class
281
282 /**
283 * Test the matchVlanId method.
284 */
285 @Test
286 public void testMatchVlanIdMethod() {
287 Criterion matchVlanId = Criteria.matchVlanId(vlanId1);
288 Criteria.VlanIdCriterion vlanIdCriterion =
289 checkAndConvert(matchVlanId,
290 Criterion.Type.VLAN_VID,
291 Criteria.VlanIdCriterion.class);
292 assertThat(vlanIdCriterion.vlanId(), is(equalTo(vlanId1)));
293 }
294
295 /**
296 * Test the equals() method of the VlanIdCriterion class.
297 */
298 @Test
299 public void testVlanIdCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800300 new EqualsTester()
301 .addEqualityGroup(matchVlanId1, sameAsMatchVlanId1)
302 .addEqualityGroup(matchVlanId2)
303 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800304 }
305
306 // VlanPcpCriterion class
307
308 /**
309 * Test the matchVlanPcp method.
310 */
311 @Test
312 public void testMatchVlanPcpMethod() {
313 Criterion matchVlanPcp = Criteria.matchVlanPcp(vlanPcp1);
314 Criteria.VlanPcpCriterion vlanPcpCriterion =
315 checkAndConvert(matchVlanPcp,
316 Criterion.Type.VLAN_PCP,
317 Criteria.VlanPcpCriterion.class);
318 assertThat(vlanPcpCriterion.priority(), is(equalTo(vlanPcp1)));
319 }
320
321 /**
322 * Test the equals() method of the VlanPcpCriterion class.
323 */
324 @Test
325 public void testVlanPcpCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800326 new EqualsTester()
327 .addEqualityGroup(matchVlanPcp1, sameAsMatchVlanPcp1)
328 .addEqualityGroup(matchVlanPcp2)
329 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800330 }
331
332 // IpProtocolCriterion class
333
334 /**
335 * Test the matchIpProtocol method.
336 */
337 @Test
338 public void testMatchIpProtocolMethod() {
339 Criterion matchIPProtocol = Criteria.matchIPProtocol(protocol1);
340 Criteria.IPProtocolCriterion ipProtocolCriterion =
341 checkAndConvert(matchIPProtocol,
342 Criterion.Type.IP_PROTO,
343 Criteria.IPProtocolCriterion.class);
344 assertThat(ipProtocolCriterion.protocol(), is(equalTo(protocol1)));
345 }
346
347 /**
348 * Test the equals() method of the IpProtocolCriterion class.
349 */
350 @Test
351 public void testIpProtocolCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800352 new EqualsTester()
353 .addEqualityGroup(matchIpProtocol1, sameAsMatchIpProtocol1)
354 .addEqualityGroup(matchIpProtocol2)
355 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800356 }
357
358 // IPCriterion class
359
360 /**
361 * Test the matchIPSrc method.
362 */
363 @Test
364 public void testMatchIPSrcMethod() {
365 Criterion matchIpSrc = Criteria.matchIPSrc(ip1);
366 Criteria.IPCriterion ipCriterion =
367 checkAndConvert(matchIpSrc,
368 Criterion.Type.IPV4_SRC,
369 Criteria.IPCriterion.class);
370 assertThat(ipCriterion.ip(), is(ip1));
371 }
372
373 /**
374 * Test the matchIPDst method.
375 */
376 @Test
377 public void testMatchIPDstMethod() {
378 Criterion matchIPDst = Criteria.matchIPDst(ip1);
379 Criteria.IPCriterion ipCriterion =
380 checkAndConvert(matchIPDst,
381 Criterion.Type.IPV4_DST,
382 Criteria.IPCriterion.class);
383 assertThat(ipCriterion.ip(), is(equalTo(ip1)));
384 }
385
386 /**
387 * Test the equals() method of the IpCriterion class.
388 */
389 @Test
390 public void testIPCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800391 new EqualsTester()
392 .addEqualityGroup(matchIp1, sameAsMatchIp1)
393 .addEqualityGroup(matchIp2)
394 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800395 }
396
397 // LambdaCriterion class
398
399 /**
400 * Test the matchLambda method.
401 */
402 @Test
403 public void testMatchLambdaMethod() {
404 Criterion matchLambda = Criteria.matchLambda(lambda1);
405 Criteria.LambdaCriterion lambdaCriterion =
406 checkAndConvert(matchLambda,
407 Criterion.Type.OCH_SIGID,
408 Criteria.LambdaCriterion.class);
409 assertThat(lambdaCriterion.lambda(), is(equalTo(lambda1)));
410 }
411
412 /**
413 * Test the equals() method of the LambdaCriterion class.
414 */
415 @Test
416 public void testLambdaCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800417 new EqualsTester()
418 .addEqualityGroup(matchLambda1, sameAsMatchLambda1)
419 .addEqualityGroup(matchLambda2)
420 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800421 }
422
423 // OpticalSignalTypeCriterion class
424
425 /**
426 * Test the matchOpticalSignalType method.
427 */
428 @Test
429 public void testMatchOpticalSignalTypeMethod() {
430 Criterion matchLambda = Criteria.matchOpticalSignalType(signalLambda1);
431 Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
432 checkAndConvert(matchLambda,
433 Criterion.Type.OCH_SIGTYPE,
434 Criteria.OpticalSignalTypeCriterion.class);
435 assertThat(opticalSignalTypeCriterion.signalType(), is(equalTo(signalLambda1)));
436 }
437
438 /**
439 * Test the equals() method of the OpticalSignalTypeCriterion class.
440 */
441 @Test
442 public void testOpticalSignalTypeCriterionEquals() {
Ray Milkeyfff20b52014-11-06 14:45:10 -0800443 new EqualsTester()
444 .addEqualityGroup(matchSignalLambda1, sameAsMatchSignalLambda1)
445 .addEqualityGroup(matchSignalLambda2)
446 .testEquals();
Ray Milkey33d90232014-11-04 10:49:00 -0800447 }
Ray Milkey33d90232014-11-04 10:49:00 -0800448}