blob: 3efc018bf333deaa7c9c7fb3d046476a4d35ac0e [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.*;
37
38/**
39 * Unit tests for criterion codec.
40 */
41public class CriterionCodecTest {
42
Ray Milkey9ee62f52015-02-06 13:47:35 -080043 CodecContext context;
44 JsonCodec<Criterion> criterionCodec;
45 final PortNumber port = PortNumber.portNumber(1);
46 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
47 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
48 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
49
50 /**
51 * Sets up for each test. Creates a context and fetches the criterion
52 * codec.
53 */
54 @Before
55 public void setUp() {
56 context = new MockCodecContext();
57 criterionCodec = context.codec(Criterion.class);
58 assertThat(criterionCodec, notNullValue());
59 }
60
61
Ray Milkey73ba84a2015-02-04 17:08:41 -080062 /**
63 * Checks that all criterion types are covered by the codec.
64 */
65 @Test
66 public void checkCriterionTypes() throws Exception {
Ray Milkey9ee62f52015-02-06 13:47:35 -080067 EnumMap<Criterion.Type, Object> formatMap =
68 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080069 assertThat(formatMap, notNullValue());
70
71 for (Criterion.Type type : Criterion.Type.values()) {
72 assertThat("Entry not found for " + type.toString(),
73 formatMap.get(type), notNullValue());
74 }
75 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080076
77 /**
78 * Tests in port criterion.
79 */
80 @Test
81 public void matchInPortTest() {
82 Criterion criterion = Criteria.matchInPort(port);
83 ObjectNode result = criterionCodec.encode(criterion, context);
84 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
85 assertThat(result.get("port").asLong(), is(port.toLong()));
86 }
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);
95 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
96 assertThat(result.get("port").asLong(), is(port.toLong()));
97 }
98
99 /**
100 * Tests metadata criterion.
101 */
102 @Test
103 public void matchMetadataTest() {
104 Criterion criterion = Criteria.matchMetadata(0xabcdL);
105 ObjectNode result = criterionCodec.encode(criterion, context);
106 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
107 assertThat(result.get("metadata").asLong(), is(0xabcdL));
108 }
109
110 /**
111 * Tests ethernet destination criterion.
112 */
113 @Test
114 public void matchEthDstTest() {
115 Criterion criterion = Criteria.matchEthDst(mac1);
116 ObjectNode result = criterionCodec.encode(criterion, context);
117 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
118 assertThat(result.get("mac").asText(), is(mac1.toString()));
119 }
120
121 /**
122 * Tests ethernet source criterion.
123 */
124 @Test
125 public void matchEthSrcTest() {
126 Criterion criterion = Criteria.matchEthSrc(mac1);
127 ObjectNode result = criterionCodec.encode(criterion, context);
128 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
129 assertThat(result.get("mac").asText(), is(mac1.toString()));
130 }
131
132 /**
133 * Tests ethernet type criterion.
134 */
135 @Test
136 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800137 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800138 ObjectNode result = criterionCodec.encode(criterion, context);
139 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800140 assertThat(result.get("ethType").asInt(), is(0x8844));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800141 }
142
143 /**
144 * Tests VLAN Id criterion.
145 */
146 @Test
147 public void matchVlanIdTest() {
148 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
149 ObjectNode result = criterionCodec.encode(criterion, context);
150 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
151 assertThat((short) result.get("vlanId").asInt(), is(VlanId.ANY.toShort()));
152 }
153
154 /**
155 * Tests VLAN PCP criterion.
156 */
157 @Test
158 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800159 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800160 ObjectNode result = criterionCodec.encode(criterion, context);
161 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800162 assertThat(result.get("priority").asInt(), is(7));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800163 }
164
165 /**
166 * Tests IP DSCP criterion.
167 */
168 @Test
169 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800170 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800171 ObjectNode result = criterionCodec.encode(criterion, context);
172 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800173 assertThat(result.get("ipDscp").asInt(), is(63));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800174 }
175
176 /**
177 * Tests IP ECN criterion.
178 */
179 @Test
180 public void matchIPEcnTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800181 Criterion criterion = Criteria.matchIPEcn((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800182 ObjectNode result = criterionCodec.encode(criterion, context);
183 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800184 assertThat(result.get("ipEcn").asInt(), is(7));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800185 }
186
187 /**
188 * Tests IP protocol criterion.
189 */
190 @Test
191 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800192 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800193 ObjectNode result = criterionCodec.encode(criterion, context);
194 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800195 assertThat(result.get("protocol").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800196 }
197
198 /**
199 * Tests IP source criterion.
200 */
201 @Test
202 public void matchIPSrcTest() {
203 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
204 ObjectNode result = criterionCodec.encode(criterion, context);
205 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
206 assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
207 }
208
209 /**
210 * Tests IP destination criterion.
211 */
212 @Test
213 public void matchIPDstTest() {
214 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
215 ObjectNode result = criterionCodec.encode(criterion, context);
216 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
217 assertThat(result.get("ip").asText(), is(ipPrefix4.toString()));
218 }
219
220 /**
221 * Tests source TCP port criterion.
222 */
223 @Test
224 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800225 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800226 ObjectNode result = criterionCodec.encode(criterion, context);
227 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800228 assertThat(result.get("tcpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800229 }
230
231 /**
232 * Tests destination TCP port criterion.
233 */
234 @Test
235 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800236 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800237 ObjectNode result = criterionCodec.encode(criterion, context);
238 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800239 assertThat(result.get("tcpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800240 }
241
242 /**
243 * Tests source UDP port criterion.
244 */
245 @Test
246 public void matchUdpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800247 Criterion criterion = Criteria.matchUdpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800248 ObjectNode result = criterionCodec.encode(criterion, context);
249 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800250 assertThat(result.get("udpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800251 }
252
253 /**
254 * Tests destination UDP criterion.
255 */
256 @Test
257 public void matchUdpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800258 Criterion criterion = Criteria.matchUdpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800259 ObjectNode result = criterionCodec.encode(criterion, context);
260 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800261 assertThat(result.get("udpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800262 }
263
264 /**
265 * Tests source SCTP criterion.
266 */
267 @Test
268 public void matchSctpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800269 Criterion criterion = Criteria.matchSctpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800270 ObjectNode result = criterionCodec.encode(criterion, context);
271 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800272 assertThat(result.get("sctpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800273 }
274
275 /**
276 * Tests destination SCTP criterion.
277 */
278 @Test
279 public void matchSctpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800280 Criterion criterion = Criteria.matchSctpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800281 ObjectNode result = criterionCodec.encode(criterion, context);
282 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800283 assertThat(result.get("sctpPort").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800284 }
285
286 /**
287 * Tests ICMP type criterion.
288 */
289 @Test
290 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800291 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800292 ObjectNode result = criterionCodec.encode(criterion, context);
293 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800294 assertThat(result.get("icmpType").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800295 }
296
297 /**
298 * Tests ICMP code criterion.
299 */
300 @Test
301 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800302 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800303 ObjectNode result = criterionCodec.encode(criterion, context);
304 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800305 assertThat(result.get("icmpCode").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800306 }
307
308 /**
309 * Tests IPv6 source criterion.
310 */
311 @Test
312 public void matchIPv6SrcTest() {
313 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
314 ObjectNode result = criterionCodec.encode(criterion, context);
315 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
316 assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
317 }
318
319 /**
320 * Tests IPv6 destination criterion.
321 */
322 @Test
323 public void matchIPv6DstTest() {
324 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
325 ObjectNode result = criterionCodec.encode(criterion, context);
326 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
327 assertThat(result.get("ip").asText(), is(ipPrefix6.toString()));
328 }
329
330 /**
331 * Tests IPv6 flow label criterion.
332 */
333 @Test
334 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800335 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800336 ObjectNode result = criterionCodec.encode(criterion, context);
337 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800338 assertThat(result.get("flowLabel").asInt(), is(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800339 }
340
341 /**
342 * Tests ICMP v6 type criterion.
343 */
344 @Test
345 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800346 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800347 ObjectNode result = criterionCodec.encode(criterion, context);
348 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800349 assertThat(result.get("icmpv6Type").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800350 }
351
352 /**
353 * Tests ICMP v6 code criterion.
354 */
355 @Test
356 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800357 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800358 ObjectNode result = criterionCodec.encode(criterion, context);
359 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800360 assertThat(result.get("icmpv6Code").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800361 }
362
363 /**
364 * Tests IPV6 target address criterion.
365 */
366 @Test
367 public void matchIPv6NDTargetAddressTest() {
368 Criterion criterion =
369 Criteria.matchIPv6NDTargetAddress(
370 Ip6Address.valueOf("1111:2222::"));
371 ObjectNode result = criterionCodec.encode(criterion, context);
372 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
373 assertThat(result.get("targetAddress").asText(), is("1111:2222::"));
374 }
375
376 /**
377 * Tests IPV6 SLL criterion.
378 */
379 @Test
380 public void matchIPv6NDSourceLinkLayerAddressTest() {
381 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
382 ObjectNode result = criterionCodec.encode(criterion, context);
383 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
384 assertThat(result.get("mac").asText(), is(mac1.toString()));
385 }
386
387 /**
388 * Tests IPV6 TLL criterion.
389 */
390 @Test
391 public void matchIPv6NDTargetLinkLayerAddressTest() {
392 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
393 ObjectNode result = criterionCodec.encode(criterion, context);
394 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
395 assertThat(result.get("mac").asText(), is(mac1.toString()));
396 }
397
398 /**
399 * Tests MPLS label criterion.
400 */
401 @Test
402 public void matchMplsLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800403 Criterion criterion = Criteria.matchMplsLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800404 ObjectNode result = criterionCodec.encode(criterion, context);
405 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800406 assertThat(result.get("label").asInt(), is(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800407 }
408
409 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800410 * Tests IPv6 Extension Header pseudo-field flags criterion.
411 */
412 @Test
413 public void matchIPv6ExthdrFlagsTest() {
414 int exthdrFlags =
415 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
416 Criterion.IPv6ExthdrFlags.ESP.getValue() |
417 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
418 Criterion.IPv6ExthdrFlags.DEST.getValue() |
419 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
420 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
421 Criterion.IPv6ExthdrFlags.HOP.getValue() |
422 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
423 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
424 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
425 ObjectNode result = criterionCodec.encode(criterion, context);
426 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
427 assertThat(result.get("exthdrFlags").asInt(), is(exthdrFlags));
428 }
429
430 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800431 * Tests lambda criterion.
432 */
433 @Test
434 public void matchLambdaTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800435 Criterion criterion = Criteria.matchLambda((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800436 ObjectNode result = criterionCodec.encode(criterion, context);
437 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800438 assertThat(result.get("lambda").asInt(), is(40000));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800439 }
440
441 /**
442 * Tests optical signal type criterion.
443 */
444 @Test
445 public void matchOpticalSignalTypeTest() {
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800446 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800447 ObjectNode result = criterionCodec.encode(criterion, context);
448 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800449 assertThat(result.get("signalType").asInt(), is(250));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800450 }
451
Ray Milkey73ba84a2015-02-04 17:08:41 -0800452}