blob: a7f6bd29e099bf96f81ed5c57dee14c7b3c9a81d [file] [log] [blame]
Ray Milkey73ba84a2015-02-04 17:08:41 -08001/*
2 * Copyright 2015 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.onosproject.codec.impl;
17
18import java.util.EnumMap;
19
Ray Milkey9ee62f52015-02-06 13:47:35 -080020import org.junit.Before;
Ray Milkey73ba84a2015-02-04 17:08:41 -080021import org.junit.Test;
Ray Milkey9ee62f52015-02-06 13:47:35 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080030import org.onosproject.net.flow.criteria.Criterion;
31
Ray Milkey9ee62f52015-02-06 13:47:35 -080032import com.fasterxml.jackson.databind.node.ObjectNode;
33
Ray Milkey73ba84a2015-02-04 17:08:41 -080034import static org.onlab.junit.TestUtils.getField;
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.Matchers.*;
Ray Milkey46670a82015-02-08 17:57:17 -080037import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080038
39/**
40 * Unit tests for criterion codec.
41 */
42public class CriterionCodecTest {
43
Ray Milkey9ee62f52015-02-06 13:47:35 -080044 CodecContext context;
45 JsonCodec<Criterion> criterionCodec;
46 final PortNumber port = PortNumber.portNumber(1);
47 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
48 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
49 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
50
51 /**
52 * Sets up for each test. Creates a context and fetches the criterion
53 * codec.
54 */
55 @Before
56 public void setUp() {
57 context = new MockCodecContext();
58 criterionCodec = context.codec(Criterion.class);
59 assertThat(criterionCodec, notNullValue());
60 }
61
62
Ray Milkey73ba84a2015-02-04 17:08:41 -080063 /**
64 * Checks that all criterion types are covered by the codec.
65 */
66 @Test
67 public void checkCriterionTypes() throws Exception {
Ray Milkey9ee62f52015-02-06 13:47:35 -080068 EnumMap<Criterion.Type, Object> formatMap =
69 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080070 assertThat(formatMap, notNullValue());
71
72 for (Criterion.Type type : Criterion.Type.values()) {
73 assertThat("Entry not found for " + type.toString(),
74 formatMap.get(type), notNullValue());
75 }
76 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080077
78 /**
79 * Tests in port criterion.
80 */
81 @Test
82 public void matchInPortTest() {
83 Criterion criterion = Criteria.matchInPort(port);
84 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080085 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080086 }
87
88 /**
89 * Tests in physical port criterion.
90 */
91 @Test
92 public void matchInPhyPortTest() {
93 Criterion criterion = Criteria.matchInPhyPort(port);
94 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080095 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080096 }
97
98 /**
99 * Tests metadata criterion.
100 */
101 @Test
102 public void matchMetadataTest() {
103 Criterion criterion = Criteria.matchMetadata(0xabcdL);
104 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800105 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800106 }
107
108 /**
109 * Tests ethernet destination criterion.
110 */
111 @Test
112 public void matchEthDstTest() {
113 Criterion criterion = Criteria.matchEthDst(mac1);
114 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800115 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800116 }
117
118 /**
119 * Tests ethernet source criterion.
120 */
121 @Test
122 public void matchEthSrcTest() {
123 Criterion criterion = Criteria.matchEthSrc(mac1);
124 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800125 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800126 }
127
128 /**
129 * Tests ethernet type criterion.
130 */
131 @Test
132 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800133 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800134 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800135 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800136 }
137
138 /**
139 * Tests VLAN Id criterion.
140 */
141 @Test
142 public void matchVlanIdTest() {
143 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
144 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800145 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800146 }
147
148 /**
149 * Tests VLAN PCP criterion.
150 */
151 @Test
152 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800153 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800154 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800155 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800156 }
157
158 /**
159 * Tests IP DSCP criterion.
160 */
161 @Test
162 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800163 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800164 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800165 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800166 }
167
168 /**
169 * Tests IP ECN criterion.
170 */
171 @Test
172 public void matchIPEcnTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800173 Criterion criterion = Criteria.matchIPEcn((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800174 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800175 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800176 }
177
178 /**
179 * Tests IP protocol criterion.
180 */
181 @Test
182 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800183 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800184 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800185 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800186 }
187
188 /**
189 * Tests IP source criterion.
190 */
191 @Test
192 public void matchIPSrcTest() {
193 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
194 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800195 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800196 }
197
198 /**
199 * Tests IP destination criterion.
200 */
201 @Test
202 public void matchIPDstTest() {
203 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
204 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800205 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800206 }
207
208 /**
209 * Tests source TCP port criterion.
210 */
211 @Test
212 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800213 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800214 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800215 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800216 }
217
218 /**
219 * Tests destination TCP port criterion.
220 */
221 @Test
222 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800223 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800224 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800225 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800226 }
227
228 /**
229 * Tests source UDP port criterion.
230 */
231 @Test
232 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800233 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800234 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800235 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800236 }
237
238 /**
239 * Tests destination UDP criterion.
240 */
241 @Test
242 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800243 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800244 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800245 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800246 }
247
248 /**
249 * Tests source SCTP criterion.
250 */
251 @Test
252 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800253 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800254 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800255 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800256 }
257
258 /**
259 * Tests destination SCTP criterion.
260 */
261 @Test
262 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800263 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800264 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800265 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800266 }
267
268 /**
269 * Tests ICMP type criterion.
270 */
271 @Test
272 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800273 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800274 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800275 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800276 }
277
278 /**
279 * Tests ICMP code criterion.
280 */
281 @Test
282 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800283 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800284 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800285 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800286 }
287
288 /**
289 * Tests IPv6 source criterion.
290 */
291 @Test
292 public void matchIPv6SrcTest() {
293 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
294 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800295 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800296 }
297
298 /**
299 * Tests IPv6 destination criterion.
300 */
301 @Test
302 public void matchIPv6DstTest() {
303 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
304 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800305 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800306 }
307
308 /**
309 * Tests IPv6 flow label criterion.
310 */
311 @Test
312 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800313 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800314 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800315 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800316 }
317
318 /**
319 * Tests ICMP v6 type criterion.
320 */
321 @Test
322 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800323 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800324 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800325 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800326 }
327
328 /**
329 * Tests ICMP v6 code criterion.
330 */
331 @Test
332 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800333 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800334 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800335 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800336 }
337
338 /**
339 * Tests IPV6 target address criterion.
340 */
341 @Test
342 public void matchIPv6NDTargetAddressTest() {
343 Criterion criterion =
344 Criteria.matchIPv6NDTargetAddress(
345 Ip6Address.valueOf("1111:2222::"));
346 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800347 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800348 }
349
350 /**
351 * Tests IPV6 SLL criterion.
352 */
353 @Test
354 public void matchIPv6NDSourceLinkLayerAddressTest() {
355 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
356 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800357 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800358 }
359
360 /**
361 * Tests IPV6 TLL criterion.
362 */
363 @Test
364 public void matchIPv6NDTargetLinkLayerAddressTest() {
365 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
366 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800367 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800368 }
369
370 /**
371 * Tests MPLS label criterion.
372 */
373 @Test
374 public void matchMplsLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800375 Criterion criterion = Criteria.matchMplsLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800376 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800377 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800378 }
379
380 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800381 * Tests IPv6 Extension Header pseudo-field flags criterion.
382 */
383 @Test
384 public void matchIPv6ExthdrFlagsTest() {
385 int exthdrFlags =
386 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
387 Criterion.IPv6ExthdrFlags.ESP.getValue() |
388 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
389 Criterion.IPv6ExthdrFlags.DEST.getValue() |
390 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
391 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
392 Criterion.IPv6ExthdrFlags.HOP.getValue() |
393 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
394 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
395 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
396 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800397
398 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800399 }
400
401 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800402 * Tests lambda criterion.
403 */
404 @Test
405 public void matchLambdaTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800406 Criterion criterion = Criteria.matchLambda((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800407 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800408 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800409 }
410
411 /**
412 * Tests optical signal type criterion.
413 */
414 @Test
415 public void matchOpticalSignalTypeTest() {
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800416 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800417 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800418 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800419 }
420
Ray Milkey73ba84a2015-02-04 17:08:41 -0800421}